What is CSS and Selectors in CSS?
Table of contents
First we need to know what is css? and How it Works with html pages. so start with basics of css.
CSS stands for Cascading Style Sheet. Basically CSS gives styling on our web pages. styling means gives background color , text color and many more... So our web page is now looking good. Also Make easy to use user Interface(UI).
Let's jump on to how css works?
so Basically, we need to add CSS in our HTML page. there are 3 ways to add css in our html.
Style tag-
We declare style tag within head tag and after the title tag and write all css in this tag.
Example -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> h1 { color: red; } </style> </head> <body> <h1>this is style tag</h1> </body> </html>
Separate CSS File-
It is Standard practice of writing css.
Make css file with extension .css , then link with HTMl page.
Example :
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>this is style tag</h1> </body> </html>
h1 { color: blue; background-color: black; }
Inline CSS-
Inline css it know by name write css in line means write css and HTML together.
Inline css priority is greater than than above 2 type.
example -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1 style="color: red; background-color: black;">this is style tag</h1> </body> </html>
Let`s Jump On to what is Selector in CSS?
First we need to know is what is selector and why we use them. so selector means how to select particular element or tag , yes it is very simple , no apply rocket science here. also we discuss why we use them because how to style element or tag , i think you find your answer, and yes you are right , beacuse we select the element or tag and than write css for them.
There are so many types of Selector in Css?
Universal Selector :-
Universal Selector is used for Remove styling which automatically applied by Browser.
* { style properties }
/* Universal Selector :- */ * { padding: 0; margin: 0; }
Invidual Selector :-
Individual Selector is used for Selcting individual tag.
p { style properties }
/* Invidual Selector :- */ p { background-color: #fff; color: black; }
Class and Id Based Selector :-
Class and Id Based selector is used by element class and id name. Class is denoted by . (dot) and Id is denoted by # (Pound).
.class_name { style properties }
#id-Name { style properties }
/* Class and ID Based Selector */ .class-name { font-size: 20px; font-weight: bold; } #id-name { background-color: #fff; color: black; }
Chained Selector :-
Chained Selector is used for apply only to elements that matches all criteria.
/* chained selctors */ li.bg-black.text-white{ background-color: #000; color: #fff; }
Combined Selector :-
Combined Selector is used for applying same Styling on more than one elements or tag.
it is denoted by , (comma).
p , h1 { style properties }
p , h1 { background-color: #fff; font-size: 20px; }
Inside an element :-
Inside an element is used for target element by element , in other words select top to down via each element.
it is denoted by one space.
div ul li { style properties }
/* Inside an element */ div ul li { background-color: aqua; }
Direct Child :-
Direct Child is used for direct targeting the element.
it denotes by > (greater than).
div > li { style properties }
/* Direct Chaild */ div > li { background-color: brown; }
Sibling selector :-
Sibiling Selector is used for targeting just after it elements. Means select it's sibilings elements.
it is denoted by + or ~ .
.sibiling > p { style properties }
/* Sibiling Selector */ .sibiling > p { background-color: #313131; }
Subscribe to my newsletter
Read articles from Shakti Dube directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by