CSS - Cascading Style Sheets
CSS - Cascading Style Sheets
CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to solve a problem External Style Sheets can save a lot of work External Style Sheets are stored in CSS files
The selector is normally the HTML element you want to style. Each declaration consists of a property and a value. The property is the style attribute you want to change. Each property has a value. CSS declarations always ends with a semicolon, and declaration groups are surrounded by curly brackets: Example: p {color:red; text-align:center;}
CSS Comments
Comments are used to explain your code, and may help you when you edit the source code at a later date. Comments are ignored by browsers.
A CSS comment begins with "/*", and ends with "*/", like this:
/*This is a comment*/ p { text-align:center; /*This is another comment*/ color:black; font-family:arial; }
Example:
You can also specify that only specific HTML elements should be affected by a class. In the example below, all p elements with class="center" will be centeraligned: p.center {text-align:center;}
Inline Styles
Cascading order
What style will be used when there is more than one style specified for an HTML element? Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority: Browser default External style sheet Internal style sheet (in the head section) Inline style (inside an HTML element) So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).
CSS Styling
CSS Backgrounds
CSS background properties are used to define the background effects of an element.
Background Color
The background-color property specifies the background color of an element. The background color of a page is defined in the body selector: body {background-color:#b0c4de;}
In the example below, the h1, p, and div elements have different background colors:
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. The background image for a page can be set like this:
body {background-image:url('paper.gif');}
Below is an example of a bad combination of text and background image. The text is almost not readable:
body {background-image:url('bgdesert.jpg');}
Some images should be repeated only horizontally or vertically, or they will look strange, like this:
body{ background-image:url('gradient2.png'); }
If the image is repeated only horizontally (repeat-x), the background will look better:
Body { background-image:url('gradient2.png'); background-repeat:repeat-x; }
In the example above, the background image is shown 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.
When using the shorthand property the order of the property values are:
background-color background-image background-repeat
backgroundattachment
backgroundposition
It does not matter if one of the property values are missing, as long as the ones that are present are in this order.
background-attachment
background-color
background-image
background-position
background-repeat
CSS Text
Text Color
The color property is used to set the color of the text. The color can be specified by:
name - a color name, like "red" RGB - an RGB value, like "rgb(255,0,0)" Hex - a hex value, like "#ff0000"
Text Alignment
The text-align property is used to set the horizontal alignment of a text. Text can be centered, or aligned to the left or right, or justified. 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 (like in magazines and newspapers).
Example h1 {text-align:center;} p.date {text-align:right;} p.main {text-align:justify;}
Text Decoration
The text-decoration property is used to set or remove decorations from text. The text-decoration property is mostly used to remove underlines from links for design purposes:
Example a {text-decoration:none;}
Text Indentation The text-indentation property is used to specify the indentation of the first line of a text.
Example p {text-indent:50px;}
vertical-align
white-space word-spacing
Sets how white space inside an element is handled Increase or decrease the space between words
CSS Fonts
CSS font properties define the font family, boldness, size, and the style of a text. Difference Between Serif and Sans-serif Fonts
"Sans" means without - these fonts do not have the lines at the ends of characters
All monospace characters has the same width
Font Family
The font family of a text is set with the font-family property. The font-family property should hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font. Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available. Note: If the name of a font family is more than one word, it must be in quotation marks, like font-family: "Times New Roman". More than one font family is specified in a commaseparated list:
Example p{font-family:"Times New Roman", Times, serif;}
Font Style
The font-style property is mostly used to specify italic text. This property has three values:
normal - The text is shown normally italic - The text is shown in italics oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
Example
Font Size
The font-size property sets the size of the text. Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs. Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs. The font-size value can be an absolute, or relative size. Absolute size:
Sets the text to a specified size Does not allow a user to change the text size in all browsers (bad for accessibility reasons) Absolute size is useful when the physical size of the output is known
Relative size:
Sets the size relative to surrounding elements Allows a user to change the text size in browsers
If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).
The example above allows Firefox, Chrome, and Safari to resize the text, but not Internet Explorer. The text can be resized in all browsers using the zoom tool (however, this resizes the entire page, not just the text).
In the example above, the text size in em is the same as the previous example in pixels. However, with the em size, it is possible to adjust the text size in all browsers. Unfortunately, there is still a problem with IE. When resizing the text, it becomes larger than it should when made larger, and smaller than it should when made smaller.
Our code now works great! It shows the same text size in all browsers, and allows all browsers to zoom or resize the text!
font-family
family-name Specifies the font family for text generic-family inherit Specifies the font size of text xx-small/ x-small/ small/ medium/ large/ x-large xx-large/ smaller/ larger /length/ %/ inherit
font-size
font-style
font-variant
Specifies whether or not a text Normal should be displayed in a small- small-caps caps font inherit Normal/ bold/ bolder/ lighter/ 100/ 200/ 300/ 400/500 600/ 700/ 800/ 900/ inherit
font-weight
Styling Links
Links can be style with any CSS property (e.g. color, font-family, background-color). Special for links are that they can be styled differently depending on what state they are in. The four links states are:
a:link - a normal, unvisited link a:visited - a link the user has visited a:hover - a link when the user mouses over it a:active - a link the moment it is clicked
Example a:link {color:#FF0000;} /* unvisited link */ a:visited {color:#00FF00;} /* visited link */ a:hover {color:#FF00FF;} /* mouse over link */ a:active {color:#0000FF;} /* selected link */
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 {textdecoration:underline;} a:active {textdecoration:underline;}
Background Color
The background-color property specifies the background color for links: Example
a:link {background-color:#B2FF99;} a:visited {backgroundcolor:#FFFF85;} a:hover {backgroundcolor:#FF704D;} a:active {backgroundcolor:#FF704D;}
CSS Lists
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
With CSS, lists can be styled further, and images can be used as the list item marker.
Some of the property values are for unordered lists, and some for ordered lists.
none
disc circle square
No marker
Default. The marker is a filled circle The marker is a circle The marker is a square
The example above does not display equally in all browsers. IE and Opera will display the image-marker a little bit higher than Firefox, Chrome, and Safari. If you want the image-marker to be placed equally in all browsers, a crossbrowser solution is explained below.
Crossbrowser Solution
The following example displays the image-marker equally in all browsers: Example
ul { list-style-type: none; padding: 0px; margin: 0px; } li { background-image: url(sqpurple.gif); background-repeat: no-repeat; background-position: 0px 5px; padding-left: 14px; }
For li:
Set the URL of the image, and show it only once (no-repeat) Position the image where you want it (left 0px and down 5px) Position the text in the list with padding-left
When using the shorthand property, the order of the values are:
list-style-type list-style-position (for a description, see the CSS properties table below) list-style-image
It does not matter if one of the values above are missing, as long as the rest are in the specified order.
list-style-type
Table Borders
To specify table borders in CSS, use the border property. The example below specifies a black border for table, th, and td elements: Example
table, th, td { border: 1px solid black; }
Notice that the table in the example above has double borders. This is because both the table, th, and td elements have separate borders. To display a single border for the table, use the bordercollapse property.
Collapse Borders
The border-collapse property sets whether the table borders are collapsed into a single border or separated: Example
table { border-collapse:collapse; } table,th, td { border: 1px solid black; }
The vertical-align property sets the vertical alignment, like top, bottom, or middle: Example
td { height:50px; vertical-align:bottom; }
Table Padding
To control the space between the border and content in a table, use the padding property on td and th elements: Example
td { padding:15px; }
Table Color
The example below specifies the color of the borders, and the text and background color of th elements: Example
table, td, th { border:1px solid green; } th { background-color:green; color:white; }
Border Width
The border-width property is used to set the width of the border. The width is set in pixels, or by using one of the three predefined values: thin, medium, or thick.
Border Color
The border-color property is used to set the color of the border. The color can be set by:
name - specify a color name, like "red" RGB - specify a RGB value, like "rgb(255,0,0)" Hex - specify a hex value, like "#ff0000"
The border-style property can have from one to four values. border-style:dotted solid double dashed;
top border is dotted right border is solid bottom border is double left border is dashed top border is dotted right and left borders are solid bottom border is double top and bottom borders are dotted right and left borders are solid all four borders are dotted
You can also set the border color to "transparent". Note: The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.
Example
p.two { border-style:solid; border-color:#98bf21; }
border-style:dotted solid;
border-style:dotted;
The border-style property is used in the example above. However, it also works with border-width and border-color.
Border - Shorthand property As you can see from the examples above, there are many properties to consider when dealing with borders. To shorten the code, it is also possible to specify all the border properties in one property. This is called a shorthand property. The shorthand property for the border properties is "border": Example border:5px solid red;
When using the border property, the order of the values are:
border-width border-style border-color
It does not matter if one of the values above are missing (although, border-style is required), as long as the rest are in the specified order.
CSS Outlines
An outline is a line that is drawn around elements, outside the border edge, to make the element "stand out". The outline properties specifies the style, color, and width of an outline. Example
outline-color
outline-style
outline-width
Outline color
CSS Margins
The CSS margin properties define the space around elements. Margin
The margin clears an area around an element (outside the border). The margin does not have a background color, and is completely transparent. The top, right, bottom, and left margin can be changed independently using separate properties. A shorthand margin property can also be used, to change all margins at once.
Possible Values
auto length % The browser sets the margin. The result of this is dependant of the browser Defines a fixed margin (in pixels, pt, em, etc.) Defines a margin in % of the containing element
margin:25px 50px;
top and bottom margins are 25px right and left margins are 50px
margin:25px;
all four margins are 25px
margin-left
auto length %
auto length % auto length %
margin-right
margin-top
CSS Padding
The CSS padding properties define the space between the element border and the element content. Padding
The padding clears an area around the content (inside the border) of an element. The padding is affected by the background color of the element. The top, right, bottom, and left padding can be changed independently using separate properties. A shorthand padding property can also be used, to change all paddings at once.
Possible Values
length Defines a fixed padding (in pixels, pt, em, etc.) % Defines a padding in % of the containing element
Padding - cont
Padding - Individual sides
In CSS, it is possible to specify different padding for different sides:
The padding property can have from one to four values. padding:25px 50px 75px 100px;
top padding is 25px right padding is 50px bottom padding is 75px left padding is 100px
padding:25px 50px;
top and bottom paddings are 25px right and left paddings are 50px
padding:25px;
all four paddings are 25px
Nesting Selectors It is possible to apply a style for a selector within a selector. In the example below, one style is specified for all p elements, and a separate style is specified for p elements nested within the "marked" class: Example:
P { color:blue; text-align:center; } .marked { backgroundcolor:blue; .marked p { color:white; }
CSS Dimension
The CSS dimension properties allow you to control the height and width of an element.
Property height Description Sets the height of an element Values auto length % inherit none length % inherit none length % inherit length % inherit length % inherit auto length % inherit
max-height
max-width
min-height
min-width
width
An inline element only takes up as much width as necessary, and does not force line breaks. Examples of inline elements:
<span> <a>
Inline display
Block display
CSS Positioning
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. There are four different positioning methods.
Static Positioning Fixed Positioning Relative Positioning Absolute Positioning
Fixed Positioning
An element with fixed position is positioned relative to the browser window. It will not move even if the window is scrolled. 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.
Example : Fixed
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. Absolutely positioned elements can overlap other elements.
Example: Relative
Example: Absolute
Overlapping Elements
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). An element can have a positive or negative stack order:
An element with greater stack order is always in front of an element with a lower stack order.
Changing cursor
cursor
Specifies what happens if content overflows an Auto/ hidden/ scroll/ visible/ element's box inherit Absolute/ fixed/ relative Specifies the type of positioning for an element static/ inherit Auto/ length/ % Sets the right margin edge for a positioned box inherit Auto/ length/ % Sets the top margin edge for a positioned box inherit number/ auto Sets the stack order of an element inherit
<html> <head> <style type="text/css"> #customers { font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; width:100%; border-collapse:collapse; } #customers td, #customers th { font-size:1em; border:1px solid #98bf21; padding:3px 7px 2px 7px; } #customers th { font-size:1.1em; text-align:left; padding-top:5px; padding-bottom:4px; background-color:#A7C942; color:#ffffff; } #customers tr.alt td { color:#000000; background-color:#EAF2D3; } </style> </head> <body> <table id="customers"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> caption {caption-side:bottom;} </style> </head> <body> <table border="1"> <caption>Table 1.1 Customers</caption> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Berglunds snabbkp</td> <td>Christina Berglund</td> <td>Sweden</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr>