`
liudaoru
  • 浏览: 1561925 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

The 6 Most Important CSS Techniques You Need To []

    博客分类:
  • Ajax
阅读更多

From: http://trevordavis.net/blog/tutorial/the-6-most-important-css-techniques-you-need-to-know/

Posted on March 30, 2008 in Tutorial | 40 Comments »

I thought I would give a quick tutorial, with examples, of the 6 most important CSS techniques that I think you need to know:

  1. Get a Consistent Base Font Size
  2. Get Consistent Margins
  3. Set a Float to Clear a Float
  4. Image Replacement
  5. Faux Columns
  6. CSS Sprites

 

1. Get a Consistent Base Font Size

body { font-size: 62.5%; }

Until IE finally supports the resizing of text in pixels, this is the best technique to gain full control over your font sizes. By setting the body font-size to 62.5%, that will set your font size to 10 pixels. That way, 1em is equal to 10 pixels.

Although I typically then set my container font-size to 1.2em, which in turn sets the font-size to 12 pixels. But still, then you know that 1em is equal to 12 pixels.

See the example »

2. Get Consistent Margins

There are some great CSS resets out there, but I usually don’t need one that goes so far. I like to just remove the margin and padding from every element.

html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote, pre, form, fieldset, table, th, td { margin: 0; padding: 0; }

Then, you can set the margins that you want accordingly.

See the example »

3. Set a Float to Clear a Float

Floating is probably one of the most important things to understand with CSS, but knowing how to clear floats is necessary too. All you need to remember is: Set a Float to Clear a Float.

I have created a simple page with two floating columns next to each other. You will notice in the example that the grey background does not contain the floating columns. So, the easiest thing to do is to set the containing element to float. But now, you will see that the container background doesn’t contain the content area.

Since the container has margin: 0 auto, we do not want to float it because it will move it to whichever side we float it. So another way to clear the float, is to insert a clearing element. In this case, I just use an empty div set to clear: both. Now, there are other ways to clear a float without markup, but I have noticed some inconsistencies with that technique, so I just sacrifice an empty div.

4. Image Replacement

Sure, there are a lot of different image replacement techniques. But, I think that you get to most benefits from this technique. I also discussed this technique in a previous article, Improved Navigation Image Replacement

The Markup

<h1 id="logo">Company Name<span></span></h1>

The CSS

h1#logo {
	height: 110px;
	overflow: hidden;
	position: relative;
	width: 560px;
}
h1#logo span {
	background: url(/wp-content/uploads/2008/03/logo.gif) no-repeat;
	display: block;
	height: 100%;
	left: 0;
	position: absolute;
	top: 0;
	width: 100%;
}

Basically, all we are doing is absolutely positioning an image over top of the HTML text.

The reason that I like this technique is because even when images are disabled, the text is still visible. Of course this means that you cannot use a transparent image to lay over top of the text, so it will not work in all situations.

See the example »

5. Faux Columns

It is very common in layouts to have 2 columns next to each other, with one column having a background color, and the other column just being white. Since the columns will almost never have the same amount of content in them, the easiest way to fake this, is to have a 1px tall background image being repeated vertically in the containing element of the 2 columns.

The Markup

<div id="container">
	<div id="primaryContent">
		<h1>Primary Column</h1>
		<p>…</p>
	</div>
	<div id="secondaryContent">
		<h2>Secondary Column</h2>
		<p>…</p>
	</div>
	<div class="clearing"></div>
</div>

The CSS

div#container {
	background: url(/wp-content/uploads/2008/03/content-bg.gif) repeat-y top right;
	padding: 10px 0;
	width: 640px;
}
div#primaryContent { float: left; padding: 0 10px; width: 400px; }
div#secondaryContent { float: right; padding: 0 10px; width: 200px; }

Pretty simple: a containing element, 2 floating columns, and a clearing element so that the containing element will contain the floating columns (as noted in the Set a Float to Clear a Float technique above).

See the example »

6. CSS Sprites

CSS sprites is the technique of combing images to lessen the number of calls that need to be made to the server. Then you just shift the position of the background image to view the correct part of the image. May sound complicated, but it just takes a little math. Note: In both of these examples, I am using the Image Replacement technique discussed above.

Example #1

For this example, we are just going to have one download link that we want to replace with a nice graphic. Then, on hover, we want to change the image.

The Markup

<a href="#" id="download">Download Now<span></span></a>

The CSS

a#download {
	display: block;
	height: 75px;
	overflow: hidden;
	position: relative;
	width: 150px;
}
a#download span {
	background: url(/wp-content/uploads/2008/03/download.gif) no-repeat;
	display: block;
	height: 100%;
	left: 0;
	position: absolute;
	top: 0;
	width: 100%;
}
a#download:hover span { background-position: 0 -75px; }

As you can see from the code, on hover, we shift the position of the background image to view a different part of the image.

Oh, and also, IE6 and 7 suck, so here are the styles we are serving to them:

Styles to IE6 and IE7
a#download { cursor: pointer; }

I realize that we could just put that in the regular stylesheet since it has no affect on other browsers, but I prefer to move stuff like that to the IE only stylesheets.

Styles to IE6 Only
a#download:hover { background-position: 0 0; }

This is some weird bug where the images shift when you hover, but then when you mouse-out, the images don’t shift back.

See the example »

Example #2

For the second example, we are going to use the CSS sprites technique, but for the entire navigation. This way, only one call needs to be made to the server to load the navigation. We are basically creating a CSS sprites matrix.

The Markup

<ul id="nav">
	<li id="home"><a href="#">Home<span></span></a></li>
	<li id="about"><a href="#">About<span></span></a></li>
	<li id="work"><a href="#">Work<span></span></a></li>
	<li id="play"><a href="#">Play<span></span></a></li>
	<li id="contact"><a href="#">Contact<span></span></a></li>
</ul>

The CSS

ul#nav { background: #91c6d5; float:left; list-style: none; width: 510px; }
ul#nav li { float: left; }
ul#nav a { color: #000; display: block; font-size: 1.5em; height: 38px; text-align: center; position: relative; }
ul#nav span { background: url(/wp-content/uploads/2008/03/nav.gif) no-repeat; display: block; height: 100%; left: 0; position: absolute; top: 0; width: 100%; }

ul#nav li#home a { width: 102px; }
ul#nav li#home a:hover span { background-position: 0 -38px; }

ul#nav li#about a { width: 106px; }
ul#nav li#about span { background-position: -102px 0; }
ul#nav li#about a:hover span { background-position: -102px -38px; }

ul#nav li#work a { width: 92px; }
ul#nav li#work span { background-position: -208px 0; }
ul#nav li#work a:hover span { background-position: -208px -38px; }

ul#nav li#play a { width: 79px; }
ul#nav li#play span { background-position: -300px 0; }
ul#nav li#play a:hover span { background-position: -300px -38px; }

ul#nav li#contact a { width: 131px; }
ul#nav li#contact span { background-position: -379px 0; }
ul#nav li#contact a:hover span { background-position: -379px -38px; }

That may seem crazy, but it all makes sense if you take the time to think about it.

Again, we have to serve some similar styles to IE6 and 7 from the previous example:

Styles to IE6 and IE7
ul#nav a { cursor: pointer; }
Styles to IE6 Only
ul#nav a:hover { background-position: 0 0; }

See the example »

In Conclusion

This list is definitely not exhaustive; they are just the 6 that I think are extremely important. What other techniques do you think are important to know?

分享到:
评论

相关推荐

    Pro CSS3 Layout Techniques(Apress,2016)

    Pro CSS3 Layout Techniques teaches you how to make the most of CSS3's existing specification, including those parts of the specification already widely implemented, as well as the upcoming modules ...

    Pro.CSS.Techniques

    This book is a collection of proven, professional, modern techniques that you can use every day to get the most out of the time you put into your projects, from start to finish. As it has finally come...

    Mastering CSS

    Rich Finelli trains you in CSS deep learning and shows you the techniques you need to work in the world of responsive, feature-rich web applications. Based on his bestselling Mastering CSS training ...

    Learn CSS In A DAY

    You also have to pay the high fees, month to month, and what is even more annoying is this: you will probably have to go to a special place in order to practice the CSS techniques! You see, when it ...

    Foundation-HTML5-with-CSS3.pdf 英文原版

    If you want to get into developing web sites, the most important thing you’ll need is a solid understanding of Hypertext Markup Language, or HTML—the most common language used to write web site ...

    HTML & CSS: The Good Parts

    Whether you handcraft individual pages or build templates, HTML & CSS: The Good Parts will help you get the most out of these tools in all aspects of web page design-from layout to typography and to ...

    Responsive Web Design with HTML5 and CSS3 [eBook]

    by applying the latest and most useful techniques provided by HTML5 and CSS3, making the design leaner and more maintainable than ever before. It also explains common best-practice methods of writing ...

    Professional CSS3(PACKT,2016).pdf

    Finally, you will learn about code architecture and CSS methodologies used in scalable apps and you’ll explore the various new features of CSS3, such as FlexBox, to help you create the most modern ...

    SW-SystemsArch-WorkingwithStakeholdersUsi

    Part II describes the most important activities you need to undertake as an architect, such as agreeing on a project’s scope, identifying and engaging stakeholders, using scenarios and patterns, ...

    Kafka Streams in Action (MEAP V9)

    This book also teaches you about all important testing and integration techniques to ensure you'll never have to sacrifice functionality. By the end of the book, you'll be ready to use Kafka Streams ...

    DATA MINING Concepts and Techniques 3rd(数据挖掘:概念与技术)

    This is the resource you need if you want to apply today's most powerful data mining techniques to meet real business challenges. * Presents dozens of algorithms and implementation examples, all ...

    Achieving Your Most Important Goals with Objectives and Key Results

    This book is intended to help all organizations start operating like the best organizations. I have seen these techniques deployed successfully in organizations as large as a 60,000 employee company ...

    How to write great essays

    How to write great essays focuses on the topics most important to you now .You won't find a comprehensive guide to mechanics,but instead you will get short but thorough lessons on the most common ...

    Enduring CSS

    Enduring CSS is a book for considering how to write CSS in the most effective manner, and how you too can create an enduring CSS code base, regardless of project size. Take a journey with me if you ...

    CSS Mastery

    While most books concentrate on basic skills, this one is different, assuming that you already know the basics and why you should be using CSS in your work, and concentrating mainly on advanced ...

    Sams Teach Yourself HTML, CSS & JavaScript 7th 2016第7版pdf 0分

    You can work through each lesson sequentially to make sure you thoroughly understand all the concepts and methodologies, or you can ...on specific lessons to learn the techniques that interest you most....

    The Book of CSS3

    CSS3 is the technology behind most of the eye-catching visuals on the Web today, but the official documentation can be dry and hard to follow. Luckily, The Book of CSS3 distills the heady technical ...

    CSS Master, 2nd Edition

    Discover how to keep ahead of the game by adhering to best practice and employing the most effective, cutting-edge CSS techniques. Now thoroughly updated in its second edition, this book covers how ...

    100 Most Important C++ Programs 无水印pdf

    100 Most Important C++ Programs 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系...

    The Definitive Guide to AdonisJs_Building Node.js App with JavaScript-2018

    To get the most out of it, you should have a firm grasp of modern JavaScript and some knowledge of how to work with relational databases and the command line. I explain new and interesting bits of ...

Global site tag (gtag.js) - Google Analytics