Professional Documents
Culture Documents
CSS Part II
CSS Part II
CSS Part II
4
position: defines the positioning of the element in
the page content flow
Positioning refers to the layout of the items on
your page.
The value is one of:
static (default)
relative – relative position according to where
the element would appear with static position
absolute – position according to the innermost
positioned parent element
fixed – same as absolute, but ignores page
scrolling
5
Left to Right, Top to Bottom
Top left of the page = (0,0)
Image with
Small print
link Image
text, bullet list
Set of links
(navigation)
Regular text
When you want to do fancier
Absolute layout, you can position “boxes”
Relative or “containers.” By doing this,
Float you interrupt the normal (top to
bottom, left to right) flow. You
can do this in three ways; Float,
Absolute, and Relative.
HTML
<div>
<p> This is the This is the normal This text is floated
normal…</p> flow of a document; right.
<p class=“float”>This text from top to bottom,
is floated right.</p> left to right. When the floated text is added, it
moves to the top right corner of the containing
</div> element, in this case the <div>. Normal text
flows around the floated text.
CSS
.float {float:right;}
HTML
<div>
<p> This is the normal… This is the normal flow of a
<span class=“abs”> This text document; from top to bottom, left
is absolutely
positioned.</span>…top to
to right. When you add the
bottom… </p> absolutely positionedThis text
text, itis
absolutely
</div> moves to the coordinates you set
positioned.
based on the top left corner of the
containing element, in this case
the <div>. Normal text flows
over the absolutely positioned
CSS text. There is no gap where the
.abs {position: absolute; text is taken from.
top: 40px;
left: 80;}
Precisely specifies the location of
an element in the browser
window
h1 { background-color:#cccccc;
padding:5px;
color: #000000;
}
#content {position: absolute;
left:200;
top:100;
font-family:Arial,sans-serif;
width:300;
}
16
This text is relatively
positioned.
HTML
<div> This is the normal flow of a document; This
text is relatively positioned from top to
<p> This is the normal…
bottom, left to right. When you add the
<span class=“rel”> This text is
relatively positioned text, it moves to the
relatively positioned.
coordinates you set based on the top left
</span> … from top to
corner of the containing element, in this case
bottom…</p>
the <div>. Normal text flows as normal, but
</div> a gap is left where the relative text used to be,
and the text overlaps with the newly
positioned relative text if they are in the same
area.
CSS
.rel {position: relative;
top: -50px;
left: -150px}
Changes the location of
h1 { background-color:#cccccc; an element in relation
padding:5px; to where it would
color: #000000; otherwise appear
}
#myContent { position: relative;
left:30px;
font-family:Arial,sans-serif;
}
19
Margin VS relative positioning
Fixed and absolutely positioned elements do
not influence the page normal flow and usually
stay on top of other elements
Their position and size is ignored when calculating
the size of parent element or position of surrounding
elements
Overlaid according to their z-index
Inline fixed or absolutely positioned elements can
apply height like block-level elements
20
Elements that seem
to “float" on the
h1 { background-color:#cccccc; right or left side of
padding:5px; either the browser
color: #000000; window or another
} element are often
p { font-family:Arial,sans-serif; configured using
} the float property.
#yls {float:right;
margin: 0 0 5px 5px;
border: solid;
} 21
Configures how and if an element is displayed
display:none ;
The element will not be displayed.
display:block ;
The element is rendered as a block element --even if it is
actually an inline element,
such as a hyperlink.
display:inline;
The element will be rendered as an inline element – even if
it is actually a block element – such as a <li>.
22
Modifies the stacking
order of elements on
a Web page.
23
top, left, bottom, right: specifies offset of
absolute/fixed/relative positioned element as
numerical values
z-index : specifies the stack level of
positioned elements
Understanding stacking context
25
Size properties (width, height, min-
width, etc.) are ignored for inline boxes
margin-top and margin-bottom are
ignored,
but margin-left and margin-right are not
ignored
26
the containing block box's text-align
property controls horizontal position of inline
boxes within it
text-align does not align block boxes within the
page
each inline box's vertical-align property
aligns it vertically within its block box
27
property description
specifies where an inline element
should be aligned vertically, with
vertical-align respect to other content on the
same line within its block
element's box
29
30
<p style="background-color: red; padding: 0px; margin: 0px">
<img src="images/smiley.png" alt="smile" />
</p> HTML
32
img.headericon {
float: right; width: 130px;
} CSS
output
property description
side to hover on; can be left, right, or
float
none (default)
36
p { background-color: fuchsia; }
h2 { clear: right; background-color: yellow; }
CSS
Mario
Mario isis aa fictional
fictional character
character in
in his
his video
video game
game series.
series.
Serving
Serving asas Nintendo's
Nintendo's mascot
mascot and
and the
the main
main protagonist
protagonist of
of
thethe series,
series, Mario
Mario hashas appeared
appeared in in over
over 200
200 video
video games
games since
since his his creation.
creation
CS380
output
37
property description
disallows floating elements
from overlapping this
clear element;
can be left, right, or none
(default)
38
div#sidebar { float: right; }
p { clear: right; } CSS
39
Clearing floats (continued)
:after { content: ""; display: block;
clear: both; height: 0; }
Triggering hasLayout in IE expands a container of
floated elements
display: inline-block;
zoom: 1;
40
1. If possible, lay out an element by aligning its
content
horizontal alignment: text-align
set this on a block element; it aligns the content within it
(not the block element itself)
vertical alignment: vertical-align
set this on an inline element, and it aligns it vertically
within its containing element
2. If alignment won't work, try floating the element
3. If floating won't work, try positioning the element
absolute/fixed positioning are a last resort and should
not be overused
41
opacity: specifies the opacity of the element
Floating point number from 0 to 1
For old Mozilla browsers use –moz-opacity
For IE use filter:alpha(opacity=value) where
value is from 0 to 100; also, "binary and script
behaviors" must be enabled and hasLayout must be
triggered, e.g. with zoom:1
42
visibility
Determines whether the element is visible
hidden: element is not rendered, but still occupies
place on the page (similar to opacity:0)
visible: element is rendered normally
43
display: controls the display of the element
and the way it is rendered and if breaks should
be placed before and after the element
inline: no breaks are placed before and after
(<span> is an inline element)
block: breaks are placed before AND after the
element (<div> is a block element)
44
h2 { display: inline; background-color: yellow; }
CSS
output
property description
sets the type of CSS box
display model an element is
displayed with
values: none, inline, block, run-in,
compact, ...
use sparingly, because it can radically alter the page
layout 45
p.secret {
visibility: hidden;
} CSS
output
46
<ul id="topmenu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul> HTML
#topmenu li {
display: inline;
border: 2px solid gray;
margin-right: 1em;
} CSS
output
48
<p><img src="images/mario.png" alt=“super mario" />
Mario is a fictional character in his video game
series.....</p> HTML
output49
p { border: 2px dashed black;
overflow: hidden; } CSS
output
50
property description
specifies what to do if an
element's content is too
overflow large;
can be auto, visible, hidden,
or scroll
51
overflow: defines the behavior of element when
content needs more space than you have specified
by the size properties or for other reasons. Values:
visible (default) – content spills out of the
element
auto - show scrollbars if needed
scroll – always show scrollbars
hidden – any content that cannot fit is clipped
52
<html>
<head>
<title>My Resume</title>
<link href=“home.css" rel="stylesheet"
type="text/css">
</head>
<body>
<div id="container">
Content goes here
</div>
</body>
</html>
HTML
#container
#banner
#nav #content
#footer
<body>
<div id="container">
<div id="banner"><img src="pic.jpg" /></div>
<div id="nav">
<span><a href="home.htm">home</a></span>
<span><a href="contact.htm">contact</a></span>
</div>
<div id=“content">
<h1>My Resume</h1> HTML
<p>Resume text</p>
</div>
<div id=“footer”>Copyright info here</div>
</div>
</body>
body {
font-size: 1em; }
#container {
width: 920px; }
#banner {
width: 920px; height: 120px; }
#nav {
float: left; width: 200px; }
#content { CSS
width: 720px; }
#footer {
font-size: .8em; }
Example :
https://www.youtube.com/watch?v=Xb0yIAxYv10
Except for imagelogo, all elements
on this page follow normal flow
wrapper contains the two columns – sets default background color
Left-column navigation
float: left;
width:100px;
Right-column content
margin-left: 100px;
58
body {margin:0;
font-family:Verdana, Arial, sans-
serif; }
#wrapper { background-color:#e8b9e8;
color:#000066; }
#leftcolumn { float:left;
width:100px; }
#rightcolumn { margin-left:100px;
background-color:#ffffff;
color:#000000; }
#logo { background-color:#eeeeee;
color: #cc66cc;
font-size:x-large;
border-bottom: 1px solid #000000;
padding:10px; }
.content {padding:20px 20px 0 20px; }
#floatright {margin:10px;
float:right; }
.footer {font-size:xx-small;
text-align:center;
clear:right;
padding-bottom:20px; }
.navBar{ text-decoration:none;
margin: 15px;
display:block; }
59
cursor: specifies the look of the mouse cursor
when placed over the element
Values: crosshair, help, pointer, progress,
move, hair, col-resize, row-resize, text, wait,
copy, drop, and others
white-space – controls the line breaking of
text. Value is one of:
nowrap – keeps the text on one line
normal (default) – browser decides whether to brake
the lines if needed
60
More powerful formatting than using
presentation tags
Your pages load faster, because browsers
cache the .css files
Increased accessibility, because rules can be
defined according given media
Pages are easier to maintain and update
61