DEV Community

ABHAY TIWARI
ABHAY TIWARI

Posted on • Updated on

CSS selectors in brief

CSS selectors are the elements used for targeting certain part of html document for styling purposes. there are various categories and types of selectors in CSS depending on which element are to be selected for styling .

<body>
    <h1>this is a heading</h1>
</body>
Enter fullscreen mode Exit fullscreen mode

look at the code snippet above . inside a body tag I have a element h1 containing some text. to style this an element selector is used how? let's look at the code snippet given below inside style tag

<style>
        h1{
            font-size: x-large;
            font-weight: bold;
            margin: 20px;
            padding: 20px;
            background-color: black;
            color: white;
        }
    </style>
Enter fullscreen mode Exit fullscreen mode

some random properties I have given to the element selected inside style tag that leads to a different style of the h1 text when displayed on browser.

Image description
class selector-these are selectors used when a group of html elements need uniform style .

id selector-these are special class of selector which target only the concerned element none other than the element that is target is affected by it. these selectors are generally used when we want to select certain unique properties of a element to change
in order to provide more unique style.

A class selector is selected with a dot(.) prefix with class name and ID selector is selected with a hash(#) prefix with ID name.

for example

<body>
    <h1 id="heading">
        CSStutorial
    </h1>
    <p class="para">
        hey there this is selectors tutorial
    </p>
</body>
Enter fullscreen mode Exit fullscreen mode
<style>
     .para{
        background-color: black;
        color: white;
     }
     #heading{
        font-size: x-large;
        color: aqua;
     }
    </style>
Enter fullscreen mode Exit fullscreen mode

Image description

CSS UNIVERSAL SELECTOR

This selector selects all the element present in html page and give them uniform styling.
this selector is used at the very beginning for giving all element some common properties at the start. which later on can be overwritten or changed . a universal selector starts with a star/asterisk(*)

 *{
            margin: 0;
            padding: 0;
        }

Enter fullscreen mode Exit fullscreen mode

adding above code sequence to the style tag in the above example makes the margin and padding of every element zero.
see by yourself
Image description

there is no gap between the two elements h1 and p in the above
image after applying the universal selector.

grouping of one or more selectors

<body>
    <h1>heading1</h1>
    <h2>heading2</h2>
    <h3>heading3</h3>
</body>
Enter fullscreen mode Exit fullscreen mode
h1{
    text-align: center;
    color: blue;
}
h2{
    text-align: center;
    color: blue;   
}
h3{
    text-align: center;
    color: blue;
}
Enter fullscreen mode Exit fullscreen mode

the above code is very hectic and tedious job to type for that purpose instead of explicitly defining properties we are going to group all heading into one selector block separated by comma .

h1, h2, h3{

    text-align: center;
    color: blue;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)