Andrej Koelewijn

  • Home
  • About
  • Departments
    • cloud
    • java
    • mobile
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • tablet
    • Uncategorized
    • web
  • Subscribe via RSS

You don’t need a css class on every html tag

February 4th, 2009  |  Published in web

Just a simple tip, if you’re just starting out with css. I see a lot of html developers adding ids or class names on every html tag they want to target in their stylesheet. This is really not necessary. You can combine tags in your css rules.

Simple example: suppose you want to style your input tags based on the page their are displayed on. You could create the following html pages.

Page 1:

...
  <body class="page1">
    <input .... />
  </body>

Page 2:

...
  <body class="page2">
    <input .... />
  </body>

To change to styling of the input tag based on the page you can put the following css rules in your stylesheet:

  body.page1 input { width: 25%; }
  body.page2 input { width: 50%; }

CSS browser detection

I also use this to target different browsers. In javascript you can use object detection to determine what’s possible and what’s not. Unfortunately you can’t do this in css. Instead, i’m using a but of javascript to determine the browser, and add this information as a class to the body tag. Now it’s very easy to target different browsers in one css stylesheet.

For example, the following html would be the result of opening page 1 from the example above in internet explorer 7:

  <body class="page1 msie msie7">
    <input .... />
  </body>

I use jQuery to detect the browser, the relevant lines:

if ($.browser.msie) {
    bc = "msie";
    bc += " msie" + ($.browser.version).split(/\./)[0];
}
$("body").addClass(bc);

Now i can have the following css rules to target different browsers:

  .msie.page1 input { width: 300px; }   /* internet explorer */
  .msie7.page1 input { width: 250px; }  /* internet explorer 7 */
  .page1 input { width: 400px; }        /* all other browsers */

I haven’t actually tested if internet explorer supports multiple classes in one css selector as used in this example, but it works in firefox.

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • SphereIt
  • StumbleUpon
  • Technorati
  • LinkedIn
  • HackerNews
  • PDF
  • Digg
  • Facebook
  • FriendFeed
  • Posterous
  • Tumblr
  • Twitter
  • RSS

Leave a Response

blog comments powered by Disqus

Tags

bi bpel camel cep css dsl esb esper google governance grails groovy gtalk html5 innovation internet ipad ivy java javascript jaxrs jersey jigsaw jquery linkeddata linux maven middleware mule noiv openoffice openweb oracle osgi oss plsql rdbms rest soa sql sun tablet web 2.0 xmpp yql

Archives

  • August 2010
  • June 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • Nice Java Decompiler tool: JD
  • VMware Player: The virtual machine is busy.
  • Adding a maven repository for installing features to ServiceMix
  • Upgrade Apache Camel in ServiceMix to version 2.3.0
  • A composite Rest service using Apache Camel

Categories

  • cloud
  • java
  • mobile
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • tablet
  • Uncategorized
  • web

Recent Comments

  • Buddhika on Using google talk from java example
  • Anonymous on A composite Rest service using Apache Camel
  • Guest on How to find true cause of com.sun.star.uno.RuntimeException?
  • Absent Code attribute in method that is not native or abstract « Gooder Code - web development blog, php, java, asp.net, html, javascript on Absent Code attribute in method that is not native or abstract
  • Rmfume on Oracle best thing that could happen to JavaFX?
Buzz
andrkoel: RT @monkchips: James Governor's Monkchips » Day of The Dead: Web Drives Strong Demand for Java Skills http://monk.ly/d4UlND
9:17 PM Sep 03, 2010, comment
andrkoel: RT @monkchips: In which my business partner @sogrady explains Why You Should Pay Attention to Node.Js http://monk.ly/a4aGIP serverside # ...
3:06 PM Sep 03, 2010, comment
andrkoel: RT @stilkov: http://bit.ly/cDdqgl - AWS Identity and Access Management — I'd hate to have to compete against Amazon's Cloud offerings
8:47 AM Sep 03, 2010, comment
andrkoel: Twitter for ipad is nice, but i still think i need a tool to summarize all info, something like feedly or flipboard is the future
8:36 AM Sep 02, 2010, comment
andrkoel: Trying out the new twitter for ipad... Curious how the panels work.
8:32 AM Sep 02, 2010, comment
 


©2010 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.