CSS Syntax: For Example, The Headline Color Can Be Defined As
CSS Syntax: For Example, The Headline Color Can Be Defined As
CSS Syntax: For Example, The Headline Color Can Be Defined As
CSS is composed of style rules that the browser interprets and then applies to the
corresponding elements in your document.
A style rule has three parts: selector, property, and value.
Try it Yourself
Type Selectors
The most common and easy to understand selectors are type selectors. This selector targets
element types on the page.
Try it Yourself
A CSS declaration always ends with a semicolon, and declaration groups are surrounded by
curly braces.
id selectors allow you to style an HTML element that has an id attribute, regardless of their
position in the document tree. Here is an example of an id selector:
The HTML:
<div id="intro">
<p> This paragraph is in the intro section.</p>
</div>
<p> This paragraph is not in the intro section.</p>
HTML
The CSS:
#intro {
color: white;
background-color: gray;
}
CSS
Try it Yourself
To select an element with a specific id, use a hash character, and then follow it with the id
of the element.
Result: Class
selectors work in a similar way. The major difference is that IDs can only be applied once
per page, while classes can be used as many times on a page as needed.
In the example below, both paragraphs having the class "first" will be affected by the CSS:
The HTML:
<div>
<p class="first">This is a paragraph</p>
<p> This is the second paragraph. </p>
</div>
<p class="first"> This is not in the intro section</p>
<p> The second paragraph is not in the intro section. </p>
HTML
The CSS:
.first {font-size: 200%;}
CSS
Try it Yourself
To select elements with a specific class, use a period character, followed by the name of the
class.
Do NOT start a class or id name with a number!
Descendant Selectors
These selectors are used to select elements that are descendants of another element. When
selecting levels, you can select as many levels deep as you need to.
For example, to target only <em> elements in the first paragraph of the "intro" section:
The HTML:
<div id="intro">
<p class="first">This is a <em> paragraph.</em></p>
<p> This is the second paragraph. </p>
</div>
<p class="first"> This is not in the intro section.</p>
<p> The second paragraph is not in the intro section. </p>
HTML
The CSS:
#intro .first em {
color: pink;
background-color:gray;
}
CSS
Try it Yourself
As a result, only the elements selected will be affected:
The descendant selector matches all elements that are descendants of a specified element.
Comments
Comments are used to explain your code, and may help you when you edit the source code
later. Comments are ignored by browsers.
Example:
p{
color: green;
/* This is a comment */
font-size: 150%;
}
CSS
Try it Yourself
The comment does not appear in the browser:
Cascade
The three main sources of style information that form a cascade are:
Inheritance
Inheritance refers to the way properties flow through the page. A child element will usually
take on the characteristics of the parent element unless otherwise defined.
For example:
<html>
<head>
<style>
body {
color: green;
font-family: Arial;
}
</style>
</head>
<body>
<p>
This is a text inside the paragraph.
</p>
</body>
</html>
HTML
Result:
Since the paragraph tag (child element) is inside the body tag (parent element), it takes on
any styles assigned to the body tag.
The CSS:
p.serif {
font-family: "Times New Roman", Times, serif;
}
p.sansserif {
font-family: Helvetica, Arial, sans-serif;
}
p.monospace {
font-family: "Courier New", Courier, monospace;
}
p.cursive {
font-family: Florence, cursive;
}
p.fantasy {
font-family: Blippo, fantasy;
}
CSS
Try it Yourself
Result:
Separate each value with a comma to indicate that they are alternatives.
If the name of a font family is more than one word, it must be in quotation marks: "Times
New Roman".
Continue
The font-family property should hold several font names as a "fallback" system. When
specifying a web font in a CSS style, add more than one font name, in order to avoid
unexpected behaviors. If the client computer for some reason doesn't have the one you
choose, it will try the next one.
It is a good practice to specify a generic font family, to let the browser pick a similar font in
the generic family, if no other fonts are available.
body {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
CSS
If the browser does not support the font Arial, it tries the next fonts (Helvetica Neue,
then Helvetica). If the browser doesn't have any of them, it will try the generic sans-serif.
Remember to use quotation marks if the font name consists of more than one word.
Well done!
The font-size property sets the size of a font. One way to set the size of fonts on the web is
to use keywords. For example xx-small, small, medium, large, larger, etc.
The HTML:
<p class="small">
Paragraph text set to be small
</p>
<p class="medium">
Paragraph text set to be medium
</p>
<p class="large">
Paragraph text set to be large
</p>
<p class="xlarge">
Paragraph text set to be very large
</p>
HTML
The CSS:
p.small {
font-size: small;
}
p.medium {
font-size: medium;
}
p.large {
font-size: large;
}
p.xlarge {
font-size: x-large;
}
CSS
Try it Yourself
Result:
Keywords are useful if you do not want the user to be able to increase the size of the font
because it will adversely affect your site's appearance.
You can also use numerical values in pixels or ems to manipulate font size.
Setting the font size in pixel values (px) is a good choice when you need pixel accuracy,
and it gives you full control over the text size.
The em size unit is another way to set the font size (em is a relative size unit). It allows all
major browsers to resize the text. If you haven't set the font size anywhere on the page, then
it is the browser default size, which is 16px.
For example:
h1 {
font-size: 20px;
}
CSS
Try it Yourself
h1 {
font-size: 1.25em;
}
CSS
Try it Yourself
Both of the examples will produce the same result in the browser, because 20/16=1.25em.
Try different combinations of text size and page zooming in a variety of browsers to ensure
that the text remains readable.
Well done!
Continue
The HTML:
<p class="italic">This is a paragraph in italic style.</p>
HTML
The CSS:
p.italic {
font-style: italic;
}
CSS
Try it Yourself
Result:
Run the code and see how it works!
The HTML:
<p class="normal">This paragraph is normal.</p>
<p class="italic">This paragraph is italic.</p>
<p class="oblique">This paragraph is oblique.</p>
HTML
The CSS:
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
CSS
Try it Yourself
The HTML <i> tag will produce exactly the same result as the italic font style.
Well done!
Continue
The font-weight controls the boldness or thickness of the text. The values can be set
as normal (default size), bold, bolder, and lighter.
The HTML:
<p class="light">This is a font with a "lighter" weight.</p>
<p class="bold">This is a font with a "bold" weight.</p>
<p class="bolder">This is a font with a "bolder" weight.</p>
HTML
The CSS:
p.light {
font-weight: lighter;
}
p.bold {
font-weight: bold;
}
p.bolder {
font-weight: bolder;
}
CSS
Try it Yourself
Result:
Run the code and see how it works!
You can also define the font weight with a number from 100 (thin) to 900 (thick),
according to how thick you want the text to be.
400 is the same as normal, and 700 is the same as bold.
The HTML:
<p class="light">This is a font with a "lighter" weight.</p>
<p class="thick">This is a font with a "bold" weight.</p>
<p class="thicker">This is a font with a "700" weight.</p>
HTML
The CSS:
p.light {
font-weight: lighter;
}
p.thick {
font-weight: bold;
}
p.thicker {
font-weight: 700;
}
CSS
Try it Yourself
Result:
The HTML <strong> tag also makes the text bold.
The CSS font-variant property allows you to convert your font to all small caps. The values
can be set as normal, small-caps, and inherit.
The HTML:
<p class="normal">Paragraph font variant set to normal.</p>
<p class="small">Paragraph font variant set to small-caps.</p>
HTML
The CSS:
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
CSS
Try it Yourself
Result:
Not every font supports CSS font-variant, so be sure to test before you publish.
The HTML:
<p class="example">The text inside the paragraph is green.</p>
The text outside the paragraph is black (by default).
HTML
The CSS:
p.example {
color: green;
}
CSS
Try it Yourself
Result:
Run the code and see how it works!
In the example below, we use hexadecimal value to set the heading color to blue, and RGB
form to make the paragraph red.
The HTML:
<h1>This is a heading</h1>
<p class="example">This is a paragraph</p>
HTML
The CSS:
h1 {
color: #0000FF;
}
p.example {
color: rgb(255,0,0);
}
CSS
Try it Yourself
Result:
Run the code and see how it works!
Well done!
Continue
Back
The text-align property specifies the horizontal alignment of text in an element. By default,
text on your website is aligned to the left. However, at times you may require a different
alignment.
The HTML:
<p class="left">This paragraph is aligned to <strong>left.</strong></p>
<p class="right">This paragraph is aligned to <strong>right.</strong></p>
<p class="center">This paragraph is aligned to <strong>center.</strong></p>
HTML
The CSS:
p.left {
text-align: left;
}
p.right {
text-align: right;
}
p.center {
text-align: center;
}
CSS
Try it Yourself
Result:
When text-align is set to "justify", each line is stretched so that every line has equal width,
and the left and right margins are straight (as in magazines and newspapers).
The vertical-align property sets an element's vertical alignment. Commonly used values
are top, middle, and bottom.
The example below shows how to vertically align the text between the table.
The HTML:
<table border="1" cellpadding="2" cellspacing="0" style="height: 150px;">
<tr>
<td class="top">Top</td>
<td class="middle">Middle</td>
<td class="bottom">Bottom</td>
</tr>
</table>
HTML
The CSS:
td.top {
vertical-align: top;
}
td.middle {
vertical-align: middle;
}
td.bottom {
vertical-align: bottom;
}
CSS
Try it Yourself
Result:
Run the code and see how it works!
The HTML:
<p>This is an <span class="baseline">inline text</span> example.</p>
<p>This is a <span class="sub">sub line text</span> example.</p>
<p> This is a <span class="super">super line text</span> example.</p>
<p> This is a <span class="pixel">pixel</span> example.</p>
HTML
The CSS:
span.baseline {
vertical-align: baseline;
}
span.sub {
vertical-align: sub;
}
span.super {
vertical-align: super;
}
span.pixel {
vertical-align: -10px;
}
CSS
Try it Yourself
Result:
Instead of px values, you can use pt (points), cm (centimeters) and % (percentage) values.
Vertical align property does not act the same way for all elements.
For example, some additional CSS styling is needed for div elements.
The HTML:
<div class="main">
<div class="paragraph">
This text is aligned to the middle
</div>
</div>
HTML
The CSS:
.main {
height: 150px; width: 400px;
background-color: LightSkyBlue;
display: inline-table;
}
.paragraph {
display: table-cell;
vertical-align: middle;
}
CSS
Try it Yourself
Result:
display: inline-table; and display: table-cell; styling rules are applied to make the
vertical-align property work with divs.
The CSS:
p.none {
text-decoration: none;
}
p.inherit {
text-decoration: inherit;
}
p.overline {
text-decoration: overline;
}
p.underline {
text-decoration: underline;
}
p.line-through {
text-decoration: line-through;
}
CSS
Try it Yourself
Result:
You can combine the underline, overline, or line-through values in a space-separated list
to add multiple decoration lines.
This value is valid but is deprecated and most browsers ignore it.
The text-indent property specifies how much horizontal space should be left before the
beginning of the first line of the text. Property values are length (px, pt, cm, em, etc.), %,
and inherit.
The HTML:
<p>This is an example of
<strong>text-indent </strong> property.
First line of our text is indented to the right in 60px.
Besides pixels you can also use other measurement units,
like pt, cm, em, etc. </p>
HTML
The CSS:
p{
text-indent: 60px;
}
CSS
Try it Yourself
Result:
Negative values are allowed. The first line will be indented to the left if the value is
negative.
The HTML:
<h1>Text-shadow example</h1>
HTML
The CSS:
h1 {
color: blue;
font-size: 30pt;
text-shadow: 5px 2px 4px grey;
}
CSS
Try it Yourself
When working with shadows, you can use any CSS-supported color format.
For the x and y offsets, various types of units can be used (like px, cm, mm, in, pc, pt, etc).
Negative values are also supported.
The example below creates a blue drop-shadow, two pixels higher than the main text, one pixel to
the left of it, and with a 0.5em blur:
The HTML:
The CSS:
h1 {
font-size: 20pt;
text-shadow: rgba(0,0,255,1) -1px -2px 0.5em;
}
CSS
Try it Yourself
Result:
The text-transform CSS property specifies how to capitalize an element's text. For example, it can
be used to make text appear with each word capitalized.
The HTML:
<p class="capitalize">
The value capitalize transforms the first
character in each word to uppercase;
all other characters remain unaffected.
</p>
HTML
The CSS:
p.capitalize {
text-transform: capitalize;
}
CSS
Try it Yourself
Result:
text-transform Values
Using text-transform property you can make text appear in all-uppercase or all-lowercase. Here is
an example:
The HTML:
The CSS:
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
CSS
Try it Yourself
Result:
The letter-spacing property specifies the space between characters in a text. The values can be set
as:
- normal defines the default style with no extra space between characters
- length defines an extra space between characters using measurement units like px, pt, cm, mm,
etc.;
- inherit inherits the property from its parent element;
The HTML:
The CSS:
p.normal {
letter-spacing: normal;
}
p.positive {
letter-spacing: 4px;
}
CSS
Try it Yourself
Result:
For defining an extra space between characters, negative values are also permitted.
Here is an example demonstrating the difference between positive and negative values:
The HTML:
The CSS:
p.positive {
letter-spacing: 4px;
}
p.negative {
letter-spacing: -1.5px;
}
CSS
Try it Yourself
Result:
The word-spacing property specifies the space between words in a text. Just like the letter-spacing
property, you can set the word-spacing values as normal, length, and inherit.
The HTML:
The CSS:
p.normal {
word-spacing: normal;
}
p.px {
word-spacing: 30px;
}
CSS
Try it Yourself
Result:
When a weird spacing is used, and it is necessary to keep the selected paragraph with normal word
spacing, the normal option is usually used.
Measurement Units
To define an extra space between words, you can use positive measurement values like px, pt, pc,
cm, mm, inches, em, and ex.
Negative values are also permitted. Here is an example to show the difference.
The HTML:
The CSS:
p.positive {
word-spacing: 20px;
}
p.negative {
word-spacing: -5px;
}
CSS
Try it Yourself
Result:
The white-space property specifies how white-space inside an element is handled. The values can
be set as normal, inherit, nowrap, etc.
The nowrap value makes the text continue on the same line until a <br> tag is encountered, and
also collapses all sequences of whitespace into a single whitespace.
The HTML:
<p>
This paragraph has multiple spaces and
a line break, but it will be ignored, as we used the nowrap value.
</p>
HTML
The CSS:
p{
white-space: nowrap;
}
CSS
Try it Yourself
Result:
The text will continue on the same line until a <br /> tag is encountered.
The HTML:
<p class="pre">
In the markup we have multiple spaces
and a line break.
</p>
<p class="preline">
In the markup we have multiple spaces
and a line break, but in the result multiple spaces are ignored.
</p>
<p class="prewrap">
In the markup we have multiple
spaces and a line break.
</p>
HTML
The CSS:
p.pre {
white-space: pre;
}
p.preline {
white-space: pre-line;
}
p.prewrap {
white-space: pre-wrap;
}
CSS
Try it Yourself
Result:
Pre-wrap value behaves as the pre value, except that it adds extra line breaks to prevent the text
breaking out of the element's box.
All HTML elements can be considered as boxes. The CSS box model represents the design and
layout of the site. It consists of margins, borders, paddings, and the actual content.
The term "box model" is used when talking about design and layout.
CSS uses the box model to determine how big the boxes are and how to place them.
The box model is also used to calculate the actual width and height of the HTML elements.
When working with boxes, it is important to understand how the total width of an element is
calculated.
For example, the total width of the box with paddings will be the sum of width plus padding
left and padding right
Here is another box with margins, border, and paddings.
The total width is the sum of left and right margins, left and right borders, left and right
paddings, and the actual width of the content.
When you set the width and height properties of an element with CSS, you set the width and height
of the content area.
When setting a background-color to a box, it covers the content area, as well as the padding.
The total height of an element is calculated the same way as the width.
The example below is the same box from the previous lesson with padding, border and margin.
To summarize, Total element height = height + top padding + bottom padding + top border +
bottom border + top margin + bottom margin
The CSS border property allows you to customize the borders of HTML elements.
In order to add a border to the element, you need to specify the size, style, and color of the border.
The example below shows how to add a solid green border to the paragraph.
The HTML:
The CSS:
p{
padding: 10px;
border: 5px solid green;
}
CSS
Try it Yourself
Result:
Border Width
The properties for the border can be set separately. The border-width property specifies the width
of the border.
The HTML:
<p class="first">
Border width of this paragraph is set to 2px.
</p>
<p class="second">
Border width of this paragraph is set to 5px.
</p>
HTML
The CSS:
p.first {
padding: 10px;
border-style: solid;
border-width: 2px;
}
p.second {
padding: 10px;
border-style: solid;
border-width: 5px;
}
CSS
Try it Yourself
Result:
Border Color
The border color of the element can be defined using a color name, RGB, or Hex values.
The HTML:
<p class="first">
Border color has been created using <strong>color name.</strong>
</p>
<p class="second">
Border color has been created using <strong>Hex values.</strong>
</p>
<p class="third">
Border color has been created using <strong>RGB values.</strong>
</p>
HTML
The CSS:
p.first {
padding: 10px;
border-style: solid;
border-width: 2px;
border-color: blue;
}
p.second {
padding: 10px;
border-style: solid;
border-width: 2px;
border-color: #FF6600;
}
p.third {
padding: 10px;
border-style: solid;
border-width: 2px;
border-color: rgb(0, 153, 0);
}
CSS
Try it Yourself
Result:
None of the border properties will have any effect unless the border-style property is set.
The HTML:
The CSS:
Try it Yourself
Result:
In CSS, it is possible to specify different borders for different sides, using the following properties:
border-top-style, border-right-style, border-bottom-style, and border-left-style for the corresponding
sides.
The HTML:
The CSS:
div {
border: 5px solid green;
width: 90px;
height: 90px;
}
CSS
Try it Yourself
Result:
The total width and height of the box will be the 90px+5px (border)+5px(border) = 100px;
The HTML:
<div>The total width of this element is <strong>100%</strong> and the total height is
<strong>100px</strong> .</div>
HTML
The CSS:
div {
border: 5px solid green;
width: 100%;
height: 90px;
}
CSS
Try it Yourself
Result:
To set the minimum and maximum height and width of an element, you can use the following
properties:
In the example below, we set the minimum height and maximum width of different paragraphs to
100px.
The HTML:
The CSS:
p.first {
border: 5px solid green;
min-height: 100px;
}
p.second {
border: 5px solid green;
max-width: 100px;
}
CSS
Try it Yourself
Result:
The HTML:
The CSS:
body {
background-color: #87CEFA;
}
CSS
Try it Yourself
Result:
The color of the background can be defined using three different formats: a color
name, hexadecimal values, and RGB.
In the example below, the body, h1, and p elements are assigned different background colors:
The HTML:
<h1>This is a heading</h1>
<p>This is a paragraph</p>
HTML
The CSS:
body {
background-color: #C0C0C0;
}
h1 {
background-color: rgb(135,206,235);
}
p{
background-color: LightGreen;
}
CSS
Try it Yourself
Result:
The CSS:
body {
background-image: url("css_logo.png");
background-color: #e9e9e9;
}
CSS
Try it Yourself
The url specifies the path to the image file. Both relative and absolute paths are supported.
Result:
By default, a background-image is placed at the top-left corner of an element, and is repeated both
vertically and horizontally to cover the entire element.
Background-image can be set not only for the whole page, but for individual elements, as well.
Below we set a background image for the <p> element.
The HTML:
The CSS:
p{
padding: 30px;
background-image: url("green_photo.jpg");
color: white;
}
CSS
Try it Yourself
Result:
To specify more than one image, just separate the URLs with commas.
Well done!
Continue
The background repeat property specifies how background images are repeated. A background
image can be repeated along the horizontal axis, the vertical axis both axes, or not repeated at
all.
The CSS:
body {
background-image: url("css_logo.png");
background-repeat: repeat-x;
}
CSS
Try it Yourself
Result: The repeat-y will repeat a
background-image only vertically.
The CSS:
body {
background-image: url("css_logo.png");
background-repeat: repeat-y;
}
CSS
Try it Yourself
Result:
If you want the image to be shown only once, use the no-repeat value.
When you set the background-repeat property to inherit, it will take the same specified value as the
property for the element's parent.
For example, we set the body background repeat only horizontally. If we set some paragraph
background-repeat values to be inherited, they will take the same property value as the body
element.
The CSS:
body {
background-image: url("css_logo.png");
background-repeat: repeat-x;
}
p{
background-image: url("css_logo.png");
background-repeat: inherit;
margin-top: 100px;
padding: 40px;
}
CSS
Try it Yourself
Result:
The background-attachment property sets whether a background image is fixed or scrolls with the
rest of the page.
Even if an element has a scrolling mechanism, a "fixed" background doesn't move with the element.
The CSS:
body {
background-image: url("css_logo.png");
background-repeat: no-repeat;
background-attachment: fixed;
}
CSS
Try it Yourself
Result:
When you set the background-attachment to scroll, the background image will scroll with the rest
of the content.
The CSS:
body {
background-image: url("css_logo.png");
background-repeat: no-repeat;
background-attachment: scroll;
}
CSS
Try it Yourself
Result:
The CSS list properties allow us to set different list item markers. In HTML, there are two 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
With CSS, lists can be styled further, and images can be used as the list item marker.
One of the ways is to use the list-style-type property, which can be set
to circle, square, decimal, disc, lower-alpha, etc.
The HTML:
<ol class="lower-alpha">
<li>Red</li>
<li>Green</li>
<li>Blue</li>
</ol>
<ul class="circle">
<li>Red</li>
<li>Green</li>
<li>Blue</li>
</ul>
<ul class="square">
<li>Red</li>
<li>Green</li>
<li>Blue</li>
</ul>
HTML
The CSS:
ol.lower-alpha {
list-style-type: lower-alpha;
}
ul.circle {
list-style-type: circle;
}
ul.square {
list-style-type: square;
}
CSS
Try it Yourself
Result:
Some of the values are for unordered lists, and some for ordered lists.
Continue
In the example below, we use an image as the list item marker, and specify the position to be inside
of the content flow.
The HTML:
The CSS:
ul {
list-style-image: url("logo.jpg");
list-style-position: inside;
}
CSS
Try it Yourself
Result:
ul {
list-style: square outside none;
}
CSS
ul {
list-style-type: square;
list-style-position: outside;
list-style-image: none;
}
CSS
Try it Yourself
Result:
If one of the property values are missing, the default value for the missing property will be inserted,
if any.
Well done!
The border-collapse property specifies whether the table borders are collapsed into a single border
or separated as default. If the borders are separate, the border-spacing property can be used to
change the spacing.
The HTML:
<table border="1">
<tr>
<td>Red</td>
<td>Green</td>
</tr>
<tr>
<td>Blue</td>
<td>Yellow</td>
</tr>
</table>
HTML
The CSS:
table {
border-collapse: separate;
border-spacing: 20px 40px;
}
CSS
Try it Yourself
Result:
The caption-side property specifies the position of a table caption. The values can be set
as top or bottom.
In the example below, we specify the placement of a table caption to top.
The HTML:
<table border="1">
<caption>Some of Our Courses</caption>
<tr>
<th>Course name</th>
<th>Lessons</th>
<th>Quizzes</th>
</tr>
<tr>
<td>C++</td>
<td>81</td>
<td>363</td>
</tr>
<tr>
<td>JavaScript</td>
<td>48</td>
<td>144</td>
</tr>
<tr>
<td>HTML</td>
<td>38</td>
<td>119</td>
</tr>
<tr>
<td>CSS</td>
<td>70</td>
<td>174</td>
</tr>
</table>
HTML
The CSS:
caption {
caption-side: top;
}
CSS
Try it Yourself
Result:
Here is the empty-cells property that is used to hide borders of empty cells in the <table> element.
The HTML:
<table border="1">
<tr>
<td>HTML</td>
<td>CSS</td>
</tr>
<tr>
<td>JavaScript</td>
<td></td>
</tr>
</table>
HTML
The CSS:
table {
border-collapse: separate;
empty-cells: hide;
}
CSS
Try it Yourself
Result:
The table-layout specifies how the width of table columns is calculated. The possible values are:
auto - when column or cell width are not explicitly set, the column width will be in proportion to
the amount of content in the cells that make up the column
fixed - when column or cell width are not explicitly set, the column width will not be affected by
the amount of content in the cells that make up the column.
The HTML:
The CSS:
table {
border-collapse: separate;
width: 100%;
border: 1px solid gray;
}
td {
border: 1px solid gray;
}
table.auto {
table-layout: auto;
}
table.fixed {
table-layout: fixed;
}
CSS
Try it Yourself
Result:
Links can be styled with any CSS property (e.g., color, font-family, background, etc.).
In addition, links can be styled differently, depending on what state they are in. The following
pseudo selectors are available:
a:link - defines the style for normal unvisited links
a:visited - defines the style for visited links
a:active - a link becomes active once you click on it
a:hover - a link is hovered when the mouse is over it
The example below creates a link that will change the style when the mouse is moved over it.
The HTML:
The CSS:
a:hover {
color: red;
}
CSS
Try it Yourself
The link from the above example would look like this:
When setting the style for several link states, there are some order rules:
- a:hover MUST come after a:link and a:visited
- a:active MUST come after a:hover
The HTML:
The CSS:
a:link {
text-decoration: none;
}
CSS
Try it Yourself
Result:
The following properties are used to control the look and feel of links:
border:none - removes border from images with links
outline:none - removes the dotted border on clicked lines in IE
CSS allows you to set your desired cursor style when you mouse over an element. For example, you
can change your cursor into a hand icon, help icon, and much more, rather than using the default
pointer.
In the example below, the mouse pointer is set to a help icon when we mouse over the span
element:
<span style="cursor:help;">
Do you need help?
</span>
HTML
Try it Yourself
Result:
There are numerous other possible values for the cursor property, such as:
default - default cursor
crosshair - cursor displays as crosshair
pointer - cursor displays hand icon
The list of possible values is quite long.
The image below demonstrates the various available styles:
CSS allows you to set your desired cursor style when you mouse over an element.
For example, if the cursor value is set to default, users may not "see" the links.
display: block
The HTML:
<span>First paragraph.</span>
<span>Second paragraph.</span>
<span>Third paragraph.</span>
<span>Fourth paragraph.</span>
<span>Fifth paragraph.</span>
HTML
The CSS:
span {
display: block;
}
CSS
Try it Yourself
Result:
display: inline
An inline element only takes up as much width as necessary, and does not force line breaks.
The CSS:
p{
display: inline;
}
CSS
Try it Yourself
The code above will produce the following result:
Setting the display property of an element only changes how the element is displayed, not what kind
of element it is. So, an inline element with display:block is not allowed to have other block
elements inside it.
display:none
display:none hides an element, so it does not take up any space. The element will be hidden, and
the page will be displayed as if the element is not there.
The HTML:
The CSS:
h1 {
display: none;
}
CSS
Try it Yourself
Result:
The visibility property specifies whether an element is visible or hidden. The most common values
are visible and hidden.
Hiding an element can be done by setting the display property to "none" or the visibility property to
"hidden". However, notice that these two methods produce different results:
visibility:hidden hides an element, but it will still take up the same space as before. The element
will be hidden, but it will still affect the layout:
Here is an example:
The HTML:
<h1>This is a heading</h1>
<div class="hidden">
This text will not display in browser.
</div>
<p>
The space above this paragraph is empty because
the visibility of the div element is set to hidden.
</p>
HTML
The CSS:
div.hidden {
visibility: hidden;
}
CSS
Try it Yourself
Result: display:none hides
an element, without holding a place for that element.
div.hidden {
display: none;
}
CSS
Try it Yourself
Result:
Positioning Elements
The CSS positioning properties allow you to position an element. It can also place an element
behind another, and specify what should happen when an element's content is too big.
Elements can be 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 positioning method.
Static Positioning
HTML elements are positioned static by default. A static positioned element is always positioned
according to the normal flow of the page.
The HTML:
The CSS:
p.position_static {
position:static;
top: 30px;
right: 5px;
color: red;
}
CSS
Try it Yourself
Result:
Static positioned elements are not affected by the top, bottom, left, and right properties.
Fixed Positioning
An element with a fixed position is positioned relative to the browser window, and will not move
even if the window is scrolled.
The position can be specified using one or more of the properties top, right, bottom, and left.
In the example below, the paragraph is fixed to 30px from the top and 5px from the right.
The CSS:
p.position_fixed {
position: fixed;
top: 30px;
right: 5px;
color: red;
}
CSS
Try it Yourself
Result:
Fixed positioned elements are removed from the normal flow. The document and other elements
behave like the fixed positioned element does not exist.
Fixed positioned elements can overlap other elements.
279 Comments
Relative Positioning
The CSS:
p{
width: 350px;
border: 1px black solid;
position: fixed;
}
span {
background: green;
color:white;
position: relative;
top: 150px;
left: 50px;
}
CSS
Try it Yourself
Result:
The content of relatively positioned elements can be moved and overlap other elements, but the
reserved space for the element is still preserved in the normal flow.
Absolute Positioning
An absolute position element is positioned relative to the first parent element that has a position
other than static. If no such element is found, the containing block is <html>.
Absolutely positioned elements are removed from the normal flow. The document and other
elements behave like the absolutely positioned element does not exist.
With CSS float, an element can be pushed to the left or right, allowing other elements to wrap
around it.
Float is often used with images, but it is also useful when working with layouts.
The values for the float property are left, right, and none.
Left and right float elements in those directions, respectively. none (the default) ensures that the
element will not float.
Below is an example of an image that is floated to the right.
The HTML:
The CSS:
img {
float: right;
}
CSS
Try it Yourself
Result:
Elements are floated horizontally, meaning that an element can only be floated left or right, not up
or down.
If you place several floating elements one after the other, they will float next to each other if there is
enough room.
As an example, in a print layout, images may be set into the page such that text wraps around them
as needed.
The CSS:
img {
float: left;
width: 120px;
margin-right: 10px;
}
p{
width: 120px;
float: left;
}
CSS
Try it Yourself
Result:
Elements that come after the floating element will flow around it. To avoid this, use the clear
property.
The clear property specifies the sides of an element where other floating elements are not allowed
to be.
In the example below, if we set the float property to the div, only the paragraph that comes after the
div will be wrapped around the image.
The HTML:
The CSS:
.floating {
float: right;
}
CSS
Try it Yourself
Result:
Using clear
Use the values right, left, and both to specify the sides of an element where other floating elements
are not allowed to be.
The default value is none, which allows floating elements on both sides.
Clearing Floats
both is used to clear floats coming from either direction.
The HTML:
The CSS:
.floating {
float: right;
}
.clearing {
clear: both;
}
CSS
Try it Yourself
Result:
As discussed earlier, every element on the page is a box. If the height of the box is not set, the
height of that box will grow as large as necessary to accommodate the content.
The HTML:
<div>
This text is inside the div element, which has a blue
background color and is floated to the left. We set a specific
height and width for the div element, and as you can see,
the content cannot fit.
</div>
HTML
The CSS:
div {
width: 150px;
height: 150px;
background-color: LightBlue;
float: left;
}
CSS
Try it Yourself
Result:
The CSS overflow property specifies the behavior that occurs when an element's content overflows
the element's box.
There are four values for the overflow property: visible (the default value), scroll, hidden,
and auto.
The value scroll results in clipped overflow, but a scrollbar is added, so the rest of the content may
be seen.
The CSS:
div {
width: 150px;
height: 150px;
background-color: LightBlue;
float: left;
overflow: scroll;
}
CSS
Try it Yourself
The code above will produce both horizontal and vertical scrollbars:
auto - If overflow is clipped, a scroll-bar should be added to make it possible to see the rest of the
content.
hidden - The overflow is clipped, and the rest of the content will be invisible.
The CSS:
div {
width: 150px;
height: 150px;
background-color: LightBlue;
float: left;
overflow: hidden;
}
CSS
Try it Yourself
Result:
When elements are positioned outside the normal flow, 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).
The CSS:
.blue {
background-color: #8EC4D0;
margin-bottom: 15px;
width: 120px;
height: 120px;
color: #FFF;
}
.red {
background-color: #FF4D4D;
position: relative;
width: 120px;
height: 120px;
color: #FFF;
margin-top: -50px;
margin-left: 50px;
}
CSS
Try it Yourself
Result:
The red box overlaps the blue box, because it was placed later in the HTML markup.
The z-index property can change this order.
Assigning a higher z-index value to the blue div and a lower z-index value to the red div will result
in the following:
The CSS:
.blue {
z-index: 3;
position: relative;
}
.red {
z-index: 2;
position: relative;
}
CSS
Try it Yourself
Result: