CSS Selectors theory you'll never forget..

CSS Selectors theory you'll never forget..

Cascading Style Sheets

Widely known as CSS, is a selective styling language used to design HTML elements and web pages. In CSS the most important thing is to learn how can one pick a specific element and target the same in web page.

CSS can be written in 2 ways :

  1. In same HTML file in <style></style> inside <head></head>.
  2. In a separate file - prepare a CSS file and link it to the current HTML page. If css file name is style.css you may link the doc as per below. emmet shortcut = link:css

<head>
    <!-- other head tags -->
    <link rel="stylesheet" href="style.css">
</head>        

CSS Selectors

A CSS selector selects the single or multiple HTML element one wants to style. Majority of the CSS game is all about acknowledging the requirement and making the selection of elements accordingly.

No alt text provided for this image

Universal Selector :

Selects everything on web page. It applies the same styles to every element on the page.

Syntax = *



<head
    <!-- other head tags -->
    <style>
        *{
            background-color:#E5D68A ;
            color: #6A1B4D;
        }
    </style>
</head>>        


No alt text provided for this image

Individual Selector :

selects particular tag from web page. one should only write the tag name, and not write brackets around the tag name.

Syntax = element name




<head>
    <!-- other head tags -->
    <style>
        p{
            background-color:#E5D68A ;
            color: #6A1B4D;
        }
    </style>
</head>        


No alt text provided for this image

Class and ID selector

Selects all elements that've the given class attribute.

Syntax = .classname

Selects an element based on the value of its id attribute. There should be ONLY ONE element with given id in a document.

Syntax = #idname


<head>
    <!-- other head tags --> 
    <style>
        .zolo{
            background-color:#E5D68A ;
            color: #6A1B4D;
        }
        #lolo{
            background-color: #35BDD0;
            color: #4d4d4d;
        }
    </style> 
</head>              


No alt text provided for this image

and selector (chained) :

Selects multiple classes or id's from HTML page. All conditions has to be true.

Syntax = #id.class or .class#id or .class.class





<head>
   <!-- other head tags -->
    <style>
        .zolo.solo{
            background-color:#E5D68A ;
            color: #6A1B4D;
        }
        .zolo#yolo{
            background-color: #35BDD0;
            color: #4d4d4d;
        }
    </style>
</head>        


No alt text provided for this image

Combined Selector :

Selects more than 1 element at a time.

Syntax = ,





<head>
   <!-- Other head tags -->
    <style>
        li,h2{
            background-color: #35BDD0;
            color: #4d4d4d;
        }
    </style>
</head>        


No alt text provided for this image

inside an element:

selects a particular order of element

Syntax = space





<head>
 <!-- other head tags -->
    <style>
        div ul p{
            background-color: #35BDD0;
            color: #4d4d4d;
        }
    </style>
</head>        



No alt text provided for this image

Direct Child Selector

A direct child is a child element that comes immediately below the parent in terms of hierarchy. That is to say, not a grandchild or great-grandchild.

Syntax = >



<head>
    <!-- other head tags -->
    <style>
        div>li{
            background-color: #35BDD0;
            color: #4d4d4d;
       }
    </style>
</head>>        


No alt text provided for this image

Siblings Selector

The second element follows the first and both share the same parent.

Syntax = ~ (not necessarily immediate) or + (necessarily immediate)




<head>
<!-- other head tags -->
    <style>
        p ~ span {
            color: #D82E2F;
        }
        h2+p{
            background-color: #35BDD0;
            color: #4d4d4d;
        }
    </style>


</head>        


Pseudo Class Selector:

It is a keyword added to a selector to define a special state of the element.

There are two types of elements: 1. Block level elements (reserves it's own space in HTML web page), 2. Inline elements (reserves the )

state of element is simply an action that is happening to it. eg; hover, click, visited, focus link

For a pseudo selector the compulsory attribute to provide in css is "content:' ';". It can be empty or have some value as empty strings always renders some value.

Nth Child selector :

It selects every element that is the nth child of it's parent. 'n' can be a number, keyword, or formula. It is a single argument that describes a pattern for matching element.

eg, odd, even, a(n)+b


Hope you enjoyed reading this article. Please like, comment and share with your friends and colleagues.


要查看或添加评论,请登录

Siddhi Bora的更多文章

  • Save your time using CSS Flexbox..

    Save your time using CSS Flexbox..

    Imagine you have 100 items in your HTML that you need to arrange in specific manner. And your customer decides to…

    1 条评论
  • Entering the Coder's World..

    Entering the Coder's World..

    Imagine you're a fashion designer and have a boutique in your city. Small business, limited buyers and moderate income.

    2 条评论

社区洞察

其他会员也浏览了