November 2, 2006

Some CSS Tricks

You will find other articles relevant to this document in these sections:
Richard Lee @ 11:00 am

1. Centering a block item horizontally

To centre a fixed width div we can use the CSS margin-left and margin-right commands set to auto. For pre IE 6 browsers you’ll need to use the text-align property set to centre:

body {
 
text-align: center;
 
}
 
div#wrapper {
 
margin: 0 auto;
text-align:left;
 
}

2. Centering content vertically

Having troubles getting vertical-align to centre? Forget it. Set the line-height to the same height as the container like so:

div#cell{
 
height: 24px;
 
line-height: 24px;
 
}

3. Using images for stylish headings

Using images for stylized headings is okay, but we should make sure the heading is still accessible to a screenreader. To do this we can use a background image on the header tag and hide the plain text from view using the text-indent command:

div#banner h1 {
 
height: 26px;
 
text-indent: -2000px;
 
background-image:url(images/header1.gif);
}

4. Getting backgrounds to stretch the full length of a container

CSS has issues controlling things vertically - take backgrounds and vertical alignments for example. If you need a background colour to extend the full length of a block element such as a div you’ll need to use a background-image and tile it vertically:

div#content {
 
background:#fff url(images/bg.gif) top left repeat-y;
}

5.Using 2 classes together

In CSS you can apply two classes to an element. For example, in my piped horizontal lists (Eg Home | About | Contact) I have a selected class to highlight the active item and a last class to remove the piped character from the last list item. To apply these two classes I simply add both classnames to the class attribute seperate by a space:

<a class="last selected" href="home.html">Home</a>
Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Furl
  • Reddit
  • YahooMyWeb

2 Comments »

  1. nice one,

    I wonder how the screen readers go with using a heading1 and a inner span that is set to display:none ? you can then write a generic CSS statement:
    h1 span { display: none; visibility: hidden; }

    then your other h1 instances can be called like:
    h1.myHeading { background: #fff url(images/header.gif) top left no-repeat; }

    ??

    Comment by Cameron Manderson — November 2, 2006 @ 2:54 pm

  2. Yes this is how I used to do it but I did read somewhere that some screen readers are now ignoring display:none - I don’t have any factual info though so don’t quote me :P

    There is one minor issue I forgot to mention with hiding text, and that is users who have images blocked through their browser - They wont see the text either!

    Comment by Richard Lee — November 2, 2006 @ 3:24 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment