CSS Document
CSS Document
the design,
layout and variations in display for different devices and screen sizes.
CSS Syntax
A CSS rule consists of a selector and a declaration block.
Multiple CSS declarations are separated with semicolons, and declaration blocks are
surrounded by curly braces.
2. 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.
3. 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.
An external style sheet can be written in any text editor, and must be saved with a .css
extension.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements that we want to style.
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.
To select elements with a specific class, write a period (.) character, followed by the class
name.
*{
text-align: center;
color: blue;
}
A CSS comment is placed inside the <style> element, and starts with /* and ends with */:
CSS Color
In CSS, a color can be specified by using a predefined color name:
CSS background-color
The background-color property specifies the background color of an element.
background-color
background-image
background-repeat
background-attachment
background-position
background (shorthand property)
CSS background-image
The background-image property specifies an image to use as the background of an element.
CSS background-repeat
By default, the background-image property repeats an image both horizontally and vertically.
background-repeat: no-repeat;
CSS background-position
The background-position property is used to specify the position of the background image.
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):
background-attachment: fixed;
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
CSS Borders
The CSS border properties allow you to specify the style, width, and color of an element's border.
The border-style property can have from one to four values (for the top border, right border, bottom
border, and the left border).
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.four {
border-style: dotted;
border-width: thick;
}
The border property is a shorthand property for the following individual border properties:
CSS Margins
The CSS margin properties are used to create space around elements, outside of any defined
borders.
margin-top
margin-right
margin-bottom
margin-left
Margin Collapse
Top and bottom margins of elements are sometimes collapsed into a single margin that is
equal to the largest of the two margins.
CSS Texts
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
Text Decoration
In this chapter you will learn about the following properties:
text-decoration-line
text-decoration-color
text-decoration-style
text-decoration-thickness
text-decoration
h2 {
text-decoration-line: line-through;
}
h3 {
text-decoration-line: underline;
}
p{
text-decoration-line: overline underline;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first
letter of each word:
text-transform: uppercase;
text-transform: lowercase;
text-transform: capitalize;
Text Spacing
text-indent
letter-spacing
line-height
word-spacing
white-space
Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:
p { text-indent: 50px;}
Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.
h1 { letter-spacing: 5px;}
Line Height
The line-height property is used to specify the space between lines
p { line-height: 0.8;}
p { line-height: 1.8;}
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):
h1 {
text-shadow: 2px 2px;
}
h1 {
text-shadow: 2px 2px red;
}
Font Families
All the different font names belong to one of the generic font families.
Specify some different fonts for three paragraphs:
.p1 { font-family: "Times New Roman", Times, serif;}
Font Style
The font-style property is mostly used to specify italic text.
Absolute size:
Relative size:
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background,
a { color: hotpink;}
When setting the style for several link states, there are some order rules:
Text Decoration
The text-decoration property is mostly used to remove underlines from links:
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
CSS Tables
Table Borders
To specify table borders in CSS, use the border property.
table, th, td {
border: 1px solid;
}
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or center) of the content
in <th> or <td>
By default, the content of <th> elements are center-aligned and the content of <td> elements
are left-aligned.
td { text-align: center;}
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in
<th> or <td>.
By default, the vertical alignment of the content in a table is middle (for both <th> and <td>
elements).
td {
height: 50px;
vertical-align: bottom;
}
Table Padding
To control the space between the border and the content in a table, use the padding property on <td>
and <th> elements:
Example
th, td {
padding: 15px;
text-align: left;
}
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
Striped Tables
tr:nth-child(even) {background-color: #f2f2f2;}
Table Color
The example below specifies the background color and text color of <th> elements:
th {
background-color: #04AA6D;
color: white;
}
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;
}
ul {
list-style-image: url('sqpurple.gif');
}
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.
The display property is used to change the default display behavior of HTML elements.
Block-level Elements
A block-level element ALWAYS starts on a new line and takes up the full width available (stretches
out to the left and right as far as it can).
<div>
<h1> - <h6>
<p>
<form>
<header>
<footer>
<section>
Inline Elements
An inline element DOES NOT start on a new line and only takes up as much width as necessary.
<span>
<a>
<img>
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.
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:
Example
.center {
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
Example
.center {
text-align: center;
border: 3px solid green;
}
CSS Forms
Styling Input Fields
Use the width property to determine the width of the input field:
input {
width: 100%;
}
Padded Inputs
Use the padding property to add space inside the text field.
Tip: When you have many inputs after each other, you might also want to add some margin, to add
more space outside of them:
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
Bordered Inputs
Use the border property to change the border size and color, and use the border-radius property to add
rounded corners:
input[type=text] {
border: 2px solid red;
border-radius: 4px;
}
#borderimg {
border: 10px solid transparent;
padding: 15px;
border-image: url(border.png) 30 round;
}