Great CSS Techniques and the Simple Truth Behind Them
Published on March 11, 2008 (ā» February 5, 2024), filed under Development (RSS feed for allĀ categories).
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.
Thereās a simple recipe to judge CSS techniques: Does the method in question require HTML additions and modifications (beyond introducing IDs or classes)?
If yes, and depending on the strictness youāre applying, the technique likely isnāt elegant and might be inadvisable. Personally, I avoid and discourage to use HTML-heavy techniques. Both theory and practice show that youāre better off looking for alternative solutionsāpresentational HTML changes do not only mean worse code but also particular problems for maintainability.
While itās nice to conceptualize such problems, here are two examples:
Questionable: Nifty Corners
The initial proposal for rounded corners asked for the following markup to be used, in conjunction with extra CSS styling:
<div id="container">
<b class="rtop">
<b class="r1"></b> <b class="r2"></b> <b class="r3"></b> <b class="r4"></b>
</b>
<!-- Content -->
<b class="rbottom">
<b class="r4"></b> <b class="r3"></b> <b class="r2"></b> <b class="r1"></b>
</b>
</div>
I wonāt comment on the use of b
elements and stuff (in my book, I already discuss HTML for this technique) but like to repeat: Great CSS techniques donāt necessitate far-reaching HTML modifications.
Recommendable: CSS Sprites
CSS Sprites are a nice way to create delay-free hover effects, one that improves performance by avoiding HTTP requests. Letās see if the theory that good CSS techniques do without HTML changes holds true:
<!-- Untouched HTML -->
It does!
ā§ This has not been a scientific approach to CSS methodology assessment, but should serve as an indicator. HTML modifications are for good reason the point of criticism when it comes to new techniques, including those āyou canāt live withoutā or that are āfor effective codingā. Presentation should not require the structure to be changed, as that couples them tightly.
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a frontend engineering leader and tech author/publisher. Iāve worked as a technical lead for companies like Google and as an engineering manager for companies like Miro, Iām a contributor to several web standards, and I write and review books for OāReilly and FrontendĀ Dogma.
I love trying things, not only in web development (and engineering management), but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (Please be critical, interpret charitably, and giveĀ feedback.)
Comments (Closed)
-
On March 11, 2008, 22:04 CET, Dave said:
Absolutely right!
Taking such a pure stance on code sometimes forces me to decide that something isnāt possible (yet). For example, is there a way to do rounded corners or some other image-based border style on a div of flexible dimensions?
Before I read any article on a tricky CSS idea, I check to see if the author outlines what support is like in major browsers. I wish authors would similarly state outright if extra HTML is required.
-
On March 12, 2008, 10:32 CET, Phil Nash said:
Seeing techiniques like nifty corners always makes me feel uncomfortable due to all the extra markup. If you have to do it, at least apply it with JavaScript.
This may be a simple analysis, but I agree and think you are totally right. If you are adding markup then you havenāt fully separated structure and style.
Dave: we are all waiting for CSS3ās border-radius to be well implemented across the board!
-
On March 12, 2008, 11:53 CET, Martin Sarsini said:
Certainly they are inferior techniques, but in some cases having a rich developed web site or web application with rich extras is something that makes it so sensational in usage and appearance, and for most of the final users all works great. In other cases you can realize that the same website is exactly the same thing without all that kb of javascript and wierd generated codeā¦ and itās so sensational in itās simplicity.
There is always a price you have to pay
-
On March 12, 2008, 12:10 CET, Gemma said:
Iād love to be able to be semantic all the time, cut off the extra added divs and such whose only purpose is to align stuff (backgrounds that are stupidly complex most of the time).
My rule of thumb tends to be:
1) Mark up content semantically and cleanly.
2) Style it as per the design, as far as possible.
3) Add extras to make it fit the design exactly.This tends to keep it as clean as humanly possible while still giving in to the fact that clients always want the design thats the hardest to build in a semantic manner. š
-
On March 12, 2008, 16:40 CET, Jens Oliver Meiert said:
Dave, right, some authors could early on say whether they ask for additional HTML. And as Phil pointed out, CSS 3 will give you at least two options for rounded corners, either by using more than one background image or via
border-radius
.Phil, thank you, though I am a little bit skeptical about the JavaScript alternativeā¦ personally, I like to check the final document tree as well š
Gemma, unless step 3 dictates everything else, this looks like a good approach!
-
On March 18, 2008, 21:48 CET, lynne said:
Jen, I briefly looked over the CSS3 and Nifty corners that you posted in regards to rounded corners with CSS. Also the w3c(always there for me).
I have had a lot of fun tinkering with rounded corners and noticed that if I base a similar technique with background images it was taking up about 18 images! Not to mention extra non-semantic wrapper and enough css I could fall over. I think that Nifty Corners hit it right when they said that you used about 18-20Kb or so.
Great CSS technique, concise and to the point.
-
On March 18, 2008, 21:51 CET, lynne said:
Iām so sorry I forgot the (s) in your nameā¦I apologize about my typing error. Again apologies. Jens*
-
On March 19, 2008, 11:41 CET, Mike said:
I also apply an additional rule - will the particular hack be needed in a few years time?
Again, rounded corners: already an established part of the spec, supported by Mozilla and by WebKit: those browsers get to show off, IE gets to look a bit square (which it is). -
On March 22, 2008, 9:13 CET, Jordan Clark said:
I tend to take a pragmatic approach to these matters: for me, its a case of deciding whether the means adds enough value to justify the ends (generally, I find it does not).
I agree with you in principle, though: that so-called ānifty cornersā technique is very ugly, and not something I would use personally. However, I am not so pedantic that I object to an extra
div
orspan
here or there, either. Itās all about balance, IMHO. -
On June 1, 2008, 1:08 CEST, Ludde said:
Nifty Corners Cube was released in March 2006, and it adds the necessary html markup via javascript.
Read More
Maybe of interest to you,Ā too:
- Next: Website Optimization Measures, Part III
- Previous: The Secret of Maintainability
- More under Development
- More from 2008
- Most popular posts
Looking for a way to comment? Comments have been disabled,Ā unfortunately.
Get a good look at web development? Try WebGlossary.infoāand The Web Development Glossary 3K. With explanations and definitions for thousands of terms of web development, web design, and related fields, building on Wikipedia as well as MDN Web Docs. Available at Apple Books, Kobo, Google Play Books, andĀ Leanpub.