This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 8 years ago.
I was wondering if there is a CSS-only way of styling an element based on it's child?
In my present situation, i've got an ul with a lot of li's in it. None of them have a separate identifying class, but only one of them got an iFrame (youtube video) inside of it. That li item, I want to style.
The CSS would somewhat be like
ul li:child-is[iframe] {
// styling
}
Is this possible?
Not today.
In the next occurrence of CSS, CSS4, you'll be able to precise the subject in a selector using an exclamation mark :
ul !li iframe {
// styling applies to the li element
}
but there's no pure CSS solution today.
Related
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 2 years ago.
I found there is no official parent selector, but there are pseudo-classes like :has, so I was wondering how one would go about applying CSS to the following kind of paragraph:
<p><em>[11] Source Text</em></p>
I'd need to apply CSS to this type of paragraph, which is one in a list of several, and the only thing they have in common (besides the structure) is the '_ftn' part of the name attribute. Unfortunately, these paragraphs are also not wrapped in another element I could build the CSS on - that's how it comes out of a plugin...
Thanks for any inputs from you CSS gurus!
The :has() CSS selector is currently not supported by any browser (reference).
However, if you want to target anchor elements which are placed within a p > em tag, you can do something like this:
Note the attribute selector [name^="_ftn"] on the a element.
p em a[name^="_ftn"] {
color: green;
font-size: 18px;
text-decoration: none;
font-weight: 700;
}
<p><em>[11] Source Text</em></p>
This question already has answers here:
not:first-child selector
(12 answers)
Closed 6 years ago.
how can I say that only the first thing should be shown?
.box:nth-of-type(2) {
display:none;}
makes only the second thing hidden, how can I say "hide everything except the first one"?
You can use the :not selector with :first-child.
.box:not(:first-child){display:none;}
OR
You can select the first element using the :first-child Selector and give it the properties of display:block;. And add the display:none; property to the other elements
.box:first-child{display:block;}
This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
Closed 8 years ago.
I have a li list with 2 items in it, which are "RMS" and "RPM". When you rollover RMS the background changes and the RPM is targeted with the following to make the RPM element change to red. I need the same thing to work for the opposite side as well (rollover RPM and the background changes but RMS isn't targeted). I can't seem to get it to work for some reason.
li:hover + li#RPM {
background-color:red;
}
li:hover + li#RMS {
background-color: #CCC;
}
JSFiddle Demo
The + CSS selector looks for the next sibling as defined. There is no previous element CSS selector. You'll probably have to accomplish this using Javascript or jQuery.
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 9 years ago.
Is there a way to write css so it would select a div above a selector you already known.
For example.
#modal .container .login{
//style
}
so the above code will look for an ID of modal > classname of container > classname of login.
And style the .login.
Now is is possible to have it go the reverse. So style the #modal only if it has a child of .login and .container
Thanks
Short answer? no. (in the current CSS spec) as already answered here.
Is there a CSS parent selector?
but, I can give you a little trick.
create a special CSS rule (a class) with your special styling for the 'parent'.
then, with JQuery, on document.ready, check for all the elements in the DOM who meets your requirement (in your case: has a child of some class), and dynamically add the special CSS class.
It's not a perfect solution, but can be helpful in some cases.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a CSS parent selector?
Is there some kind of .this selector in css?
Or maybe parent selector?
Thanks
There is no parent selector existing in CSS.
The only thing existing that comes somehow near to that is this:
a < img { border: none; }
But this is not a parent selector. This would select a tags but only if they contained an img tag.
There is no parent selector in CSS. See Is there a CSS parent selector?.
Why would you need a this selector? It would be redundant, because it implies you have already selected the target element.
well, I don't know if you mean this, but you can always start from the "parent" element, and then go deeper... e.g.
div#main_div div#sub_div1 div#sub_div2 span a {
/* your style here */
}
your style will then apply to the a-element. However, only if you have this order (sub_div2 has to be in sub_div1, which has to be in main_div) in your html code as well...