CSS
Here are 10 amazing ways to boost your website design, using only
HTML and CSS (no JavaScript):
-
Transparency
Hover your mouse over the second image below to toggle between transparent and normal mode.
By default, the image is set to appear transparent. With mouseover, the image appears as it normally would. Both transparency and mouseover effects are easy to achieve with just a few lines of CSS:
<style>
img.kiwi {opacity:0.5; filter:alpha(opacity=50);}
a:hover img.kiwi {opacity:1; filter:alpha(opacity=100)}
</style>
… so the complete code looks like this:
<!DOCTYPE html>
<html>
<head>
<title>How to Create Transparency with CSS</title>
<style type=”text/css”>
img.kiwi {opacity:0.5; filter:alpha(opacity=50);}
a:hover img.kiwi {opacity:1; filter:alpha(opacity=100)}
</style>
</head>
<body>
<img alt=”kiwi” src=”kiwi.jpg”/>
</body>
</html>
Here’s an example of the same kiwi image with transparency, placed in a div that contains an abstract background image:
-
The @media rule for print and hand-held devices
The @media rule lets you specify how a web page looks on a computer screen versus how it looks when printed. For example, let’s say you want all paragraphs on your web page to display in black Arial font when seen on a computer screen, but you prefer gray Times font when the web page is printed.
Your style should look like this:
<style>
@media screen {p {font-family:arial, sans-serif; color:black;}}
@media print {p {font-family:times,serif; color:gray;}}
</style>
The style above tells your web page that it needs to appear a certain way on screen, and another way on paper. But what if you want the font to be bold, both on screen and in print?
You can target multiple media types like this:
<style>
@media screen,print {p {font-style:bold;}}
</style>
* Other types of media you can target with CSS:Media Type: Used For: all all media type devices aural speech & sound synthesizers braille braille tactile feedback devices embossed paged braille printers handheld small or handheld devices print printers projection projected presentations, like slides screen computer screens tty media using a fixed-pitch character grid, like teletypes & terminals tv television-type devices
-
Centering with the auto margin and text-align properties
The align=”center” attribute and the <center></center> attribute are both old and outdated. Sure, sometimes “old is gold”, but not in this case. If you’ve used either of these methods to center something on every page of your fifty page website….imagine updating those fifty pages when you suddenly decide you prefer a left alignment instead.
The preferred CSS technique for centering block-level elements (in this case, a div) is:
div {margin-left:auto; margin-right:auto;}
But if you’re one of the FEW people who care about IE5, then you might want to use an alternate method, since auto margins are not supported by IE5:
body {text-align:center;}
div.content {text-align:left;}
This method successfully centers the div with all the content, without disrupting the elements inside the div.
body(content div)
The text in this div is aligned left, even
though the div itself is centered.The moral of the story – Both CSS center-align methods mentioned above are useful so use whichever suits the circumstance at hand. Quit using the align=”left” and <center></center> attributes. Hop on the CSS bandwagon. The CSS versions are much superior when it comes to updating a website, and they’re W3C standards compliant.
-
Multiple classes, side by side
You can assign an element as many classes as you’d like, separating each class with a space. For instance:
<style>
.winter {width:100px; height:100px}
.summer {border:1px dashed black}
.spring {background-color:orange}
</style>
<div class=”winter summer spring”></div>
And the result…
-
Overlapping Elements with absolute positioning and z-index
CSS allows you to choose the positioning of elements on your website. By default, all elements use relative positioning, even if you don’t specify this in your stylesheet:
position:relative
If you want to overlap an element, you must change its default positioning from relative to absolute, like so:
position:absolute
Use z-index to specify which elements overlap the others. A style of z-index:1 puts an element at the bottom, then z-index:2 in front of it, and so on. Remember, the element with the highest z-index will be closest to you when looking at the screen.
Check out the example below to see absolute positioning and z-index in action:
and the code…<style>
.container {width:100%; height:380px; background-color:black}
.blue, .green, .yellow
{position:absolute; width:150px; height:150px; border:2px dashed red;}
.blue {background-color:blue; z-index:1; margin:60px 0 0 60px}
.green {background-color:green; z-index:3; margin:60px 0 0 300px}
.yellow {background-color:yellow; z-index:2; margin:160px 0 0 180px}
</style>
<div class=”container”>
<div class=”blue”></div>
<div class=”green”></div>
<div class=”yellow”></div>
</div>
-
Swapping an image upon mouseover
In addition to applying the hover effect on text links, it’s nice to be able to use it for images.
For instance, hover your mouse over the image below to see the image swap take place.
How can you achieve this? I’m sure there are many ways out there. One way is to use the hover style combined with the background-image style. Here are the images and code for the panda-bird example:<style>
.imagebox, a {width:264px; height:180px;}
.imagebox {background-image:url(bird.jpg)}
.imagebox a {display:block; background-image:url(panda.jpg)}
.imagebox a:hover {background-image:none}
</style>
<div class=”imagebox”>
<a href=”#”></a>
</div>
-
Placing elements side by side using floats
Divs are easy to use since they’re block elements that don’t share their horizontal space with any other element. That’s great if you’re designing downward, but what if you want to place a bunch of divs side by side? A lot of people resort to using tables to lay out their content, since tables give you the freedom to populate rows and columns…pretty easy huh? Well, you can achieve the same task by using CSS. Are tables bad? Not really…but tables can’t be placed in a stylesheet, so updating 50 pages full of tables could get a little cumbersome.
Here’s what CSS can do, without the use of tables:
…try it yourself:<style>
.hi {float:left; margin:0 20px 20px 0; background-color:gray; width:130px; height:80px;}
.bye {clear:both; width:430px; height:150px; background-color:black;}
</style>
<div class=”hi”>


Wow, we need someone like you on our development team! We can all code but don’t know crap about CSS. I have to stop using tables for everything and use the power of the div with CSS!
I want to thank the blogger very much not only for this post but also for his all previous efforts. I found stylisticweb.com to be very interesting. I will be coming back to stylisticweb.com for more information.
I didn’t understand the concluding part of your article, could you please explain it more?
Youngie, are you referring to #10…the section about tooltips? If so, then basically, what I was saying is that many people simply prefer to use the “HTML title attribute” which functions as a VERY basic tooltip (mouseover the red text in #10 to see how the title attribute works)…
But the CSS tooltip demo is for people who wish for something beyond the basic HTML title attribute…since CSS allows you to control the color, size, transparency, etc. of your tooltip.
The HTML title attribute comes as-is and you cannot change the appearance. So the last part of the article provides the code for the CSS tooltip, for people who wish to use it on their websites.
Hope that answers your question.
I am not going to be original this time, so all I am going to say that your blog rocks, sad that I don’t have suck a writing skills