CSS Syntax: Cascading Style Sheet (CSS)
CSS Syntax: Cascading Style Sheet (CSS)
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers. Development of large
websites, where fonts and color information were added to every single page,
became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
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.
1
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
To select an element with a specific id, write a hash (#) character, followed by the id
of the element.
<html>
<head>
<style>
p{
text-align: center;
color: red;
2
}
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
3
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.
<html>
<head>
<style>
.center {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
</style>
4
</head>
<body>
</body>
</html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
5
</html>
Comments are used to explain the code, and may help when you edit the source code
at a later date.
A CSS comment starts with /* and ends with */. Comments can also span multiple
lines:
<style>
p{
color: red;
text-align: center;
/* This is
a multi-line
comment */
</style>
</head>
<html>
<body>
6
<h2>Color Names Examples</h2>
<p>Note: You will learn more about the background-color and the color property later in our tutorial.</p>
<h2 style="background-color:red">
Red background-color
</h2>
<h2 style="background-color:green">
Green background-color
</h2>
</body>
</html>
<html>
<body>
</h2>
</h2>
</h2>
7
<h2 style="background-color:rgb(255, 165, 0)">
</h2>
</body>
</html>
<html>
<body>
<h2 style="background-color:#FF0000">
</h2>
<h2 style="background-color:#00FF00">
</h2>
</body>
</html>
<html>
<head>
<style>
body {
background-color: lightblue;
</style>
</head>
8
<body>
<h1>Hello World!</h1>
</body>
</html>
<html>
<head>
<style>
h1 {
background-color: green;
div {
background-color: lightblue;
p{
background-color: yellow;
</style>
</head>
<body>
<div>
</div>
9
</body>
</html>
10