spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / authoring / style / sheets / multiclass To page 1To page 2current page
[previous]

Saving Bytes by Using Multiple CSS Classes on Elements

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

If you look carefully at the style sheet used in the previous example, you will note that Layer1 through to Layer3 share common attribute values.

   position: absolute; 
   width: 200px; 
   height: 115px; 
   top: 120px;
   z-index: 1;

Knowing this we can create a new class

.pos {
   position: absolute; 
   width: 200px; 
   height: 115px; 
   top: 120px;
   z-index: 1; 
}

And then apply it to each element that we want to use those attribute values on by applying the classes as such:

<div id="Layer1" class="maintext pos">This 
  is some text. </div>
<div id="Layer2" class="maintext pos">This 
  is some text that is centered. </div>
<div id="Layer3" class="maintext pos">This 
  is more text. </div>

Let's check the result:

View CSS Example 3 (Opens in new window.)

Our document now comes in at 992 bytes, the lightest of all the examples we have presented! The thing of it is that we are working with a simple document. The savings would be even more considerable if we applied these same principles to a fully fledged Web site that is geared towards a standards only audience, simply because the technique has added flexibility to the more traditional approaches.

For example, let's assume we wanted to add a paragraph to the page, we could then create a class called center and have that class apply to Layer2 and the <p> tag as well.

<div id="Layer2" class="maintext pos center">This 
  is text in Layer2 that is centered. </div>

<p class="maintext center"> I was here</p>

View CSS Example 4 (Opens in new window.)

In this example, the div element Layer2 utilizes an extra class (pos) beyond that of the <p> tag; but because that specific class is also shared with Layer1 and Layer3 we are maximizing the usage of our CSS classes. Similarly because the center class is shared between the <div> and <p> elements, we are gaining coding efficiency, which in turn allows us to create lighter pages. In short, we are creating a CSS system that utilizes a re-usable component strategy for optimal results and best of all it works on the all the newer browsers.

Using this technique may cause a person to think about building pages in a slightly different way than we typically are accustomed too. In effect, we are moving from a mentality of "what works" to "what works best," which, in itself, is a good thing. It is good, because probably for the first time in the Web's short history, we can shift the focus to optimization and productivity, rather than worrying about whether it works or not.

Sure there are cross browser differences even today, even among the newer browsers, but by and large these are exceptions, rather than the rule. Generally we can code in one way and find that most things work well across the newer browsers, which of course is the whole point of using a Web standard.

Whether you are ready to embrace Web standards as "a way of being," right here and right now is really something only you can decide. But given the rapid decline of non standards based browsers it is almost certain that standards based techniques will become the norm--not the exception--in the not too distant future. As such we all need to think about the ways we currently build Web pages and move towards learning newer and better methods, which consistently demonstrate practical advantages over older techniques.

About the Author

Eddie Traversa is a multiple award winning developer who works as an independent Web consultant. He likes to play around with the latest technologies, and is currently writing a book on Dynamic XHTML with Jeff Rouyer of http://www.htmlguru.com/ fame. His experimental DHTML site is located at http://dhtmlnirvana.com/.


home / authoring / style / sheets / multiclass To page 1To page 2current page
[previous]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior

Created: November 27, 2002
Revised: November 27, 2002

URL: http://webreference.com/authoring/style/sheets/multiclass/3.html