All paragraphs inside a div of class "main" will have a line-height of 1.5 ex (about 150%).
Here are some of the advanced words in the css language, with examples of code and results.
Contents
|
More properties for "boxes"
Remember that each html tag makes a box. There can be boxes inside of boxes. |
||||||
|---|---|---|---|---|---|---|
| properties for boxes: Float and Position | ||||||
| property name | possible values | example code | example display | default | use in | percentages refer to |
|
float
See sample code in the css-howtos directory |
[ left, right ] |
div.image {
float: left;
width: 25%;
margin-left: 40px;
}
<div class="image"> Some words to fill up the paragraph so we have something to see </div> <div class="image"> In some browsers, the tops of the paragraphs won't line up. </div> |
Some words to fill up the paragraph so
we have something to see
In some browsers, the tops of the paragraphs
won't line up.
|
... | All elements | n/a |
| position | [ static, fixed, relative, absolute, inherit ] |
static:
The default position.
div {
position: static;
}
<img src="images/bullit1.gif" alt="bullet" /> <div> This div is static. </div> |
This div is "static" - it is in its default position.
|
static | All elements. | n/a |
|
fixed:
The box is fixed with respect to some outside reference.
<p style="position: fixed; top: 30px; right: 30px;
padding: .5ex .5em .5ex .5em;
color: #cc0000; background-color: #ffffcc;" />
Practice makes perfect.
</p>
|
(The floating "Practice makes perfect" on the left side of this web page is from this code.) |
|||||
|
relative:
The box is offset relative to its normal position,
div {
position: relative;
left: 40px;
}
<img src="images/bullit1.gif" alt="bullet" /> <div> This div is shifted 40 pixels to the left. </div> |
This div is "relative", and shifted 40 pixels to the left.
|
|||||
|
absolute:
The box is offset with respect to it's root element
(which is usually its containing block).
See next item for example: |
||||||
| top | [ <length>, <percentage>, auto, <inherit>, ] |
The distance of a box's top|left|bottom|right edge
div {
width: 100px;
height: 100px;
border: 1px dashed #ccffcc;
}
img {
position: absolute;
top: 20px;
left: 50px;
}
<div> <img src="images/bullit1.gif" alt="bullet" /> </div> |
... | Positioned elements. | height of containing block | |
| left | ||||||
| bottom | ||||||
| right | ||||||
| properties for boxes: More Display | ||||||
|---|---|---|---|---|---|---|
| property name | possible values | example code | example display | default | use in | percentages refer to |
| visibility | [ hidden, visible, collapse, inherit ] |
p.seeit {
visibility: visible;
}
p.noseeit {
visibility: hidden;
}
<p class="seeit"> This paragraph is visible. </p> <p class="noseeit"> This paragraph is hidden! </p> |
This paragraph is visible. |
visible | All elements. | n/a |
| z-index | [ <number>, auto, inherit ] |
auto:
The stack level of the generated box in the
div.one {
background-color: #cccccc;
position: relative;
z-index: 2;
left: 20px;
width: 200px;
height: 150px;
}
div.two {
background-color: #cc0000;
position: relative;
z-index: 1;
left: 35px;
bottom: 250px;
width: 200px;
height: 150px;
}
img {
position: relative;
z-index: 3;
left: 60px;
bottom: 130px;
width: 200px;
height: 150px;
}
<div class="one"> </div> <img src="images/kalm-valley.jpg" alt="kalmiopsis" /> <div class="two"> </div> |
|
... | Any positioned element: That is, we use z-index together with position | n/a |
| overflow |
[
visible,
hidden,
scroll,
auto,
inherit
]
|
div {
width: 50px;
height: 80px;
border: 1px solid #cc0000;
}
<div> <img src="webwalker-logo.gif" alt="logo" /> </div> <div style="overflow: hidden;"> <img src="webwalker-logo.gif" alt="logo" /> </div> |
|
visible | Block-level elements, table cells, and linline block elements. | n/a |
| Organising our code – what is that? and why? | ||||||
|---|---|---|---|---|---|---|
| defining a Relation to other selectors | ||||||
| selector name | description | example code | example display | |||
|
universal selector:
* More at: |
Matches any element. |
* {
font-family: times, serif;
border: 4px groove #cc6633;
}
<p> We can have a style for all elements with the *. </p> <ul> <li> All elements on the page will have the same style. </li> </ul> |
We can have a style for all elements with the *.
|
|||
|
descendant selector:
A B More at: |
Matches any element B that is a child of any element A. |
div.main p {
line-height: 1.5em;
}
<div class="main"> <p> All paragraphs inside a div of class "main" will have a line-height of 1.5 ex (about 150%). </p> </div> <p> All paragraphs not inside this div will have their default line-height. </p> |
All paragraphs inside a div of class "main" will have a line-height of 1.5 ex (about 150%). All paragraphs not inside this div will have their default line-height. |
|||
|
child selector:
A>B More at: |
Matches any element B that is a direct child of any element A. |
div.main>p {
line-height: 1.5em;
}
<div class="main"> <p> All paragraphs inside a div of class "main" will have a line-height of 1.5 ex (about 150%). </p> </div> <p> All paragraphs not inside this div will have their default line-height. </p> |
All paragraphs inside a div of class "main" will have a line-height of 1.5 ex (about 150%). All paragraphs not inside this div will have their default line-height. |
|||
|
adjacent sibling selector:
A+B More at: |
Matches any element B that immediately follows any element A. |
p+ul {
margin-top: 0;
}
<p> An unordered list usually has some margin at the top. </p> <ul> <li> Often when a list follows a paragraph, we would like to have less or no margin. (The paragraph will have some margin at the bottom, which is usually enough.) </li> </ul> |
An unordered list usually has some margin at the top.
|
|||
|
attribute selector:
A[att] More at: |
Matches any element A that has the given attribute defined, whatever its value. |
table[border] {
background-color: #66ff66;
}
<table border="1"> <tr> <td> This table has a border, so it has a green background. </td> </tr> </table> <br /> <table> <tr> <td> This table has no border, so it has no background color. </td> </tr> </table> <br /> <table border="6"> <tr> <td> This table has a border, so it also has a green background. </td> </tr> </table> |
|
|||
|
attribute selector with value:
A[att="val"] More at: |
Matches any element A that has the given attribute defined to the given value. |
table[border="3"] {
background-color: #ffccff;
}
<table border="6"> <tr> <td> This table has a border of 6, so it has no background color. </td> </tr> </table> <br /> <table border="3"> <tr> <td> This table has a border of 3, so it has a background color. </td> </tr> </table> <br /> <table> <tr> <td> This table has no border, so it has no background color. </td> </tr> </table> |
|
|||
|
attribute selector with exactly one of several values:
A[att~="val"]
More at: |
Match when the element's "att" attribute value is a space-separated list of "words",
one of which is exactly "val".
(Sometimes you might have more than one class for a tag. This will allow you to pick one of them for a style.) |
p[class~="example"] {
background-color: #ffccff;
}
<p class="example code display"> This paragraph has several classes – one of them is "example", so it has a background color. </p> <p class="code display"> This paragraph has several classes, but none of them is "example", so no background color. </p> |
This paragraph has several classes – one of them is "example", so it has a background color. This paragraph has several classes, but none of them is "example", so no background color. |
|||
|
attribute selector with partly one of several values:
A[att|="val"]
More at: |
Match when the element's "att" attribute value is a hyphen- separated list of "words", beginning with "val". The match always starts at the beginning of the attribute value. This is primarily intended to allow language subcode matches (e.g., the "lang" attribute in HTML) |
p[lang|="en"] {
background-color: #ffccff;
}
<p lang="en-US"> This paragraph has language set that starts with "en", so has a background color. </p> <p lang="de"> This paragraph has language set to "de", so no background color. </p> |
This paragraph has language set that starts with "en", so has a background color. This paragraph has language set to "de", so no background color. |
|||
| writing your css codes: Order matters! | ||||||
|
Here's the magic part: The cascade in cascading style sheet! It goes like this: If you put a style in your external stylesheet, and later in the stylesheet put a different one, that later one will win. If you put a style in your external stylesheet, and put a different style in a tag on a page, the tag style will win.
I can explain this better in person.
|
||||||
| More information |
|---|
|
about class and id: See the css faq about the doctype: See the css faq More about using CSS at the css faq |
|
| copyright / license |
top ↑ |
|---|---|
|
Q: Can i copy this file? Can i give to other people? Answer: Sure, you can give it to anyone, according to the terms of the license below. It is copyright © 2004 James Walker. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included here. If not found, find it at www.fsf.org - the Free Software Foundation. |
|
| translation |
top ↑ |
|---|---|
|
It would be very useful to have translations of this document in any language. Most preferred would be Tibetan and Chinese. Please contact me if you can do a translation for us. |
|
| contact |
top ↑ |
|---|---|
|
Was this helpful? Is it clear, or confusing? I would be happy to hear anything about how to make this better. You can contact me at the email address i have given you, or at http://www.webwalker.to/contact.php |
|
All content not copyright by anyone else is
copyright © 2003–2010 James Walker.
License for use is the GNU Free Documentation License.
Find it:
here in the
License directory
or
at the Free Software Foundation,
www.fsf.org