CSS Syntax
- The selector points to the HTML element you want to style.
- The declaration block contains one or more declarations separated by semicolons.
- Each declaration includes a CSS property name and a value, separated by a colon.
- Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces.
- P is a selector in CSS (it points to the HTML element you want to style: <p>).
- Color is a property, and red is the property value.
- Text-align is a property, and center is the property value.
CSS SELECTORS
- Simple selectors(simple selector based on name, id, class)
- Combinator selector (Combinator selector based on a specific relationship between them) .
- Pseudo-class selectors (Pseudo-class selector based on a certain state) .
- Pseudo-element selectors (select and style a part of an element).
- Attribute selectors (select elements based on an attribute or attribute value)
1. CSS Element Selector
- 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.
- The id selector use the id attribute of an HTML element to select a specific element
3. CSS Class Selector -
- 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.
4. CSS Universal Selector -
5. CSS Grouping Selector -
- The grouping selector selects all the HTML elements with the same style definitions.
- Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
CSS Combinators
- descendant selector (space)
- child selector (>)
- adjacent sibling selector (+)
- general sibling selector (~)
The following example selects all <p> elements inside <div> elements:
<!DOCTYPE html>
<html>
<head>
<style>
div p {
background-color: yellow;
}
</style>
</head>
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><p>Paragraph 3 in the div.</p></section>
</div>
<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>
</body>
</html>
2. Child Selector- The child selector selects all elements that are the children of a specified element.
The following example selects all <p> elements that are children of a <div>element:
<!DOCTYPE html>
<html>
<head>
<style>
div > p {
background-color: yellow;
}
</style>
</head>
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<p>Paragraph 4 in the div.</p>
</div>
<p>Paragraph 5. Not in a div.</p>
<p>Paragraph 6. Not in a div.</p>
</body>
</html>
3. Adjacent Selector-The adjacent sibling selector selects all elements that are the adjacent siblings of a specified element.
Sibling elements must have the same parent element, and "adjacent" means"immediately following".
The following example selects all <p> elements that are placed immediately after<div>elements:



No comments:
Post a Comment