This question already has answers here:
How to select a text node with CSS
(6 answers)
Closed 7 years ago.
How do I select (hide) the date in the following code?
<div class="myDiv">
<h5>
<strong>News header</strong> 2015-03-05
</h5>
</div>
The problem is that the date has no id, no class nor a paragraph tag. Perhaps there's a way to use a sibling selector or similar?
I don't have access to the html, just the css.
You can target it with some styling:
.myDiv h5 {
color: red;
}
.myDiv h5 * {
color: lime;
}
<div class="myDiv">
<h5>
<strong>News header</strong> 2015-03-05
</h5>
</div>
However, your edit asks for showing/hiding of the date!
Unfortunately this cannot be achieved because hiding a parent element will also hide all the child elements within i.e. hiding .mdDiv h5 will hide everything inside it.
The only way to hide it separately is to place it within it's own element e.g.
<div class="myDiv">
<h5>
<strong>News header</strong> <span>2015-03-05</span>
</h5>
</div>
Then you can attack it with .myDiv h5 span { display: none }
not too sure of you try to achieve. if it is about styling color or bg, then overwrite css for the strong element:
h5 {
color:purple;
background:yellow;
display:table;
}
h5 strong {
color:yellow;
background:purple;
}
<div class="myDiv">
<h5>
<strong>News header</strong> 2015-03-05
</h5>
</div>
Related
Let's say I have several <p> elements inside a <div> like this:
HTML
<div>
<p></p>
<p></p>
<p></p> <!-- I want to select the "last" element, <p> or not <p> -->
</div>
Now, say I want to style the last element, <p> or not <p>. One way to do that is to use the last-child selector on the <p>'s like this:
CSS
<style>
p:last-child {
/* Awesome styles go here */
}
</style>
Question
How do I style the last element (<p> or not <p>) by referencing <div> but NOT <p>?
Live Code Example
p:last-child {
color: red;
font-weight: bold
}
<div>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p> <!-- I want to select the "last" element, <p> or not <p> -->
</div>
You can use > in your selector to describe direct children:
div > p:last-child {
}
This CSS will affect only the p tags which are the last child of div tag.
If, for some reason, you don't want to reference p at all, you can use * selector:
div > *:last-child {
}
It will affect the last child of the div whatever it is.
For example:
div > *:last-child {
font-weight: bold;
color: red;
}
<div>
<p>1</p>
<p>2</p>
<p>3</p>
</div>
<div>
1
2
3
</div>
<div>
<span>1</span>
<i>2</i>
<i>3</i>
</div>
This question already has answers here:
CSS Child vs Descendant selectors
(8 answers)
Closed 9 years ago.
I've seen two example,
.someclass > .inner{...}
and
.someclass .inner{...}
works the same way. Is there are any difference between them which i'm not seeing?
The first applies only to immediate children. The second, to any descendant.
So given this CSS:
.someclass > .inner { color: red }
.someclass .inner { font-weight: bold }
the following applies:
<div class="someclass">
<div class="inner">
Bold and red
</div>
<div>
<div class="inner">
Just bold.
</div>
</div>
</div>
.someclass>.inner {
color: red
}
.someclass .inner {
font-weight: bold
}
<div class="someclass">
<div class="inner">
Bold and red
</div>
<div>
<div class="inner">
Just bold.
</div>
</div>
</div>
.someclass > .inner{...}
- only apply to ".inner" that are direct children to ".someclass".
.someclass .inner{...} - applies to any ".inner" that are inside ".someclass", even if there are elements between them.
My code can add divs as child or sibling, I've created two classes and two :hover for each class but when I put the mouse on a child the parent hover is activated as well.
.Group {
background-color: white;
}
.Group2 {
background-color: white;
}
.Group:hover {
background-color: yellow;
}
.Group2:hover {
background-color: red;
}
<div class="Group">
root
<div class="Group">1st child
<div class="Group2">2nd child
<div class="Group">3rd child</div>
</div>
</div>
<div class="Group">1st child</div>
</div>
If you hover over a child element, you're also hovering over the parent, there's no way around that.
What you could do is set a different or additional class on the 1st child elements that don't have further children.
Highlighting background colors in nested blocks when hovering
If you are trying to activate the background colors of the nested div blocks, I think you might be looking for the following.
For the HTML, use distinct class names to identify each layer of nested div blocks:
<div class="Group">root
<div class="Group1">1st child
<div class="Group2">2nd child
<div class="Group3">3rd child</div>
</div>
</div>
<div class="Group1">1st child</div>
</div>
and the CSS:
.Group, .Group1, .Group2, .Group3 {
background-color:transparent;
}
.Group:hover {
background-color:yellow;
}
.Group1:hover {
background-color:pink;
}
.Group2:hover {
background-color:red;
}
.Group3:hover {
background-color:orange;
}
Demo fiddle: http://jsfiddle.net/audetwebdesign/Scr9G/
As you mouse over each nested div successively, the background color changes in sequence.
If you are trying to target a nested element with :hover without the effect bubbling up through the parent/ancestor blocks, you will need to use JavaScript/jQuery to create the selection rules that you need.
Quirky Hack Using <p> Tags
The following construction exhibits the behavior that the OP would like to see:
<p class="Group">root
<p class="Group">1st child
<p class="Group2">2nd child
<p class="Group">3rd child</p>
</p>
</p>
<p class="Group">1st child</p>
</p>
and the CSS is as before:
.Group {
background-color:white;
}
.Group2 {
background-color:white;
}
.Group:hover {
background-color:yellow;
}
.Group2:hover {
background-color:red;
}
Second demo fiddle: http://jsfiddle.net/audetwebdesign/cf2mn/
In this case, the OP was trying to nest <p> tags, which actually do not work like nesting other block elements like <div>.
When using <p> tags, the closing </p> tag is optional if followed by other flow elements like p, div, ul and so on.
In this case, the HTML snippet show above is equivalent to:
<p class="Group">root</p>
<p class="Group">1st child</p>
<p class="Group2">2nd child</p>
<p class="Group">3rd child</p>
<p class="Group">1st child</p>
which means that all the p tabs are siblings and there are no parent-child relationships, which is why the CSS appears to be working as the OP desired.
If div tags had been used instead of p tags, the resulting DOM would have exhibited the parent-child relationships and the CSS would have shown the original behavior that the OP did not want.
Using the p tags may give the desired effect for the CSS, but it works only because the DOM elements are siblings instead of parent-child. (In addition, the nested p tags will not validate.)
It is worth noting that:
CSS 2.1 does not define if the parent of an element that is ':active' or ':hover' is also in that state.
so it is better not to rely on the state of the parent element when apply a pseudo-element on a child element.
References:
About :hover: http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
About optional closing of p tag: http://www.w3.org/TR/html-markup/p.html#p
There's no way to prevent parent elements to get background color.. rather use this code
<div class="root">
Main Root
<div class="group">
First One
</div>
<div class="group2">
Second One
</div>
<div class="group">
Third One
</div>
.group:hover {
background: yellow;
}
.group2:hover {
background: red;
}
SEE THE DEMO HERE
Give separate ids to the divs and when a child div is hovered over, change its background image color and remove the background color of the parent all at the same time. I guess that is the only way around this
The simplest and best way is probably to use any other element, li for instance.
css:
.Group, .Group2, .Group3
{
background:white;
display: block;
list-style: none;
}
.parent
{
margin: 0;
display: block;
padding: 0;
}
.Group:hover
{
background: yellow;
}
.Group2:hover
{
background: red;
}
.Group3:hover
{
background: yellow;
}
html:
<ul class="parent" >
<li class="Group" >root
<li class="Group">1st child
<li class="Group2">2nd child
<li class="Group3">3rd child</li>
</li >
</li >
<li class="Group">1st child</li >
</li>
</ul>
You should add sub-element for content and use element+element pseudo selector.
.group-content:hover {
background-color: yellow;
}
.group-content:hover + .group {
background-color: red;
}
<div class="group">
<div class="group-content">1st child</div>
<div class="group">
<div class="group-content">2nd child</div>
<div class="group">
<div class="group-content">3nd child</div>
</div>
</div>
</div>
<div class="group">
<div class="group-content">1st child</div>
</div>
We have a DOM like this:
<div class="outer">
<div class="inner"> <!--// No "copyright" in this node //-->
<div class="content">...</div>
</div>
<div class="inner">
<div class="content">...</div>
<div class="copyright">...</div> <!--// DISPLAY THIS ONE //-->
</div>
<div class="inner">
<div class="content">...</div>
<div class="content">...</div>
<div class="content">...</div>
<div class="copyright">...</div> <!--// Hide this one //-->
</div>
<div class="inner">
<div class="content">...</div>
<div class="content">...</div>
<div class="copyright">...</div> <!--// Hide this one too, etc. //-->
</div>
<!--// etc. //-->
</div>
All elements with class "copyright" must be hidden, with exception of the very first one.
We tried to apply this approach, but unfortunately with no success. It must be a CSS only solution. Any idea?
Thanks for your help!
In this case, each .copyright is the first and only one of its kind in .inner, so you need to select by .inner instead. If you don't need to apply any special rules to the first child, you don't need to use the approach I describe in that other question; simply use this to hide the other elements:
.inner ~ .inner .copyright {
display: none;
}
This is still the top answer on Google for "css select first occurrence of class" so adding the simple technique I found to work.
This solution doesn't specifically solve the OP but does allow you to select the first element with a class amongst siblings.
You can use a combination of the sibling and not selectors as shown in this JSFiddle
For example:
.my-class:not(.my-class ~ .my-class) {
background: red;
}
How does this work?
The sibling selector (~) selects elements which are somewhere after other elements.
So this would select every element except the first one:
.my-class ~ .my-class {
background: red;
}
We then just use the :not selector to reverse this, i.e. select only the first element.
I have only tested this on Chrome but think it should work on most modern browsers.
Try this one JSfiddle
div.inner > .copyright { display:none; }
div.inner:first-child .copyright { display:block; background:#000; }
I have the following, simplified, code:
<div id="content">
<p>text</p>
<div id="container">
<div id="col1">
<p>text</p>
</div>
<div id="col2">
<p>text</p>
</div>
</div>
<p>text</p>
</div>
</div>
Whenever I set some values to the #content p element in the CSS file, the changes also apply to the #col1 p and #col2 p.
What I would like to do is select only the children p elements of the #content div and not select the grandchildren p elements.
I imagine that one way of doing it would be to add a class to the first children and then apply properties throught it.
Is there any better way of doing it in either CSS2 or CSS3?
Use the CSS Greater than sign > (Child selectors):
#content > p
A child selector matches when an element is the child of some element.
You can also set another style for the deeper <p> elements that override the one you already specified. Like so:
#content p { color: red; }
#content div p { color: black; }