Perhaps the most important part of webpage design is the layout. This can be controlled by:
Property: | display |
---|---|
Default value: | Inline |
Inherited: | No |
How do you want your element to appear? As an inline element? As a block element? Or at all? Most elements are either inline
or block
, but there are a number of other values as well. The most common are:
a
, abbr
, acronym
, em
, span
, and strong
h1
- h6
, div
, and p
.Below is a webpage with four span
elements, one that is set as an inline element, one as a block, one that is inline-block, and one that's not shown.
span
Elements Displayed Four WaysThe page below is the result of the above code:
Please note that the height
and width
properties have no effect on inline elements.
Properties: |
|
---|---|
Default value: | auto (both) |
Inherited: | No (both) |
Block and inline-block elements are two-dimensional, as stated earlier. Normally, block elements stretch all the way across the window and are as long as they need to be, but you can set their vertical size (height
) and horizontal size (width
).
One example of an inline-block element is (unsurprisingly) img
—by its very nature, it has height and width but is an inline element. Its normal size is the size of the picture, but you can stretch it or shrink it to whatever size you want. Do note: That will do nothing to the file size of the image and thus nothing to how long the image takes to download. A 400-kilobyte 1000 X 4000 pixel photograph displayed as a 10 X 40 pixel thumbnail on a webpage will still take forever to download.
Both can be set as any of the following:
auto
Property: | overflow |
---|---|
Default value: | visible |
Inherited: | No |
When you set an element's size explicitely, it is entirely possible that its content will take up more room than the element is supposed to. To deal with this is the property of the overflow
property, which has the following values:
It's not just a vertical scrollbar that shows up, but a horizontal one as well, if required. Do note that some people find horizontal scrollbars very annoying. Below are various paragraphs given both height and width, and displaying the overflow properties. First, the stylesheet:
And the result:
The second and third paragraph look quite similar, but a closer look reveals that while both paragraphs have vertical scrollbars, the third has an additional inactive horizontal scrollbar as well.
Property: | position |
---|---|
Default value: | static |
Inherited: | No |
Positioning an element is a means of deciding where it's supposed to go. There are four keywords that decide how an element is positioned:
How they each work is quite different, so I'll explain the properties that define an element's actual position first:
If position
is set to static
, none of the above properties will have any effect.
For position:absolute;
, the element is taken out of the normal flow of elements entirely and positioned according to the properties defined above. This does mean that an absolutely positioned element can overlap with other elements and content.
For position:absolute;
, top
, left
, and right
set the the distance between the top, left, or right side of the element (respectively) from the top, left, or right side (respectively again) of the webpage. So, if you have position
set to absolute
and top
to 0
, that element will be right at the top of the webpage, and when you scroll down, you soon won't see it anymore.
The bottom
property sets the distance between the bottom of the element and the bottom of the viewport before the page is scrolled. When you scroll it, the element moves with the page.
Below is a stylesheet displaying absolute positioning. The 1,000-pixel-high div
element is there to make sure the page can be scrolled up and down and contains no text whatsoever.
The result can be seen below. The image on the right shows what happens when you scroll down:
The size of the window does not matter; the elements are always where they are according to the sides of the viewport, which is important to remember when designing a webpage.
One other thing that's important to know about absolute positioning: If you absolutely position the child element of an absolutely positioned element (and you can do this), the position will be calculated from the parent element, rather than from the viewport.
This has several practical applications. One of the more common applications is to make the links in a navigation list line up with a picture.
For position:fixed;
, the element is positioned exactly like it is for position:absolute;
with one difference: a fixed element doesn't move. To demonstrate this, I've used almost the exact same code I used to demonstrate absolute positioning, but tweaked it a little: I made the font size for the paragraphs smaller, gave the div
element a margin instead of a size so it wouldn't overlap with the paragraphs (note: this is a really common trick), and said div
element now contains some text. Below is the stylesheet:
The result is below. On the right is the result of scrolling down.:
The Lorem Ipsum
text moves, the paragraphs don't.
If you absolutely position the child elements of an element with a fixed position, the child elements will stay where they are according to their positioning with their parent element.
Relative positioning, unlike absolute or fixed positioning, doesn't take an element out of the flow. Instead, it's offset from where it normally would be, and the following elements are placed as if the positioned element was never moved at all.
Below is a stylesheet using relative positioning.
And the result, showing the second paragraph offset, but the third almost getting overlapped because it hasn't moved at all. Do note the horizontal scrollbar, because the second paragraph is just as wide as the others, even though it's been moved over.
Property: | float |
---|---|
Default value: | none |
Inherited: | No |
Floating is a little tricky to describe. Below are the keywords of the float
property:
The floated element is not taken out of flow entirely; the content that comes before it in the code comes before it on the page. A floated element is set to either the left or right side of the screen (depending on the keyword you use) and the following content flows around it.
Below is a webpage demonstrating this:
The stylesheet for this page this may look complex, but it really isn't. In fact, in this particular stylesheet, there is not a single property in the following stylesheet that hasn't been covered yet: colour was explained in the chapter dealing with CSS Value Types and Font and Text; padding, margins, and borders were all covered in The CSS Box Model; backgrounds in CSS Backgrounds, and width and height earlier in this chapter. I'll say it again: you've seen it all before.
It is important to note that if two adjacent paragraphs are both floated left, right, or one left and one right, they will appear side-by-side.
Property: | clear |
---|---|
Default value: | none |
Inherited: | No |
When one or more elements are floated, the clear
property explicitly states that the element it is applied to does not flow around the floated element, but is placed below the bottom edge of the floated element. This can be used to create a new row of floated elements, if so desired.
The clear
property has the following keywords:
To demonstrate the clear
property, I created a page with an identical stylesheet as the one used to demonstrate the float
property, but added a single rule to affect the fifth paragraph:
I'd like to turn you all loose and simply let you experiment, but I should also give you an idea of where to begin.
To begin with, absolute and fixed positioning shouldn't be for the main content. Let that remain static. Instead, use it to position stuff like your menus and your page title. Give your static elements margins to keep them from being overlapped.
Floating can be used to put a bunch of block elements in a line, but inline-block elements work just as well.
Lastly, never be afraid to combine positioning, background images, and so on to create the kind of page you want.
I'll share a trick with you right now: namely, how to get the effect with the first letter in each chapter of this very book. In case you're using an older browser and the first letter looks no different from the rest, I'll provide an image of how it looks:
p
element of a div
element with the class
value chap_intro,
Times New Romanfont family, with a
seriffallback.
p
element of a div
element with the class
value chap_introis not indented.
p
element of a div
element with the class
value chap_introclears the float, whether it needs to or not.
Using a combination of background images, absolute positioning, and overriding the default display, I have created a webpage in which the navigation menu is arranged along an image. A portion of the (admittedly garish) page is shown below:
Now this has a rather long stylesheet—much longer than most of my examples—but when you get into visually rich webpages, though, stylesheets do get pretty lengthy, as you'll see if you look at The Main Stylesheet For This Book. In any case, here's the gist of what happens: The page is given a background colour, the ul
element used as a navigation menu is given a background image and has all list styling stripped from it, the li
elements are aligned with the background image and given background images of their own, and the a
elements are made block elements and given a border and a background. Lastly, the div
element containing the content is given a margin so it's not overlapping this thing.
So here's the code:
The bad news is the CSS is rather lengthy. The good news is that if you use an external stylesheet, you only have to type all that out once. The stuff you do have to type out a zillion times is pretty simple.