Creating a logo with CSS
In this article I'll explain how to create and format a logo by using CSS. Our inspirational logo is the following.
Figure 1. V-Day logo
Table of contents
- The markup
- General styles
- Positioning and dimensions
- Inserting the background image
- Setting the font size and color
- The sub-title
- Final settings
- Download examples
The markup
We start with the following markup:
Listing 1. Markup for a logo
<div id="logo"><h1><span id="css">CSS</span><span id="img"><a href="#" title="Long description of the image">D</a></span><span id="day">Day</span></h1><h2>Until the quirks stop</h2></div>
As you can see, since we want to use a background image on the #img element, we've also provided a link to a long
description of the image. Our structure appears as follows in Lynx:
Figure 2. Screenshot taken from Lynx
General styles
The general styles are really simple.
Listing 2. General styles
#logo {background: #fff;color: #000;font: 100%/1 Arial, Helvetica, sans-serif;}
We've set the base font size on our logo, plus a text color and a background color.
Positioning and dimensions
Now we can set the positioning and dimensions of the elements.
Listing 3. Positioning and dimensions
#logo {padding: 1em;}#logo h1, #logo h2 {margin: 0;}#logo h1 {height: 150px;}#logo h1 span {float: left;height: 100%;}
To position the children of the h1 element, we use the float property.
The height of the h1 element equals the height of the background image that we're going
to insert. Note that also the children of the h1 element share the same height with
their parent.
Inserting the background image
The background image will be inserted as follows:
Listing 4. Inserting the background image
#logo h1 #img {width: 80px;height: 150px;background: transparent url("../img/csslogo.png") no-repeat top left;text-indent: -1000em;}
The dimensions of the #img element equal the dimensions of the background image. We get rid of the
D-link's text by using a negative text indent, since we're working with an element that's been floated, that is, a block-level
element.
Setting the font size and color
Now we can set the font size and color of the children of the h1 element.
Listing 5. Setting the font size and color
#logo h1 #css, #logo h1 #day {font-size: 150px;text-transform: uppercase;}#logo h1 #css {color: #c00;}
We use pixels to get a precise control over the font sizing. In this case, ems are not required, since we're dealing with a text that has enormous dimensions.
The sub-title
The sub-title (h2 element) has the following styles:
Listing 6. The sub-title
#logo h2 {text-transform: uppercase;font-size: 55px;display: inline;background: #000;color: #fff;position: relative;top: 10px;}
Since the background color must cover only the text area and not the whole containing block, we turn this element
in an inline-level element. Then we create a vertical space by using the position property set to
relative with a vertical offset of 10 pixels.
Final settings
Finally, we want to vertically align the text of the h1 element with the baseline of the background image.
So we could write the following:
Listing 7. Final settings
#logo h1 #css, #logo h1 #day {position: relative;top: 15px;}
As we've seen above, we've used the position property set to
relative, but now with a vertical offset of 15 pixels.