Styling Links With CSS
Styling Links With CSS
Links
In this tutorial you will learn how to style different states of a link using CSS.
See the tutorial on HTML links to learn more about links and how to create them.
Example
Try this code »
a:link { /* unvisited link */
color: #ff0000;
text-decoration: none;
border-bottom: 1px solid;
}
a:visited { /* visited link */
color: #ff00ff;
}
a:hover { /* mouse over link */
color: #00ff00;
border-bottom: none;
}
a:active { /* active link */
color: #00ffff;
}
The order in which you are setting the style for different states of links is
important, because what defines last takes precedence over the style rules
defined earlier.
Note: In general, the order of the pseudo classes should be the following
— :link, :visited, :hover, :active, :focus in order for these to work properly.
However, there is no change in the appearance of link in case of the hover state.
It remains blue, purple or red depending on which state (i.e. unvisited, visited or
active) they are in.
Now let's see how to customize the links by overriding its default styling.
Example
Try this code »
a:link {
color: #1ebba3;
}
a:visited {
color: #ff00f4;
}
a:hover {
color: #a766ff;
}
a:active {
color: #ff9800;
}
The following example shows how to remove or set underline for different states
of a link.
Example
Try this code »
a:link, a:visited {
text-decoration: none;
}
a:hover, a:active {
text-decoration: underline;
}
Let's check out the following example and see how it really works:
Example
Try this code »
a:link, a:visited {
color: white;
background-color: #1ebba3;
display: inline-block;
padding: 10px 20px;
border: 2px solid #099983;
text-decoration: none;
text-align: center;
font: 14px Arial, sans-serif;
}
a:hover, a:active {
background-color: #9c6ae1;
border-color: #7443b6;
}
CSS Lists
In this tutorial you will learn how to format HTML lists using CSS.
Unordered lists — A list of items, where every list items are marked with
bullets.
Ordered lists — A list of items, where each list items are marked with
numbers.
Definition list — A list of items, with a description of each item.
See the tutorial on HTML lists to learn more about the lists and how to create
them.
In the following section we will discuss the properties that can be used to style
HTML lists.
But, you can change this default list marker type to any other type such as roman
numerals, latin letters, circle, square, and so on using the list-style-type property.
Let's try out the following example to understand how this property actually
works:
Example
Try this code »
ul {
list-style-type: square;
}
ol {
list-style-type: upper-roman;
}
However, you can also position these markers or bullet points inside of the list
item's display boxes using the list-style-position property along with the
value inside. In this case the lines will wrap under the marker instead of being
indented. Here's an example:
Example
Try this code »
ol.in li {
list-style-position: inside;
}
ol.out li {
list-style-position: outside;
}
The style rule in the following example assigns a transparent PNG image
"arrow.png" as the list marker for all the items in the unordered list. Let's try it out
and see how it works:
Example
Try this code »
ul li {
list-style-image: url("images/bullet.png");
}
The following example displays the image markers equally in all browsers:
Example
Try this code »
ul {
list-style-type: none;
}
ul li {
background-image: url("images/bullet.png");
background-position: 0px 5px; /* X-pos Y-pos (from top-left) */
background-repeat: no-repeat;
padding-left: 20px;
}
The following style rule sets all the list properties in a single declaration.
Example
Try this code »
ul {
list-style: square inside url("images/bullet.png");
}
Example
Try this code »
ul {
padding: 0;
list-style: none;
background: #f2f2f2;
}
ul li {
display: inline-block;
}
ul li a {
display: block;
padding: 10px 25px;
color: #333;
text-decoration: none;
}
ul li a:hover {
color: #fff;
background: #939393;
}
But when you create an HTML table without any styles or attributes, browsers
display them without any border. With CSS you can greatly improve the
appearance your tables.
CSS provides several properties that allow you to control the layout and
presentation of the table elements. In the following section you will see how to
use CSS to create elegant and consistent tables.
Example
Try this code »
table, th, td {
border: 1px solid black;
}
By default, browser draws a border around the table, as well as around all the
cells, with some space in-between, which results in double border. To get rid of
this double border problem you can simply collapse the adjoining table cell
borders and create clean single line borders.
In the separate border model, which is the default, each table cell has its own
distinct borders, whereas in the collapsed border model, adjacent table cells
share a common border. You can set the border model for an HTML table by
using the CSS border-collapse property.
The following style rules will collapse the table cell borders and apply one pixel
black border.
Example
Try this code »
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
}
Note: You can also remove the space between the table cell borders through
setting the value of CSS border-spacing property to 0. However, it only removes
the space but do not merge the borders like when you set the border-
collapse to collapse.
Adjusting Space inside Tables
By default, the browser creates the table cells just large enough to contain the
data in the cells.
To add more space between the table cell contents and the cell borders, you can
simply use the CSS padding property. Let's try out the following example and see
how it works:
Example
Try this code »
th, td {
padding: 15px;
}
You can also adjust the spacing between the borders of the cells using the
CSS border-spacing property, if the borders of your table are separated (which is
default).
The following style rules apply the spacing of 10 pixels between all borders within
a table:
Example
Try this code »
table {
border-spacing: 10px;
}
Example
Try this code »
table {
width: 100%;
}
th {
height: 40px;
}
auto — Uses an automatic table layout algorithm. With this algorithm, the
widths of the table and its cells are adjusted to fit the content. This is the
default value.
fixed — Uses the fixed table layout algorithm. With this algorithm, the
horizontal layout of the table does not depend on the contents of the cells;
it only depends on the table's width, the width of the columns, and borders
or cell spacing. It is normally faster than auto.
The style rules in the following example specify that the HTML table is laid out
using the fixed layout algorithm and has a fixed width of 300 pixels. Let's try it
out and see how it works:
Example
Try this code »
table {
width: 300px;
table-layout: fixed;
}
The following style rules will left-align the text inside the <th> elements.
Example
Try this code »
th {
text-align: left;
}
The following style rules will vertically bottom-align the text inside
the <th> elements.
Example
Try this code »
th {
height: 40px;
vertical-align: bottom;
}
The caption can be placed either at the top or bottom of the table. The default
position is top.
Example
Try this code »
caption {
caption-side: bottom;
}
Tip: To change the horizontal alignment of the table's caption text (e.g. left or
right), you can simply use the CSS text-align property, like you do with normal
text.
Example
Try this code »
table {
border-collapse: separate;
empty-cells: hide;
}
The following style rules will highlight every odd rows within the table body.
Example
Try this code »
tbody tr:nth-child(odd) {
background-color: #f2f2f2;
}
A zebra-striped table typically looks something like the following picture.
Example
Try this code »
<div style="overflow-x: auto;">
<table>
... table content ...
</table>
</div>