View Full Version : Precedent of class or id as parent in descendant selectors
jscheuer1
04-13-2017, 07:40 PM
Wow, that was a mouthful, and I'm not even sure I phrased that correctly for what I'm asking. An example would probably be more clear. Say I have a div:
<div id="slider" class="full">bunch of stuff here, including other elements like divs and img elements</div>
So now, in the stylesheet, which has precedent? Is it:
#slider
or
.full
or just whichever comes later in the stylesheet?
Further, if I want to - say control the css a contained div which is the first div child of the above div code, which has precedence? Is it:
#slider > div
or:
.full > div
or just whichever comes later in the stylesheet?
I'll probably have to experiment to see for certain, may even vary with some browser(s). In my experience I believe class has precedence, though one might easily assume id would. It's more unique.
jscheuer1
04-13-2017, 07:54 PM
Looks like it is id:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#slider > div {background: red;}
.full > div {background: black;}
</style>
</head>
<body>
<div id="slider" class="full"><div>Hello World!</div></div>
</body>
</html>
The bg is red.
But both together are king:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#slider.full > div {background: black;}
#slider > div {background: red;}
</style>
</head>
<body>
<div id="slider" class="full"><div>Hello World!</div></div>
</body>
</html>
bg is black.
Beverleyh
04-14-2017, 08:40 AM
Hi John,
An id always trumps a class (or multiple chained classes) as a CSS selector. It comes down to specificity, with different selectors having more "weight" than others. There's a point system that calculates which selectors (or combinations of selectors) have the highest value, but generally the order comes down to (from highest to lowest);
- inline style attribute
- id
- class, pseudo-class, attribute
- element
The universal selector (*) has no weight. And the !important override, while not a selector, out-weighs everything! (even inline attribute styles) :)
There's a great explanation for everything here https://css-tricks.com/specifics-on-css-specificity/
molendijk
04-15-2017, 09:19 PM
And js always wins (except when css has !important to prevent this). So the background is yellow in:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#slider > div {background: red;}
</style>
</head>
<body>
<div id="slider"><div>Hello World!</div></div>
<script>document.getElementById('slider').childNodes[0].style.background='yellow'</script>
</body>
</html>
EDIT:
And this is not because the javascript comes last, since the background is also yellow in:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>document.write('<div id="slider"><div>Hello World!<\/div><\/div>')</script>
<script>document.getElementById('slider').childNodes[0].style.background='yellow'</script>
<style type="text/css">
#slider > div {background: red;}
</style>
</head>
<body>
</body>
</html>
jscheuer1
04-16-2017, 02:15 AM
Right Arie. But I'm trying to write/leave as much to the stylesheet as possible as I go along here apropos to our previous discussion about slideshows as regards the new one I'm putting together. Not finished, but you can see a working demo here:
http://jscheuer1.com/garden/images/cssassist3f.htm
May be updated.
molendijk
04-16-2017, 10:49 AM
Thanks John, the slider looks very nice.
I noticed that at about 700px width and less the images are too much cropped due to background-size: cover. So it might be a good idea to add something like:
@media screen and (max-width: 700px) {
#slider.full > div > div, #slider.full > div > a > div {background-size: contain; background-repeat: no-repeat; background-color: black}
}
Just an idea.
jscheuer1
04-16-2017, 04:02 PM
That's a good idea. I think I may leave that for customization on the designer's part, or include it and note it as "configurable", because that would likely change depending upon the images. There really is no problem with cropping if you toggle out of full screen mode in that example. There's still a lot more I want to do with this code (have already moved on to beta 3fa, which will have more changes and probably later versions before its alpha) before making it officially available, though I have no problem with folks using it as is for now and/or playing with it to see what it can do/be modded for, as long as the credit remains.
Decided to include a version of that:
@media screen and (max-width: 700px) { /* optional and configurable */
#slider.full > div > div, #slider.full > div > a > div {
background-size: contain;
background-repeat: no-repeat;
background-color: #fff;
}
#slider.fullScreen > div > div, #slider.fullScreen > div > a > div {
background-color: #666;
}
}
Which necessitated adding the highlighted:
#slider .descwrap {
border: 0 solid #aaa; /* optional decorations */
border-radius: 0 0 25px 25px; /* optional decorations */
position: absolute; /* required */
bottom: 0.5ex; /* required */
transition: height 1s; /* required for animation the s is seconds, adjust for longer or shorter animation of descriptions, decimals allowed */
width: 100%; /* required */
overflow: hidden; /* required */
background: transparent !important; /* recommended */
}
Now that I've seen it both ways, I'm thinking it might be better to leave it out and let the user shorten the window (or on mobile, switch to landscape) to compensate. Seems to merit mention in the documentation though, not quite to that stage yet.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.