Chapter 4 Advanced CSS
Chapter 4 Advanced CSS
Chapter 4
1
Web Programming
2
Web Programming 1
NORMAL FLOW
Normal flow refers here to how the browser will normally display block-level
elements and inline elements from left to right and from top to bottom
•Inline elements do not form their own blocks but instead are displayed within
lines
•<em>, <a>, <img>, and <span>
3
Web Programming 1
NORMAL FLOW
4
Web Programming 1
NORMAL FLOW
5
Web Programming 1
Inline Elements
There are actually two types of inline elements: replaced and nonreplaced.
Replaced inline elements are elements whose content and thus appearance
is defined by some external resource, such as <img> and the various form
elements.
6
Web Programming 1
Working Together
7
Web Programming 1
Take control
It is possible to change whether an element is block-level or inline via the CSS
display property. Consider the following two CSS rules:
li { display: inline; }
These two rules will make all <span> elements behave like block-level elements
and all <li> elements like inline (that is, each list item will be displayed on the
same line).
8
Web Programming 2
Positioning Elements
It is possible to move an item from its regular position in the normal flow, and
•position it so it is always visible in a fixed position while the rest of the content
scrolls.
9
Web Programming 2
Positioning
10
Web Programming 2
Relative Positioning
In relative positioning an element is displaced out of its normal flow position
and moved relative to where it would have been placed
The other content around the relatively positioned element “remembers” the
element’s old position in the flow; thus the space the element would have
occupied is preserved
11
Web Programming 2
Relative Positioning
12
Web Programming 2
Absolute Positioning
When an element is positioned absolutely, it is removed completely from
normal flow.
Unlike with relative positioning, space is not left for the moved element, as it is
no longer in the normal flow.
13
Web Programming 2
Absolute Positioning
14
Web Programming 2
Absolute Positioning
A moved element via
absolute position is actually
positioned relative to its
nearest positioned ancestor
container
15
Web Programming 2
Z-Index
When overlapping elements items closest to the viewer (and thus on the top)
have a larger z-index
•simply setting the z-index value of elements will not necessarily move them
on top or behind other items.
16
Web Programming 2
Z-Index
17
Web Programming 2
Z-Index
18
Web Programming 2
Fixed Position
19
Web Programming 3
FLOATING ELEMENTS
It is possible to displace an element out of its position in the normal flow via
the CSS float property
When an item is floated, it is moved all the way to the far left or far right of
its containing block and the rest of the content is “re-flowed” around the
floated element
20
Web Programming 3
FLOATING ELEMENTS
Notice that a floated block-level
element must have a width specified, if
you do not, then the width will be set
to auto, which will mean it implicitly
fills the entire width of the containing
block, and there thus will be no room
available to flow content around the
floated item.
21
Web Programming 3
22
Web Programming 3
23
Web Programming 3
All other content in the containing block (including other floated elements) will
flow around all the floated elements
24
Web Programming 3
Floating side by
side
25
Web Programming 3
26
Web Programming 3
Containing Floats
Another problem that can occur with floats is when an element is floated
within a containing block that contains only floated content. In such a case, the
containing block essentially disappears
27
Web Programming 3
Containing Floats
One solution would be to float the container as well, but depending on the
layout this might not be possible. A better solution would be to use the
overflow property
28
Web Programming 3
Positioning is often used for smaller design changes, such as moving items
relative to other elements within a container
In such a case, relative positioning is used to create the positioning context for
a subsequent absolute positioning move
29
Web Programming 3
Positioning Context
30
Web Programming 3
Positioning Context
31
Web Programming 3
Hiding elements
Two different ways to hide elements in CSS:
•The display property takes an item out of the flow: it is as if the element no
longer exists
•The visibility property just hides the element, but the space for that
element remains.
32
Web Programming 3
Hiding elements
33
Web Programming 3
34
Web Programming 4
35
Web Programming 4
36
Web Programming 4
37
Web Programming 4
38
Web Programming 4
39
Web Programming 4
Positioning discussion
Notice that with positioning it is easier to construct our source document with
content, the main <div> can be placed first.
40
Web Programming 4
Positioning discussion
What would happen if one of the sidebars had a lot of content and was thus
quite long?
•In the floated layout, this would not be a problem at all, because when an
item is floated, blank space is left behind.
41
Web Programming 4
Positioning discussion
42
Web Programming 5
Satisfying both users can be difficult; the approach to take for one type of site
content might not work as well with another site with different content.
43
Web Programming 5
•Fixed Layout
•Liquid Layout
•Hybrid Layout
44
Web Programming 5
Fixed Layout
In a fixed layout, the basic width of the design is set by the designer, typically
corresponding to an “ideal” width based on a “typical” monitor resolution.
The advantage of a fixed layout is that it is easier to produce and generally has
a predictable visual result.
•It is also optimized for typical desktop monitors; however, as more and
more user visits are happening via smaller mobile devices 45
Web Programming 5
Fixed Layout
46
Web Programming 5
47
Web Programming 5
Liquid Layout
In a liquid layout (also called a fluid layout) widths are not specified using
pixels, but percentage values.
Advantage:
•adapts to different browser sizes,
Disadvantages:
•Liquid layouts can be more difficult to create because some elements,
such as images, have fixed pixel sizes
•the line length (which is an important contributing factor to readability)
may become too long or too short
48
Web Programming 5
Liquid Layout
49
Web Programming 5
Hybrid Layout
A hybrid layout combines pixel and percentage measurements.
•percentages would make more sense for the main content or navigation
areas, with perhaps min and max size limits in pixels set for the
navigation areas
50
Web Programming 6
Responsive Design
In a responsive design, the page “responds” to changes in the browser size that
go beyond the width scaling of a liquid layout.
One of the problems of a liquid layout is that images and horizontal navigation
elements tend to take up a fixed size, and when the browser window shrinks to
the size of a mobile browser, liquid layouts can become unusable. In a
responsive layout, images will be scaled down and navigation elements will be
replaced as the browser shrinks,
51
Web Programming 6
Responsive Design
52
Web Programming 6
Responsive Design
There are four key components that make responsive design work.
1.Liquid layouts
53
Web Programming 6
Responsive Design
Responsive designs begin with a liquid layout, that is, one in which most
elements have their widths specified as percentages. Making images scale in
size is actually quite straightforward, in that you simply need to specify the
following rule:
img {
max-width: 100%;
}
54
Web Programming 6
Responsive Design
A key technique in creating
responsive layouts makes
use of the ability of
current mobile browsers
to shrink or grow the web
page to fit the width of the
screen.
55
Web Programming 6
Responsive Design
If the developer has created a responsive site that will scale to fit a smaller
screen, she may not want the mobile browser to render it on the full-size
viewport. The web page can tell the mobile browser the viewport size to use
via the viewport
<meta> element
<html>
<head>
<meta name="viewport" content="width=device-width" />
56
Web Programming 6
Responsive Design
57
Web Programming 6
Media Queries
The other key component of responsive designs is CSS media queries. A media
query is a way to apply style rules based on the medium that is displaying the
file. You can use these queries to look at the capabilities of the device, and
then define CSS rules to target that device.
58
Web Programming 6
Media Queries
Contemporary responsive sites will typically provide CSS rules for phone
displays first, then tablets, then desktop monitors, an approach called
progressive enhancement, in which a design is adapted to progressively more
advanced devices
59
Web Programming 6
Media Queries
60
Web Programming 6
Media Queries
61
Web Programming 7
CSS Frameworks
A CSS framework is a procreated set of CSS classes or other software tools that
make it easier to use and work with CSS.
•Grid systems
•CSS preprocessors.
62
Web Programming 7
Grid Systems
Grid systems make it easier to create multicolumn layouts. Some of the most
popular are:
•Bootstrap (twitter.github.com/bootstrap)
•Blueprint (www.blueprintcss.org)
•960 (960.gs)
Grid Systems
64
Web Programming 7
65
Web Programming 7
66
Web Programming 7
67
Web Programming 7
CSS Preprocessors
CSS preprocessors are tools that allow the developer to write CSS that takes
advantage of programming ideas such as variables, inheritance, calculations,
and functions.
Most sites make use of some type of color scheme, perhaps four or five colors.
Many items will have the same color.
68
Web Programming 7
CSS Preprocessors
69
Web Programming 7
70