What is CSS?
- CSS stands for Cascading Style Sheets.
- CSS describes how HTML elements are to be displayed on screen, paper, or in other media.
- CSS saves a lot of work. It can control the layout of multiple web pages all at once.
- External stylesheets are stored in CSS files.
- CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.
Example
<html>
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This page has a light blue background color.</p>
</body>
</html>
selector {
property: value;
}
Example:
p {
text-align: center;
}
1.Selector: Points to the HTML element you want to style. For example,
p
points to paragraph elements.2.Declaration Block: Contains one or more declarations surrounded by curly braces
{}
.3.Declaration: Each declaration includes:
(i)A CSS property name (like
color
, font-size
, etc.).(ii)A value (like
blue
, 16px
, etc.) for that property.4.Multiple Declarations: Declarations are separated by semicolons inside the block.
CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
- Simple selectors (select elements based on name, id, class)
- Combinator selectors (select elements based on a specific relationship between them)
- Pseudo-class selectors (select elements based on a certain state)
- Pseudo-elements selectors (select and style a part of an element)
- Attribute selectors (select elements based on an attribute or attribute value)
- The id selector uses the id attribute of an HTML element to select a specific element.
- The id of an element is unique within a page, so the id selector is used to select one unique element!
- To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Example :
<p id="para1">This text will be centered.</p>
#para1 {
text-align: center;
}
- The class selector selects HTML elements with a specific class attribute.
- To select elements with a specific class, write a period (.) character, followed by the class name.
Example :
<p class="center">This text will be centered.</p>
.center { text-align: center;
}
- You can also specify that only specific HTML elements should be affected by a class.
Example :
<p class="center">This text will be centered and red.</p>
p.center {
text-align: center; color: red;
}
- The universal selector (*) selects all HTML elements on the page.
Example :
* {
text-align: center;
color: blue;
}
- The descendant selector matches all elements that are descendants of a specified element.
- The descendant selector in CSS allows you to target and style elements that are nested (or "descended") within other elements.
0 Comments