Your cart is currently empty!
Learn CSS (Full Tutorial)

CSSย Introduction (#1)
CSS is the language we use to style a Web page.
What is CSS?
- CSS stands for Cascading Style Sheets
- CSS describes how HTML elements are to be displayed on screen, paper, or in other media
- CSS saves a lot of work. It can control the layout of multiple web pages all at once
- External stylesheets are stored in CSS files
CSS Demo โ One HTML Page โ Multiple Styles!
Here we will show one HTML page displayed with four different stylesheets.
Why Use CSS?
CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.
CSS Example
bodyย {
ย ย background-color:ย lightblue;
}
h1ย {
ย ย color:ย white;
ย ย text-align:ย center;
}
pย {
ย font-family:ย verdana;
ย ย font-size:ย 20px;
}
CSS Solved a Big Problem
HTML was NEVER intended to contain tags for formatting a web page!
HTML was created to describe the content of a web page, like:
<h1>This is a heading</h1> <p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers. Development of large websites, where fonts and color information were added to every single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (Swift Games) created CSS.
CSS removed the style formatting from the HTML page!
If you donโt know what HTML is, we suggest that you read ourย HTML Tutorial.
CSS Saves a Lot of Work!
The style definitions are normally saved in external .css files.
With an external stylesheet file, you can change the look of an entire website by changing just one file!
CSSย Syntax (#2)
A CSS rule consists of a selector and a declaration block.
CSS Syntax

The selector points to the HTML element you want to style.
The declaration block contains one or more declarations separated by semicolons.
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces.
Example
In this example all <p> elements will be center-aligned, with a red text color:
pย {
ย ย color:ย red;
ย ย text-align:ย center;
}
Example Explained
pย is a selector in CSS (it points to the HTML element you want to style: <p>).colorย is a property, andยredย is the property valuetext-alignย is a property, andยcenterย is the property value
CSSย Selectors (#3)
A CSS selector selects the HTML element(s) you want to style.
CSS Selectors
CSS selectors are used to โfindโ (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
- Simple selectors (select elements based on name, id, class)
- Combinator selectorsย (select elements based on a specific relationship between them)
- Pseudo-class selectorsย (select elements based on a certain state)
- Pseudo-elements selectorsย (select and style a part of an element)
- Attribute selectorsย (select elements based on an attribute or attribute value)
This page will explain the most basic CSS selectors.
The CSS element Selector
The element selector selects HTML elements based on the element name.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
pย {
ย ย text-align:ย center;
ย ย color:ย red;
}
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Example
The CSS rule below will be applied to the HTML element with id=โpara1โณ:
#para1ย {
ย ย text-align:ย center;
ย ย color:ย red;
}
Note:ย An id name cannot start with a number!
The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with class=โcenterโ will be red and center-aligned:
.centerย {
ย text-align:ย center;
ย ย color:ย red;
}
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only <p> elements with class=โcenterโ will be red and center-aligned:
p.centerย {
ย text-align:ย center;
ย ย color:ย red;
}
HTML elements can also refer to more than one class.
Example
In this example the <p> element will be styled according to class=โcenterโ and to class=โlargeโ:
<pย class="center large">This paragraph refers to two classes.</p>
Note:ย A class name cannot start with a number!
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
*ย {
ย ย text-align:ย center;
ย ย color:ย blue;
}
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h1ย {
ย ย text-align:ย center;
ย ย color:ย red;
}
h2ย {
ย text-align:ย center;
ย color:ย red;
}
pย {
ย ย text-align:ย center;
ย ย color:ย red;
}
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
h1, h2, pย {
ย text-align:ย center;
ย ย color:ย red;
}
How To Add CSS (#4)
When a browser reads a style sheet, it will format the HTML document according to the information in the style sheet.
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
- External CSS
- Internal CSS
- Inline CSS
External CSS
With an external style sheet, you can change the look of an entire website by changing just one file!
Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.
Example
External styles are defined within the <link> element, inside the <head> section of an HTML page:
<!DOCTYPEย html> <html> <head> <linkย rel="stylesheet"ย href="mystyle.css"> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
Here is how the โmystyle.cssโ file looks:
โmystyle.cssโ
bodyย {
ย ย background-color:ย lightblue;
}
h1ย {
ย ย color:ย navy;
ย ย margin-left:ย 20px;
}
Note:ย Do not add a space between the property value and the unit:
Incorrect (space):ย margin-left: 20 px;
Correct (nospace):ย margin-left: 20px;
Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
<!DOCTYPEย html> <html> <head> <style> bodyย { ย background-color:ย linen; } h1ย { ย color:ย maroon; ย margin-left:ย 40px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.
Example
Inline styles are defined within the โstyleโ attribute of the relevant element:
<!DOCTYPEย html> <html> <body> <h1ย style="color:blue;text-align:center;">This is a heading</h1> <pย style="color:red;">This is a paragraph.</p> </body> </html>
Tip:ย An inline style loses many of the advantages of a style sheet (by mixing content with presentation). Use this method sparingly.
Multiple Style Sheets
If some properties have been defined for the same selector (element) in different style sheets, the value from the last read style sheet will be used.
Assume that anย external style sheetย has the following style for the <h1> element:
h1ย {
ย ย color:ย navy;
}
Then, assume that anย internal style sheetย also has the following style for the <h1> element:
h1ย {
ย ย color:ย orange;ย ย ย
}
Example
If the internal style is definedย afterย the link to the external style sheet, the <h1> elements will be โorangeโ:
<head> <linkย rel="stylesheet"ย type="text/css"ย href="mystyle.css"> <style> h1ย { ย ย color:ย orange; } </style> </head>
Example
However, if the internal style is definedย beforeย the link to the external style sheet, the <h1> elements will be โnavyโ:
<head> <style> h1ย { ย ย color:ย orange; } </style> <linkย rel="stylesheet"ย type="text/css"ย href="mystyle.css"> </head>
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will โcascadeโ into a new โvirtualโ style sheet by the following rules, where number one has the highest priority:
- Inline style (inside an HTML element)
- External and internal style sheets (in the head section)
- Browser default
So, an inline style has the highest priority, and will override external and internal styles and browser defaults.
CSSย Comments (#5)
CSS comments are not displayed in the browser, but they can help document your source code.
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later date.
Comments are ignored by browsers.
A CSS comment is placed inside theย <style>ย element, and starts withย /*ย and ends withย */:
Example
/* This is a single-line comment */
pย {
ย color:ย red;
}
You can add comments wherever you want in the code:
Example
pย {
ย color:ย red;ย ย /* Set text color to red */
}
Comments can also span multiple lines:
Example
/* This is
a multi-line
comment */
pย {
ย color:ย red;
}
HTML and CSS Comments
From the HTML tutorial, you learned that you can add comments to your HTML source by using theย <!--...-->ย syntax.
In the following example, we use a combination of HTML and CSS comments:
Example
<!DOCTYPEย html> <html> <head> <style> pย { ย color:ย red;ย /* Set text color to red */ } </style> </head> <body> <h2>My Heading</h2> <!-- These paragraphs will be red --> <p>Hello World!</p> <p>This paragraph is styled with CSS.</p> <p>CSS comments are not shown in the output.</p> </body> </html>
CSSย Backgrounds (#6)
The CSS background properties are used to add background effects for elements.
In these chapters, you will learn about the following CSS background properties:
background-colorbackground-imagebackground-repeatbackground-attachmentbackground-positionbackgroundย (shorthand property)
CSS background-color
Theย background-colorย property specifies the background color of an element.
Example
The background color of a page is set like this:
bodyย {
ย ย background-color:ย lightblue;
}
With CSS, a color is most often specified by:
- a valid color name โ like โredโ
- a HEX value โ like โ#ff0000โ
- an RGB value โ like โrgb(255,0,0)โ
Other Elements
You can set the background color for any HTML elements:
Example
Here, the <h1>, <p>, and <div> elements will have different background colors:
h1ย {
ย ย background-color:ย green;
}
divย {
ย ย background-color:ย lightblue;
}
pย {
ย ย background-color:ย yellow;
}
Opacity / Transparency
Theย opacityย property specifies the opacity/transparency of an element. It can take a value from 0.0 โ 1.0. The lower value, the more transparent:
opacity 1
opacity 0.6
opacity 0.3
opacity 0.1
Example
divย {
ย background-color:ย green;
ย ย opacity:ย 0.3;
}
Note:ย When using theย opacityย property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.
Transparency using RGBA
If you do not want to apply opacity to child elements, like in our example above, useย RGBAย color values. The following example sets the opacity for the background color and not the text:
100% opacity
60% opacity
30% opacity
10% opacity
An RGBA color value is specified with: rgba(red, green, blue,ย alpha). Theย alphaย parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
Example
divย {
ย background:ย rgba(0, 128, 0, 0.3)ย /* Green background with 30% opacity */
}
CSSย Background Image (#7)
CSS background-image
Theย background-imageย property specifies an image to use as the background of an element.
By default, the image is repeated so it covers the entire element.
Example
Set the background image for a page:
bodyย {
ย ย background-image:ย url("paper.gif");
}
Example
This example shows aย bad combinationย of text and background image. The text is hardly readable:
bodyย {
ย ย background-image:ย url("bgdesert.jpg");
}
Note:ย When using a background image, use an image that does not disturb the text.
The background image can also be set for specific elements, like the <p> element:
Example
pย {
ย ย background-image:ย url("paper.gif");
}
CSSย Background Image Repeat (#8)
CSS background-repeat
By default, theย background-imageย property repeats an image both horizontally and vertically.
Some images should be repeated only horizontally or vertically, or they will look strange, like this:
Example
bodyย {
ย ย background-image:ย url("gradient_bg.png");
}
If the image above is repeated only horizontally (background-repeat: repeat-x;), the background will look better:
Example
bodyย {
ย ย background-image:ย url("gradient_bg.png");
ย ย background-repeat:ย repeat-x;
}
Tip:ย To repeat an image vertically, setย background-repeat: repeat-y;
CSS background-repeat: no-repeat
Showing the background image only once is also specified by theย background-repeatย property:
Example
Show the background image only once:
bodyย {
ย ย background-image:ย url("img_tree.png");
ย ย background-repeat:ย no-repeat;
}
In the example above, the background image is placed in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much.
CSS background-position
Theย background-positionย property is used to specify the position of the background image.
Example
Position the background image in the top-right corner:
bodyย {
ย background-image:ย url("img_tree.png");
ย background-repeat:ย no-repeat;
ย background-position:ย right top;
}
CSSย Background Attachment (#9)
CSS background-attachment
Theย background-attachmentย property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page):
Example
Specify that the background image should be fixed:
bodyย {
ย ย background-image:ย url("img_tree.png");
ย ย background-repeat:ย no-repeat;
ย ย background-position:ย right top;
ย background-attachment:ย fixed;
}
Example
Specify that the background image should scroll with the rest of the page:
bodyย {
ย ย background-image:ย url("img_tree.png");
ย ย background-repeat:ย no-repeat;
ย ย background-position:ย right top;
ย background-attachment:ย scroll;
}
CSSย Background Shorthand (#10)
CSS background โ Shorthand property
To shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property.
Instead of writing:
bodyย {
ย background-color:ย #ffffff;
ย background-image:ย url("img_tree.png");
ย background-repeat:ย no-repeat;
ย background-position:ย right top;
}
You can use the shorthand propertyย background:
Example
Use the shorthand property to set the background properties in one declaration:
bodyย {
ย background:ย #ffffff url("img_tree.png") no-repeat right top;
}
When using the shorthand property the order of the property values is:
background-colorbackground-imagebackground-repeatbackground-attachmentbackground-position
It does not matter if one of the property values is missing, as long as the other ones are in this order. Note that we do not use the background-attachment property in the examples above, as it does not have a value.
CSSย Borders (#11)
The CSS border properties allow you to specify the style, width, and color of an elementโs border.
I have borders on all sides.
I have a red bottom border.
I have rounded borders.
CSS Border Style
Theย border-styleย property specifies what kind of border to display.
The following values are allowed:
dottedย โ Defines a dotted borderdashedย โ Defines a dashed bordersolidย โ Defines a solid borderdoubleย โ Defines a double bordergrooveย โ Defines a 3D grooved border. The effect depends on the border-color valueridgeย โ Defines a 3D ridged border. The effect depends on the border-color valueinsetย โ Defines a 3D inset border. The effect depends on the border-color valueoutsetย โ Defines a 3D outset border. The effect depends on the border-color valuenoneย โ Defines no borderhiddenย โ Defines a hidden border
Theย border-styleย property can have from one to four values (for the top border, right border, bottom border, and the left border).
Example
Demonstration of the different border styles:
p.dottedย {border-style:ย dotted;}
p.dashedย {border-style:ย dashed;}
p.solidย {border-style:ย solid;}
p.doubleย {border-style:ย double;}
p.grooveย {border-style:ย groove;}
p.ridgeย {border-style:ย ridge;}
p.insetย {border-style:ย inset;}
p.outsetย {border-style:ย outset;}
p.noneย {border-style:ย none;}
p.hiddenย {border-style:ย hidden;}
p.mixย {border-style:ย dotted dashed solid double;}
Result:
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border. The effect depends on the border-color value.
A ridge border. The effect depends on the border-color value.
An inset border. The effect depends on the border-color value.
An outset border. The effect depends on the border-color value.
No border.
A hidden border.
A mixed border.
CSS Border Width (#12)
Theย border-widthย property specifies the width of the four borders.
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:
Example
Demonstration of the different border widths:
p.oneย {
ย border-style:ย solid;
ย border-width:ย 5px;
}
p.twoย {
ย border-style:ย solid;
ย ย border-width:ย medium;
}
p.threeย {
ย border-style:ย dotted;
ย ย border-width:ย 2px;
}
p.fourย {
ย border-style:ย dotted;
ย ย border-width:ย thick;
}
Result:
Specific Side Widths
Theย border-widthย property can have from one to four values (for the top border, right border, bottom border, and the left border):
Example
p.oneย {
ย border-style:ย solid;
ย border-width:ย 5px 20px;ย /* 5px top and bottom, 20px on the sides */
}
p.twoย {
ย border-style:ย solid;
ย border-width:ย 20px 5px;ย /* 20px top and bottom, 5px on the sides */
}
p.threeย {
ย border-style:ย solid;
ย border-width:ย 25px 10px 4px 35px;ย /* 25px top, 10px right, 4px bottom and 35px left */
}
CSS Border โ Individual Sides (#13)
From the examples on the previous pages, you have seen that it is possible to specify a different border for each side.
In CSS, there are also properties for specifying each of the borders (top, right, bottom, and left):
Example
pย {
ย border-top-style:ย dotted;
ย border-right-style:ย solid;
ย ย border-bottom-style:ย dotted;
ย ย border-left-style:ย solid;
}
Result:
The example above gives the same result as this:
Example
pย {
ย border-style:ย dotted solid;
}
So, here is how it works:
If theย border-styleย property has four values:
- border-style: dotted solid double dashed;
- top border is dotted
- right border is solid
- bottom border is double
- left border is dashed
If theย border-styleย property has three values:
- border-style: dotted solid double;
- top border is dotted
- right and left borders are solid
- bottom border is double
If theย border-styleย property has two values:
- border-style: dotted solid;
- top and bottom borders are dotted
- right and left borders are solid
If theย border-styleย property has one value:
- border-style: dotted;
- all four borders are dotted
Example
/* Four values */
pย {
ย border-style:ย dotted solid double dashed;
}
/* Three values */
pย {
ย border-style:ย dotted solid double;
}
/* Two values */
pย {
ย border-style:ย dotted solid;
}
/* One value */
pย {
ย border-style:ย dotted;
}
Theย border-styleย property is used in the example above. However, it also works withย border-widthย andย border-color.
CSS Border โ Shorthand Property
Like you saw in the previous page, there are many properties to consider when dealing with borders.
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-widthborder-styleย (required)border-color
Example
pย {
ย border:ย 5px solid red;
}
Result:
Some text
You can also specify all the individual border properties for just one side:
Left Border
pย {
ย border-left:ย 6px solid red;
}
Result:
Some text
Bottom Border
pย {
ย border-bottom:ย 6px solid red;
}
Result:
Some text
CSS Rounded Borders
Theย border-radiusย property is used to add rounded borders to an element:
Normal border
Round border
Rounder border
Roundest border
Example
pย {
ย ย border:ย 2px solid red;
ย ย border-radius:ย 5px;
}
More Examples
All the top border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the top border in one declaration.
Set the style of the bottom border
This example demonstrates how to set the style of the bottom border.
Set the width of the left border
This example demonstrates how to set the width of the left border.
Set the color of the four borders
This example demonstrates how to set the color of the four borders. It can have from one to four colors.
Set the color of the right border
This example demonstrates how to set the color of the right border.
CSSย Padding (#14)
Padding is used to create space around an elementโs content, inside of any defined borders.
CSS Padding
The CSSย paddingย properties are used to generate space around an elementโs content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).
Padding โ Individual Sides
CSS has properties for specifying the padding for each side of an element:
padding-toppadding-rightpadding-bottompadding-left
All the padding properties can have the following values:
- lengthย โ specifies a padding in px, pt, cm, etc.
- %ย โ specifies a padding in % of the width of the containing element
- inherit โ specifies that the padding should be inherited from the parent element
Note:ย Negative values are not allowed.
Example
Set different padding for all four sides of a <div> element:
divย {
ย padding-top:ย 50px;
ย ย padding-right:ย 30px;
ย ย padding-bottom:ย 50px;
ย ย padding-left:ย 80px;
}
Padding โ Shorthand Property
To shorten the code, it is possible to specify all the padding properties in one property.
Theย paddingย property is a shorthand property for the following individual padding properties:
padding-toppadding-rightpadding-bottompadding-left
So, here is how it works:
If theย paddingย property has four values:
- padding: 25px 50px 75px 100px;
- top padding is 25px
- right padding is 50px
- bottom padding is 75px
- left padding is 100px
Example
Use the padding shorthand property with four values:
divย {
ย padding:ย 25px 50px 75px 100px;
}
If theย paddingย property has three values:
- padding: 25px 50px 75px;
- top padding is 25px
- right and left paddings are 50px
- bottom padding is 75px
Example
Use the padding shorthand property with three values:
divย {
ย padding:ย 25px 50px 75px;
}
If theย paddingย property has two values:
- padding: 25px 50px;
- top and bottom paddings are 25px
- right and left paddings are 50px
Example
Use the padding shorthand property with two values:
divย {
ย padding:ย 25px 50px;
}
If theย paddingย property has one value:
- padding: 25px;
- all four paddings are 25px
Example
Use the padding shorthand property with one value:
divย {
ย padding:ย 25px;
}
Padding and Element Width
The CSSย widthย property specifies the width of the elementโs content area. The content area is the portion inside the padding, border, and margin of an element.
So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.
Example
Here, the <div> element is given a width of 300px. However, the actual width of the <div> element will be 350px (300px + 25px of left padding + 25px of right padding):
divย {
ย ย width:ย 300px;
ย ย padding:ย 25px;
}
To keep the width at 300px, no matter the amount of padding, you can use theย box-sizingย property. This causes the element to maintain its actual width; if you increase the padding, the available content space will decrease.
Example
Use the box-sizing property to keep the width at 300px, no matter the amount of padding:
divย {
ย width:ย 300px;
ย ย padding:ย 25px;
ย ย box-sizing:ย border-box;
}
CSSย Height, Width and Max-width (#15)
The CSSย heightย andย widthย properties are used to set the height and width of an element.
The CSSย max-widthย property is used to set the maximum width of an element.
CSS Setting height and width
Theย heightย andย widthย properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.
CSS height and width Values
Theย heightย andย widthย properties may have the following values:
autoย โ This is default. The browser calculates the height and widthlengthย โ Defines the height/width in px, cm etc.%ย โ Defines the height/width in percent of the containing blockinitialย โ Sets the height/width to its default valueinheritย โ The height/width will be inherited from its parent value
CSS height and width Examples
Example
Set the height and width of a <div> element:
divย {
ย ย height:ย 200px;
ย width:ย 50%;
ย background-color:ย powderblue;
}
Example
Set the height and width of another <div> element:
divย {
ย height:ย 100px;
ย ย width:ย 500px;
ย ย background-color:ย powderblue;
}
Note:ย Remember that 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!
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!
Note:ย If you for some reason use both theย widthย property and theย max-widthย property on the same element, and the value of theย widthย property is larger than theย max-widthย property; theย max-widthย property will be used (and theย widthย property will be ignored).
Example
This <div> element has a height of 100 pixels and a max-width of 500 pixels:
divย {
ย max-width:ย 500px;
ย height:ย 100px;
ย background-color:ย powderblue;
}
CSSย Box Model (#16)
All HTML elements can be considered as boxes.
The CSS Box Model
In CSS, the term โbox modelโ is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model:
Explanation of the different parts:
- 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
Demonstration of the box model:
divย {
ย ย width:ย 300px;
ย ย border:ย 15px solid green;
ย padding:ย 50px;
ย margin:ย 20px;
}
Width and Height of an Element
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
Important:ย When you set the width and height properties of an element with CSS, you just set the width and height of theย content area. To calculate the full size of an element, you must also add padding, borders and margins.
Example
This <div> element will have a total width of 350px:
divย {
ย width:ย 320px;
ย padding:ย 10px;
ย border:ย 5px solid gray;
ย margin:ย 0;
}
Here is the calculation:
The total width of an element should be calculated like this:
Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height of an element should be calculated like this:
Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
CSSย Outline (#17)
An outline is a line drawn outside the elementโs border.
CSS Outline
An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element โstand outโ.
CSS has the following outline properties:
outline-styleoutline-coloroutline-widthoutline-offsetoutline
Note:ย Outline differs fromย borders! Unlike border, the outline is drawn outside the elementโs border, and may overlap other content. Also, the outline is NOT a part of the elementโs dimensions; the elementโs total width and height is not affected by the width of the outline.
CSS Outline Style
Theย outline-styleย property specifies the style of the outline, and can have one of the following values:
dottedย โ Defines a dotted outlinedashedย โ Defines a dashed outlinesolidย โ Defines a solid outlinedoubleย โ Defines a double outlinegrooveย โ Defines a 3D grooved outlineridgeย โ Defines a 3D ridged outlineinsetย โ Defines a 3D inset outlineoutsetย โ Defines a 3D outset outlinenoneย โ Defines no outlinehiddenย โ Defines a hidden outline
The following example shows the differentย outline-styleย values:
Example
Demonstration of the different outline styles:
p.dottedย {outline-style:ย dotted;}
p.dashedย {outline-style:ย dashed;}
p.solidย {outline-style:ย solid;}
p.doubleย {outline-style:ย double;}
p.grooveย {outline-style:ย groove;}
p.ridgeย {outline-style:ย ridge;}
p.insetย {outline-style:ย inset;}
p.outsetย {outline-style:ย outset;}
Result:
A dotted outline.
A dashed outline.
A solid outline.
A double outline.
A groove outline. The effect depends on the outline-color value.
A ridge outline. The effect depends on the outline-color value.
An inset outline. The effect depends on the outline-color value.
An outset outline. The effect depends on the outline-color value.
CSSย Text (#18)
CSS has a lot of properties for formatting text.
TEXT FORMATTING
This text is styled with some of the text formatting properties. The heading uses the text-align, text-transform, and color properties. The paragraph is indented, aligned, and the space between characters is specified.
Text Color
Theย colorย property is used to set the color of the text. The color is specified by:
- a color name โ like โredโ
- a HEX value โ like โ#ff0000โ
- an RGB value โ like โrgb(255,0,0)โ
The default text color for a page is defined in the body selector.
Example
bodyย {
ย color:ย blue;
}
h1ย {
ย color:ย green;
}
Text Color and Background Color
In this example, we define both theย background-colorย property and theย colorย property:
Example
bodyย {
ย background-color:ย lightgrey;
ย color:ย blue;
}
h1ย {
ย background-color:ย black;
ย color:ย white;
}
divย {
ย background-color:ย blue;
ย color:ย white;
}
Important:ย High contrast is very important for people with vision problems. So, always ensure that the contrast between the text color and the background color (or background image) is good!
CSS Text Alignment and Text Direction
In this chapter you will learn about the following properties:
text-aligntext-align-lastdirectionunicode-bidivertical-align
Text Alignment
Theย text-alignย property is used to set the horizontal alignment of a text.
A text can be left or right aligned, centered, or justified.
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):
Example
h1ย {
ย text-align:ย center;
}
h2ย {
ย ย text-align:ย left;
}
h3ย {
ย ย text-align:ย right;
}
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):
Example
divย {
ย ย text-align:ย justify;
}
Text Align Last
Theย text-align-lastย property specifies how to align the last line of a text.
Example
Align the last line of text in three <p> elements:
p.aย {
ย text-align-last:ย right;
}
p.bย {
ย text-align-last:ย center;
}
p.cย {
ย ย text-align-last:ย justify;
}
Text Direction
Theย directionย andย unicode-bidiย properties can be used to change the text direction of an element:
Example
pย {
ย ย direction:ย rtl;
ย unicode-bidi:ย bidi-override;
}
Vertical Alignment
Theย vertical-alignย property sets the vertical alignment of an element.
Example
Set the vertical alignment of an image in a text:
img.aย {
ย vertical-align:ย baseline;
}
img.bย {
ย vertical-align:ย text-top;
}
img.cย {
ย vertical-align:ย text-bottom;
}
img.dย {
ย vertical-align:ย sub;
}
img.eย {
ย vertical-align:ย super;
}
CSSย Text Shadow (#19)
Text Shadow
Theย text-shadowย property adds shadow to text.
In its simplest use, you only specify the horizontal shadow (2px) and the vertical shadow (2px):
Text shadow effect!
Example
h1ย {
ย ย text-shadow:ย 2px 2px;
}
Next, add a color (red) to the shadow:
Text shadow effect!
Example
h1ย {
ย ย text-shadow:ย 2px 2px red;
}
Then, add a blur effect (5px) to the shadow:
Text shadow effect!
Example
h1ย {
ย ย text-shadow:ย 2px 2px 5px red;
}
More Text Shadow Examples
Example 1
Text-shadow on a white text:
h1ย {
ย color:ย white;
ย text-shadow:ย 2px 2px 4px #000000;
}
Example 2
Text-shadow with red neon glow:
h1ย {
ย text-shadow:ย 0 0 3px #ff0000;
}
Example 3
Text-shadow with red and blue neon glow:
h1ย {
ย text-shadow:ย 0 0 3px #ff0000, 0 0 5px #0000ff;
}
Example 4
h1ย {
ย color:ย white;
ย text-shadow:ย 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
}
CSSย Fonts (#20)
Choosing the right font for your website is important!
Font Selection is Important
Choosing the right font has a huge impact on how the readers experience a website.
The right font can create a strong identity for your brand.
Using a font that is easy to read is important. The font adds value to your text. It is also important to choose the correct color and text size for the font.
Generic Font Families
In CSS there are five generic font families:
- Serifย fonts have a small stroke at the edges of each letter. They create a sense of formality and elegance.
- Sans-serifย fonts have clean lines (no small strokes attached). They create a modern and minimalistic look.
- Monospaceย fonts โ here all the letters have the same fixed width. They create a mechanical look.
- Cursiveย fonts imitate human handwriting.
- Fantasyย fonts are decorative/playful fonts.
All the different font names belong to one of the generic font families.
Difference Between Serif and Sans-serif Fonts

Note:ย On computer screens, sans-serif fonts are considered easier to read than serif fonts.
Some Font Examples
| Generic Font Family | Examples of Font Names |
|---|---|
| Serif | Times New Roman Georgia Garamond |
| Sans-serif | Arial Verdana Helvetica |
| Monospace | Courier New Lucida Console Monaco |
| Cursive | Brush Script MT Lucida Handwriting |
| Fantasy | Copperplate Papyrus |
The CSS font-family Property
In CSS, we use theย font-familyย property to specify the font of a text.
Note: If the font name is more than one word, it must be in quotation marks, like: โTimes New Romanโ.
Tip:ย Theย font-familyย property should hold several font names as a โfallbackโ system, to ensure maximum compatibility between browsers/operating systems. 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). The font names should be separated with comma.
Example
Specify some different fonts for three paragraphs:
.p1ย {
ย font-family:ย "Times New Roman", Times, serif;
}
.p2ย {
ย font-family:ย Arial, Helvetica, sans-serif;
}
.p3ย {
ย font-family:ย "Lucida Console", "Courier New", monospace;
}
CSSย Icons (#21)
Icons can easily be added to your HTML page, by using an icon library.
How To Add Icons
The simplest way to add an icon to your HTML page, is with an icon library, such as Font Awesome.
Add the name of the specified icon class to any inline HTML element (likeย <i>ย orย <span>).
All the icons in the icon libraries below, are scalable vectors that can be customized with CSS (size, color, shadow, etc.)
Font Awesome Icons
To use the Font Awesome icons, go toย fontawesome.com, sign in, and get a code to add in theย <head>ย section of your HTML page:
<script src="https://kit.fontawesome.com/yourcode.js" crossorigin="anonymous"></script>
Note:ย No downloading or installation is required!
Example
<!DOCTYPEย html> <html> <head> <scriptย src="https://kit.fontawesome.com/a076d05399.js"ย crossorigin="anonymous"></script> </head> <body> <iย class="fas fa-cloud"></i> <iย class="fas fa-heart"></i> <iย class="fas fa-car"></i> <iย class="fas fa-file"></i> <iย class="fas fa-bars"></i> </body> </html>
Bootstrap Icons
To use the Bootstrap glyphicons, add the following line inside theย <head>ย section of your HTML page:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
Note:ย No downloading or installation is required!
Example
<!DOCTYPEย html>
<html>
<head>
<linkย rel=โstylesheetโย href=โhttps://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.cssโ>
</head>
<body><iย class=โglyphicon glyphicon-cloudโ></i>
<iย class=โglyphicon glyphicon-removeโ></i>
<iย class=โglyphicon glyphicon-userโ></i>
<iย class=โglyphicon glyphicon-envelopeโ></i>
<iย class=โglyphicon glyphicon-thumbs-upโ></i></body>
</html>
Result:
Google Icons
To use the Google icons, add the following line inside theย <head>ย section of your HTML page:
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
Note:ย No downloading or installation is required!
Example
<!DOCTYPEย html> <html> <head> <linkย rel="stylesheet"ย href="https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <iย class="material-icons">cloud</i> <iย class="material-icons">favorite</i> <iย class="material-icons">attachment</i> <iย class="material-icons">computer</i> <iย class="material-icons">traffic</i> </body> </html>
CSSย Links (#22)
With CSS, links can be styled in many different ways.
Text Linkย Text Linkย Link Buttonย Link Button
Styling Links
Links can be styled with any CSS property (e.g.ย color,ย font-family,ย background, etc.).
Example
aย {
ย ย color:ย hotpink;
}
In addition, links can be styled differently depending on whatย stateย they are in.
The four links states are:
a:linkย โ a normal, unvisited linka:visitedย โ a link the user has visiteda:hoverย โ a link when the user mouses over ita:activeย โ a link the moment it is clicked
Example
/* unvisited link */
a:linkย {
ย color:ย red;
}
/* visited link */
a:visitedย {
ย color:ย green;
}
/* mouse over link */
a:hoverย {
ย ย color:ย hotpink;
}
/* selected link */
a:activeย {
ย color:ย blue;
}
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
Text Decoration
Theย text-decorationย property is mostly used to remove underlines from links:
Example
a:linkย {
ย text-decoration:ย none;
}
a:visitedย {
ย text-decoration:ย none;
}
a:hoverย {
ย ย text-decoration:ย underline;
}
a:activeย {
ย ย text-decoration:ย underline;
}
Background Color
Theย background-colorย property can be used to specify a background color for links:
Example
a:linkย {
ย ย background-color:ย yellow;
}
a:visitedย {
ย background-color:ย cyan;
}
a:hoverย {
ย ย background-color:ย lightgreen;
}
a:activeย {
ย ย background-color:ย hotpink;
}ย
Link Buttons
This example demonstrates a more advanced example where we combine several CSS properties to display links as boxes/buttons:
Example
a:link, a:visitedย {
ย ย background-color:ย #f44336;
ย color:ย white;
ย padding:ย 14px 25px;
ย ย text-align:ย center;
ย ย text-decoration:ย none;
ย ย display:ย inline-block;
}
a:hover, a:activeย {
ย ย background-color:ย red;
}
More Examples
Example
This example demonstrates how to add other styles to hyperlinks:
a.one:linkย {color:ย #ff0000;}
a.one:visitedย {color:ย #0000ff;}
a.one:hoverย {color:ย #ffcc00;}
a.two:linkย {color:ย #ff0000;}
a.two:visitedย {color:ย #0000ff;}
a.two:hoverย {font-size:ย 150%;}
a.three:linkย {color:ย #ff0000;}
a.three:visitedย {color:ย #0000ff;}
a.three:hoverย {background:ย #66ff66;}
a.four:linkย {color:ย #ff0000;}
a.four:visitedย {color:ย #0000ff;}
a.four:hoverย {font-family:ย monospace;}
a.five:linkย {color:ย #ff0000;ย text-decoration:ย none;}
a.five:visitedย {color:ย #0000ff;ย text-decoration:ย none;}
a.five:hoverย {text-decoration:ย underline;}
Example
Another example of how to create link boxes/buttons:
a:link, a:visitedย {
ย background-color:ย white;
ย color:ย black;
ย border:ย 2px solid green;
ย padding:ย 10px 20px;
ย text-align:ย center;
ย text-decoration:ย none;
ย display:ย inline-block;
}
a:hover, a:activeย {
ย background-color:ย green;
ย color:ย white;
}
Example
This example demonstrates the different types of cursors (can be useful for links):
<spanย style="cursor: auto">auto</span><br> <spanย style="cursor: crosshair">crosshair</span><br> <spanย style="cursor: default">default</span><br> <spanย style="cursor: e-resize">e-resize</span><br> <spanย style="cursor: help">help</span><br> <spanย style="cursor: move">move</span><br> <spanย style="cursor: n-resize">n-resize</span><br> <spanย style="cursor: ne-resize">ne-resize</span><br> <spanย style="cursor: nw-resize">nw-resize</span><br> <spanย style="cursor: pointer">pointer</span><br> <spanย style="cursor: progress">progress</span><br> <spanย style="cursor: s-resize">s-resize</span><br> <spanย style="cursor: se-resize">se-resize</span><br> <spanย style="cursor: sw-resize">sw-resize</span><br> <spanย style="cursor: text">text</span><br> <spanย style="cursor: w-resize">w-resize</span><br> <spanย style="cursor: wait">wait</span>
CSSย Lists (#23)
Unordered Lists:
- Coffee
- Tea
- Coca Cola
- Coffee
- Tea
- Coca Cola
Ordered Lists:
- Coffee
- Tea
- Coca Cola
- Coffee
- Tea
- Coca Cola
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 CSS list properties allow you to:
- Set different list item markers for ordered lists
- Set different list item markers for unordered lists
- Set an image as the list item marker
- Add background colors to lists and list items
Different List Item Markers
Theย list-style-typeย property specifies the type of list item marker.
The following example shows some of the available list item markers:
Example
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;
}
Note: Some of the values are for unordered lists, and some for ordered lists.
An Image as The List Item Marker
Theย list-style-imageย property specifies an image as the list item marker:
Example
ulย {
ย list-style-image:ย url('sqpurple.gif');
}
Position The List Item Markers
Theย list-style-positionย property specifies the position of the list-item markers (bullet points).
โlist-style-position: outside;โ means that the bullet points will be outside the list item. The start of each line of a list item will be aligned vertically. This is default:
- Coffee โ A brewed drink prepared from roasted coffee beansโฆ
- Tea
- Coca-cola
โlist-style-position: inside;โ means that the bullet points will be inside the list item. As it is part of the list item, it will be part of the text and push the text at the start:
- Coffee โ A brewed drink prepared from roasted coffee beansโฆ
- Tea
- Coca-cola
Example
ul.aย {
ย list-style-position:ย outside;
}
ul.bย {
ย ย list-style-position:ย inside;
}
Remove Default Settings
Theย list-style-type:noneย property can also be used to remove the markers/bullets. Note that the list also has default margin and padding. To remove this, addย margin:0ย andย padding:0ย to <ul> or <ol>:
Example
ulย {
ย list-style-type:ย none;
ย margin:ย 0;
ย padding:ย 0;
}
List โ Shorthand property
Theย list-styleย property is a shorthand property. It is used to set all the list properties in one declaration:
Example
ulย {
ย list-style:ย square inside url("sqpurple.gif");
}
When using the shorthand property, the order of the property values are:
list-style-typeย (if a list-style-image is specified, the value of this property will be displayed if the image for some reason cannot be displayed)list-style-positionย (specifies whether the list-item markers should appear inside or outside the content flow)list-style-imageย (specifies an image as the list item marker)
If one of the property values above are missing, the default value for the missing property will be inserted, if any.
Styling List With Colors
We can also style lists with colors, to make them look a little more interesting.
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
olย {
ย background:ย #ff9999;
ย padding:ย 20px;
}
ulย {
ย background:ย #3399ff;
ย padding:ย 20px;
}
ol liย {
ย background:ย #ffe5e5;
ย color:ย darkred;
ย padding:ย 5px;
ย margin-left:ย 35px;
}
ul liย {
ย background:ย #cce5ff;
ย color:ย darkblue;
ย margin:ย 5px;
}
Result:
- Coffee
- Tea
- Coca Cola
- Coffee
- Tea
- Coca Cola
CSSย Tables (#24)
The look of an HTML table can be greatly improved with CSS:
| Company | Contact | Country |
|---|---|---|
| Alfreds Futterkiste | Maria Anders | Germany |
| Berglunds snabbkรถp | Christina Berglund | Sweden |
| Centro comercial Moctezuma | Francisco Chang | Mexico |
| Ernst Handel | Roland Mendel | Austria |
| Island Trading | Helen Bennett | UK |
| Kรถniglich Essen | Philip Cramer | Germany |
| Laughing Bacchus Winecellars | Yoshi Tannamuri | Canada |
| Magazzini Alimentari Riuniti | Giovanni Rovelli | Italy |
Table Borders
To specify table borders in CSS, use theย borderย property.
The example below specifies a solid border for <table>, <th>, and <td> elements:
| Firstname | Lastname |
|---|---|
| Peter | Griffin |
| Lois | Griffin |
Example
table, th, tdย {
ย ย border:ย 1px solid;
}
Full-Width Table
The table above might seem small in some cases. If you need a table that should span the entire screen (full-width), addย width: 100%ย to the <table> element:
| Firstname | Lastname |
|---|---|
| Peter | Griffin |
| Lois | Griffin |
Example
tableย {
ย ย width:ย 100%;
}
Double Borders
Notice that the table in the examples above have double borders. This is because both the table and the <th> and <td> elements have separate borders.
To remove double borders, take a look at the example below.
Collapse Table Borders
Theย border-collapseย property sets whether the table borders should be collapsed into a single border:
| Firstname | Lastname |
|---|---|
| Peter | Griffin |
| Lois | Griffin |
Example
tableย {
ย ย border-collapse:ย collapse;
}
If you only want a border around the table, only specify theย borderย property for <table>:
| Firstname | Lastname |
|---|---|
| Peter | Griffin |
| Lois | Griffin |
Example
tableย {
ย border:ย 1px solid;
}
CSSย Table Size (#25)
Table Width and Height
The width and height of a table are defined by theย widthย andย heightย properties.
The example below sets the width of the table to 100%, and the height of the <th> elements to 70px:
| Firstname | Lastname | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
Example
tableย {
ย ย width:ย 100%;
}
thย {
ย ย height:ย 70px;
}
To create a table that should only span half the page, useย width: 50%:
| Firstname | Lastname | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
Example
tableย {
ย ย width:ย 50%;
}
CSSย Table Style (#26)
Table Padding
To control the space between the border and the content in a table, use theย paddingย property on <td> and <th> elements:
| First Name | Last Name | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
Example
th, tdย {
ย padding:ย 15px;
ย text-align:ย left;
}
Horizontal Dividers
| First Name | Last Name | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
Add theย border-bottomย property to <th> and <td> for horizontal dividers:
Example
th, tdย {
ย ย border-bottom:ย 1px solid #ddd;
}
Hoverable Table
Use theย :hoverย selector on <tr> to highlight table rows on mouse over:
| First Name | Last Name | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
Example
tr:hoverย {background-color:ย coral;}
Striped Tables
| First Name | Last Name | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
For zebra-striped tables, use theย nth-child()ย selector and add aย background-colorย to all even (or odd) table rows:
Example
tr:nth-child(even)ย {background-color:ย #f2f2f2;}
Table Color
The example below specifies the background color and text color of <th> elements:
| First Name | Last Name | Savings |
|---|---|---|
| Peter | Griffin | $100 |
| Lois | Griffin | $150 |
| Joe | Swanson | $300 |
Example
thย {
ย background-color:ย #04AA6D;
ย ย color:ย white;
}
CSSย Layout โ The display Propertyย (#27)
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.
Click to show panel
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).
Examples of block-level elements:
-
<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.
This isย an inline <span> element insideย a paragraph.
Examples of inline elements:
-
<span>
-
<a>
-
<img>
Display: none;
display: none;ย is commonly used with JavaScript to hide and show elements without deleting and recreating them. Take a look at our last example on this page if you want to know how this can be achieved.
Theย <script>ย element usesย display: none;ย as default.
Override The Default Display Value
As mentioned, every element has a default display value. However, you can override this.
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.
A common example is making inlineย <li>ย elements for horizontal menus:
Example
liย {
ย ย display:ย inline;
}
Note:ย 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.
The following example displays <span> elements as block elements:
Example
spanย {
ย display:ย block;
}
The following example displays <a> elements as block elements:
Example
aย {
ย display:ย block;
}
Hide an Element โ display:none or visibility:hidden?
display:none

visibility:hidden

Reset

Hiding an element can be done by setting theย displayย property toย none. The element will be hidden, and the page will be displayed as if the element is not there:
Example
h1.hiddenย {
ย ย display:ย none;
}
visibility:hidden;ย also hides an element.
However, the element will still take up the same space as before. The element will be hidden, but still affect the layout:
Example
h1.hiddenย {
ย ย visibility:ย hidden;
}
CSSย Layout โ width and max-width (#28)
Using width, max-width and margin: auto;
As mentioned in the previous chapter; a block-level element always takes up the full width available (stretches out to the left and right as far as it can).
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:
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:
Tip:ย Resize the browser window to less than 500px wide, to see the difference between the two divs!
Here is an example of the two divs above:
Example
div.ex1ย {
ย ย width:ย 500px;
ย ย margin:ย auto;
ย ย border:ย 3px solid #73AD21;
}
div.ex2ย {
ย max-width:ย 500px;
ย margin:ย auto;
ย border:ย 3px solid #73AD21;
}
CSSย Layout โ The position Propertyย (#29)
Theย positionย property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).
The position Property
Theย positionย property specifies the type of positioning method used for an element.
There are five different position values:
staticrelativefixedabsolutesticky
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.
An element withย position: static;ย is not positioned in any special way; it is always positioned according to the normal flow of the page:
Here is the CSS that is used:
Example
div.staticย {
ย ย position:ย static;
ย ย border:ย 3px solid #73AD21;
}
position: relative;
An element withย position: relative;ย is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
Here is the CSS that is used:
Example
div.relativeย {
ย position:ย relative;
ย left:ย 30px;
ย border:ย 3px solid #73AD21;
}
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
div.fixedย {
ย position:ย fixed;
ย bottom:ย 0;
ย right:ย 0;
ย width:ย 300px;
ย border:ย 3px solid #73AD21;
}
position: fixed;position: absolute;
An element withย position: absolute;ย is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
Note:ย Absolute positioned elements are removed from the normal flow, and can overlap elements.
Here is a simple example:
This <div> element has position: relative;
Here is the CSS that is used:
Example
div.relativeย {
ย ย position:ย relative;
ย width:ย 400px;
ย height:ย 200px;
ย border:ย 3px solid #73AD21;
}
div.absoluteย {
ย ย position:ย absolute;
ย top:ย 80px;
ย right:ย 0;
ย width:ย 200px;
ย height:ย 100px;
ย border:ย 3px solid #73AD21;
}
position: sticky;
An element withย position: sticky;ย is positioned based on the userโs scroll position.
A sticky element toggles betweenย relativeย andย fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport โ then it โsticksโ in place (like position:fixed).
Note:ย Internet Explorer does not support sticky positioning. Safari requires a -webkit- prefix (see example below). You must also specify at least one ofย top,ย right,ย bottomย orย leftย for sticky positioning to work.
In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.
Example
div.stickyย {
ย position:ย -webkit-sticky;ย /* Safari */
ย position:ย sticky;
ย top:ย 0;
ย background-color:ย green;
ย border:ย 2px solid #4CAF50;
}
Positioning Text In an Image
How to position text over an image:
Example

CSSย Layout โ The z-index Propertyย (#30)
Theย z-indexย property specifies the stack order of an element.
The z-index Property
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).
An element can have a positive or negative stack order:
This is a heading
Because the image has a z-index of -1, it will be placed behind the text.
Example
imgย {
ย position:ย absolute;
ย ย left:ย 0px;
ย top:ย 0px;
ย z-index:ย -1;
}
Note:ย z-indexย only works onย positioned elementsย (position: absolute, position: relative, position: fixed, or position: sticky) andย flex itemsย (elements that are direct children of display: flex elements).
Another z-index Example
Example
Here we see that an element with greater stack order is always above an element with a lower stack order:
<html> <head> <style> .containerย { ย position:ย relative; } .black-boxย { ย position:ย relative; ย z-index:ย 1; ย border:ย 2px solid black; ย height:ย 100px; ย margin:ย 30px; } .gray-boxย { ย position:ย absolute; ย z-index:ย 3; ย background:ย lightgray; ย height:ย 60px; ย width:ย 70%; ย left:ย 50px; ย top:ย 50px; } .green-boxย { ย position:ย absolute; ย z-index:ย 2; ย background:ย lightgreen; ย width:ย 35%; ย left:ย 270px; ย top:ย -15px; ย height:ย 100px; } </style> </head> <body> <divย class="container"> ย ย <divย class="black-box">Black box</div> ย ย <divย class="gray-box">Gray box</div> ย ย <divย class="green-box">Green box</div> </div> </body> </html>
Without z-index
If two positioned elements overlap each other without aย z-indexย specified, the element definedย last in the HTML codeย will be shown on top.
Example
Same example as above, but here with no z-index specified:
<html> <head> <style> .containerย { ย position:ย relative; } .black-boxย { ย position:ย relative; ย border:ย 2px solid black; ย height:ย 100px; ย margin:ย 30px; } .gray-boxย { ย position:ย absolute; ย background:ย lightgray; ย height:ย 60px; ย width:ย 70%; ย left:ย 50px; ย top:ย 50px; } .green-boxย { ย position:ย absolute; ย background:ย lightgreen; ย width:ย 35%; ย left:ย 270px; ย top:ย -15px; ย height:ย 100px; } </style> </head> <body> <divย class="container"> ย ย <divย class="black-box">Black box</div> ย ย <divย class="gray-box">Gray box</div> ย ย <divย class="green-box">Green box</div> </div> </body> </html>
CSSย Layout โ float and clear (#31)
The CSSย floatย property specifies how an element should float.
The CSSย clearย property specifies what elements can float beside the cleared element and on which side.
The float Property
Theย floatย property is used for positioning and formatting content e.g. let an image float left to the text in a container.
Theย floatย property can have one of the following values:
leftย โ The element floats to the left of its containerrightย โ The element floats to the right of its containernoneย โ The element does not float (will be displayed just where it occurs in the text). This is defaultinheritย โ The element inherits the float value of its parent
In its simplest use, theย floatย property can be used to wrap text around images.
Example โ float: right;
The following example specifies that an image should float to theย rightย in a text:

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.ย
Example
imgย {
ย float:ย right;
}
Example โ float: left;
The following example specifies that an image should float to theย leftย in a text:

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.ย
Example
imgย {
ย float:ย left;
}
Example โ No float
In the following example the image will be displayed just where it occurs in the text (float: none;):

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.ย
Example
imgย {
ย float:ย none;
}
Example โ Float Next To Each Other
Normally div elements will be displayed on top of each other. However, if we useย float: leftย we can let elements float next to each other:
Example
divย {
ย float:ย left;
ย padding:ย 15px;
}
.div1ย {
ย background:ย red;
}
.div2ย {
ย background:ย yellow;
}
.div3ย {
ย background:ย green;
}
CSSย Layout โ clear and clearfix (#32)
The clear Property
When we use theย floatย property, and we want the next element below (not on right or left), we will have to use theย clearย property.
Theย clearย property specifies what should happen with the element that is next to a floating element.
Theย clearย property can have one of the following values:
noneย โ The element is not pushed below left or right floated elements. This is defaultleftย โ The element is pushed below left floated elementsrightย โ The element is pushed below right floated elementsbothย โ The element is pushed below both left and right floated elementsinheritย โ The element inherits the clear value from its parent
When clearing floats, you should match the clear to the float: If an element is floated to the left, then you should clear to the left. Your floated element will continue to float, but the cleared element will appear below it on the web page.
Example
This example clears the float to the left. Here, it means that the <div2> element is pushed below the left floated <div1> element:
div1ย {
ย ย float:ย left;
}
div2ย {
ย ย clear:ย left;
}
The clearfix Hack
If a floated element is taller than the containing element, it will โoverflowโ outside of its container. We can then add a clearfix hack to solve this problem:
Without Clearfix

With Clearfix

Example
.clearfixย {
ย ย overflow:ย auto;
}
Theย overflow: autoย clearfix works well as long as you are able to keep control of your margins and padding (else you might see scrollbars). Theย new, modern clearfix hackย however, is safer to use, and the following code is used for most webpages:
Example
.clearfix::afterย {
ย ย content:ย "";
ย ย clear:ย both;
ย ย display:ย table;
}
CSSย Layout โ display: inline-block (#33)
The display: inline-block Value
Compared toย display: inline, the major difference is thatย display: inline-blockย allows to set a width and height on the element.
Also, withย display: inline-block, the top and bottom margins/paddings are respected, but withย display: inlineย they are not.
Compared toย display: block, the major difference is thatย display: inline-blockย does not add a line-break after the element, so the element can sit next to other elements.
The following example shows the different behavior ofย display: inline,ย display: inline-blockย andย display: block:
Example
span.aย {
ย ย display:ย inline;ย /* the default for span */
ย width:ย 100px;
ย ย height:ย 100px;
ย padding:ย 5px;
ย border:ย 1px solid blue;
ย ย background-color:ย yellow;
}
span.bย {
ย display:ย inline-block;
ย width:ย 100px;
ย height:ย 100px;
ย padding:ย 5px;
ย border:ย 1px solid blue;
ย background-color:ย yellow;
}
span.cย {
ย ย display:ย block;
ย ย width:ย 100px;
ย ย height:ย 100px;
ย padding:ย 5px;
ย border:ย 1px solid blue;
ย background-color:ย yellow;
}
Using inline-block to Create Navigation Links
One common use forย display: inline-blockย is to display list items horizontally instead of vertically. The following example creates horizontal navigation links:
Example
.navย {
ย background-color:ย yellow;
ย ย list-style-type:ย none;
ย ย text-align:ย center;ย
ย padding:ย 0;
ย margin:ย 0;
}
.nav liย {
ย ย display:ย inline-block;
ย font-size:ย 20px;
ย ย padding:ย 20px;
}
CSSย Layout โ Horizontal & Vertical Align (#34)
Center elements
horizontally and vertically
Center Align Elements
To horizontally center a block element (like <div>), useย margin: auto;
Setting the width of the element will prevent it from stretching out to the edges of its container.
The element will then take up the specified width, and the remaining space will be split equally between the two margins:
This div element is centered.
Example
.centerย {
ย ย margin:ย auto;
ย width:ย 50%;
ย border:ย 3px solid green;
ย padding:ย 10px;
}
Note:ย Center aligning has no effect if theย widthย property is not set (or set to 100%).
Center Align Text
To just center the text inside an element, useย text-align: center;
This text is centered.
Example
.centerย {
ย text-align:ย center;
ย border:ย 3px solid green;
}
Center an Image
To center an image, set left and right margin toย autoย and make it into aย blockย element:

Example
imgย {
ย ย display:ย block;
ย margin-left:ย auto;
ย margin-right:ย auto;
ย ย width:ย 40%;
}
Left and Right Align โ Using position
One method for aligning elements is to useย position: absolute;:
In my younger and more vulnerable years my father gave me some advice that Iโve been turning over in my mind ever since.
Example
.rightย {
ย ย position:ย absolute;
ย right:ย 0px;
ย ย width:ย 300px;
ย ย border:ย 3px solid #73AD21;
ย ย padding:ย 10px;
}
Note:ย Absolute positioned elements are removed from the normal flow, and can overlap elements.
Left and Right Align โ Using float
Another method for aligning elements is to use theย floatย property:
Example
.rightย {
ย ย float:ย right;
ย width:ย 300px;
ย ย border:ย 3px solid #73AD21;
ย ย padding:ย 10px;
}
The clearfix Hack
Note:ย If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. You can use the โclearfix hackโ to fix this (see example below).
Without Clearfix

With Clearfix

Then we can add the clearfix hack to the containing element to fix this problem:
Example
.clearfix::afterย {
ย content:ย "";
ย clear:ย both;
ย display:ย table;
}
Center Vertically โ Using padding
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottomย padding:
I am vertically centered.
Example
.centerย {
ย ย padding:ย 70px 0;
ย ย border:ย 3px solid green;
}
To center both vertically and horizontally, useย paddingย andย text-align: center:
I am vertically and horizontally centered.
Example
.centerย {
ย padding:ย 70px 0;
ย ย border:ย 3px solid green;
ย text-align:ย center;
}
Center Vertically โ Using line-height
Another trick is to use theย line-heightย property with a value that is equal to theย heightย property:
I am vertically and horizontally centered.
Example
.centerย {
ย line-height:ย 200px;
ย ย height:ย 200px;
ย border:ย 3px solid green;
ย ย text-align:ย center;
}
/* If the text has multiple lines, add the following: */
.center pย {
ย ย line-height:ย 1.5;
ย ย display:ย inline-block;
ย ย vertical-align:ย middle;
}
Center Vertically โ Using position & transform
Ifย paddingย andย line-heightย are not options, another solution is to use positioning and theย transformย property:
I am vertically and horizontally centered.
Example
.centerย {
ย ย height:ย 200px;
ย ย position:ย relative;
ย border:ย 3px solid green;
}
.center pย {
ย ย margin:ย 0;
ย position:ย absolute;
ย top:ย 50%;
ย left:ย 50%;
ย transform:ย translate(-50%, -50%);
}
Center Vertically โ Using Flexbox
You can also use flexbox to center things. Just note that flexbox is not supported in IE10 and earlier versions:
Example
.centerย {
ย display:ย flex;
ย justify-content:ย center;
ย align-items:ย center;
ย height:ย 200px;
ย border:ย 3px solid green;
}
CSSย Image Gallery (#35)
CSS can be used to create an image gallery.
Image Gallery
The following image gallery is created with CSS:
Example
<html> <head> <style> div.galleryย { ย ย margin:ย 5px; ย border:ย 1px solid #ccc; ย ย float:ย left; ย ย width:ย 180px; } div.gallery:hoverย { ย ย border:ย 1px solid #777; } div.gallery imgย { ย ย width:ย 100%; ย ย height:ย auto; } div.descย { ย padding:ย 15px; ย text-align:ย center; } </style> </head> <body> <divย class="gallery"> ย ย <aย target="_blank"ย href="img_5terre.jpg"> ย ย ย ย <imgย src="img_5terre.jpg"ย alt="Cinque Terre"ย width="600"ย height="400"> ย ย </a> ย ย <divย class="desc">Add a description of the image here</div> </div> <divย class="gallery"> ย ย <aย target="_blank"ย href="img_forest.jpg"> ย ย ย ย <imgย src="img_forest.jpg"ย alt="Forest"ย width="600"ย height="400"> ย ย </a> ย ย <divย class="desc">Add a description of the image here</div> </div> <divย class="gallery"> ย ย <aย target="_blank"ย href="img_lights.jpg"> ย ย ย ย <imgย src="img_lights.jpg"ย alt="Northern Lights"ย width="600"ย height="400"> ย ย </a> ย ย <divย class="desc">Add a description of the image here</div> </div> <divย class="gallery"> ย ย <aย target="_blank"ย href="img_mountains.jpg"> ย ย ย ย <imgย src="img_mountains.jpg"ย alt="Mountains"ย width="600"ย height="400"> ย ย </a> ย ย <divย class="desc">Add a description of the image here</div> </div> </body> </html>
More Examples
Responsive Image Gallery
How to use CSS media queries to create a responsive image gallery that will look good on desktops, tablets and smart phones.

CSSย Image Sprites (#36)
Image Sprites
An image sprite is a collection of images put into a single image.
A web page with many images can take a long time to load and generates multiple server requests.
Using image sprites will reduce the number of server requests and save bandwidth.
Image Sprites โ Simple Example
Instead of using three separate images, we use this single image (โimg_navsprites.gifโ):
![]()
With CSS, we can show just the part of the image we need.
In the following example the CSS specifies which part of the โimg_navsprites.gifโ image to show:
Example
#homeย {
ย width:ย 46px;
ย ย height:ย 44px;
ย background:ย url(img_navsprites.gif) 0 0;
}
Example explained:
<img id="home" src="img_trans.gif">ย โ Only defines a small transparent image because the src attribute cannot be empty. The displayed image will be the background image we specify in CSSwidth: 46px; height: 44px;ย โ Defines the portion of the image we want to usebackground: url(img_navsprites.gif) 0 0;ย โ Defines the background image and its position (left 0px, top 0px)
This is the easiest way to use image sprites, now we want to expand it by using links and hover effects.
Image Sprites โ Create a Navigation List
We want to use the sprite image (โimg_navsprites.gifโ) to create a navigation list.
We will use an HTML list, because it can be a link and also supports a background image:
Example
#navlistย {
ย ย position:ย relative;
}
#navlist liย {
ย margin:ย 0;
ย padding:ย 0;
ย list-style:ย none;
ย position:ย absolute;
ย top:ย 0;
}
#navlist li, #navlist aย {
ย height:ย 44px;
ย display:ย block;
}
#homeย {
ย left:ย 0px;
ย width:ย 46px;
ย ย background:ย url('img_navsprites.gif') 0 0;
}
#prevย {
ย left:ย 63px;
ย width:ย 43px;
ย ย background:ย url('img_navsprites.gif') -47px 0;
}
#nextย {
ย left:ย 129px;
ย ย width:ย 43px;
ย ย background:ย url('img_navsprites.gif') -91px 0;
}
Example explained:
- #navlist {position:relative;} โ position is set to relative to allow absolute positioning inside it
- #navlist li {margin:0;padding:0;list-style:none;position:absolute;top:0;} โ margin and padding are set to 0, list-style is removed, and all list items are absolute positioned
- #navlist li, #navlist a {height:44px;display:block;} โ the height of all the images are 44px
Now start to position and style for each specific part:
- #home {left:0px;width:46px;} โ Positioned all the way to the left, and the width of the image is 46px
- #home {background:url(img_navsprites.gif) 0 0;} โ Defines the background image and its position (left 0px, top 0px)
- #prev {left:63px;width:43px;} โ Positioned 63px to the right (#home width 46px + some extra space between items), and the width is 43px.
- #prev {background:url(โimg_navsprites.gifโ) -47px 0;} โ Defines the background image 47px to the right (#home width 46px + 1px line divider)
- #next {left:129px;width:43px;}- Positioned 129px to the right (start of #prev is 63px + #prev width 43px + extra space), and the width is 43px.
- #next {background:url(โimg_navsprites.gifโ) -91px 0;} โ Defines the background image 91px to the right (#home width 46px + 1px line divider + #prev width 43px + 1px line divider )
Image Sprites โ Hover Effect
Now we want to add a hover effect to our navigation list.
Tip:ย Theย :hoverย selector can be used on all elements, not only on links.
Our new image (โimg_navsprites_hover.gifโ) contains three navigation images and three images to use for hover effects:
![]()
Because this is one single image, and not six separate files, there will beย no loading delayย when a user hovers over the image.
We only add three lines of code to add the hover effect:
Example
#home a:hoverย {
ย background:ย url('img_navsprites_hover.gif') 0 -45px;
}
#prev a:hoverย {
ย ย background:ย url('img_navsprites_hover.gif') -47px -45px;
}
#next a:hoverย {
ย ย background:ย url('img_navsprites_hover.gif') -91px -45px;
}
Example explained:
- #home a:hover {background: url(โimg_navsprites_hover.gifโ) 0 -45px;} โ For all three hover images we specify the same background position, only 45px further down
CSSย Website Layout (#37)
Website Layout
A website is often divided into headers, menus, content and a footer:
There are tons of different layout designs to choose from. However, the structure above, is one of the most common, and we will take a closer look at it in this tutorial.
Header
A header is usually located at the top of the website (or right below a top navigation menu). It often contains a logo or the website name:
Example
.headerย {
ย background-color:ย #F1F1F1;
ย text-align:ย center;
ย padding:ย 20px;
}
Result:
Header
Navigation Bar
A navigation bar contains a list of links to help visitors navigating through your website:
Example
/* The navbar container */
.topnavย {
ย overflow:ย hidden;
ย background-color:ย #333;
}
/* Navbar links */
.topnav aย {
ย ย float:ย left;
ย display:ย block;
ย color:ย #f2f2f2;
ย text-align:ย center;
ย padding:ย 14px 16px;
ย text-decoration:ย none;
}
/* Links - change color on hover */
.topnav a:hoverย {
ย background-color:ย #ddd;
ย color:ย black;
}
Result
Content
The layout in this section, often depends on the target users. The most common layout is one (or combining them) of the following:
- 1-columnย (often used for mobile browsers)
- 2-columnย (often used for tablets and laptops)
- 3-column layoutย (only used for desktops)
1-column:
2-column:
3-column:
We will create a 3-column layout, and change it to a 1-column layout on smaller screens:
Example
/* Create three equal columns that float next to each other */
.columnย {
ย float:ย left;
ย width:ย 33.33%;
}
/* Clear floats after the columns */
.row:afterย {
ย content:ย "";
ย display:ย table;
ย clear:ย both;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other on smaller screens (600px wide or less) */
@media screen and (max-width: 600px)ย {
ย ย .columnย {
ย ย ย width:ย 100%;
ย ย }
}
Result
Column
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique.
Column
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique.
Column
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique.
Tip:ย To create a 2-column layout, change the width to 50%. To create a 4-column layout, use 25%, etc.
Tip:ย A more modern way of creating column layouts, is to use CSS Flexbox. However, it is not supported in Internet Explorer 10 and earlier versions. If you require IE6-10 support, use floats (as shown above).
Unequal Columns
The main content is the biggest and the most important part of your site.
It is common withย unequalย column widths, so that most of the space is reserved for the main content. The side content (if any) is often used as an alternative navigation or to specify information relevant to the main content. Change the widths as you like, only remember that it should add up to 100% in total:
Example
.columnย {
ย float:ย left;
}
/* Left and right column */
.column.sideย {
ย ย width:ย 25%;
}
/* Middle column */
.column.middleย {
ย ย width:ย 50%;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px)ย {
ย .column.side, .column.middleย {
ย ย ย width:ย 100%;
ย ย }
}
Result:
Side
Lorem ipsum dolor sit amet, consectetur adipiscing elitโฆ
Main Content
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan convallis.
Side
Lorem ipsum dolor sit amet, consectetur adipiscing elitโฆ
Footer
The footer is placed at the bottom of your page. It often contains information like copyright and contact info:
Example
.footerย {
ย background-color:ย #F1F1F1;
ย text-align:ย center;
ย padding:ย 10px;
}
Result:
CSSย Math Functions (#38)
The CSS math functions allow mathematical expressions to be used as property values. Here, we will explain theย calc(),ย max()ย andย min()ย functions.
The calc() Function
Theย calc()ย function performs a calculation to be used as the property value.
CSS Syntax
calc(expression)
| Value | Description |
|---|---|
| expression | Required. A mathematical expression. The result will be used as the value. The following operators can be used: + โ * / |
Let us look at an example:
Example
Use calc() to calculate the width of a <div> element:
#div1ย {
ย position:ย absolute;
ย left:ย 50px;
ย width:ย calc(100% - 100px);
ย border:ย 1px solid black;
ย background-color:ย yellow;
ย padding:ย 5px;
}
The max() Function
Theย max()ย function uses the largest value, from a comma-separated list of values, as the property value.
CSS Syntax
max(value1,ย value2, โฆ)
| Value | Description |
|---|---|
| value1,ย value2, โฆ | Required. A list of comma-separated values โ where the largest value is chosen |
Let us look at an example:
Example
Use max() to set the width of #div1 to whichever value is largest, 50% or 300px:
#div1ย {
ย background-color:ย yellow;
ย height:ย 100px;
ย width:ย max(50%, 300px);
}
The min() Function
Theย min()ย function uses the smallest value, from a comma-separated list of values, as the property value.
CSS Syntax
min(value1,ย value2, โฆ)
| Value | Description |
|---|---|
| value1,ย value2, โฆ | Required. A list of comma-separated values โ where the smallest value is chosen |
Let us look at an example:
Example
Use min() to set the width of #div1 to whichever value is smallest, 50% or 300px:
#div1ย {
ย background-color:ย yellow;
ย height:ย 100px;
ย width:ย min(50%, 300px);
}
All CSS Math Functions
| Function | Description |
|---|---|
| calc() | Allows you to perform calculations to determine CSS property values |
| max() | Uses the largest value, from a comma-separated list of values, as the property value |
| min() | Uses the smallest value, from a comma-separated list of values, as the property value |

