Syntax Selectors
- CSS Syntax and Selectors
- Basic CSS syntax
- Types of selectors (element, class, ID, attribute, etc.)
- Grouping and nesting selectors
CSS Syntax and Selectors
Section titled “CSS Syntax and Selectors”Basic CSS Syntax
Section titled “Basic CSS Syntax”CSS (Cascading Style Sheets) is used to style and layout web pages. It consists of rules, each of which comprises a selector and a declaration block. The basic syntax is as follows:
selector { property: value;}
- Selector: Indicates the HTML element(s) to be styled.
- Property: Specifies the aspect of the element to be styled (e.g., color, font-size).
- Value: Specifies the setting for the property.
Example:
/* set all `<p>` elements to have blue text and a font size of 16 pixels. */p { color: blue; font-size: 16px;}
This rule will set all <p>
elements to have blue text and a font size of 16 pixels.
Types of Selectors
Section titled “Types of Selectors”- Element Selector
- Selects HTML elements based on their tag name.
- Example:
h1 { color: green;}
- Class Selector
- Selects HTML elements based on the class attribute.
- Classes can be reused across multiple elements.
- Syntax: Use a dot (
.
) followed by the class name. - Example:
.highlight { background-color: yellow;}
- ID Selector
- Selects a single HTML element based on the ID attribute.
- IDs must be unique within a page.
- Syntax: Use a hash (
#
) followed by the ID name. - Example:
#main-header { text-align: center;}
- Attribute Selector
- Selects HTML elements based on their attributes.
- Example:
input[type="text"] { border: 1px solid #ccc;}
- Universal Selector
- Selects all elements on the page.
- Syntax: Use an asterisk (
*
). - Example:
* { margin: 0; padding: 0;}
- Descendant Selector
- Selects elements that are descendants of a specified element.
- Syntax: Separate selectors with a space.
- Example:
div p { color: red;}
- Child Selector
- Selects elements that are direct children of a specified element.
- Syntax: Separate selectors with a greater-than sign (
>
). - Example:
ul > li { list-style-type: none;}
- Adjacent Sibling Selector
- Selects an element that is immediately preceded by a specified element.
- Syntax: Use a plus sign (
+
). - Example:
h1 + p { margin-top: 0;}
- General Sibling Selector
- Selects all elements that are siblings of a specified element.
- Syntax: Use a tilde (
~
). - Example:
h1 ~ p { color: gray;}
Grouping and Nesting Selectors
Section titled “Grouping and Nesting Selectors”Grouping Selectors
Section titled “Grouping Selectors”Grouping selectors allows you to apply the same styles to multiple elements without repeating the style rules. Separate each selector with a comma.
Example:
h1,h2,h3 { font-family: "Arial", sans-serif; color: navy;}
This rule will apply the same styles to all <h1>
, <h2>
, and <h3>
elements.
Nesting Selectors
Section titled “Nesting Selectors”Nesting selectors means placing one selector inside another to target elements more specifically. In CSS, nesting is often represented by using descendant selectors, child selectors, or other combinators.
Example of descendant selectors:
nav ul li a { text-decoration: none; color: black;}
This rule will style all <a>
elements that are inside <li>
elements, which are inside <ul>
elements, which are inside a <nav>
element.
Example of child selectors:
div > p { margin-left: 20px;}
This rule will style all <p>
elements that are direct children of a <div>
element.
By understanding CSS syntax and the various types of selectors, you can write more efficient and powerful style rules to control the appearance of your web pages.