How to use Selectors in CSS- complete guide

blog img

CSS selectors are patterns used to select elements in HTML documents for styling. Here’s a breakdown of different types of CSS selectors with examples:

CSS (Cascading Style Sheets) is used to style HTML elements. Here’s a basic structure of CSS syntax:

Basic CSS Syntax:

css

selector {
    property: value;
}
  • Selector: Specifies which HTML element(s) to style.
  • Property: The style property to change (e.g., color, font-size, margin).
  • Value: The value for the style property.

Example:

css

/* Styling a paragraph */
p {
    color: blue;
    font-size: 16px;
}

/* Styling a class */
.my-class {
    background-color: yellow;
    padding: 10px;
}

/* Styling an ID */
#my-id {
    border: 1px solid black;
}

/* Pseudo-classes */
a:hover {
    text-decoration: underline;
}

/* Combining selectors */
h1, h2 {
    color: green;
}

/* Media queries */
@media (max-width: 600px) {
    body {
        background-color: lightgray;
    }
}

1. Universal Selector (*)

Selects all elements in the document.

css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

This resets the margin and padding of all elements to zero, ensuring consistent spacing.

2. Type Selector

Selects all elements of a specified type.

css

p {
    color: blue;
}

This changes the text color of all <p> elements to blue.

3. Class Selector (.)

Selects elements with a specific class.

css

.highlight {
    background-color: yellow;
}

This applies a yellow background to any element with the class highlight.

4. ID Selector (#)

Selects a single element with a specific ID.

css

#header {
    font-size: 24px;
}

This sets the font size of the element with the ID header to 24 pixels.

5. Attribute Selector

Selects elements based on their attributes.

css

input[type="text"] {
    border: 1px solid gray;
}

This styles all text input fields with a gray border.

6. Descendant Selector (Space)

Selects elements that are descendants of a specified element.

css

div p {
    color: green;
}

This changes the text color of all <p> elements inside <div> elements to green.

7. Child Selector (>)

Selects elements that are direct children of a specified element.

css

ul > li {
    list-style-type: square;
}

This applies a square bullet style to only the direct <li> children of a <ul>.

8. Adjacent Sibling Selector (+)

Selects an element that is directly after another specified element.

css

h1 + p {
    margin-top: 20px;
}

This applies a top margin of 20 pixels to the first <p> element that immediately follows an <h1>.

9. General Sibling Selector (~)

Selects all siblings after a specified element.

css

h1 ~ p {
    color: red;
}

This changes the text color of all <p> elements that are siblings (but not necessarily immediate) following an <h1>.

10. Grouping Selector (,)

Selects multiple elements to apply the same styles.

css

h1, h2, h3 {
    font-family: Arial, sans-serif;
}

This sets the font family of all <h1>, <h2>, and <h3> elements to Arial.

11. Pseudo-classes

Selects elements based on their state.

css

a:hover {
    text-decoration: underline;
}

This underlines links when a user hovers over them.

12. Pseudo-elements

Selects a specific part of an element.

css

p::first-line {
    font-weight: bold;
}

This makes the first line of all paragraphs bold.

You can use CSS in three main ways:

  • Inline CSS: Style within an HTML tag using the style attribute.
  • Internal CSS: Style inside a <style> tag in the HTML <head>.
  • External CSS: Style in a separate .css file linked to the HTML.

Note: CSS selectors are essential for targeting HTML elements to apply styles effectively. By using different types of selectors, you can create specific and flexible designs that enhance user experience.

Share your thoughts

Your email address will not be published. All fields are required.

Related post

  1. How to styled background – easy way

    CSS backgrounds define the background effects for elements in a…

  1. Mastering CSS: A Complete Guide to Styling

    A complete CSS tutorial typically covers all aspects of CSS…

Popular post

  1. Eclipse IDE – Create New Java Project.

    Opening the New Java Project…

  1. How to start the project in android studio

    Android Studio Open the Android…

  1. How to use ACOSH function in excel

    The ACOSH function returns the…

  1. Complete Header tags in html – easy to learn

    H tags can be used…

  1. Best features in Python programme – easy to learn

    Python is the most widely…