SlideShare a Scribd company logo
❮ Previous Next ❯
CSS Syntax and Selectors
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
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.
A CSS declaration always ends with a semicolon, and declaration blocks are
surrounded by curly braces.
In the following example all <p> elements will be center-aligned, with a red text
color:
Example
p {
color: red;
text-align: center;
}
Try it Yourself »
CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element
name, id, class, attribute, and more.
The element Selector
The element selector selects elements based on the element name.
You can select all <p> elements on a page like this (in this case, all <p> elements
will be center-aligned, with a red text color):
Example
p {
text-align: center;
color: red;
}
Try it Yourself »
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be 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 style rule below will be applied to the HTML element with id="para1":
Example
#para1 {
text-align: center;
color: red;
}
Try it Yourself »
Note: An id name cannot start with a number!
The class Selector
The class selector selects elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the
name of the class.
In the example below, all HTML elements with class="center" will be red and center-
aligned:
Example
.center {
text-align: center;
color: red;
}
Try it Yourself »
You can also specify that only specific HTML elements should be affected by a class.
In the example below, only <p> elements with class="center" will be center-
aligned:
Example
p.center {
text-align: center;
color: red;
}
Try it Yourself »
HTML elements can also refer to more than one class.
In the example below, the <p> element will be styled according to class="center"
and to class="large":
Example
<p class="center large">This paragraph refers to two classes.</p>
Try it Yourself »
Note: A class name cannot start with a number!
Grouping Selectors
If you have elements with the same style definitions, like this:
h1 {
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.
In the example below we have grouped the selectors from the code above:
Example
h1, h2, p {
text-align: center;
color: red;
}
Try it Yourself »
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
❮ Previous Next ❯
CSS Comments
Comments are used to explain the code, and may help when you edit the source
code at a later date.
Comments are ignored by browsers.
A CSS comment starts with /* and ends with */. Comments can also span multiple
lines:
Example
p {
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
Try it Yourself »
Test Yourself with Exercises!
Copyright 1999-2019 by Refsnes Data. All Rights Reserved.

More Related Content

PPTX
PPTX
Css selectors
PPTX
introduction to CSS
PPTX
What is css
ODP
Css.prabu
PDF
CSS Selectors
PPTX
Web engineering
PPTX
Id and class selector
Css selectors
introduction to CSS
What is css
Css.prabu
CSS Selectors
Web engineering
Id and class selector

What's hot (20)

PPTX
Types of Selectors (HTML)
PPTX
Cascading style-sheet-
KEY
Lecture2
PPT
Week3 css
PDF
Pirouette chemometrics modelling software
PPT
Cascading Style Sheets
PPTX
Turorial css
PPTX
Css syntax
PPTX
The three types of style sheet lesson two fourth quarter fourth year
PPTX
CSS(Cascading Stylesheet)
PPSX
CSS tutorial
PPTX
Empowerment Technologies Lecture 10 (Philippines SHS)
PPT
PDF
Html styles
Types of Selectors (HTML)
Cascading style-sheet-
Lecture2
Week3 css
Pirouette chemometrics modelling software
Cascading Style Sheets
Turorial css
Css syntax
The three types of style sheet lesson two fourth quarter fourth year
CSS(Cascading Stylesheet)
CSS tutorial
Empowerment Technologies Lecture 10 (Philippines SHS)
Html styles
Ad

Similar to Css (20)

PPT
Lecture 5 _ Introduction to CSS-1.ppt. Cascading Style Sheet
PPTX
CSS selectors and their types and their examples in css.pptx
PDF
Web Design Course: CSS lecture 1
PPTX
Ia css
PPTX
Casecading Style Sheets for Hyper Text Transfer Protocol.pptx
PPTX
Cascading Style Sheets for web browser.pptx
PPTX
css.ppt
PPTX
Web Development - Lecture 5
PPTX
Lecture-6.pptx
PPTX
WEB TECHNOLOGY Unit-2.pptx
PPTX
chitra
PDF
CSS notes
PPTX
cascading style sheets- About cascading style sheets on the selectors
PPTX
Ifi7174 lesson2
PPTX
CSS_Day_ONE (W3schools)
PPTX
Cascading style sheets, Introduction to web programming
PDF
HTML2.pdf
PPTX
Introduction to CSS.pptx
PDF
Styling with CSS
PPT
working with internet technologies using CSS
Lecture 5 _ Introduction to CSS-1.ppt. Cascading Style Sheet
CSS selectors and their types and their examples in css.pptx
Web Design Course: CSS lecture 1
Ia css
Casecading Style Sheets for Hyper Text Transfer Protocol.pptx
Cascading Style Sheets for web browser.pptx
css.ppt
Web Development - Lecture 5
Lecture-6.pptx
WEB TECHNOLOGY Unit-2.pptx
chitra
CSS notes
cascading style sheets- About cascading style sheets on the selectors
Ifi7174 lesson2
CSS_Day_ONE (W3schools)
Cascading style sheets, Introduction to web programming
HTML2.pdf
Introduction to CSS.pptx
Styling with CSS
working with internet technologies using CSS
Ad

More from Ankit Dubey (20)

PDF
Unit 1 android and it's tools quiz {mad cwipedia}
PDF
Scheduling
PDF
Chapter 4
PDF
Chapter 3
PDF
Chapter 2
PDF
Chapter 1
PDF
Chapter 5
PDF
Ch5 cpu-scheduling
PDF
Ch4 threads
PDF
Ch3 processes
PPT
Ch2 system structure
PPT
Ch1 introduction-to-os
PDF
Android i
PDF
Mongodb mock test_ii
PDF
Android mock test_iii
PDF
Android mock test_ii
PDF
Ajp notes-chapter-06
PDF
Ajp notes-chapter-05
PDF
Ajp notes-chapter-04
PDF
Ajp notes-chapter-03
Unit 1 android and it's tools quiz {mad cwipedia}
Scheduling
Chapter 4
Chapter 3
Chapter 2
Chapter 1
Chapter 5
Ch5 cpu-scheduling
Ch4 threads
Ch3 processes
Ch2 system structure
Ch1 introduction-to-os
Android i
Mongodb mock test_ii
Android mock test_iii
Android mock test_ii
Ajp notes-chapter-06
Ajp notes-chapter-05
Ajp notes-chapter-04
Ajp notes-chapter-03

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PPT on Performance Review to get promotions
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Sustainable Sites - Green Building Construction
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Well-logging-methods_new................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
573137875-Attendance-Management-System-original
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Current and future trends in Computer Vision.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
web development for engineering and engineering
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT on Performance Review to get promotions
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Sustainable Sites - Green Building Construction
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Well-logging-methods_new................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
additive manufacturing of ss316l using mig welding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
R24 SURVEYING LAB MANUAL for civil enggi
573137875-Attendance-Management-System-original
III.4.1.2_The_Space_Environment.p pdffdf
OOP with Java - Java Introduction (Basics)
Current and future trends in Computer Vision.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Geodesy 1.pptx...............................................
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
web development for engineering and engineering
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Model Code of Practice - Construction Work - 21102022 .pdf

Css

  • 1. ❮ Previous Next ❯ CSS Syntax and Selectors CSS Syntax A CSS rule-set consists of a selector and a declaration block: 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. A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces. In the following example all <p> elements will be center-aligned, with a red text color: Example p { color: red; text-align: center; }
  • 2. Try it Yourself » CSS Selectors CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class, attribute, and more. The element Selector The element selector selects elements based on the element name. You can select all <p> elements on a page like this (in this case, all <p> elements will be center-aligned, with a red text color): Example p { text-align: center; color: red; } Try it Yourself » The id Selector The id selector uses the id attribute of an HTML element to select a specific element. The id of an element should be unique within a page, so the id selector is used to select one unique element!
  • 3. To select an element with a specific id, write a hash (#) character, followed by the id of the element. The style rule below will be applied to the HTML element with id="para1": Example #para1 { text-align: center; color: red; } Try it Yourself » Note: An id name cannot start with a number! The class Selector The class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class. In the example below, all HTML elements with class="center" will be red and center- aligned: Example .center { text-align: center; color: red; }
  • 4. Try it Yourself » You can also specify that only specific HTML elements should be affected by a class. In the example below, only <p> elements with class="center" will be center- aligned: Example p.center { text-align: center; color: red; } Try it Yourself » HTML elements can also refer to more than one class. In the example below, the <p> element will be styled according to class="center" and to class="large": Example <p class="center large">This paragraph refers to two classes.</p> Try it Yourself » Note: A class name cannot start with a number!
  • 5. Grouping Selectors If you have elements with the same style definitions, like this: h1 { 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. In the example below we have grouped the selectors from the code above: Example h1, h2, p { text-align: center; color: red; } Try it Yourself »
  • 6. Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 » ❮ Previous Next ❯ CSS Comments Comments are used to explain the code, and may help when you edit the source code at a later date. Comments are ignored by browsers. A CSS comment starts with /* and ends with */. Comments can also span multiple lines: Example p { color: red; /* This is a single-line comment */ text-align: center; } /* This is a multi-line comment */ Try it Yourself » Test Yourself with Exercises! Copyright 1999-2019 by Refsnes Data. All Rights Reserved.