Chapter 2 CSS
Chapter 2 CSS
TBS/2018-2019
Introduction
2
page.html
Result
How to Insert CSS? (3/5)
Internal CSS
7
One file:
page.html Result
How to Insert CSS? (4/5)
External stylesheet
8
Result
How to Insert CSS? (5/5)
External stylesheet
9
Advantages
Specify the style once for every instance of each class.
Example: Specify the font once for all text on the HTML page
that you’ve identified as a “header”.
All the HTML pages of the entire website can link to the same
stylesheet file: a separate file.
Can change the style of the entire site by editing only ONE
FILE: the CSS file.
Declaration block
CSS comments
12
p { h1, h2, p {
color: red; color: red;
text-align: center; text-align: center;
} }
<body>
<h1 class="center">Red and center-aligned .center {
all HTML
heading</h1> color: red;
elements with
<p class="center">Red and center-aligned text-align: center; class="center"
paragraph.</p> } will be red and
center-aligned
</body>
In the example below, only <p> elements with class="center" will be center-aligned:
p.center {
color: red;
text-align: center;
}
CSS Elements
15
Backgrounds,
Borders,
Margins,
Padding,
Height/Width,
Division,
Combinators,
Font,
Text,
Lists,
Tables,
Counters,
Position,
and much more..
Color
16
p, h1 { background-color: lightblue; }
Values:
no-repeat
repeat
repeat-x
repeat-y
Background (2/2)
18
background-attachment: If you are using an image as a background. You can
set whether the background scrolls with the page or is fixed when the user
scrolls down the page with the background-attachment property.
background-position:
Values:
body { background-position: value; } top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
x-% y-%
x-pos y-pos
Border (1/2)
19
The CSS border properties allow you to specify the style, width, and color of an
element's border:
border-style:
Values:
border-style: value; dashed, dotted
double, groove
hidden, inset
none, outset
ridge, solid
border-color:
border-width:
border-width: 2px 10px 4px 20px;
Border (2/2)
20
In CSS, there is properties for specifying each of the borders (top, right, bottom,
and left):
p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
You can set the color, style and width of the borders around an element in one
declaration by using the shorthand property border :
p{
border: 5px solid red;
}
Margin & padding
21
1
margin-top
padding-Left
padding-right
padding-top
margin-right
Margin-Left
2
4
padding-bottom
margin-bottom
3
Margin
22
The margin property declares the margin between an HTML element and the
elements around it. The margin property can be set for the top, left, right and
bottom of an element.
The height and width can be set to auto (this is default. Means that the browser
calculates the height and width), or be specified in length values, like px, cm,
etc., or in percent (%) of the containing block:
div {
width: 500px;
height: 100px;
border: 3px solid #73AD21;
}
Divisions are a block level HTML element used to define sections of an HTML
file. A division can contain all the parts that make up your website. Including
additional divisions, images, text and so on.
<body> <body>
<div > <div id=”container”>
Site contents go here. Site contents go here.
</div> </div>
</body> </body>
div { #container {
width: 70%; width: 70%;
margin: auto; margin: auto;
padding: 20px; padding: 20px;
border: 1px solid #666; border: 1px solid #666;
} }
Combinators
26
descendant selector (space): The following example selects all <p> elements inside
<div> elements:
div p { background-color: yellow; }
child selector (>): The following example selects all <p> elements that are immediate
children of a <div> element:
div > p { background-color: yellow; }
adjacent sibling selector (+): The following example selects all <p> elements that are
placed immediately after <div> elements:
general sibling selector (~): The following example selects all <p> elements that are not
in <div> elements:
div ~ p { background-color: yellow; }
Font
27
The font property can set the style, weight, variant, size, line height and font:
font-family:
font-style:
font-style: value; Values: normal, italic, oblique
1em is equal to the
font-size: current font size.
The default text size
in browsers is 16px.
font-size: 100%; font-size: 40px; font-size: 2.5em; So, the default size
of 1em is 16px.
font-weight:
font-variant:
color: you can set the color and align a text with the color and text align attributes.
h1 { color: rgb(255,0,0);
text-align: justify; }
text-decoration:
Values:
None, underline, Overline,
h1 { text-decoration: value; }
line through, blink
text-transformation:
Values:
text-transform: values;
none, capitalize, lowercase, uppercase
h1 { word-spacing: 10px;}
Lists
30
The CSS list properties allow you to:
Set different list item markers for ordered and unordered lists:
Values:
list-style-type: value; disc, circle, square, decimal, lower-roman
upper-roman, lower-alpha, upper-alpha, none
Set an image as the list item marker:
Set the position of the list item markers: specifies whether the list-item markers should
appear inside or outside the content flow:
ol { background: #ff9999; }
Tables
31
border:
table, th, td {
border: 1px solid black;
}
border-collapse: set whether the table borders should be collapsed into a single border.
table {
border-collapse: collapse;
}
CSS counters are like "variables". The variable values can be incremented by
CSS rules (which will track how many times they are used):
The position property changes how elements are positioned on your webpage.
position: value;
Values:
static: An element with position: static; is not positioned in any special way; it is always
positioned according to the normal flow of the page:
relative: places the element in the normal flow of your HTML file and then offsets it by
some amount using the properties left, right, top and bottom.
absolute: removes the element from the normal flow of your HTML file, and positions it
to the top left of its nearest parent element that has a position declared other than static.
Fixed: An element that is positioned with a fixed value, will not scroll with the
document. It will remain in its position regardless of the scroll position of the page.
When positioning elements with relative, absolute or fixed values the following
properties are used to offset the element: Top, left, right, bottom.