CSS Chapter 2.1
CSS Chapter 2.1
Chapter 2
Layout, Comments, Colors, and Backgrounds
CSS
Comments
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 is placed inside the <style> element, and starts with /* and
ends with */:
Example
/* This is a single-line comment */
p{
color: red;
}
CSS Comments
• You can add comments wherever you want in the code:
Example
p{
color: red; /* Set text color to red */
}
CSS Comments
• Comments can also span multiple lines:
Example
/* This is
a multi-line
comment */
p{
color: red;
}
HTML and CSS Comments
• From the HTML tutorial, you learned that you can add comments to your
HTML source by using the <!--...--> syntax.
• In the following example, we use a combination of HTML and CSS
comments:
CSS Colors
CSS Colors
• Colors are specified using predefined color names, or RGB,
HEX, HSL, RGBA, HSLA values.
• In CSS, a color can be specified by using a predefined color
name:
CSS Background Color
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
CSS Text Color
Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
CSS Border Color
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
CSS Color Values
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
Example
body {
background-color: lightblue;
}
Other Elements
You can set the background color for any HTML elements:
Example
Here, the <h1>, <p>, and <div> elements will have different background colors:
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p{
background-color: yellow;
}
Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can take a value from
0.0 - 1.0. The lower value, the more transparent:
Example
div {
background-color: green;
opacity: 0.3;
}
Opacity / Transparency
CSS Borders
CSS Border
The CSS border properties allow you to specify the style, width, and color of an element's
border.
CSS Border Style
The border-style property specifies what kind of border to display.
p.two { p.three {
border-style: solid; border-style: dotted;
border-width: medium; border-width: 2px;
} }
p.four {
border-style: dotted;
border-width: thick;
}
CSS Border Width
Specific Side Widths
• The border-width property can have from one to four values (for the top border, right
border, bottom border, and the left border):
CSS Border color
Specific Border color
Specific Border sides