4 CSS 30 05 2024
4 CSS 30 05 2024
Courtesy:Nick Foxall
1
The Basis of CSS layout
The 3 core concepts to understand about CSS
layout are:
1. The CSS box model
2. Floating
3. Positioning
2
The CSS Box Model
Every block element in CSS is effectively inside a box, and
can have margins, padding and borders applied to it.
3
The CSS Box Model
Every CSS box is divided into regions,
consisting of:
1. Content
2. Padding
3. Border
4. Margins
4
The CSS Box Model
Margin
Border
Padding
Content
5
The CSS Box Model
Margin
Border
Padding
6
Margins & Padding
Margins and Padding may seem similar at first
glance. But each has its own effect on content,
particularly on any backgrounds assigned to block
and div elements.
Margin
Padding
Content
7
Margins & Padding
Margins
Margins define the space around elements outside the
border
Margins
Margin-bottom: 30px collapse to Margin-bottom: 30px
form a
Margin-top: 20px single
margin
Content Area
Content Area
11
Margin Collapse: Contained Elements
Before After
Margins
Margin-top: 30px collapse to Margin-top: 30px
form a single
Margin-top: 20px margin
Content Area
Content Area
Containing (outer)
block element
12
CSS Shorthand: Margin & Padding
For margin and padding (and others), CSS provides a
number of shorthand properties that can save on writing
lines and lines of code. Instead of writing this:
0
#container {
12
margin-top: 0;
margin-right: 5px;
margin-bottom: 6px; Content Area
margin-left: 5px; 5 5
12 10
padding-top: 20px;
padding-right: 10px;
30
padding-bottom: 30px;
6
padding-left: 12px;
}
13
CSS Shorthand: Margin & Padding
…Its much easier to write this:
#container {
padding: 20px 10px 30px 12px;
margin: 0px 5px 6px 5px; 0
20
}
30
14
CSS Shorthand: Margin and Padding
You can also apply just one value, example:
#container {
5
padding: 20px;
20
margin: 5px;
}
Content Area
Which will apply the value 5
20 20
5
15
CSS Shorthand: Margin and Padding
And you can apply two values, example:
#container {
0
padding: 10px 20px; 10
margin: 0px 5px;
}
Content Area
The first value is applied to 5
20 20
5
16
CSS Shorthand: Margin and Padding: auto
A useful value to remember is „auto‟:
#container {
0
padding: 10px 20px; 10
margin: 0px auto;
}
Content Area
Usually applied to the left & auto
20 20
auto
17
Borders
Borders can be applied to any block element
18
Borders
The core border properties are:
Width: absolute (px, in, cm, or ‘thin’, ‘medium’, ‘thick’),
or relative (ems)
Style: dotted, dashed, solid, double, groove, ridge,
inset, outset, hidden, etc
Color: ‘blue’, ‘red’, #FF9900, etc
19
CSS Floats: “Normal Flow”
CSS boxes for block
elements are stacked, one
above the other, so that
they‟re read from top to
bottom.
block element boxes text text text text text text text text text text
text text text text text text text text text text
using floats. text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
Setting the float block text text text text text text textinline
text text text
text text text text text text text text text text
property to left or right text text text text text text text text text text
text text text text text text text text text text
causes a box to be text text text text text text text text text text
text text text text text text text text text text
taken out of its text text text text text text text text text text
text text text text text text text text text text
position in the
normal flow and
moved as far left or
right as possible.
21
Float Values
The Float property has text text text text textinline
text text text text text
three value options: text text text text text text text text text text
text text text text text text text text text text
text textinline
text text text text text text text text
text text text text text text text text text text
float: left; text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
float: right; block
text text text text text text text text text text
text text text text text text textinline
text text text
text text text text text text text text text text
float: none; text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
22
Restoring the Normal Flow: “Clear”
To restore the “normal
text text text text textinline
text text text text text
flow”, we can use the text text text text text text text text text text
clear property. text text text text text text text text text text
text textinline
text text text text text text text text
text text text text text text text text text text
The clear property has text text text text text text text text text text
text text text text text text text text text text
three value options: text text text text text text text text text text
text text text text text text text text text text
block text text text text text text textinline
text text text
clear: left; text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
clear: right; text text text text text text text text text text
text text text text text text text text text text
clear: both; text text text text text text text text text text
text text text text text text text text text text
23
CSS Positioning
The third core concept to understand in CSS layout (after
the „box model‟ and „floats‟), is positioning.
• Relative Positioning
• Absolute Positioning
24
CSS Positioning: Relative Positioning
A relatively positioned element will stay exactly where it is,
in relation to the normal flow.
You can then offset its position “relative” to its starting point
in the normal flow:
Containing box
25
CSS Positioning: Relative Positioning
In this example, box 2 is offset 20px, top and left. The
result is the box is offset 20px from its original position in
the normal flow. Box 2 may overlap other boxes in the flow,
but other boxes still recognise its original position in the
flow.
top: 20px
#myBox {
Left: 20px
position: relative;
Box 1 Position: relative Box 3
left: 20px;
top: 20px; Box 2
}
Containing box
26
CSS Positioning: Absolute Positioning
An absolutely positioned box is taken out of the normal
flow, and positioned in relation to its nearest positioned
ancestor (i.e. its containing box).
top: 20px
Left: 20px
Box 1 Box 3
Position: absolute
Box 2
#myBox {
Left: 20px
position: absolute;
Box 1 Box 3
left: 20px; Position: absolute
top: 20px;
Box 2
}
Containing box (relatively positioned ancestor)
28
CSS Positioning: Fixed Positioning
Fixed Positioning is a sub-category of Absolute Positioning
29
Floats & Positioning
Summary:
Floats (also a form of positioning) takes boxes out of the normal
flow and “floats” them left or right edges of the containing block
30
Background Images in CSS
It is also possible to use the background CSS
property any block element (including div‟s) to
place a background image behind other elements.
31
Background Images in CSS: Fixed Position
Background images will normally scroll with the containing
box, and the rest of the page
#sidebar {
float: right;
width: 300px;
margin-left: 25px;
background-image: url(images/harbour.jpg);
background-attachment: fixed;
}
32
Using Background Images
Background images are useful in allowing us to
visually define a page, and separate content into a
deliberate visual hierarchy.
34
Fixed-Width Rounded Corner Boxes
Simple flat-colour, fixed-width
boxtop.gif
rounded corner boxes require
only 2 images:
boxbottom.gif
boxshaded_bottom.gif
35
Flexible-Width Rounded Corner Boxes
Flexible-width rounded corner boxshadedtopleft.gif
boxshadedtopright.gif
boxes are a little more
complicated...
boxshadedbottomright.gif
36
Flexible-Width Rounded Corner Boxes
The CSS,
HTML
structure H2 heading
.flexbox
.flexbox-outer
.flexbox-inner
.flexbox h2
37
Centering in the Browser Window
We‟ve seen how to center a box horizontally in the
window:
Set the body to center
Create a container div (fixed or relative width), with left
and right margins set to auto
38
Centering in the Browser Window
Like this:
Set the body to center
Create a container div (fixed or relative width), with..
• Position: absolute;
• top: 50%;
•
• left: 50%;
• margin-top: -300px;
•
• margin-left: -400px;
40
Using divs to Define CSS boxes
id example: in the XHTML: class example: in the XHTML: