views:

3843

answers:

24

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?

+1  A: 

I know this isn't a CSS feature, but it sure can make these easier to use and live with if you're using Visual Studio. How to create custom CSS intellisense schema in Visual Studio 2005 and 2008.

overslacked
+6  A: 

Not so much hidden features, but a question featuring CSS tips which every beginning developer should know about

Paul Dixon
+14  A: 

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

Ben Alpert
Knew this one but pretty handy. Also don't forget if that isn't an option, you can use the overflow property without resorting to ugly clearing divs.
alex
I would vote this up but it should be a community wiki answer
John Sheehan
Sorry. Should have done it from the start.
Ben Alpert
I don't know why everyone uses divs with a clear, i think br is a much more semantically relevant element to use, and I would consider a br with a clear on it to be less ugly then floating everything whether it needs it or not
Matt Briggs
+19  A: 

Maybe negative margins and absolute positioned elements in relative positioned elements.

See How would YOU do this with CSS? for examples.

Gumbo
care to rather explain? also plz wiki it
hasen j
+4  A: 

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.

Ben Alpert
No, it's positioned relative to the closest parent with *any* 'position:' other than the default, 'static'
Gareth
@Gareth - almost, "fixed" also doesn't count
annakata
@annakata: Are you sure? I just tried it quickly, and the absolute div was positioned inside it's parent - a fixed div. I added another absolute div with with the same specs but with no parent (well, body would be its default parent), and it was positioned differently than the one inside the fixed - i.e. it was positioned at the bottom of the page. So I assume absolute-inside-fixed works.
AgentConundrum
A: 

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.

Kees de Kooter
You could probably add that last paragraph to any Stack Overflow answer. But it wouldn't be very helpful would it?
alex
You are absolutely right Alex. I deleted the last paragraph.
Kees de Kooter
+4  A: 

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>
hasen j
IE6 only supports with inline elements, am I correct?
alex
not sure which browser supports or doesn't support divs, but that's why I said it doesn't work on divs!
hasen j
Why would divs not be supported? A div is a span with display: block (but may have extra styling by a browser or author stylesheet).
strager
don't you know how browsers are nice to us? it doesn't work on an element that has block display, online inline elements.
hasen j
@strager while you are correct, don't underestimate IE's implementation (or lack) of standards
alex
.iblock { display: -moz-inline-stack; display:inline-block; zoom:1; *display:inline; /* Alignment Fix */ vertical-align:top;}
Andres
+6  A: 

Currently only for Safari 3 but quite interesting: CSS Animations

alex
You probably meant webkit.
Tchalvak
+9  A: 

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...
facildelembrar
+6  A: 

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.

VirtuosiMedia
I'm pretty sure child selectors ARE a feature. =]
strager
They ARE a feature, but the fact that you can use them to isolate IE6 is more of a trick.
VirtuosiMedia
+2  A: 

Another IE6 selector

* html .something
{
  color:red;
}

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

korchev
Note zoom will not validate... if this matters to you then try height: 1% or similar to trigger hasLayout
alex
+24  A: 

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>
Binoj Antony
This is a great answer - it's surprising how many people don't know this.
Sohnee
Why did I assume you can only do this with two classes.....
Baddie
Also note the fine distinction between `.bold.red {}` and `.bold .red {}`...
Ates Goral
If multiple classes had conflicting properties (e.g. if `.red` and `.GoldBg` both specified the `color` property) then the CSS specificity rules apply; the order of classes in the `class="..."` attribute shouldn't matter.
ide
+19  A: 

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/

Sohnee
You can also use spriting for javascript controlled animations. Just cycle through the sprites on a setInterval etc.
Matthew Lock
Good suggestion from Matthew Lock - the bonus with that suggestion is that you just change the position of the image, rather than the source of the image - so pre-loading isn't necessary on your entire animation set.
Sohnee
You can see an example here http://sstatic.net/so/img/sprites.png
alex
+9  A: 

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;
}
Steve Harrison
I don't believe this works in IE, though.
MiffTheFox
Surely it works in IE8?
Bobby Jack
I think this is supported all the way down to IE5.5
Arve Systad
A: 

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; } 
A: 
.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

TheBrain
Browser hacks aern't features? Oh well, still useful!
alex
It's best practice to add separate stylesheets for IE6 and IE7 using Conditional Comments http://www.quirksmode.org/css/condcom.html instead of relying on rendering engine bugs.
Tom
This is a really BAD practice. Listen to @Tom ;)
gregers
+9  A: 
Nikita Prokopov
This is the coolest one yet! +1
alex
How much support is there for this one accross major browsers?
Shawn
@Shawn: WebKit based browsers (Safari + Chrome), Firefox 3.1+ (I think), and Opera 10.5. So it's pretty widespread, except for IE--as always. :)
musicfreak
You can transform using the proprietary IE "filter:" thing like you used to do for transparent PNGs. Though the IE transform/rotation parameters require some basic trigonometry calculations. And standard obnoxious "filter:" bugs still apply.
Mufasa
+1  A: 

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; }
Nimbuz
This only works on elements in IE6/7 that are inline by default right?
alex
Works on DIV, which is a block element.
Nimbuz
+1  A: 

I have no Idea whether this is a hidden feature, but I just wowed seeing this: http://www.romancortes.com/blog/css-3d-meninas/

zedoo
+15  A: 

You can display the document’s title element:

head, title {
    display: block;
}
Gumbo
Wow, nice, and also rather strange.
Tchalvak
+1 for probably not useful but still interesting
Brendan Long
that's unique. I thought none of the <head> elements were renderable.
Raine
+1  A: 

Word wrapping can be done easily using css, without any help of server-side technology.

word-wrap: break-word;
sumanchalki
+4  A: 

You can create scrolling areas without resorting to frames by using CSS's overflow property. Example:

div.foo {
    border:   1px solid;
    width:    300px;
    height:   300px;
    overflow: auto;
}

overflow: auto means if the content can't fit within the div, horizontal and/or vertical scroll bars will appear as needed.

overflow: scroll means both scroll bars will always be present. If you only want one scroll bar to always be present, use overflow-x or overflow-y (which are supported by modern browsers and IE6).

Some of you may be thinking "duuuh", but I was surprised to learn that scrolling areas can be created without frames.

Joey Adams
+3  A: 

Last week I came across an amazingly useful CSS property I had never heard of:

text-rendering: optimizeLegibility;

Safari, Chrome and Firefox all understand this property, and when set enable advanced kerning and ligatures. Here's a great demo.

kingjeffrey
it makes Chinese characters look horrible on some browsers/systems
hasen j