Paul Irish recently wrote a post on using the universal selector to set border-box
box-sizing
on everything:
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
I’ve been wanting to try that for a while and I’m happy to say I’ve used it on a few projects already and it’s awesome. It also got my thinking what other properties might be similarly useful in applying to all elements on the page.
Transitions
* {
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-ms-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
}
If you’re on a desktop browser, try it out right now. Open your browsers dev tools and add a new CSS rule. I happen to be in Chrome so:
I just tried it in the WordPress dashboard where I’m writing this and it’s pretty wacky fun, especially in the side navigation. Performance could certainly be a concern here, so I’m not exactly advocating it’s use right now, but it’s fun to play with. When everything on the page has a quick and equal transition, I find it a nice, softened, comforting feel.
Non-Repeating Backgrounds
I bet overall you tell backgrounds to no-repeat
more often than you actually leave them repeating.
* {
background-repeat: no-repeat;
}
That way you can do:
.logo {
background-image: url(logo.png);
}
Without worrying that the image you’ve set will repeat and be weird if the container is slightly bigger than the logo. And you also don’t have to use the shorthand for background, which sets/resets background-color
, background-attachment
, and background-position
whether you want it to or not.
I know Estelle Weyl is a fan. =)
Relative Positioning
* {
position: relative;
}
If everything starts out with relative positioning, that means z-index
“just works” instead of the confusing issue where default statically positioned elements ignore it. It also means it’s easier to nudge things around with top
, left
, right
, and bottom
which all “just work” on relatively positioned elements. It might be hard to apply this to an existing layout but starting from scratch with this shouldn’t be too hard.
Middle Alignment
* {
vertical-align: middle;
}
I find myself setting this value a lot, especially on projects that use icons. It doesn’t affect much on most layouts I tried applying it on, as it only really comes up when inline or inline-block elements line up with each other on the same line. Essentially, I find myself setting this more than resetting a default, which I think makes it a good candidate for universal settage.
* {position: relative;}
will crash IE6, which may or may not be a bad thing. . .On another note, I do use the transition on just about everything I do these days, it’s really nice on :hovers for links, and I haven’t run into any performance issues, yet.
“* {position: relative;} will crash IE6, which may or may not be a bad thing. . .”
hehe
I don’t know, I honestly dislike IE 6 a lot and have not refrained from making a site that, even though it is compatible, is not visible at all in IE 6 by doing an if statement with a style code saying *{display:none}; just to troll IE 6 users.
I chuckled too when I read that IE6 comment
I back you up on this one.
I’ve been using
* { transition: all 0.2s ease; }
for a while now and never noticed any speed issues. Even on low bandwidth.Thanks for the tip, I’ll just be adding this to all my sites now =)
LOL!! :D IE6??? Really??? :D :D
Crashes IE6? I’m in!!
Crashes IE6? Count me in =)
Hahaha, knowing this eases the pain a bit.
Ha, nice!
Maybe I’ll actually use this one now.
Nice! Directly used it!
IE6 uuuuh man! :)
Then this code will be perfect :D
@Julien, are you serious?
> Even on low bandwidth.
What does CSS and client-side rendering have to do with bandwidth?
As for `* { position:relative }`. You do realize that you will lose the ability to position things absolutely once and for all?
@Julien, he meant client-side rendering performance, not network performance :)
This made my day! :D
As much as I think that *{position: relative;} would be awesome, I should experiment with it but I’m losing care for IE 6, I think we all should. IE 6 is dead now and while some of use
….while some of us lt IE 7 conditions for IE 6, I think it’s really time to stop caring for it and concentrate on rubbing out IE 7 now. IE 7 is the only real one I see with the major bugs (i.e. bad margins), so I think it’s time to start rubbing it out.
Stupid replyers … Do you make websites for yourself only, or to make some contents available to the users, whatever the tool they use ? You stupid bunch of degenerated selfish egocentric pseudo-dev. Usability and accessibility should be your main concern, not the size of you little device. Displaced vanity.
I’ve been using the above since I learnt them from your blog of course :) Universal easing transitions are very effective. But I’ve stopped giving a damn about the IE6. The people using it should realize it’s 21st century we’re living in.
It’s funny you should say that actually. You have to remember the internet is still very new and moving very fast.
IE6 DID come out in the 21st century. It came out on August 27, 2001 (after Windows XP was completed).
In the last 10 years the internet has become a whole new beast. And in 10 years it’ll be a completely different beast. And 10 years after that, another one. That’s just way technology works.
I wouldnt use position: relative on the universal selector.
Quite often I find myself using it on elements which have some child using position: absolute. This allows me to use as point of reference the position of the parent, and not the beginning of the page.
If every elements is relative, using this technique would be much more complicated, or maybe even impossible in some cases.
I think what you mean is that you don’t *always* want the positioning context to the parent, as it would if all elements were relative. In that case you could reset the parent element back to static. But yeah could be weird.
Yeah, exactly.
If the elements with position: absolute had *many* ancestors, reseting the ancestors to static could take more code than declaring “position: relative” just on the elements that actually need it.
Transitions on everything could be a performance nightmare. These features come at a cost and we should exercise restraint.
I’d also advise against using border-box everywhere. It comes with its own fair share of issues. For example, you lose the 1-1 relation between specified width and rendered width. With border-box you might end up with
width: 20px;
andwidth:0;
to have the same result. Also, there are browser bugs related with it, such as Gecko not respecting it in (min|max)-(width|height) properties.I like the rest, nice article Chris!
I would be careful using “transition: all“. I came into huge performance issues using this. And in conjunction with the “*” selector, this could be really a “performance nightmare” like you said. And i like to sleep well :-)
Lea, could you explain how you “lose the 1-1 relations between specified width and rendered width”? I thought that’s what “border-box” was intended to fix. Set the width to 100px, and the rendered with (including padding and borders is 100px).
Also, does anyone have references they can point to about any browser bugs related to “border-box”
Not only performance lacks with transition… also CSS-Sprites for Buttons with hover effects would look creepy…
I have always heard to steer clear of * selectors as it is an ‘expensive’ selector, as far as performance goes. That’s why css resets have that big huge strings of selectors before the padding:0px; margin:0px etc…
Has that thinking changed?
I would also really be interested in this answer.. if it has one haha.
I don’t think the * selector by itself gives a noticeable performance hit. It’s when you combine it with other selectors (and what you use it to apply) that might start to cause issues on very large, complex pages.
Something like this would cause a big performance drag:
But using it by itself isn’t that big of a deal.
In theory using * by itself should if nothing else, yield the cheapest possible logical comparisons, ideally none at all – just “apply this rule no matter what”.
Of course actually applying the style rule to each element has a computational cost too, so if you’re targeting too many elements needlessly there is some extra work being done as well as bloating the styles. I’ve never seen anyone talk about how expensive it actually is to apply a style/property to an element VS. excluding it via selectors though, so at what point one approach wins over the other I can’t say.
Mozilla appears to require the ‘s’ on the time properties. So it should be 0.2s, not 0.2.
It appears -webkit does too.
I have tried * { display: inline-block; } on projects where I am not supporting IE6/7.
It’s very cool! For these reasons:
* All elements respect box model properties and vertical-align.
* No floats, except when whitespace in code places an unwanted gutter between elements. Sometimes that gutter is fine and you get it with no extra effort.
I could be wrong about this, but I was under the impression that the universal selector is bad from a performance standpoint since it literally goes through and assigns that CSS to every possible element, even ones that you are not using?
Short answer: No.
Chris’s linked article explains this a little more (“Performance” section): http://paulirish.com/2012/box-sizing-border-box-ftw/
Get your own post ideas fatty.
playing with this one a few sites that use http://adapt.960.gs/
THAT is some interesting stuff!
Somebody PLEASE give me some serious pros and cons before I put this on all my sites lol
Honestly, I really like the regular box model. It takes a short while to get used to it, but there is something really nice about setting a width and having the content actually being that width. Especially where images are involved.
There are some things you can’t do with the content-box model unless you have calc() – and even then it’s less elegant.
I think your use-cases for content-box are totally valid, though possibly in the minority of regularly-encountered scenarios.
Also, you could always
img { box-sizing: content-box; }
Some of these are pretty cool. I am not sure about transitions as they may be kind of resource intensive for the UA, even if they aren’t activated.
The position relative , aside from IE 6, has the downfall of reducing control. what I mean i that MANY TIMES it is useful to position an object absolutely relative to an ancestor. so if we *’ed {position:relative} every element we would need to go back an individually assign position:static to all the element we want “skipped” so we are back to where we started as far as the amount of code necessary. :/
OH, and let us not forget what *{} rules can do to form elements, which cannot be reset back.
You’re thinking of
position: absolute;
position: relative;
will position an element relative to its natural coordinate (“natural” meaning as though no positioning were used at all).@BrianMB – the point is that absolute positions are based off the nearest parent with a position other than static. If everything is relatively positioned, by default absolute positioning will always be in relation to the element’s immediate parent, and it will be more difficult if you need to position based on some more distant parent.
the transition one reminded me of a relatively old post from WebDesignerDepot.com which mentioned a similar technique
http://www.webdesignerdepot.com/2010/01/css-transitions-101/
Interesting that Lea recommends against using it for the box model, and Paul Irish does recommend using it. Both are experts, so which should I believe?
I think it’s more about being aware of the consequences when deciding whether or not to use it. Lea mentions a few issues when using it across the board, so if you aren’t comfortable with dealing with these issues, then you probably shouldn’t use it.
Like the background-repeat suggestion. Would avoid the transitions on everything for performance reasons.
Already having fun playing around with it a little bit, just using Stylish, and applying a global transition to every web page – it’s kind of fun, but also a bit wacky sometimes…
Easy to try out, though:
I’ve used:
* { transition:0.1s; margin:0; padding:0; }
in a few projects before (along with Prefix Free to handle vendor prefixing) and it has really saved me some time.
I’m usually against setting up classes for specific things like color, etc, but the examples you give of using classes like this for alignment et. al. are intriguing.
Though it’s nice to not have to change your markup, if you have elements that you’re sure won’t change for a long time this technique is really practical.
Just trying the
*
rule for WP dashboard as you said, and found that the background changes perfectly! Very nice effect. I also love other ideas of setting non-repeated background and middle alignment. I often see these problems.Thanks!
isn’t box-sizing: border-box; is the default box model in IE6? so the 1st rule should be save to use across browser isn’t?
only in quirks mode (rare)
I’ve used ” * {position: relative}” in a project before, despite that I really liked how it fits on modern browsers, IE8 sucked really bad.
I ended up making a separate file just to fix IE8/7 and had a lot of (wierd) problems. Even firefox 5 had some issues.
Some of them where fixed by setting html {position: static}.. this was after a few hours of trying random stupid stuff that just might work.. I hade a few tables, and positioning inside the cells was horribly wrong, also a few line-height weirdness..
Overall not worth it, and also the performance was bad, I’ve rewrote the stylesheet using the CSS performance tips that I’ve found on various posts and really made the pages more smooth/fast in IE8.
This article is very good, I even played with Opera’s CSS performance tool; for me, the guidelines where very useful:
https://developer.mozilla.org/en/Writing_Efficient_CSS
I’ve found that using transitions on the universal selector can cause a weird text resizing issues in Wekbit-based browsers, whereas this doesn’t happen if you spell out the html elements like below:
I haven’t noticed using transitions on the universal selector a resource hog though. How would you notice? All the animation seems to work as it should
setting a relative position on every element is just “wrong” :p, I think this will cause lots of problems in older browsers. But apart from that I realy like the idea of the non-repeating backgrounds.
I’m also a bit worried about performance if you set transition to every element but you could always use it like this, so that it doesn’t match every item.
* { background-repeat: no-repeat }
does not work for all background images because if you use the shorthand property
.logo { background-image: url(logo.png) }
the unspecified individual properties will be set to their default values, i.e. ‘background-repeat’ will be set to “repeat”. Because of the selector’s higher specifity this value will be applied.
To set “no-repeat” for all background images, you’d have to make it ‘!important’ — with all the troubles you’d run into.
in your example, you’re not using the shorthand property – things will work as intended.
But if you were using the shorthand, yes, thing would go awry.
I liked the no-repeat idea until I realised it was of no real use. If you are writing longhand CSS then you would gain far more by switching to short hand than you ever could with this idea. It is a shame browser manufacturers hadn’t set no-repeat as the default originally.
Blimey, until now I was under impression using * is quite expensive performance vice. Apparently it’s not. Good to know :-)
Ive found its useful when switching off the hover attribute for mobile devices like the iPad and iPhone otherwise you have to double tap the button. You could use other methods but this is quick and easy and you just have to place it in @media query.
*hover { display:none; }
Chrome 17 dies when you try to add * to via inspector now ? that didn’t happen before
I noticed this to.
I’m quite surprised at the recent trend of using the universal selector. A few years back when we were all using * { padding: 0; margin: 0 } to reset styles it quickly became apparent that this was not a best practice and we moved to more selective resets style solutions so the browser wasn’t bogged down applying rules to everything.
How come the practice is now ok? Why not make a more specific reset style snippet to target only the elements you want box-sizing, transitions, relative, etc on?
OK so I should have read through the comments first. It appears quite a few others share my concerns but Paul Irish discusses the issue of * performance at the end of this article: http://paulirish.com/2012/box-sizing-border-box-ftw/
It’s just like everything IT/programming related. The speed of computers, servers, internet connections all increase. So using something like * { } isn’t likely to cause a problem on most sites unless it’s supremely complicated.
But as the speed and power increase we find ourselves becoming less frugal and using code that was once considered sloppy.
How many people really try to get every kilobyte shaved off their images or write and then rewrite their HTML and CSS to make it as small and efficient as possible? Of course you should aspire to do the best you can but it’s not the end of the world (unless you start dumping 4MB background images into your pages)
I like the theory behind
* { box-sizing: border-box }
, but I also quite like the Twitter Bootstrap framework. Anyone know if they work together, or does Twitter’s careful spacing just break if you try to force it to border-sizing?Definitely do not use the universal selector and position relative. I have tried this for a couple of years now and found that internal table elements do not like it and it causes layout issues. (I can’t remember which browser). The parent table element is fine though.
Finally to answer the comment about the parent issue – IMO all dynamic content should be inserted at the end of the DOM which means there are no parent issues. The position to then display the position: absolute element can then be calculated from the originators position inside the DOM.
Nice post. Cheers.
Instead of
* { transition: all 0.2s ease; }
I would prefer set this rule only in some specific elements (and not to all elements) but anyway with some kind of flexibility . e.g.That’s a great way to control it. Some background images could do a not-so-fine effect, and its not ok to lost control.
If we all would use transitions everywhere, everyone would go nuts.
Imagine using Gmail and hovering or clicking something always brings up a non functional (cause thats what it is) animation.. That would suck. I think it’s just a temporary hype. Same with border-radius. 1 year ago I saw many sites using about 20px and nowadays I see people going back to 10px even 4px just to give it a little bit of style but not more.
Look at cars, every 10 years they also switch from overal styles. It’s kind a fashion.
Or am I getting too old? :)
Just a slight, short animation of 0.2 or 0.3 seconds adds a nice shine to a site. You only really notice it when you hover over the links.
i have been using this for over millennia
Its, fun to use
animates everything :)
This is will give everything an outline without messing with the layout like adding a border. It’s good for checking overall layout.
I personally use the CSS property ‘outline’ as it is much quicker to type out:
Awesome article. I’ve always been really hesitant to add new things within my universal selectors but I think I’m gonna start playing around with some of these!
I tried putting
*{position: relative}
in my site and it broke the WordPress search bar. I couldn’t highlight it and my submit button wouldn’t go into its hover state. Is that normal?What’s the positioning used for the search bar and its child elements? Also, is your universal selector before or after the search styles? If the search bar positioning is set to “absolute,” and if it comes BEFORE the universal selector, the universal selector may be overriding the search bar positioning.
I had the same issue with the position:relative. Lost an hour of my life trying to figure out why all my img links could no longer be selected.
I like using relative positioning for the universal selector. As well, I now declare float and overflow values globally for certain elements, just below my reset. I then override them, if necessary, later in the stylesheet for specific needs ( ex: “div {overflow:hidden;}” ).
I’ve used three of these (box-sizing: border-box;, background-repeat: no-repeat; and position: relative;) on my WordPress framework… I had a couple of minor teething problems, and I think I’m going to have to re-learn how to do some of the things I do – but for the most part, they seem to work well.
When I tried to use the * selector while on your page, it crashed Chrome! Fun, eh?
This happens to me, too… every time.
Very interesting article. I’d love to use Paul Irish’s tip, and yours too Chris, but i’m forever in doubt of using the * selector.
I thoroughly agree!
What absolutely haphazard advice. These suggestions advocate developers to apply blanket rules that are meant to be used as adjustments. Even box-sizing should never be applied universally. You need to offer graceful degradation for clients that don’t understand box-sizing.
Furthermore, maintenance is hindered, as we forget about these blanket rules below the fold.
I’m very unimpressed with developers encouraging their followers to be lazy, which is exactly what it is.
*{
-webkit-font-smoothing: antialiased;
}
Good one!
I just found out today that the universal selector did not apply to :before and :after pseudo elements. I had rendered a CSS shape and thought I could just round out all of the sharp corners after everything was in place. Not true. This works, but…
*, *:before, *:after { border-radius: 2px; }
Since these elements are obviously not in the realm of universal, do they exist in their own Pseudo Universe?
BING – lightbulb! If what I just said is true:
at least when making complex shapes. This should save me loads of time and tidy things up. Thanks… somebody! Paul maybe?
You can just use
Also, *::selection{} works great for making your whole site have a unique highlight.
* on vertical-align? Doesn’t it work only for TD and display table-cell?
On the box-sizing topic I’ve been meaning to ask: Chris, what’s your opinion on dropping IE7 support in favor of offering a more solid experience in the more modern browsers?
Wrote a basic Chrome extension which applies the transition rule on all elements. Check it out at Chrome Web Store
Really Helpful post, I needed the code for the -webkit and this was very handy, thanks for taking the time to share this, BTW!!! I HATE YOU, Your comment forms looking AMAZING!!!
Steve Loneton
Web Design Bournemouth and SEO
Keep up the good work ;)
It is so much fun to use transitions with the global selector… now I am going to update some of my older Projects :D
I’ve used the * selector for the box-sizing property too, seems to work well where’s it supported.
But when I started added other dynamic properties like background-sizing and transitions, all hell broke loose ;-)
Always remember that just because you may have the advantage of working on hi-spec kit, there are millions of people all over the world who don’t have dual/quad core processors, superfast graphics processors, or even up-to-date browsers.
My advice, be realistic about how many properties you assign to the * selector. For larger scale projects where perhaps the CSS differs from page to page, be more selective and use selector groupings rather than the wildcard.
I’d also suggest being even more restrained when using it with media queries for smaller devices. By and large many effects that look great on a desktop are simply lost on smaller devices due to screen size limitations.
Off-topic, but is anyone else seeing the responses to this thread displayed in a random-ish order?
I didn’t think so at first, but I see the one below yours by snillor is 5 days before yours. . . Odd.
I too have recently started using transitions. Very nice and simple way to add a touch of flair.
When I don’t support old versions of ie I use * to hide the whole page, then I display a message telling them to ugrade.
I was using position: relative; on my universal selector, as a result of this post – but earlier today I removed it – it was causing more trouble than it was worth.
box-sizing: border-box; is working well (apart from a minor clash with colorbox, but a couple of minor edits to the colorbox css fixed that, and background-repeat: no-repeat; also seems to be working well.
How can I add a new CSS rule via Chrome? Everytime I click on the “+”, and enter any rule (IE “*”) just to start, the browser windows crashes: it gets blue and tells some error has occurred….. ;( I cannot experiment anything from here…!
Thanks
Guys, how can I insert a new CSS rule with the Chrome inspector? Everytime I try to add it and name it (“+” button), Chrome crashes: the screen gets blue, and it says that some error has occurred, like it’s trying to refresh the page with my new rule… ;(
thanks
I’m going to be using this trick in all my css form now on, I didn’t notice any lag; I love things not insto-breaking when I tweak a few pixels.
I got to think what other elements are nearly as wide as * and I thought of a and dive. I took the transitions idea and applied it to the a element to give the feel of touch in the browser and I’m very pleased with it.
*{ cursor: default; }
input, textarea{ cursor: auto; }
a{ cursor: pointer; } /* etc */
interesting article i’ll be sure to try some of these out myself. please help me out and visit my website i have interesting articles myself.
*
{
-webkit-transform: rotate(-11deg);
-moz-transform: rotate(-11deg);
}
Can turn a site into abstact art.
Also, adding hover makes it interactive…
*:hover
{
-webkit-transform: rotate(-11deg);
-moz-transform: rotate(-11deg);
}
Sliiiightly modified smapties code. I like this one:
Pro tip: Make a userscript matching all websites, let it add this css and install it at someone’s computer ^^,
loving the transition code, nice simple way to add a bit of animation to the page. We are always struggling with clients using old browsers, and it really cool to add all these extra bonus features to users on the lates browsers!
IE6… Really?
Great info. Especially the part on universal transitions. Such a subtle effect yet so stylish (do you feel a “but” coming?).
But.
Apparently css3 transitions seem to mess up all of jQuery’s animation functions – ex. for a slideDown menu etc .-
Anyone else got that problem? Any fixes?
Thought of using *:hover (ex. for some anchors) instead but still, it’s a matter of curiosity to find out why it breaks up like that.
Browserstack is getting a workout today. Love the comments. I use inline-block on too many things already, so I have some experiments to do now.
After some playing around a little this seems totally reasonable and will actually save me some time in future projects.
Nav is when I notice this stuff anyway.
For the duration of any loading event
* { cursor:wait; }
*{display:none;}
Great tips! I would be careful setting “vertical-align: middle” globally, though. You can inadvertently change the height of elements and cause alignment problems with nested inline-elements.
Here’s a demo http://jsbin.com/uReMoHi/latest
What about using break-word on every element?
{
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
i am working on liferay project i am using above syntax in CSS how can exempt this for some classes