CSS isn’t always easy to deal with. Depending on your skills and your experience, CSS coding can sometimes become a nightmare, particularly if you aren’t sure which selectors are actually being applied to document elements. An easy way to minimize the complexity of the code is as useful as not-so-well-known CSS attributes and properties you can use to create a semantically correct markup.
We’ve taken a close look at some of the most interesting and useful CSS tricks, tips, ideas, methods, techniques and coding solutions and listed them below. We also included some basic techniques you can probably use in every project you are developing, but which are hard to find once you need them.
And what has come out of it is an overview of over 70 expert tips, which can improve your efficiency of CSS coding. You might be willing to check out the list of references and related articles in the end of this post.
We’d like to express sincere thank to all designers who shared their ideas, techniques, methods, knowledge and experience with their readers. Thank you, we, coders, designers, developers, information architects - you name it - really appreciate it.
Update (29/05/2007): Brazilian-Portuguese translation of the article is also available. Thanks to Maurício Samy Silva.
@import url("reset.css");
@import url("global.css");
@import url("flash.css");
@import url("structure.css");
<style type="text/css" media="Screen">
/*/*/@import url("css/master.css");/**/
</style>
<p class="floatLeft alignLeft width75">...</p>
), make use of them debugging your markup. (updated) [Richard K. Miller] .width100 { width: 100%; }
.width75 { width: 75%; }
.width50 { width: 50%; }
.floatLeft { float: left; }
.floatRight { float: right; }
.alignLeft { text-align: left; }
.alignRight { text-align: right; }
reset.css
, global.css
, flash.css
(if needed) and structure.css
and on occasion a typography style sheet. Here is an example of a “master” style sheet and how it is embedded in the document:” h2 { }
#snapshot_box h2 {
padding: 0 0 6px 0;
font: bold 14px/14px "Verdana", sans-serif; }
#main_side h2 {
color: #444;
font: bold 14px/14px "Verdana", sans-serif; }
.sidetagselection h2 {
color: #fff;
font: bold 14px/14px "Verdana", sans-serif; }
/* -----------------------------------*/
/* ---------->>> GLOBAL <<<-----------*/
/* -----------------------------------*/
body {
background:#fdfdfd;
color:#333;
font-size:1em;
line-height:1.4;
margin:0;
padding:0;
}
/* Structure */
, /* Typography */
etc.” [CSS Tips and Tricks] # /*
# Dark grey (text): #333333
# Dark Blue (headings, links) #000066
# Mid Blue (header) #333399
# Light blue (top navigation) #CCCCFF
# Mid grey: #666666
# */
parent_child
pattern. [10 CSS Tips] <small>
, <em>
and <strong>
. “Many times you’ll have a section in your design that calls for various typographical weights/looks all on the same line, or very close to each other. drop in random divs and classes because I feel they’re not semantic and defeat the purpose of your nice XHTML everywhere else.” Instead, use semantic tags. [Mike Rundle’s 5 CSS Tips] #000 is the same as #000000, #369 is the same as #336699
[Roger Johansson] a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: purple; }
a:active { color: red; }
margin
, padding
and border
properties can save a lot of space. margin: top right bottom left;
margin:1em 0 2em 0.5em;
(margin-top: 1em; margin-right: 0; margin-bottom: 2em; margin-left: 0.5em;)
border:width style color;
border:1px solid #000;
background: color image repeat attachment position;
background:#f00 url(background.gif) no-repeat fixed 0 0;
font: font-style (italic/normal) font-variant (small-caps) font-weight font-size/line-height font-family;
font: italic small-caps bold 1em/140% "Lucida Grande",sans-serif;
font-size
is 16px; applying the rule, you’ll get one Em standing for roughly ten pixels (16 x 62.5% = 10). “I tend to put a font-size on the body tag with value: 62.5%. This allows you to use EMs to specify sizes while thinking in PX terms, e.g. 1.3em is approximately 1.3px. ” [Jonathan Snook] <meta http-equiv="content-type" content="text/ html;charset=utf-8" />
h1 {
text-transform: uppercase;
}
font-variant
property is used to display text in a small-caps font, which means that all the lower case letters are converted to uppercase letters, but all the letters in the small-caps font have a smaller font-size compared to the rest of the text. h1 {
font-variant: small-caps;
}
p {
font-family: Arial, Verdana, Helvetica, sans-serif;
}
line-height
. “line-height:1.4
” for readable lines, reasonable line-lengths that avoid lines much longer than 10 words, and colors that provide contrast without being too far apart. For example, pure black on pure white is often too strong for bright CRT displays, so I try to go with an off-white (#fafafa
is a good one) and a dark gray (#333333
, another good one).” [Christian Montoya] html
-element. This odd 100.01% value for the font size compensates for several browser bugs. First, setting a default body font size in percent (instead of em) eliminates an IE/Win problem with growing or shrinking fonts out of proportion if they are later set in ems in other elements. Additionally, some versions of Opera will draw a default font-size of 100% too small compared to other browsers. Safari, on the other hand, has a problem with a font-size of 101%. The current “best” suggestion is to use the 100.01% value for this property.” [CSS: Getting into good habits] div { border:1px red dashed; }
works like a charm. There are also bookmarklets that apply borders and do other things for you.” You can also use * { border: 1px solid #ff0000; }
. [Chric Casciano]. Adding a border to specific elements can help identify overlap and extra white space that might not otherwise be obvious. [CSS Crib Sheet] * { border: 1px solid #f00; }
Roger Johansson has written an extremely useful series of articles about CSS 2.1 Selectors. These articles are highly recommended to read - some useful aspects can be found in the list below. Note that selectors ‘>’ and ‘+’ aren’t supported in IE6 and earlier versions of Internet Explorer (updated).
div > strong { color:#f00; }
p + p { color:#f00; }
[att]
Matches elements that have an att attribute, regardless of its value.
[att=val]
Matches elements that have an att attribute with a value of exactly “val”.
[att~=val]
Matches elements whose att attribute value is a space-separated list that contains “val”. In this case “val” cannot contain spaces.
[att|=val]
Matches elements whose att attribute value is a hyphen-separated list that begins with “val”. The main use for this is to match language subcodes specified by the lang attribute (xml:lang in XHTML), e.g. “en”, “en-us”, “en-gb”, etc.
p
elements that have a title
attribute, regardless of which value it has: p[title] { color:#f00; }
div[class=error] { color:#f00; }
blockquote[class=quote][cite] { color:#f00; }
<a name="anchor">
) you’ll notice it picks up :hover
and :active
pseudo-classes. To avoid this, you’ll need to either use id
for anchors instead, or style with a slightly more arcane syntax: :link:hover, :link:active
” [Dave Shea] a[rel~="nofollow"]::after {
content: "/2620";
color: #933;
font-size: x-small;
}
a[rel~="tag"]::after {
content: url(http://www.technorati.com/favicon.ico);
}
rel="external"
relationship to indicate a link to an external site. However, adding that to each and every link is time consuming and and unnecessary. This style rule will place an north east arrow after any link on your site to an external site. [Handy CSS] a[href^="http://"]:not([href*="smashingmagazine.com"])::after {
content: "/2197";
}
outline: none;
. To remove dotted links use outline: none;
a:focus {
outline: none;
}
<hr>
to separate posts beautifully. “Restyling the horizontal rule (<hr>) with an image can be a beautiful addition to a web page. [CSS: Best Practices] <ul>
<li><a href="#" class="home">Home</a></li>
<li><a href="#" class="about">About us</a></li>
<li><a href="#" class="contact">Contact us</a></li>
</ul>
id
into the <body>
tag. The id should be representative of where users are in the site and should change when users move to a different site section. #home .home, #about .about, #contact .contact
{
commands for highlighted navigation go here
}
margin: 0 auto;
to horizontally centre the layout. “To horizontally centre an element with CSS, you need to specify the element’s width and horizontal margins.” [Roger Johansson] <div id="wrap">
<!-- Your layout goes here -->
</div>
#wrap {
width:760px; /* Change this to the width of your layout */
margin:0 auto;
}
<?xml version="1.0" ?>
<?xml-stylesheet type="text/css" href="http://you.com/rss.css" ?>
...
@import
trick. [Roger Johansson] @import "main.css";
<link rel="stylesheet" type="text/css" href="print.css" media="print">
<style type=”text/css” media=”print”> @import url(print.css); </style>
#regular_logo
{
background:url('test.png'); width:150px; height:55px;
}
/* / */
* html #regular_logo
{
background:none;
float:left;
width:150px;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='test.png', sizingMethod='scale');
}
/* */
min-width
and max-width
in IE. You can use Microsoft’s dynamic expressions to do that. [Ten More CSS Trick you may not know] #container
{
min-width: 600px;
max-width: 1200px;
width:expression(document.body.clientWidth < 600? "600px" : document.body.clientWidth > 1200? "1200px" : "auto");
}
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->