CSS attribute selector + descendant gives a bug in Webkit? - css

Consider this CSS:
[data-color="red"] h1 {
background-color:red;
}
[data-color="blue"] h1 {
background-color:blue;
}
And this HTML:
<div data-color="red">
<h1>red</h1>
</div>
<div data-color="blue">
<h1>blue</h1>
</div>
<div data-color="blue">
<h1>blue</h1>
</div>
Now take a look at the following demo of the above code in Webkit and any other browser:
http://jsfiddle.net/aUCkn/
What's strange is that if you put each h1 on the same line, i.e.:
<div data-color="red"><h1>red</h1>
</div>
<div data-color="blue"><h1>blue</h1>
</div>
<div data-color="blue"><h1>blue</h1>
</div>
It works in Webkit too:
http://jsfiddle.net/aUCkn/1/
Does anyone know where this comes from? Am I doing something wrong or is Webkit acting stupid here?

The first jsFiddle is broken in my Chrome 12.0.742.112 (stable).
However, it works in my Chrome 14.0.803.0 dev-m.
So, they're already aware of and have fixed the bug. You just have to wait for the fix to land in the stable channel.
I'll try to find a link to a bug report, if one exists.

Try adding [_] {} to your CSS (doesn't matter where).
In reality, it can be any attribute selector rule, without a descendant clause, that will potentially match the attributed elements selected by the original selectors, i.e.: [data-color] {} and div[data-color] {} will fix the others, but a[data-color] {} will not fix it.
I've tested it on the posted fiddle (http://jsfiddle.net/aUCkn/) and it works for Safari (5.1.2).
My co-worker and I found it after playing with lots of random ideas for workarounds.

When using Chrome (Webkit) I get this issue as well, though it seems to work fine in Firefox and IE9. It definitely appears to be a problem with Webkit that the extra whitespaces cause problems.

--- UPDATE ---
This issue is fixed in Chrome as of version ~18, it continues to be an issue in other WebKit based browsers.
As far as solutions for this issue, Michael Morton's answer above is the best one here - it is more flexible and performant than mine. I have upvoted Michael's answer and suggest you use it.
Here's a CSS hack that fixes the issue: http://jsfiddle.net/aUCkn/101/
You just need to put * + before the selector. Now before you all grab your pitchforks and torches over the use of the star selector, remember that it is all the way to the left, so it shouldn't affect performance in any meaningful way ;)
PS - I can also confirm this bug affects ALL versions of ALL WebKit browsers on ALL platforms and devices - other than Chrome 14+. What an absolutely terrible bug, now the majority of WebKit browsers need to retract the claim they support even the now-ancient CSS2.1 spec. Wow, LOL.

Related

What is the use of this CSS hack in Safari?

What is the use of this CSS hack in Safari?
/* CSS Hack Safari */
#dummy {;# }
I found it in this page:
http://jscroller2.markusbordihn.de/example/endless/
After doing some Googling, I was able to find something pertaining to Safari and browser hacks..
Apparently, older versions of Safari would ignore CSS declarations that were preceded by a hashtags. For instance:
h1 {
font-size:24px;
font-size:30px;#
}
If Safari were to come across something similar to this, it would make h1 24px, and completely ignore the 30px.. However, say IE, or Chrome were to see this, they would read the declaration and make the fontsize 30px because of the fact that they don't take the hashtag into consideration. To my knowledge this little 'hack' no longer works on newer versions of Safari.. Here is my reference.
Now back to the question:
#dummy {;#}
This doesn't particularly do anything, so I don't really see why this was in their code.
However, I am assuming that something was originally placed in there, and later removed due to the fact that this hack no longer works..
This is a rather interesting source on browser hacks..

ie8 css visibility - fixed div or ul

My problem is that although the site seems to work fine in Chrome, in version of IE8 the fixed bar(div and ul) is invisible. I have tested with X-UA-COMPATIBLE option, IF IE conditional sentence, but I can not solve it at all. I have struggled about 1 week, now I am so tired.
And in IE9, it is much slower than that of chrome.
My site address is http://kyuh.maddesign.co.kr
I do apologize if there is a obvious and simple answer that I've been too stupid to spot.
At last, this problem is fixed.
For a long time of googling and many tries, I have found what is working in my case.
It is.... {zoom: 1} css option.
.ie8 classname {zoom: 1}
If you have same problem, try this one!

CSS2 Selectors not working on IE8

Does anyone know why the following CSS code is not working on IE8, and yet it's working on EVERY OTHER browser?
table.wrap tr:first-child td, table.wrap tr:last-child td { height:20px; }
table.wrap td:first-child, table.wrap td:last-child { width: 20px; }
I understand that IE8 does not support CSS3 features. But I don't think I'm using CSS3 here.
I sincerely appreciate your help.
Thank you so much!
See Quirksmode.org for a full compatibility chart of all CSS features across all the various browsers.
As you'll see from the link above, IE8 does not support the last-child feature.
It does support first-child, but since you've put them together in the same selector, it will throw the whole thing away because it doesn't recognise the last-child part.
Also note note that Quirksmode first-child as being buggy in IE8, so even though it is supported, you may want to be careful about using it.
To solve the problem, you might want to try an IE hack to get it to support extra CSS features.
One that looks quite good is Selectivizr. You might also want to look into Dean Edwards' ie7.js / ie8.js / ie9.js. Both of these aim to patch missing features into older versions of IE. They're not perfect, but they may resolve the problem for you.
Hope that helps.
IE isn't compatible: http://reference.sitepoint.com/css/pseudoclass-lastchild
I believe last-child is a part of css3 and not supported by IE8.

CSS: <h2> is longer in IE7 than in FF

The string "Fernando Salgado Alonso" is much longer in IE7 than in FF.
Why? What can I do to fix it?
That happens because different browsers have slightly different default css for displaying unstyled content.
You need to use a css reset and apply your own rules on top of that. That way you will be sure that it displays the same on all browsers.
Some well know resets are:
http://meyerweb.com/eric/tools/css/reset/
http://code.google.com/p/blueprintcss/
http://developer.yahoo.com/yui/reset/
http://developer.yahoo.com/yui/3/cssreset/
Also have a look at: https://stackoverflow.com/questions/116754/best-css-reset

Can we use CSS 2.1 selectors in practice?

The main culprit behind this question is of course IE6, (almost) everybody agrees that a website should support IE6 since it is used by more than 15% of the visitors (for Yahoo it is still an A-Graded browser).
IE6 doesn't support CSS 2.1, so can we use CSS 2.1 selectors in our stylesheets? Let me give an example:
<body>
<div class="header">
</div>
<div class="content">
<h1>Title</h1>
<p>First paragraph</p>
<p>Second paragraph</p>
</div>
<div class="footer">
</div>
</body>
My css could look like this:
body > div {width: 760px;} /* header content and footer = 760px wide */
h1 + p { margin-top: 5px;} /* the first paragraph after the h1 tag should have a smaller margin */
But IE6 won't understand this, so anyway to be browser compatible I should write it like this:
.header, .content, .footer { width:760px; }
And probably I have to give the first paragraph some class name and define it like that in my css. I could make a IE6 stylesheet specific that defines those rules, but that seems so double up (and still doesn't help in the case of the first paragraph needing a class name)...
If you're serious about supporting IE6 100% then you should avoid using CSS that wont work with it. One of the reasons for using those fancy selectors is to make your life easier, but it's not going to make your life easier if you have to rewrite them anyway for IE6. Finally, fancy selectors like that are slow in Firefox, so maybe just avoid them all together.
If you approach your site design from a progressive enhancement perspective then you should be fine, as more modern browsers will just get a better experience than those using ie6. If you are just looking to cut corners or save time developing, then you have to make a choice if ie6 users are important for your site or not.
I think the concept of "supporting" IE6 is the wrong idea, if we say no to that are we just not going to allow IE6 users access to our site and content? Of course not. So the question really is, how much time do you want to spend making the experience of your site the same for IE6 as other browsers.
My own standpoint is that I "support" IE6, in that a user of IE6 will be able to access all a site's content and all it's features, but they might not get the same visual or interaction experience as a Firefox 3 user.
So to answer your question, yes we can use CSS 2.1 and 3.0 selectors to achieve certain affects, as long as the content is still there for IE6 and with an acceptable visual appearance. What's acceptable will depend on the project (and likely the client!).
Your example was a good showcase of this:
p { margin-top: 10px; }
h1 + p { margin-top: 5px;} /* the first paragraph after the h1 tag should have a smaller margin
Here IE6 will still get the paragraph content, and there will still be all important white-space between them, they just won't get the reduced spacing on the first one. That's a fair compromise IMO for the reduced clutter in your HTML.
Another good example would be rounded corners. You can use -moz-border-radius and -webkit-border-radius to get rounded corners in Firefox and Safari, enhancing the visual experience of your site, but IE users still get the content albeit with plain old square corners (and then there are plenty of JavaScript solutions out there to achieve this for people with JavaScript enabled).
This would all come under the heading Progressive Enhancement
Your browser audience will dictate what you support as you've already stated, even a 'techy' website like w3schools still reports the IE6 market share at 17% for march.
That said, if you're going to support it, obviously you need alternatives.
I read another intersting thread about targetting just IE8 yesterday on stackoverflow, and I suspect supporting each browser will be easier if you can target them. I'm in a .net environment and use the approach that someone posted about half way down that thread so that my CSS selectors can very readily become:
body > div {width: 760px;}
.IE6 div.header, .IE6 div.content, .IE6 div.footer { width: 760px; }
Obviously you would find your own way of targetting browsers, but the CSS will equate to something similar.
It's not to the developer but to the site owner to decide whether to ignore IE6. You should give the site owner the stats for that specific site and then the decision should be made.
If you, or the owner, decide to NOT sacrifice the IE6 folks then it makes no sense whatsoever to use those fancy selectors. There is big downside and no upside as of yet.
For every IE6 user on mine site I got big red alert saying, their browser is ancient, and they should download a real browser, like Firefox ;] That's the solution. If webmasters won't say it to users, who will?
Javascript like JQuery.
CSS Hack. But I don't suggest it. Because it isn't W3C standard.
CSS Classname. By using CSS Classname replaces CSS Selector.

Resources