CSS Borders: Border Style
CSS Borders: Border Style
Border Style
The border-style property specifies what kind of border to display.
The border-style property can have from one to four values (for the top
border, right border, bottom border, and the left border).
Example
{border-style: hidden;}
</style>
</head>
<body>
<html>
<head>
<style>
</body>
</html>
Border Color
The border-color property is used to set the color of the four borders.
The border-color property can have from one to four values (for the top border,
right border, bottom border, and the left border).
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border-style: solid;
border-width: 5px;
p.two {
border-style: solid;
border-width: medium;
78
p.three {
border-style: dotted;
border-width: 2px;
p.four {
border-style: dotted;
border-width: thick;
p.five {
border-style: double;
border-width: 15px;
}
p.six {
border-style: double;
border-width: thick;
p.seven {
border-style: solid;
</style>
</head>
<body>
</body>
</html>
In CSS, there is also properties for specifying each of the borders (top, right,
bottom, and left):
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border-style: solid;
border-color: red;
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: solid;
</style>
</head>
<body>
<p><b>Note:</b> The "border-color" property does not work if it is used alone. Use the "border-style"
property to set the borders first.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
</style>
</head>
<body>
</body>
</html>
To shorten the code, it is also possible to specify all the individual border
properties in one property.
The border property is a shorthand property for the following individual border
properties:
• border-width
• border-style (required)
• border-color
<!DOCTYPE html>
<html>
<head>
<style>
div {
</style>
</head>
<body>
</body>
</html>
CSS Margins
The CSS margin properties set the size of the white space OUTSIDE the border.
Note: The margins are completely transparent - and cannot have a background color!
With CSS, you have full control over the margins. There are CSS properties for
setting the margin for each side of an element (top, right, bottom, and left).
• margin-top
• margin-right
• margin-bottom
• margin-left
Note: It is also possible to use negative values for margins; to overlap content.
The following example sets different margins for all four sides of a <p>
element:
EXAMPLE
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: yellow;
}
p.ex {
border:1px solid red;
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
</style>
</head>
<body>
<h2>Using Individual margin Properties:</h2>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div.container {
border: 1px solid red;
margin-left: 100px;
}
p.one {
margin-left: inherit;
}
</style>
</head>
<body>
<div class="container">
<p class="one">This is a paragraph with an inherited left
margin (from the div element).</p>
</div>
</body>
</html>
The margin property is a shorthand property for the following individual margin
properties:
• margin-top
• margin-right
• margin-bottom
• margin-left
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: yellow;
}
p.ex {
border:1px solid red;
margin: 100px 150px 100px 80px;
}
</style>
</head>
<body>
• margin: 25px;
o all four margins are 25px
Use of The auto Value
You can set the margin property to auto to horizontally center the element
within its container.
The element will then take up the specified width, and the remaining space will
be split equally between the left and right margins:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:300px;
margin: auto;
border: 1px solid red;
}
</style>
</head>
<body>
<h2>Use of the auto Value</h2>
<p>You can set the margin property to auto to
horizontally center the element within its container.
The element will then take up the specified width, and
the remaining space will be split equally between the
left and right margins:</p>
<div>
This div will be centered because it has margin: auto;
</div>
</body>
</html>
CSS Padding
The CSS padding properties define the white space between the element
content and the element border.
The padding clears an area around the content (inside the border) of an
element.
With CSS, you have full control over the padding. There are CSS properties for
setting the padding for each side of an element (top, right, bottom, and left).
• padding-top
• padding-right
• padding-bottom
• padding-left
The following example sets different padding for all four sides of a <p>
element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border: 1px solid red;
background-color: yellow;
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
</style>
</head>
<body>
</body>
</html>
• padding-top
• padding-right
• padding-bottom
• padding-left
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border: 1px solid red;
background-color: yellow;
</style>
</head>
<body>
<p class="one">This paragraph has a top and bottom padding of 50px, a left padding of 80px, and a right
padding of 30px.</p>
</body>
</html>
• padding: 25px;
o all four paddings are 25px
This <div> element has a height of 100 pixels and a width of 500 pixels.
Note: The height and width properties do not include padding, borders, or
margins; they set the height/width of the area inside the padding, border, and
margin of the element!
The following example shows a <div> element with a height of 100 pixels and a
width of 500 pixels:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 500px;
height: 100px;
</style>
</head>
<body>
<div>
</body>
</html>
Setting max-width
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values, like px, cm, etc., or in percent
(%) of the containing block, or set to none (this is default. Means that there is
no maximum width).
The problem with the <div> above occurs when the browser window is smaller
than the width of the element (500px). The browser then adds a horizontal
scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling
of small windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference
between the two divs!
This <div> element has a height of 100 pixels and a max-width of 500 pixels.
The following example shows a <div> element with a height of 100 pixels and a
max-width of 500 pixels:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
max-width: 500px;
height: 100px;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
CSS Text
Text Color
The color property is used to set the color of the text.
With CSS, a color is most often specified by:
Look at CSS Color Values for a complete list of possible color values.
The default text color for a page is defined in the body selector.
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: blue;
h1 {
color: green;
</style>
</head>
<body>
<p>This is an ordinary paragraph. Notice that this text is blue. The default text color for a page is defined
in the body selector.</p>
</body>
</html>
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
The following example shows center aligned, and left and right aligned text (left
alignment is default if text direction is left-to-right, and right alignment is
default if text direction is right-to-left):
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
h2 {
text-align: left;
h3 {
text-align: right;
}
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
<p>The three headings above are aligned center, left and right.</p>
</body>
</html>
When the text-align property is set to "justify", each line is stretched so that
every line has equal width, and the left and right margins are straight (like in
magazines and newspapers):
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 10px;
width: 200px;
height: 200px;
text-align: justify;
}
</style>
</head>
<body>
<p>The text-align: justify; value stretches the lines so that each line has equal width (like in newspapers
and magazines).</p>
<div>
In my younger and more vulnerable years my father gave me some advice that I've been turning over in
my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just remember that all the
people in this world haven't had the advantages that you've had.'
</div>
</body>
</html>
Text Decoration
The text-decoration property is used to set or remove decorations from text.
<!DOCTYPE html>
<html>
<head>
<style>
a{
text-decoration: none;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
a{
text-decoration: none;
</style>
</head>
<body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
</style>
</head>
<body>
</body>
</html>
Text Transformation
The text-transform property is used to specify uppercase and lowercase
letters in a text.
<!DOCTYPE html>
<html>
<head>
<style>
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
</style>
</head>
<body>
Text Indentation
The text-indent property is used to specify the indentation of the first line of
a text:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-indent: 50px;
}
</style>
</head>
<body>
</body>
</html>
Letter Spacing
The letter-spacing property is used to specify the space between the
characters in a text.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
letter-spacing: 3px;
h2 {
letter-spacing: -3px;
</style>
</head>
<body>
</body>
</html>
Line Height
The line-height property is used to specify the space between lines:
<!DOCTYPE html>
<html>
<head>
<style>
p.small {
line-height: 0.7;
}
p.big {
line-height: 1.8;
}
</style>
</head>
<body>
<p>
This is a paragraph with a standard line-
height.<br>
The default line height in most browsers is about
110% to 120%.<br>
</p>
<p class="small">
This is a paragraph with a smaller line-height.<br>
This is a paragraph with a smaller line-height.<br>
</p>
<p class="big">
This is a paragraph with a bigger line-height.<br>
This is a paragraph with a bigger line-height.<br>
</p>
</body>
</html>
Text Direction
The direction property is used to change the text direction of an element:
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
direction: rtl;
</style>
</head>
<body>
</body>
</html>
Word Spacing
The word-spacing property is used to specify the space between the words in
a text.
The following example demonstrates how to increase or decrease the space
between words:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
word-spacing: 10px;
h2 {
word-spacing: -5px;
</style>
</head>
<body>
</body>
</html>
CSS Fonts
The CSS font properties define the font family, boldness, size, and the style
of a text.
• generic family - a group of font families with a similar look (like "Serif"
or "Monospace")
• font family - a specific font family (like "Times New Roman" or "Arial")
Serif Times New Roman Serif fonts have small lines at the e
Georgia characters
Note: On computer screens, sans-serif fonts are considered easier to read than serif fo
Font Family
The font family of a text is set with the font-family property.
The font-family property should hold several font names as a "fallback" system.
If the browser does not support the first font, it tries the next font, and so on.
Start with the font you want, and end with a generic family, to let the browser
pick a similar font in the generic family, if no other fonts are available.
Note: If the name of a font family is more than one word, it must be in
quotation marks, like: "Times New Roman".
<!DOCTYPE html>
<html>
<head>
<style>
p.serif {
p.sansserif {
</style>
</head>
<body>
<h1>CSS font-family</h1>
</body>
</html>
Font Style
The font-style property is mostly used to specify italic text.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-style: normal;
p.italic {
font-style: italic;
p.oblique {
font-style: oblique;
</style>
</head>
<body>
</body>
</html>
Font Size
The font-size property sets the size of the text.
Being able to manage the text size is important in web design. However, you
should not use font size adjustments to make paragraphs look like headings, or
headings look like paragraphs.
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for
paragraphs.
Absolute size:
Relative size:
Note: If you do not specify a font size, the default size for normal text, like paragraphs
(16px=1em).
<html>
<head>
<style>
h1 {
font-size: 40px;
h2 {
font-size: 30px;
p{
font-size: 14px;
</style>
</head>
<body>
<p>This is a paragraph.</p>
</html>
1em is equal to the current font size. The default text size in browsers is 16px.
So, the default size of 1em is 16px.
The size can be calculated from pixels to em using this formula: pixels/16=em
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
h2 {
}
p{
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>Specifying the font-size in em allows all major browsers to resize the text.
Unfortunately, there is still a problem with older versions of IE. When resizing the text, it becomes
larger/smaller than it should.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 100%;
h1 {
font-size: 2.5em;
h2 {
font-size: 1.875em;
p{
font-size: 0.875em;
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>Specifying the font-size in percent and em displays the same size in all major browsers, and allows all
browsers to resize the text!</p>
</body>
</html>
Font Weight
The font-weight property specifies the weight of a font:
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-weight: normal;
p.light {
font-weight: lighter;
p.thick {
font-weight: bold;
p.thicker {
font-weight: 900;
}
</style>
</head>
<body>
</body>
</html>
Font Variant
The font-variant property specifies whether or not a text should be displayed
in a small-caps font.
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
</style>
</head>
<body>
</body>
</html>
CSS Links
Styling Links
Links can be styled with any CSS property (e.g. color, font-
family, background, etc.).
<!DOCTYPE html>
<html>
<head>
<style>
a{
color: hotpink;
</style>
</head>
<body>
</body>
</html>
In addition, links can be styled differently depending on what state they are in.
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
Text Decoration
The text-decoration property is mostly used to remove underlines from links:
Example
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
text-decoration: none;
a:visited {
text-decoration: none;
a:hover {
text-decoration: underline;
a:active {
text-decoration: underline;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
Background Color
The background-color property can be used to specify a background color for
links:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
a:hover {
background-color: lightgreen;
a:active {
background-color: hotpink;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
CSS Lists
HTML Lists and CSS List Properties
In HTML, there are two main types of lists:
• unordered lists (<ul>) - the list items are marked with bullets
• ordered lists (<ol>) - the list items are marked with numbers or letters
The following example shows some of the available list item markers:
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
ol.c {
list-style-type: upper-roman;
ol.d {
list-style-type: lower-alpha;
</style>
</head>
<body>
<ul class="a">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="b">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ol class="c">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ol class="d">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
Note: Some of the values are for unordered lists, and some for ordered lists.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-image: url('sqpurple.gif');
</style>
</head>
<body>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
</style>
</head>
<body>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
When using the shorthand property, the order of the property values are:
If one of the property values above are missing, the default value for the missing property will be
inserted, if any.
Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to the <li>
tag will affect the individual list items:
Example
<!DOCTYPE html>
<html>
<head>
<style>
ol {
background: #ff9999;
padding: 20px;
ul {
background: #3399ff;
padding: 20px;
ol li {
background: #ffe5e5;
padding: 5px;
margin-left: 35px;
ul li {
background: #cce5ff;
margin: 5px;
</style>
</head>
<body>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
CSS Tables
The look of an HTML table can be greatly improved with CSS:
Company Contact
The example below specifies a black border for <table>, <th>, and <td>
elements:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Notice that the table in the example above has double borders. This is because
both the table and the <th> and <td> elements have separate borders.
Example
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
table, td, th {
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
<p><b>Note:</b> If a !DOCTYPE is not specified, the border-collapse property can produce unexpected
results
</body>
</html>
If you only want a border around the table, only specify the border property for
<table>:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
</style>
</head>
<body>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
The example below sets the width of the table to 100%, and the height of the
<th> elements to 50px:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
th {
height: 50px;
</style>
</head>
<body>
<p>Set the width of the table, and the height of the table header row:</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or
center) of the content in <th> or <td>.
By default, the content of <th> elements are center-aligned and the content of
<td> elements are left-aligned.
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
}
th {
text-align: left;
</style>
</head>
<body>
<p>This property sets the horizontal alignment (like left, right, or center) of the content in th or td:</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or
middle) of the content in <th> or <td>.
By default, the vertical alignment of the content in a table is middle (for both
<th> and <td> elements).
The following example sets the vertical text alignment to bottom for <td>
elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
td {
height: 50px;
vertical-align: bottom;
</style>
</head>
<body>
<p>This property sets the vertical alignment (like top, bottom, or middle) of the content in th or td.</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Table Padding
To control the space between the border and the content in a table, use
the padding property on <td> and <th> elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
text-align: left;
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 15px;
</style>
</head>
<body>
<p>This property adds space between the border and the content in a table.</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Horizontal Dividers
Add the border-bottom property to <th> and <td> for horizontal dividers:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
Example
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
tr:hover{background-color:#f5f5f5}
</style>
</head>
<body>
<h2>Hoverable Table</h2>
<p>Move the mouse over the table rows to see the effect.</p>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-
color to all even (or odd) table rows:
Example
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
text-align: left;
padding: 8px;
tr:nth-child(even){background-color: #f2f2f2}
</style>
</head>
<body>
<h2>Striped Table</h2>
<p>For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd)
table rows:</p>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Table Color
The example below specifies the background color and text color of <th>
elements:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
text-align: left;
padding: 8px;
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<h2>Colored Table Header</h2>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small to
display the full content:
First Name Last Name Points Points Points Points Points Points Points Points P
Jill Smith 50 50 50 50 50 50 50 50 5
Eve Jackson 94 94 94 94 94 94 94 94 9
Adam Johnson 67 67 67 67 67 67 67 67 6
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
text-align: left;
padding: 8px;
tr:nth-child(even){background-color: #f2f2f2}
</style>
</head>
<body>
<h2>Responsive Table</h2>
<p>A responsive table will display a horizontal scroll bar if the screen is too
small to display the full content. Resize the browser window to see the effect:</p>
<p>To create a responsive table, add a container element (like div) with <strong>overflow-
x:auto</strong> around the table element:</p>
<div style="overflow-x:auto;">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
</tr>
<tr>
<td>Adam</td>
<td>Johnson</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
</tr>
</table>
</div>
</body>
</html>
The CSS box model is essentially a box that wraps around every HTML element.
It consists of: margins, borders, padding, and the actual content.
• Content - The content of the box, where text and images appear
• Padding - Clears an area around the content. The padding is transparent
• Border - A border that goes around the padding and content
• Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
padding: 25px;
margin: 25px;
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of:
margins, borders, padding, and the actual content.</p>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.</div>
</body>
</html>
Important: When you set the width and height properties of an element with CSS, you
and height of the content area. To calculate the full size of an element, you must also
and margins.
Assume we want to style a <div> element to have a total width of 350px:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 320px;
padding: 10px;
margin: 0;
</style>
</head>
<body>
<div>The picture above is 350px wide. The total width of this element is also 350px.</div>
</body>
</html>
Here is the math:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Total element width = width + left padding + right padding + left border + right
border + left margin + right margin
Total element height = height + top padding + bottom padding + top border +
bottom border + top margin + bottom margin
Note for old IE: Internet Explorer 8 and earlier versions, include padding and border in
To fix this problem, add a <!DOCTYPE html> to the HTML page.
CSS Outline
The CSS outline properties specify the style, color, and width of an outline.
This element has a thin black border and a double outline that is 10px wide and
green.
CSS Outline
An outline is a line that is drawn around elements (outside the borders) to make
the element "stand out".
However, the outline property is different from the border property - The outline
is NOT a part of an element's dimensions; the element's total width and height
is not affected by the width of the outline.
Outline Style
The outline-style property specifies the style of the outline.
The following example first sets a thin black border around each <p> element,
then it shows the different outline-style values:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
outline-color:red;
</style>
</head>
<body>
</body>
</html>
Note: None of the OTHER CSS outline properties described below will have ANY effect unless
styleproperty is set!
Outline Color
The outline-color property is used to set the color of the outline.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
outline-style: double;
outline-color: red;
</style>
</head>
<body>
</body>
</html>
Outline Width
The outline-width property specifies the width of the outline.
The width can be set as a specific size (in px, pt, cm, em, etc) or by using one
of the three pre-defined values: thin, medium, or thick.
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
outline-style: double;
outline-color: red;
outline-width: thick;
p.two {
outline-style: double;
outline-color: green;
outline-width: 3px;
</style>
</head>
<body>
</body>
</html>
• outline-width
• outline-style (required)
• outline-color
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
</body>
</html>
The display property is the most important CSS property for controlling
layout.
The display Property
The display property specifies if/how an element is displayed.
Every HTML element has a default display value depending on what type of
element it is. The default display value for most elements is block or inline.
Block-level Elements
A block-level element always starts on a new line and takes up the full width
available (stretches out to the left and right as far as it can).
• <div>
• <h1> - <h6>
• <p>
• <form>
• <header>
• <footer>
• <section>
Inline Elements
An inline element does not start on a new line and only takes up as much width
as necessary.
• <span>
• <a>
• <img>
Display: none;
display: none; is commonly used with JavaScript to hide and show elements
without deleting and recreating them.
Changing an inline element to a block element, or vice versa, can be useful for
making the page look a specific way, and still follow the web standards.
Example
<!DOCTYPE html>
<html>
<head>
<style>
li {
display: inline;
</style>
</head>
<body>
<ul>
</ul>
</body>
</html>
element with display: block; is not allowed to have other block elements inside it.
<html>
<head>
<style>
span {
display: block;
</style>
</head>
<body>
<span>A display property with a value of "block" results in</span><span>a line break between the two
elements.</span>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1.hidden {
display: none;
</style>
</head>
<body>
<p>Notice that the h1 element with display: none; does not take up any space.</p>
</body>
</html>
However, the element will still take up the same space as before. The element
will be hidden, but still affect the layout:
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1.hidden {
visibility: hidden;
</style>
</head>
<body>
</body>
</html>
Setting the width of a block-level element will prevent it from stretching out to
the edges of its container. Then, you can set the margins to auto, to
horizontally center the element within its container. The element will take up
the specified width, and the remaining space will be split equally between the
two margins:
This <div> element has a width of 500px, and margin set to auto.
Note: The problem with the <div> above occurs when the browser window is
smaller than the width of the element. The browser then adds a horizontal
scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling
of small windows. This is important when making a site usable on small devices:
This <div> element has a max-width of 500px, and margin set to auto.
Tip: Drag the browser window to smaller than 500px wide, to see the difference
between the two divs!
Example
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
width:500px;
margin: auto;
}
div.ex2 {
max-width:500px;
margin: auto;
</style>
</head>
<body>
<br>
<p><strong>Tip:</strong> Drag the browser window to smaller than 500px wide, to see the difference
between
</body>
</html>
• static
• relative
• fixed
• absolute
Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set first.
They also work differently depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
properties.
<!DOCTYPE html>
<html>
<head>
<style>
div.static {
position: static;
</style>
</head>
<body>
<h2>position: static;</h2>
<p>An element with position: static; is not positioned in any special way; it is
<div class="static">
</div>
</body>
</html>
position: relative;
An element with position: relative; is positioned relative to its normal
position.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
position: relative;
left: 30px;
</style>
</head>
<body>
<h2>position: relative;</h2>
<p>An element with position: relative; is positioned relative to its normal position:</p>
<div class="relative">
</div>
</body>
</html>
position: fixed;
An element with position: fixed; is positioned relative to the viewport,
which means it always stays in the same place even if the page is scrolled. The
top, right, bottom, and left properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally have
been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS
that is used:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
</style>
</head>
<body>
<h2>position: fixed;</h2>
<p>An element with position: fixed; is positioned relative to the viewport, which means it always stays
in the same place even if the page is scrolled:</p>
<div class="fixed">
</div>
</body>
</html>
position: absolute;
An element with position: absolute; is positioned relative to the nearest
positioned ancestor (instead of positioned relative to the viewport, like fixed).
Example
Overlapping Elements
When elements are positioned, they can overlap other elements.
The z-index property specifies the stack order of an element (which element
should be placed in front of, or behind, the others).
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
</html>
The following example specifies that an image should float to the right in a text:
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: right;
</style>
</head>
<body>
<p>In this example, the image will float to the right in the paragraph, and the text in the paragraph will
wrap around the image.</p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum,
nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec
congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis
sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc
sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed
ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non
fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p>
</body>
</html>
Elements after a floating element will flow around it. To avoid this, use
the clear property.
The clear property specifies on which sides of an element floating elements are
not allowed to float:
Example
<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
float: left;
width: 100px;
height: 50px;
margin: 10px;
.div2 {
.div3 {
float: left;
width: 100px;
height: 50px;
margin: 10px;
.div4 {
clear: left;
</style>
</head>
<body>
<h2>Without clear</h2>
<div class="div1">div1</div>
<div class="div2">div2 - Notice that the div2 element is after div1, in the HTML code. However, since
div1 is floated to the left, this happens: the text in div2 is floated around div1, and div2 surrounds the
whole thing.</div>
<h2>Using clear</h2>
<div class="div3">div3</div>
<div class="div4">div4 - Using clear moves div4 down below the floated div3. The value "left" clears
elements floated to the left. You can also clear "right" and "both".</div>
</body>
</html>
Then we can add overflow: auto; to the containing element to fix this
problem:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
}
.img1 {
float: right;
.clearfix {
overflow: auto;
.img2 {
float: right;
</style>
</head>
<body>
<p>In this example, the image is taller than the element containing it, and it is floated, so it overflows
outside of its container:</p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
<p style="clear:right">Add a clearfix class with overflow: auto; to the containing element, to fix this
problem:</p>
<div class="clearfix"><img class="img2" src="Racecss.gif" alt="RACESchools.com" width="100"
height="140">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
.clearfix {
overflow: auto;
}
nav {
float: left;
width: 200px;
section {
margin-left: 206px;
</style>
</head>
<body>
<div class="clearfix">
<nav>
<span>nav</span>
<ul>
</ul>
</nav>
<section>
<span>section</span>
<p>Notice we have put a clearfix on the div container. It is not needed in this example, but it would be if
the nav element was longer than the non-floated section content.</p>
</section>
<section>
<span>section</span>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est,
ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet.</p>
</section>
</div>
</body>
</html>
inline-block elements are like inline elements but they can have a width and a
height.
Examples
The old way - using float (notice that we also need to specify
a clear property for the element after the floating boxes):
Example
<!DOCTYPE html>
<html>
<head>
<style>
.floating-box {
float: left;
width: 150px;
height: 75px;
margin: 10px;
.after-box {
clear: left;
</style>
</head>
<body>
</body>
</html>
Example
CSS Layout - Horizontal Align
The element will then take up the specified width, and the remaining space will
be split equally between the two margins:
Example
<!DOCTYPE html>
<html>
<head>
<style>
.center {
margin: auto;
width: 60%;
padding: 10px;
</style>
</head>
<body>
<div class="center">
<p><b>Note: </b>Using margin:auto will not work in IE8, unless a !DOCTYPE is declared.</p>
</div>
</body>
</html>
Tip: Center aligning has no effect if the width property is not set (or set to
100%).
Example
<!DOCTYPE html>
<html>
<head>
<style>
.right {
position: absolute;
right: 0px;
width: 300px;
padding: 10px;
}
</style>
</head>
<body>
<div class="right">
<p>In my younger and more vulnerable years my father gave me some advice that I've been turning
over in my mind ever since.</p>
</div>
</body>
</html>
Note: Absolute positioned elements are removed from the normal flow, and can
overlap elements.
There is also a problem with IE8 and earlier, when using position. If a
container element (in our case <div class="container">) has a specified width,
and the !DOCTYPE declaration is missing, IE8 and earlier versions will add a
17px margin on the right side. This seems to be space reserved for a scrollbar.
So, always set the !DOCTYPE declaration when using position:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
.container {
position: relative;
width: 100%;
.right {
position: absolute;
right: 0px;
width: 300px;
background-color: #b0e0e6;
</style>
</head>
<body>
<div class="container">
<div class="right">
<p><b>Note: </b>When aligning using the position property, always include the !DOCTYPE declaration!
If missing, it can produce strange results in IE browsers.</p>
</div>
</div>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
.right {
float: right;
width: 300px;
padding: 10px;
</style>
</head>
<body>
<div class="right">
<p>In my younger and more vulnerable years my father gave me some advice that I've been turning
over in my mind ever since.</p>
</div>
</body>
</html>
There is also a problem with IE8 and earlier, when using float. If the
!DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px
margin on the right side. This seems to be space reserved for a scrollbar. So,
always set the !DOCTYPE declaration when using float:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
}
.right {
float: right;
width: 300px;
background-color: #b0e0e6;
</style>
</head>
<body>
<div class="right">
<p><b>Note: </b>When aligning using the float property, always include the !DOCTYPE declaration! If
missing, it can produce strange results in IE browsers.</p>
</div>
</body>
</html>
CSS Combinators
CSS Combinators
Descendant Selector
The descendant selector matches all elements that are descendants of a
specified element.
The following example selects all <p> elements inside <div> elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
Div p {
background-color: yellow;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
Child Selector
The child selector selects all elements that are the immediate children of a
specified element.
The following example selects all <p> elements that are immediate children of a
<div> element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
Div > p {
background-color: yellow;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
Sibling elements must have the same parent element, and "adjacent" means
"immediately following".
The following example selects all <p> elements that are placed immediately
after <div> elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div + p {
background-color: yellow;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
General Sibling Selector
The general sibling selector selects all elements that are siblings of a specified
element.
The following example selects all <p> elements that are siblings of <div>
elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div ~ p {
background-color: yellow;
</style>
</head>
<body>
<div>
</div>
</html>
CSS Pseudo-classes
Syntax
The syntax of pseudo-classes:
selector:pseudo-class {
property:value;
}
Anchor Pseudo-classes
Links can be displayed in different ways:
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
a.highlight:hover {
color: #ff0000;
</style>
</head>
<body>
</body>
</html>
When you hover over the link in the example, it will change color.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> For :first-child to work in IE8 and earlier, a DOCTYPE must be declared.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
p i:first-child {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> For :first-child to work in IE8 and earlier, a DOCTYPE must be declared.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child i {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> For :first-child to work in IE8 and earlier, a DOCTYPE must be declared.</p>
</body>
</html>
In the example below, :lang defines the quotation marks for <q> elements
with lang="no":
Example
<!DOCTYPE html>
<html>
<head>
<style>
q:lang(no) {
</style>
</head>
<body>
<p>In this example, :lang defines the quotation marks for q elements with lang="no":</p>
<p><b>Note:</b> IE8 supports the :lang pseudo class only if a !DOCTYPE is specified.</p>
</body>
</html>
CSS Pseudo-elements
selector::pseudo-element {
property:value;
}
The double colon replaced the single-colon notation for pseudo-elements in CSS3. This was a
to distinguish between pseudo-classes and pseudo-elements.
The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and
For backward compatibility, the single-colon syntax is acceptable for CSS2 and CSS1 pseudo-
The following example formats the first line of the text in all <p> elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p::first-line {
color: #ff0000;
font-variant: small-caps;
</style>
</head>
<body>
<p>You can use the ::first-line pseudo-element to add a special effect to the first line of a text. Some
more text. And even more, and more, and more, and more, and more, and more, and more, and more,
and more, and more, and more, and more.</p>
</body>
</html>
• font properties
• color properties
• background properties
• word-spacing
• letter-spacing
• text-decoration
• vertical-align
• text-transform
• line-height
• clear
The following example formats the first letter of the text in all <p> elements:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: #ff0000;
font-size: xx-large;
</style>
</head>
<body>
<p>You can use the ::first-letter pseudo-element to add a special effect to the first character of a
text!</p>
</body>
</html>
• font properties
• color properties
• background properties
• margin properties
• padding properties
• border properties
• text-decoration
• vertical-align (only if "float" is "none")
• text-transform
• line-height
• float
• clear
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.intro::first-letter {
color: #ff0000;
font-size:200%;
</style>
</head>
<body>
</body>
</html>
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of a paragraph will be red, in an xx-
large font size. The rest of the first line will be blue, and in small-caps. The rest
of the paragraph will be the default font size and color:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: #ff0000;
font-size: xx-large;
p::first-line {
color: #0000ff;
font-variant: small-caps;
</style>
</head>
<body>
<p>You can combine the ::first-letter and ::first-line pseudo-elements to add a special effect to the first
letter and the first line of a text!</p>
</body>
</html>
The following example inserts an image before the content of each <h1>
element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1::before {
content: url(smiley.gif);
</style>
</head>
<body>
<h1>This is a heading</h1>
<h1>This is a heading</h1>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1::after {
content: url(smiley.gif);
</style>
</head>
<body>
<h1>This is a heading</h1>
<h1>This is a heading</h1>
</body>
</html>
CSS - The ::selection Pseudo-element
The ::selection pseudo-element matches the portion of an element that is
selected by a user.
The following example makes the selected text red on a yellow background:
Example
• Home
• News
• Contact
• About
Horizontal
• Home
• News
• Contact
o About
• Home
• News
• Contact
o About
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation
bars.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the <ul> and <li> elements
makes perfect sense:
Example
<!DOCTYPE html>
<html>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>Note: We use href="#" for test links. In a real web site this would be URLs.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
</style>
</head>
<body>
<p>In this example, we remove the bullets from the list, and its default padding and margin.</p>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Example explained:
The code in the example above is the standard code used in both vertical, and
horizontal navigation bars.
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
li a {
display: block;
width: 60px;
background-color: #dddddd;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the links to show the link area.</p>
<p>Notice that the whole link area is clickable, not just the text.</p>
</body>
</html>
Example explained:
You can also set the width of <ul>, and remove the width of <a>, as they will
take up the full width available when displayed as block elements. This will
produce the same result as our previous example:
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
li a {
display: block;
background-color: #dddddd;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the links to show the link area.</p>
<p>Notice that the whole link area is clickable, not just the text.</p>
</body>
</html>
• Home
• News
• Contact
• About
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
li a {
display: block;
color: #000;
text-decoration: none;
background-color: #555;
color: white;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Add an "active" class to the current link to let the user know which page he/she
is on:
• Home
• News
• Contact
• About
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
li a {
display: block;
color: #000;
text-decoration: none;
li a.active {
background-color: #4CAF50;
color: white;
li a:hover:not(.active) {
background-color: #555;
color: white;
</style>
</head>
<body>
<p>In this example, we create an "active" class with a green background color and a white text. The
class is added to the "Home" link.</p>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Center Links & Add Borders
Add text-align:center to <li> or <a> to center the links.
Add the border property to <ul> add a border around the navbar. If you also
want borders inside the navbar, add aborder-bottom to all <li> elements,
except for the last one:
• Home
• News
• Contact
• About
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
li a {
display: block;
color: #000;
text-decoration: none;
li {
text-align: center;
li:last-child {
border-bottom: none;
li a.active {
background-color: #4CAF50;
color: white;
li a:hover:not(.active) {
background-color: #555;
color: white;
</style>
</head>
<body>
<p>In this example, we center the navigation links and add a border to the navigation bar.</p>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
li a {
display: block;
color: #000;
text-decoration: none;
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<h3>Try to scroll this area, and see how the sidenav sticks to the page</h3>
<p>Notice that this div element has a left margin of 25%. This is because the side navigation is set to
25% width. If you remove the margin, the sidenav will overlay/sit on top of this div.</p>
<p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is
too long (for example if it has over 50 links inside of it).</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
</div>
</body>
</html>
One way to build a horizontal navigation bar is to specify the <li> elements as
inline, in addition to the "standard" code above:
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
li {
display: inline;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
li {
display: inline;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Example explained:
• display: inline; - By default, <li> elements are block elements. Here,
we remove the line breaks before and after each list item, to display them
on one line
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
li {
float: left;
li a {
display: block;
padding: 8px;
background-color: #dddddd;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p><b>Note:</b> If a !DOCTYPE is not specified, floating items can produce unexpected results.</p>
<p>A background color is added to the links to show the link area. The whole link area is clickable, not
just the text.</p>
<p><b>Note:</b> overflow:hidden is added to the ul element to prevent li elements from going outside
of the list.</p>
</body>
</html>
Example explained:
• float: left; - use float to get block elements to slide next to each other
• display: block; - Displaying the links as block elements makes the whole link area
clickable (not just the text), and it allows us to specify padding (and height, width, margins, etc.
if you want)
• padding: 8px; - Since block elements take up the full width available, they cannot float
next to each other. Therefore, specify some padding to make them look good
• background-color: #dddddd; - Add a gray background-color to each a element
Tip: Add the background-color to <ul> instead of each <a> element if you want a full-width
background color:
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #dddddd;
li {
float: left;
li a {
display: block;
padding: 8px;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the list instead of each link to create a full-width background
color.</p>
<p><b>Note:</b> overflow:hidden is added to the ul element to prevent li elements from going outside
of the list.</p>
</body>
</html>
• Home
• News
• Contact
• About
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
li a:hover {
background-color: #111;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Add an "active" class to the current link to let the user know which page he/she
is on:
• Home
• News
• Contact
• About
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Right-Align Links
Right-align links by adding a new <ul> inside the <ul> with float:right;:
• Home
• News
• Contact
o About
o Login
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<ul style="float:right;list-style-type:none;">
<li><a href="#about">About</a></li>
<li><a href="#login">Login</a></li>
</ul>
</ul>
</body>
</html>
Border Dividers
Add the border-right property to <li> to create link dividers:
• Home
• News
• Contact
o About
o Login
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li:last-child {
border-right: none;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<ul style="float:right;list-style-type:none;">
<li><a href="#about">About</a></li>
<li><a href="#login">Login</a></li>
</ul>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li:last-child {
border-right: none;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<ul style="float:right;list-style-type:none;">
<li><a href="#about">About</a></li>
<li><a href="#login">Login</a></li>
</ul>
</ul>
</body>
</html>
Fixed Navigation Bar
Make the navigation bar stay at the top or the bottom of the page, even when
the user scrolls the page:
• Home
• News
• Contact
<!DOCTYPE html>
<html>
<head>
<style>
body {margin:0;}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<div style="padding:20px;margin-top:30px;background-color:#1abc9c;height:1500px;">
<h2>The navigation bar will stay at the top of the page while scrolling</h2>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {margin:0;}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
}
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<div style="padding:20px;background-color:#1abc9c;height:1500px;">
<h2>The navigation bar will stay at the bottom of the page while scrolling</h2>
</div>
</body>
</html>
• Home
• News
• Contact
• About
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f3f3f3;
li {
float: left;
li a {
display: block;
color: #666;
text-align: center;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #ddd;
li a.active {
color: white;
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
CSS Dropdowns
Dropdown Examples
Move the mouse over the examples below:
Dropdown Text
Dropdown Menu
Other:
Basic Dropdown
Create a dropdown box that appears when the user moves the mouse over an
element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
.dropdown:hover .dropdown-content {
display: block;
</style>
</head>
<body>
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
</body>
</html>
Example Explained
HTML) Use any element to open the dropdown content, e.g. a <span>, or a
<button> element.
Use a container element (like <div>) to create the dropdown content and add
whatever you want inside of it.
Wrap a <div> element around the elements to position the dropdown content
correctly with CSS.
CSS) The .dropdown class use position:relative, which is needed when we want
the dropdown content to be placed right below the dropdown button
(using position:absolute).
Instead of using a border, we have used the box-shadow property to make the
dropdown menu look like a "card".
The :hover selector is used to show the dropdown menu when the user moves
the mouse over the dropdown button.
Dropdown Menu
Create a dropdown menu that allows the user to choose an option from a list:
Dropdown Menu
<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
right: 0;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
.dropdown:hover .dropdown-content {
display: block;
.dropdown:hover .dropbtn {
background-color: #3e8e41;
</style>
</head>
<body>
<p>Determine whether the dropdown content should go from left to right or right to left with the left
and right properties.</p>
<div class="dropdown" style="float:left;">
<button class="dropbtn">Left</button>
</div>
</div>
<button class="dropbtn">Right</button>
<div class="dropdown-content">
</div>
</div>
</body>
</html>
Right
If you want the dropdown menu to go from right to left, instead of left to right,
add right: 0;
Example
<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
right: 0;
background-color: #f9f9f9;
min-width: 160px;
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
.dropdown:hover .dropdown-content {
display: block;
.dropdown:hover .dropbtn {
background-color: #3e8e41;
</style>
</head>
<body>
<h2>Aligned Dropdown Content</h2>
<p>Determine whether the dropdown content should go from left to right or right to left with the left
and right properties.</p>
<button class="dropbtn">Left</button>
</div>
</div>
<button class="dropbtn">Right</button>
<div class="dropdown-content">
</div>
</div>
</body>
</html>