close[x]


CSS

CSS-Home CSS-Environment CSS-Basic syntax CSS-Selector CSS-How To Add CSS CSS-Comment CSS-Color CSS-Background CSS-Font CSS-Text CSS-Boarder CSS-Margin CSS-Padding CSS-Height/Width CSS-Link CSS-Table CSS-Visibility CSS-Scrollable CSS-Float/Clear CSS-Line CSS-Align CSS-Dimension CSS-Event CSS-Pusedo class CSS-Position CSS-Transparency CSS-Dropdown CSS-Form CSS-List/Counter CSS-Website layout CSS-Text effect CSS-Image effect CSS-Button effect CSS-page number CSS-Box CSS-@media CSS-Multi device CSS-Multi device 1 CSS-Multi device 2



learncodehere.com




CSS - Selector

CSS selectors are used to select the content HTML elements you want to style.

There are several different types of selectors in CSS.

  • Element Selector
  • Id Selector
  • Class Selector
  • Universal Selector
  • Group Selector

  • Element Selector

    The element selector selects the HTML element by name.


    Example : Element Selector

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    p {
    text-align: center;
    color: blue;
    }
    </style>
    </head>
    <body>

    <p>Code Always.</p>
    <p id="para1">Code Always To Become Best Programmer.</p>
    <p>CSS Element Selector</p>

    </body>
    </html>

    HTML <p> elements are affected by the CSS properties.

    Result

    Code Always.

    Code Always To Become Best Programmer.

    CSS Element Selector



    ID Selector

    The id selector uses the id attribute of an HTML element to select a specific element.

    Id is always unique within the page so it is chosen to select a single, unique element.

    To select an element with a specific id, write a hash (#) character, followed by the id of the element.


    Example : ID Selector

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    #para1 {
    text-align: center;
    color: blue;
    }
    </style>
    </head>
    <body>

    <p>Code Always.</p>
    <p id="para1">Code Always To Become Best Programmer.</p>
    <p>CSS Element Selector</p>

    </body>
    </html>

    HTML with id #para1 elements are affected by the CSS properties.

    Result

    Code Always.

    Code Always To Become Best Programmer.

    CSS Element Selector


    Class Selector

    The class selector selects HTML elements with a specific class attribute. .

    To select an element with a specific class, write a dot (.) character, followed by the class of the element.


    Example : Class Selector

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    .headerparagrph {
    text-align: center;
    color: blue;
    font-size: 20px;
    }
    </style>
    </head>
    <body>

    <p Class="header_paragraph">Code Always.</p>
    <p id="para1">Code Always To Become Best Programmer.</p>
    <p>CSS Element Selector</p>

    </body>
    </html>

    HTML with class header_paragraph elements are affected by the CSS properties.

    Result

    Code Always.

    Code Always To Become Best Programmer.

    CSS Element Selector



    Universal Selector

    The universal selector selects all the elements on the pages.

    To select an all elements, write a dot (*) character.


    Example : Universal Selector

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    *{
    text-align: center;
    color: blue;
    font-size: 20px;
    }
    </style>
    </head>
    <body>

    <p Class="header_paragraph">Code Always.</p>
    <p id="para1">Code Always To Become Best Programmer.</p>
    <p>CSS Element Selector</p>

    </body>
    </html>

    All HTML elements are affected by the CSS properties.

    Result

    Code Always.

    Code Always To Become Best Programmer.

    CSS Element Selector


    Grouping Selector

    The grouping selector is used to select all the elements with the same style definitions.

    Look at the following CSS code (the h4, and p elements have the same style definitions):


    Example : Grouping Selector

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    h6 {
    text-align: center;
    color: blue;

    }
    p {
    text-align: center;
    color: blue;
    }
    </style>
    </head>
    <body>

    <p Class="header_paragraph">Code Always.</p>
    <p id="para1">Code Always To Become Best Programmer.</p>
    <h6>CSS Element Selector</h6>

    </body>
    </html>

    HTML with h6 and p1 elements are affected by the CSS properties.

    Result

    Code Always.

    Code Always To Become Best Programmer.

    CSS Element Selector

    CSS Selectors

    Selector Example Description
    .class .header Selects all elements with class="header"
    #id #definition Selects the element with id="definition"
    * * Selects all elements
    element p Selects all <p> elements
    element,element,.. div, p, h4 Selects all <div> elements and all <p> elements and all <h4> elements