Beruflich Dokumente
Kultur Dokumente
position Property
The position property specifies the
type of positioning method used for an
element (static, relative, fixed or
absolute).
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
Try it Yourself
static
relative
fixed
absolute
position: static;
HTML elements are positioned static by
default.
Static positioned elements are not affected
by the top, bottom, left, and right
properties.
An element with position: static; is not
positioned in any special way; it is always
position: relative;
An element with position: relative; is
positioned relative to its normal position.
Setting the top, right, bottom, and left
properties of a relatively-positioned
element will cause it to be adjusted away
from its normal position. Other content will
not be adjusted to fit into any gap left by
the element.
This <div> element has position: relative;
Here is the CSS that is used:
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
position: fixed;
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
Example
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
Try it Yourself
Try it Yourself
This <div> element has position: fixed;
Overlapping Elements
position: absolute;
This is a heading
Example
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Try it Yourself
An element with greater stack order is
always in front of an element with a lower
stack order.
Bottom Left
Top Left
Top Right
Bottom Right
Centered
Positioning Text In an
Image
Description
bottom
clip
cursor
left
overflow
Example
Styling Textareas
overflow-x
Specifies what to do with the left/right edges of the content if it overflows the elem
content area
overflow-y
Specifies what to do with the top/bottom edges of the content if it overflows the ele
content area
Example
position
right
top
z-index
Bordered Inputs
Use the border property to change the
border size and color, and use the borderradius property to add rounded corners:
First Name
Example
input[type=text] {
border: 2px solid red;
border-radius: 4px;
}
ul {
list-style-position: inside;
}
list items
Example
Example
ul.a {
list-style-type: circle;
}
ol {
background: #ff9999;
padding: 20px;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
margin: 5px;
}
Table Borders
Example
Horizontal Alignment
Example
table, th, td {
border: 1px solid black;
}
Collapse Table
Borders
The border-collapse property sets
whether the table borders should be
collapsed into a single border:
Example
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
Example
table {
width: 100%;
}
th {
height: 50px;
}
Example
th {
text-align: left;
}Try it Yourself
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).
The following example sets the vertical
text alignment to bottom for <td>
elements:
Example
td {
height: 50px;
vertical-align: bottom;
}Try it Yourself
Example
Table Padding
To control the space between the border
and the content in a table, use
the padding property on <td> and <th>
th, td {
border-bottom: 1px solid #ddd;
}Try it Yourself
elements:
Hoverable Table
Example
th, td {
padding: 15px;
text-align: left;
}Try it Yourself
First Name
Last Nam
Peter
Griffin
Horizontal Dividers
First Name
Last Name
Lois
Griffin
Peter
Griffin
Swanson
Joe
Example
Lois
Griffin
Joe
Swanson
Striped Tables
First Name
Last Nam
Peter
Griffin
Lois
Griffin
Joe
Swanson
Example
Joe
Swanson th {
background-color: #4CAF50;
color: white;
}Try it Yourself
For zebra-striped tables, use the nthchild() selector and add a backgroundcolor to all even (or odd) table rows:
Example
tr:nth-child(even) {background-color: #f2
f2f2}Try it Yourself
Table Color
The example below specifies the
background color and text color of <th>
elements:
First Name
Responsive Table
A responsive table will display a horizontal
scroll bar if the screen is too small to
display the full content:
First Name
Last Name
Points
Points
Point
Jill
Smith
50
50
50
Eve
Jackson
94
94
94
Adam
Johnson
67
67
67
Example
Peter
Griffin
<div style="overflow-x:auto;">
<table>
... table content ...
</table>
Lois
Griffin
</div>
background-clip
Property
Description
border
Theinnumbers
in the table specify the first
Sets all the border properties
one declaration
browser version that fully supports the
property.
border-collapse
border-spacing
Browser Support
Property
caption-side
empty-cells
table-layout
4.0
background-size
4.0
Specifies whether or not to display borders and background
on
empty cells in
1.0 -webkittable
background-origin
1.0
background-clip
Sets the layout algorithm to be used for a table
CSS3 Backgrounds
CSS3 contains a few new background
properties, which allow greater control of
the background element.
In this chapter you will learn how to add
multiple background images to one
element.
You will also learn about the following new
CSS3 properties:
background-size
background-origin
4.0
CSS3 Multiple
Backgrounds
CSS3 allows you to add multiple
background images for an element,
through the background-image property.
The different background images are
separated by commas, and the images are
stacked on top of each other, where the
first image is closest to the viewer.
The following example has two background
images, the first image is a flower (aligned
to the bottom and right) and the second
Example
#example1 {
background-image: url(img_flwr.gif),
url(paper.gif);
background-position: right bottom, left
top;
background-repeat: no-repeat, repeat;
}Try it Yourself
Multiple background images can be
specified using either the individual
background properties (as above) or
thebackground shorthand property.
The following example uses
the background shorthand property
(same result as example above):
Example
#example1 {
background: url(img_flwr.gif) right
bottom no-repeat, url(paper.gif) left top
repeat;
}Try it Yourself
CSS3 Background
Size
The CSS3 background-size property
allows you to specify the size of
background images.
Before CSS3, the size of a background
image was the actual size of the image.
CSS3 allows us to re-use background
images in different contexts.
Example
#div1 {
background: url(img_flower.jpg);
background-size: 100px 80px;
background-repeat: no-repeat;
}Try it Yourself
The two other possible values
for background-size are contain and cov
er.
The contain keyword scales the
background image to be as large as
possible (but both its width and its height
must fit inside the content area). As such,
depending on the proportions of the
background image and the background
positioning area, there may be some areas
of the background which are not covered
by the background image.
The cover keyword scales the background
image so that the content area is
completely covered by the background
image (both its width and height are equal
to or exceed the content area). As such,
some parts of the background image may
not be visible in the background
positioning area.
The following example illustrates the use
of contain and cover:
Example
#div1 {
background: url(img_flower.jpg);
background-size: contain;
background-repeat: no-repeat;
}
#div2 {
background: url(img_flower.jpg);
background-size: cover;
background-repeat: no-repeat;
}Try it Yourself
Define Sizes of
Multiple Background
Images
The background-size property also
accepts multiple values for background
size (using a comma-separated list), when
working with multiple backgrounds.
The following example has three
background images specified, with
different background-size value for each
image:
Example
#example1 {
background: url(img_flwr.gif) left top
no-repeat, url(img_flwr.gif) right bottom
no-repeat, url(paper.gif) left top repeat;
background-size: 50px, 130px, auto;
}Try it Yourself
Example
html {
background: url(img_flower.jpg) norepeat center fixed;
background-size: cover;
}Try it Yourself
#example1 {
border: 10px solid black;
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: content-box;
CSS3
background-clip
Property
}Try it Yourself
Example
#example1 {
border: 10px dotted black;
padding: 35px;
background: yellow;
background-clip: content-box;
CSS3 Background
Properties
}
Example
Property
Description
background
background-clip
A shorthand property
background-image
for setting all the background
Specifies
properties
one in
orone
mo
declaration
background-origin
Specifies the painting area of the background
background-size