Introduction
In this section, we will cover the basics of CSS syntax and selectors. Understanding these fundamentals is crucial for writing effective and efficient CSS code. By the end of this lesson, you will be able to write basic CSS rules and use different types of selectors to target HTML elements.
CSS Syntax
CSS (Cascading Style Sheets) is used to style HTML elements. A CSS rule consists of a selector and a declaration block.
Basic Structure
- Selector: Specifies the HTML element(s) to be styled.
- Declaration Block: Contains one or more declarations separated by semicolons.
- Declaration: Consists of a property and a value, separated by a colon.
Example
In this example:
- The selector
p
targets all<p>
elements. - The declarations
color: blue;
andfont-size: 16px;
set the text color to blue and the font size to 16 pixels, respectively.
Types of Selectors
CSS provides various types of selectors to target HTML elements in different ways.
- Universal Selector
The universal selector (*
) targets all elements on the page.
- Type Selector
The type selector targets elements by their tag name.
- Class Selector
The class selector targets elements with a specific class attribute. It is denoted by a period (.
) followed by the class name.
- ID Selector
The ID selector targets a single element with a specific ID attribute. It is denoted by a hash (#
) followed by the ID name.
- Attribute Selector
The attribute selector targets elements based on the presence or value of an attribute.
- Descendant Selector
The descendant selector targets elements that are descendants of a specified element.
- Child Selector
The child selector targets direct children of a specified element.
- Sibling Selectors
-
Adjacent Sibling Selector: Targets an element that is immediately preceded by a specified element.
h1 + p { margin-top: 0; }
-
General Sibling Selector: Targets all elements that are siblings of a specified element.
h1 ~ p { color: gray; }
Practical Examples
Example 1: Styling Paragraphs
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Syntax and Selectors</title> <style> p { color: blue; font-size: 18px; } </style> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html>
Example 2: Using Class and ID Selectors
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Syntax and Selectors</title> <style> .highlight { background-color: yellow; } #main-title { font-size: 24px; color: darkblue; } </style> </head> <body> <h1 id="main-title">Welcome to CSS</h1> <p class="highlight">This paragraph is highlighted.</p> <p>This paragraph is not highlighted.</p> </body> </html>
Exercises
Exercise 1: Basic Selectors
Task: Write CSS to style the following HTML elements:
- All
<h2>
elements should have a color ofpurple
. - The element with the ID
special
should have a background color oflightblue
. - All elements with the class
note
should have a font size of14px
.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Exercise 1</title> <style> /* Write your CSS here */ h2 { color: purple; } #special { background-color: lightblue; } .note { font-size: 14px; } </style> </head> <body> <h2>Heading 2</h2> <p id="special">This is a special paragraph.</p> <p class="note">This is a note.</p> <p>This is a regular paragraph.</p> </body> </html>
Exercise 2: Advanced Selectors
Task: Write CSS to style the following HTML elements:
- All
<li>
elements that are direct children of<ul>
should have a color ofgreen
. - The first
<p>
element after an<h2>
should have a margin-top of0
. - All
<p>
elements that are siblings of an<h2>
should have a color ofgray
.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Exercise 2</title> <style> /* Write your CSS here */ ul > li { color: green; } h2 + p { margin-top: 0; } h2 ~ p { color: gray; } </style> </head> <body> <ul> <li>Item 1</li> <li>Item 2</li> </ul> <h2>Heading 2</h2> <p>This paragraph is immediately after the heading.</p> <p>This paragraph is a sibling of the heading.</p> </body> </html>
Conclusion
In this lesson, we covered the basic syntax of CSS and various types of selectors. Understanding these concepts is essential for writing effective CSS. Practice using different selectors to target HTML elements and style them according to your needs. In the next lesson, we will learn how to add CSS to HTML in different ways.
CSS Mastery: From Beginner to Advanced
Module 1: Introduction to CSS
- What is CSS?
- CSS Syntax and Selectors
- How to Add CSS to HTML
- Basic CSS Properties
- CSS Colors
- CSS Units and Measurements
Module 2: Text and Font Styling
- Text Properties
- Font Properties
- Google Fonts Integration
- Text Alignment and Spacing
- Text Decoration and Transformation
Module 3: Box Model and Layout
- Understanding the Box Model
- Margin and Padding
- Border and Outline
- Width and Height
- Box Sizing
- CSS Display Property
Module 4: Positioning and Floating
- CSS Position Property
- Static, Relative, Absolute, and Fixed Positioning
- CSS Float and Clear
- Creating Layouts with Float
- CSS Z-Index
Module 5: Flexbox
- Introduction to Flexbox
- Flex Container Properties
- Flex Item Properties
- Creating Layouts with Flexbox
- Responsive Design with Flexbox
Module 6: CSS Grid
- Introduction to CSS Grid
- Grid Container Properties
- Grid Item Properties
- Creating Layouts with CSS Grid
- Responsive Design with CSS Grid
Module 7: Advanced CSS Techniques
Module 8: Responsive Design
- Introduction to Responsive Design
- Media Queries
- Responsive Typography
- Responsive Images
- Mobile-First Design
Module 9: Preprocessors and Frameworks
- Introduction to CSS Preprocessors
- Sass Basics
- Less Basics
- Introduction to CSS Frameworks
- Using Bootstrap