CSS tricks

Resetting the default CSS

Resetting the CSS to get rid of arbitrary display settings makes sure your CCS code has no assumptions built into it.

/* reset */
html,body,div,ul,ol,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,pre,form,p,blockquote,fieldset,input { color: #000; margin: 0; padding: 0; }
h1,h2,h3,h4,h5,h6,pre,code,address,caption,cite,code,em,strong,th { font-size: 1em; font-weight: normal; font-style: normal; }
ul,ol { list-style: none; }
fieldset,img { border: none; }
caption,th { text-align: left; }
table { border-collapse: collapse; }
td,th { vertical-align: top; }

Note: You may consider setting img to display as block

img { display: block; }

Anchors

Anchors must be spec'd in this order:

a:link { color: #336699; }
a:visited { color: #336699; }
a:hover { color: #003366; }
a:active { color: #336699; }

Background image

Must set padding or else inner content may affect background.

div#banner { width: 930px; height: 250px; background: url(../images/banner_cern.jpg) no-repeat; margin: 0 10px 10px 0; padding: 10px; }

Text-to-image replacement using CSS

Replace some text with an image, using a block tag.

<h1 id="fancyType">Fancy Typography</h1>
h1#myTodoList { height: 0; width: 542px; padding: 96px 0 0 0; line-height: 96px; background: url(images/fancy_type.gif) no-repeat; overflow: hidden; }

Where red type is the height of the image.

Rollovers with CSS

Note: this switches this to a block element.

<a href="#" id="cssButton">Button</a>
a#cssButton { display: block; height:0 ; width: 150px; padding: 25px 0 0 0; line-height: 25px; background: url(images/css_button.gif) no-repeat; overflow:hidden; }
a#cssButton:hover { background: url(images/css_button.gif) no-repeat 0 -25px;}
a#cssButton:active { background: url(images/css_button.gif) no-repeat 0 -50px;}
a#cssButton.selected { background: url(images/css_button.gif) no-repeat 0 -75px;}

Horizontal links with CSS

<div class="horizontalLinks">
<ul>
	<li><a href="#">Item 1</a></li>
	<li><a href="#">Item 2</a></li>
	<li><a href="#">Item 3</a></li>
	<li><a href="#">Item 4</a></li>
</ul>
</div>
div.horizontalLinks ul { margin: 0; padding: 0;}
div.horizontalLinks ul li { display:inline; margin-right: 6px; text-indent:0; }
div.horizontalLinks ul li + li { border-left: 1px solid #000000; }
div.horizontalLinks ul li + li a { margin-left: 8px; }

Nav bars with CSS

Nav Bar

<div id="nav">
	<ul>
		<li id="nav-products"><a href="products.html">Products</a></li>
		<li id="nav-solutions"><a href="solutions.html">Solutions</a></li>
		<li id="nav-support"><a href="support.html">Support</a></li>
		<li id="nav-downloads"><a href="downloads.html">Downloads</a></li>
		<li id="nav-about"><a href="about.html">About</a></li>
	</ul>
</div>
div#nav ul { margin: 0; padding: 0; height: 40px; background: url(../images/nav_bar.gif) no-repeat;}
div#nav ul li { display: inline; }
div#nav ul li a { float: left; width: 100px; height: 0; padding-top: 40px; overflow: hidden; background-image: url(../images/nav_bar.gif); }

div#nav ul li#nav-products a { width: 135px; background-position: 0 0; }
div#nav ul li#nav-solutions a { width: 135px; background-position: -135px 0; }
div#nav ul li#nav-support a { width: 115px; background-position: -270px 0; }
div#nav ul li#nav-downloads a { width: 138px; background-position: -385px 0; }
div#nav ul li#nav-about a { width: 94px; background-position: -523px 0; }

div#nav ul li#nav-products a:hover { width: 135px; background-position: 0 -40px; }
div#nav ul li#nav-solutions a:hover { width: 135px; background-position: -135px -40px; }
div#nav ul li#nav-support a:hover { width: 115px; background-position: -270px -40px; }
div#nav ul li#nav-downloads a:hover { width: 138px; background-position: -385px -40px; }
div#nav ul li#nav-about a:hover { width: 94px; background-position: -523px -40px; }

div#nav ul li#nav-products a:active { width: 135px; background-position: 0 -80px; }
div#nav ul li#nav-solutions a:active { width: 135px; background-position: -135px -80px; }
div#nav ul li#nav-support a:active { width: 115px; background-position: -270px -80px; }
div#nav ul li#nav-downloads a:active { width: 138px; background-position: -385px -80px; }
div#nav ul li#nav-about a:active { width: 94px; background-position: -523px -80px; }

div#nav ul li#nav-products.selected a { width: 135px; background-position: 0 -120px; }
div#nav ul li#nav-solutions.selected a { width: 135px; background-position: -135px -120px; }
div#nav ul li#nav-support.selected a { width: 115px; background-position: -270px -120px; }
div#nav ul li#nav-downloads.selected a { width: 138px; background-position: -385px -120px; }
div#nav ul li#nav-about.selected a { width: 94px; background-position: -523px -120px; }

Nav bars with drop downs with CSS

See nav bars with drop downs.

Table-style fixed layouts with CSS

See example here.

<div style="width:400px; background:silver; overflow:auto;">

	<div style="clear:both; width:400px;">
		<div style="float:left; width:300px; background:cyan;">
			<p>Div 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
		</div>
		<div style="float:left; width:100px; background:yellow;">
			<p>Div 2</p>
		</div>
	</div>

	<div style="clear:both; width:400px;">
		<div style="float:left; width:100px; background:green;">
			<p>Div 3</p>
		</div>
		<div style="float:left; width:300px; background:red;">
			<p>Div 4: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
		</div>
	</div>

</div>