apply CSS style to element containing certain elements? [duplicate] - css

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>

Related

CSS Conditional h2 text [duplicate]

This question already has answers here:
Is there a CSS selector for elements containing certain text?
(20 answers)
Closed 7 years ago.
Is it possible to set a conditional for the h2 text of a class? the h2 does not have a unique class or id to it, and multiple h2's with a dynamic changing order. So i can't target say the 3rd h2 because its always shifting.
h2.arker.fun.header {
// some statement If h2 text = "Gamer" then set text-transform: lowercase
}
There was once proposed one pseudo class known as :contains to handle this situation. But they haven't added it yet in the current draft of CSS3.
However you may use some Javascript. For Example to deal with this problem
for (var i= document.links.length; i-->0;){
if (/\bSpecificWord\b/i.test(document.links[i].innerHTML)
document.links[i].style.color= 'red'; }
Hope you are not restrained from using Javascript.
As others (#Paulie_D, #Mia Marks, #YourFriend) have already pointed out, there is no CSS < 4 selector based on content and the closest you can get is the attribute selector.
Just a crazy idea:
h2.arker.fun.header:before {
content: attr(data-text);
}
h2.arker.fun.header[data-text="Gamer"] {
text-transform: lowercase;
}
<h2 class="arker fun header" data-text="Gamer"></h2>
don't do it at home...
u can add attribute in that h2 tag
and put the condition like:
h2[if='mytype']{text-transform:lowercase;}

Style parent if child contains X [duplicate]

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.

CSS selectors above a selector [duplicate]

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.

CSS Rule, Never seen before [duplicate]

This question already has answers here:
What does an asterisk (*) do in a CSS selector?
(5 answers)
Closed 9 years ago.
Ok, looking at some code in a CSS Stylesheet assigned to a project, I noticed this:
* {
margin: 0px;
padding: 0px;
text-decoration: none;
}
What does this bit do?? What elements does it effect exactly?? Strange, never heard tell of using the asterisks as a selector or whatever it is supposed to be for. What does the asterisk do exactly?
* affects (I should say "represents the qualified name of") all elements. Per spec:
http://www.w3.org/TR/selectors/#universal-selector
The asterisk is the 'universal selector' and applies the style to all elements on the page. This code will reset everything to have no margins, padding or text-decoration.
Universal selector on W3.org
It is the universal selector, much like the '$' in jQuery. It is usually used in the reset

Css "parent" or "this" selector [duplicate]

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...

Resources