Hidden features of CSS

Viewed 16549

I have definitely picked up some useful tips in the hidden features style questions concerning PHP and XHTML.

So here is one to cover CSS. While easy to pick up, it takes a little while to learn about everything, their default behaviors, properties etc

Here are some to start the ball

@charset "UTF-8"; /* set the character set. must be first line as Gumbo points out in comments */

.element {
        /* takes precedence over other stylings */
        display: block !important;

        /* mozilla .... rounded corners with no images */
        -moz-border-radius: 10px; 

        /* webkit equivalent */
        -webkit-border-radius: 10px 
}

These are not so much hidden, but their use is not often widespread. What tips, tricks, rare features have you discovered with CSS?

27 Answers

You can display the document’s title element:

head, title {
    display: block;
}

Apply multiple styles/classes to an element like this class="bold red GoldBg"

<html><head>
<style>
.bold {font-weight:bold}
.red {color:red}
.GoldBg {background-color:gold}
</style>
</head><body>
<p class="bold red GoldBg">Foo.Bar(red)</p>
</body></html>

I really like CSS sprites.

Rather than have 20 images for all your site buttons and logos (and therefore 20 http requests with the latency around each one) you just use one image, and position it each time so only the bit you want is visible.

It's difficult to post an example as you'd need to see the component image and the placement CSS - but I've blogged Google's use of it here: http://www.stevefenton.co.uk/Content/Blog/Date/200905/Blog/Google-Uses-Image-Sprites/

The fact that floating a parent element will cause it to expand to contain all of its floated children.

You can set a variable width for an absolutely positioned element by specifying both left and right properties. This gives you more control than simply setting width to a percentage.

For example:

#myElement {
    position: absolute;
    left: 5px;
    right: 10px;
}

An alternative Example

#myElement{ /* fill up the whole space :) */
   background: red;
   position:absolute;
   left: 0;
   right:0;
   top: 0;
   bottom: 0;
}

My ones are:

  • all properties of aural sheets like azimuth, pitch...
  • some properties of the print module like page-break-after: avoid;
  • counter-increment: section 1;
  • border-collapse: collapse;
  • background-color: transparent;
  • outline: 1px solid...

Not really a feature, but useful nonetheless: The child selector works in all browsers except IE6, allowing you to isolate IE6 without using hacks or conditional stylesheets or invalidating your code. Thus, the link in the following code will be red in IE6, blue in every other browser.

CSS

/*Red for IE6*/
.link {color:#F00;}
/*Blue for everything else*/
#content>.link {color:#00F;}

HTML

<div id="content">
    <a class="link" href="#">Link</a>
</div>

Here is a list of selectors (for CSS2) and a browser compatibility chart.

Transparent PNG in IE6 This fixes PNG transparency in IE6. Set background to non and include the image in filter. No need for any javascript or htc.

.whatever {
   background: none; /* Hide the current background image so you can replace it with the filter*/
   width: 500px; /* Must specify width */
   height: 176px; /* Must specify height */
   filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='vehicles.png');
}

Sets page-breaking behavior after an element - Cross browser

table {
   page-break-after:always
} 

You can use the properties always, avoid, auto, left, right, inherent. Read docs at http://www.w3schools.com/CSS/pr_print_pageba.asp

A way to number sections and sub-sections with "Section 1", "1.1", "1.2", etc - Cross browser

h2:before 
{
   counter-increment:subsection;
   content:counter(section) "." counter(subsection) " ";
}

http://www.w3schools.com/CSS/pr_gen_counter-increment.asp

Collapse Table borders into a single border or detached as in standard HTML - Cross browser

table
{
   border-collapse:collapse;
}

http://www.w3schools.com/css/pr_tab_border-collapse.asp

Remove selection Border or dotted line from button or input fields. Has other great uses - Cross browser

button{
   outline:0;
}

http://www.w3schools.com/CSS/pr_outline.asp

* html for 100% height in IE6

* html .move{
   height:100%;
}

Allow long words to break and wrap onto the next line - CSS3 Cross browser

.whatever {
   word-wrap:break-word;
}

http://www.w3schools.com/css3/css3_pr_word-wrap.asp

Shorthands

Before

font-size: 1em;
line-height: 1.5em;
font-weight: bold;
font-style: italic;
font-family: serif 

After

font: 1em/1.5em bold italic serif;

Before

background-color: #fff;
background-image: url(image.gif);
background-repeat: no-repeat;
background-position: top left;

After

background: #fff url(image.gif) no-repeat top left;

Before

list-style: #fff;
list-style-type: disc;
list-style-position: outside;
list-style-image: url(image.gif) 

After

list-style: disc outside url(something.gif);

Before

margin-top: 2px;
margin-right: 1px;
margin-bottom: 3px;
margin-left: 4px 

After

margin:2px 1px 3px 4px; /*also works for padding*/
margin:0; /*You can also do this for all 0 borders*/
margin:2px 3px 5px; /*  you can do this for top 2px, left/right 3px, bottom 5px and ;    

The :before and :after pseudo-elements

The following rule causes the string "Chapter: " to be generated before each H1 element:

H1:before { 
  content: "Chapter: ";
  display: inline;
}

For more, http://www.w3.org/TR/CSS2/generate.html

inline blocks (alternative to floating divs):

.inline_block
{
    display:-moz-inline-box;
    display:inline-block;
}  

Don't apply this class to a div! it won't work! apply it to a span (or an inline element)

<span class="inline_block">
</span>

Not really "hidden", but understanding the box model and positioning model will help tremendously.

Like, knowing that a position: absolute element is positioned relative to its first parent that is styled with position: relative.

Another IE6 selector

* html .something
{
  color:red;
}

Fixing random IE6 rendering bugs - apply zoom:1 which will trigger layout.

Cross browser inline-block works on block and inline elements using the combined declarations:

.column { 
-moz-inline-box; -moz-box-orient:vertical; display:inline-block; vertical-align:top; 
} 

for standards browsers including Firefox 2, and:

.ie_lte7 .column { display:inline; } 

Cross-browser (IE6+, FF, Safari) float alternative:

.inline-block {
    display: inline-block;
    display: -moz-inline-box;
    -moz-box-orient: vertical;
    vertical-align: top;
    zoom: 1;
    *display: inline; }
.class {
/* red for chrome, ff, safari, opera */
background-color: red;
/* green for IE6 */
.background-color: green;
/* blue for IE7+ */
_background-color: blue;
}

will render your <whatever> background different in those browser categories

The border-radius stuff is part of the CSS3 specification. As CSS3 is still not completely finished the more progressive browsers in the meantime implement parts of it with their own properties (-moz, -webkit). So we can already enjoy rounded corners, cleanly coded in pure css.

Unfortunately the other big player in the browser market still shows no sign of implementing css3 features.

Related