|
|||||||||||||
|
|||||||||||||
Introduction to CSSHistory of Electronic Publishing Electronic publishing arose from a historical context of real-time story telling, asynchronous writing, and physical type setting that supported reproducible printing on paper. Computers have only been involved in the process for fifty or so years. We can look backward to get a sense of where our current CSS standard comes from, but I find it just as interesting is to look forward. New York Times Digital CEO Martin Nisenholtz suggests we are evolving towards a new realm of electronic publishing whereby:
This seems important to keep in mind while we entrench ourselves in CSS. With CSS, we can extract the look and feel of our presentation from the content — and, so, we should ask ourselves how CSS would evolve to support the world view of Martin above (since he will likely have gained some respect and power to help drive change quickly). Will CSS continue to help present a mixed, multimedia-based media? Ask yourself "how far we have already moved towards this view?" Then, ask yourself "if you like the vision or not?" History of Cascading Style Sheets The W3C recommends you read their The CSS saga if you are interested in the history of CSS. Since the W3C coordinated the specification and made it a successful standard, they should know well how that all transpired. Using Cascading Style Sheets with DIV elements The following CSS layout primer (from here on to the bottom of this Web page) comes to you mainly through the courtesy of Ross Shannon. The The div#navigation {width: 200px; background: gray; padding: 10px; }
This example code uses some very simple CSS code. All block-level elements can have a width property, specified in units or as a percentage, and then I just added a background color and some padding space around the Floating ElementsSince divisions are block-level (i.e. they default to 100% of the available screen width and add line breaks between each other), they will all just stack up underneath one another unless you position them in some way. The simplest way to do this is to use the CSS float property, the backbone of most CSS layouts. You can float any element left or right, and it will align itself over to the side of whatever element it is contained within. To create columned layouts, you simply float all of the column divisions to the same side and they will line up beside each other, as long as they fit. Laying out content in this way has immediate benefits such as progressive downloading (as the text is loaded it is displayed onto the page immediately, so your visitor can read as the page is forming around the text). You can also give each column specific margins and padding, giving you greater freedom to space your content out. With these floating elements you can mimic a table structure, and have your page in a traditional layout without all the drawbacks of tables. But CSS wasn't content to merely emulate the layout mechanisms of the past, now you can control the position of elements on the page down to the pixel. CSS PositioningThere are two types of positioning: absolute and relative. The codes you'll be using are: tag {position: choice; top: 0px; bottom: 0px; left: 0px; right: 0px; } Browser Compatibility Note: Absolute and relative positioning is a feature of the CSS2 specification and so is supported by » Internet Explorer 4+, » Mozilla, » Firefox, » Opera and » Safari. For best results use the newest browsers available, as they will have improved and more accurate rendering capabilities. Do not use these if your users may be using older browsers. Netscape 4.7's positioning support is full of glitches, and is largely erroneous. Absolute PositioningIf you position an element (an image, a table, or whatever) absolutely on your page, it will appear at the exact pixel you specify. Say I wanted a graphic to appear 46 pixels from the top of the page and 80 pixels in from the right, I could do it. The CSS code you'll need to add into the image is: img {position: absolute; top: 46px; right: 80px; } You just add in which method of positioning you're using at the start, and then push the image out from the sides it's going to be closest to. You can add the CSS directly into the tag using the The image would appear like so. As you can see, it is possible to have things overlapping with absolute positioning. Positioning LayersTo create what we call layers with the
First you specify the position of the layer, then its dimensions (which is optional, the layer will resize itself). If you want to give color to the layer's background, add the background-color: red; attribute in with the rest of your CSS code. As usual, you can use websafe colors, or named colors. Anything that could go on a normal page can be positioned with To get layers overlapping each other you need to use the z-index command. Add z-index: 1 in with the positioning code and this element will appear above everything without this command. If you want something else to go over this layer too, add z-index: 2 and so on. The higher the index, the closer the Put the layer that holds your page's content at the top of your code. This is what readers want to see immediately. Your navigation and other presentational components can then load around this, allowing your reader to begin reading as soon as possible and making your navigation available when it is most likely to be used: after the page has been read. To see some examples of designs laid out with both float and absolute positioning, have a look-see at my redesigns section. Relative PositioningWhen you position something relatively, you are modifying its position from where it would have been if you hadn't changed anything. I find that to be the easiest way of thinking about it. For instance, in the next sentence, I'll offset some words 12px down and 22px right relative to their start position. Well, here are some words (some words) The words in parentheses are the words in their original positions, and the bold ones are the moved words. The CSS code to move them is: <span style="position: relative; top: 12px; left: 22px;">some words</span> You should notice that if you want to move something left, you use the right code, and push it away from that side and vice-versa. To override an inherited position property, and make the element just a normal part of the page again, set it to position: static. Horizontal CenteringCentering a #wrapper {width: 760px; margin: 0 auto; } That will work in browsers like Firefox, Safari or Opera. However, this will have no effect in versions of Internet Explorer below 7. There is a hack we can use though, so that we get horizontal centering in all browsers. To whit, IE incorrectly centers block-level elements if the element that they’re contained in has text-align: center applied. So we can apply this property to the body {text-align: center; } One final step is then necessary. The line above will, of course, center all the text inside the centered elements as well, which is generally not what we want, so we need to align the text within back to the left. So here’s all the code:
body {text-align: center; } Easy when you know how, eh? Be careful if you’re planning on mixing absolute positioning and this centering method in the same layout. If you want other elements to be absolutely positioned inside the wrapper, make it relatively positioned first. #wrapper {position: relative; width: 760px; margin: 0 auto; text-align: left; } This will make an inner element that you absolutely position at, for example, top: 0; left: 0; appear at the top left corner of the wrapper, and not of the the top left of the entire window. |