I'm trying to make a div with text within it that is going to change whenever the user hovers over the div area, and not only the p tag. I am however not able to make my solution work.
JSfiddle
div{
height:200px;
width:400px;
background-color:#fbfb2b;
}
div:hover + p{
color:#fff;
}
Take away the + symbol in your css:
JSFiddle
CSS:
div{
height:200px;
width:400px;
background-color:#fbfb2b;
}
div:hover p{
color:#fff;
}
If you were looking for the selector which means direct descendant of div, you wanted >.
eg:
div:hover > p{
/*styles*/
}
Which would have worked for:
<div>
<p>Stuff</p>
</div>
But not
<div>
<span>
<p>Incorrect HTML example</p>
</span>
</div>
With your current CSS, you're trying to select the sibling.
If your HTML was like this:
<div></div>
<p>Some piece of text that is gonna change color when hovering the div</p>
the colour of p would have changed.
Ultimately however, with this specific HTML, you don't even need to include the p in the css and can just do div:hover, but if you're going to have other elements in it, then you should keep the p.
Take out the + P
div:hover {
color: #fff;
}
In your css you have used like this.
div:hover + p{
color:#fff;
}
It means you are applying the hover style for the siblings element not child element. In your case you need to remove + and add just space.
SIBLINGS ELEMENT PROVED HERE
div:hover p{
color:#fff;
}
CHILD ELEMENT PROVED HERE
there is no need to mention the <p> at all you can simply state the colour of all child elements by setting the style on the parent:
JSFiddle
div:hover {
color:#fff;
}
However if you did just want to target the paragraph text only you would use a > (child combinator) to target the P ONLY.
You must read rule about descendant selectors here.
If you need more info about selectors in css
Solved:
div:hover p{
color: #fff;
}
Your Css:
div:hover + p{
color:#fff;
}
+ selector select all <p> elements that are placed immediately after <div> elements.
Demo Fiddle With "+" selector
You can see, In the above fiddle div:hover + p select the outer <p> element, which are placed immediately after <div> elements.
But in your Case you do not need to use + selector. Because you want to select the child element of hovered div.
So, you should try this:
div:hover p{
color:#fff;
}
Working Example
Related
On this answer: https://stackoverflow.com/a/1725486/2519402 to a question, it states:
It sounds like you had h1 .myClass instead of h1.myClass - there's an
important distinction:
h1 .myClass { } /* any element with class="myClass" within an <h1> */
h1.myClass { } /* any <h1> with class="myClass" */
I don't have enough points to ask my question as a comment on that answer.
So, based on what is said above, shouldn't the following code work:
<style>
h3 .h3nobtmgn {
margin-bottom:-20px;
}
</style>
<h3><strong class="h3nobtmgn">Why would I need or want this item?</strong></h3>
Yes, but vertical margin styles won't work on an inline element like <strong>. http://www.w3.org/TR/CSS21/box.html#propdef-margin-top
So your CSS selector will target the correct element but the style you applied will have no effect.
For that to work you can try:
<style>
h3 .h3nobtmgn {
display: block;
margin-bottom:-20px;
}
</style>
<h3><strong class="h3nobtmgn">Why would I need or want this item?</strong></h3>
Yes it does.
h1.myClass would change the appearance of
<h1 class="myClass">...</h1>
And h1 .myClass would change the appearance of
<h1> ... <span class="myClass">...</span></h1>
You will see through http://www.w3schools.com/cssref/trysel.asp that when you are doing div p it will select all p inside of div. So, the answer is yes.
here is a sample: https://jsfiddle.net/r5d0kkb5/
which shows selectors for div p and div .B and also div .A for your thoughts.
Code:
<div class="A">
<p >
A
</p>
<p class="B">
B
</p>
</div>
Css:
div p {
background-color: cyan;
}
div .B{
font-size: 32px;
}
div .A{
color: red;
}
I have read Is there a CSS selector for the first direct child only? and http://www.w3schools.com/cssref/css_selectors.asp
I guess I have to apply the effect to the first-child of the <h1> tag, but I couldn't get it to work. So instead, I'm trying to use the nth-child, but still no luck.
JSFiddle
<section>
<article>
<h1>Test Details</h1>
<ul>
<li>Layer This</li>
<li>Layer That</li>
<li>Layers</li>
</ul>
</article>
</section>
<section>
<article>
<h1>Campaign details</h1>
<p>Text</p>
</article>
</section>
CSS
section {
padding:30px;
}
section article {
background:#EBEBEB;
}
section article h1 {
background:#0C79CB;
padding:10px;
}
/* This is where I am struggling */
section article h1:nth-child(2):before {
background-color:white !important;
content:'';
height:10px;
display:block;
}
If you open the fiddle, you'll note that the header has a blue background, and the content has a grey background. All I'm trying to do is to 'insert' a line of white:
Current:
Desired (note white between the blue and grey)
Please note, I know this is quite trivial if I just add a new div with a class, or even add a border-bottom:solid 5px white; to the <h1> tag, the point is I'm trying to learn about CSS selectors so is this possible using CSS Selectors?
:first-child can be used with or without knowing the element type.
You can either do parent > :first-child to match any first child, whatever node type it is, or you can do parent > p:first-child to only match the first child if it's a p tag.
You can also do parent > p:first-of-type to match the first p inside parent, even if it isn't the first child.
To complete the example you are attempting using pseudo elements:
It is possible to use :nth-child(1) to select the first child like :first-child. Note: In this example it is pointless, as you will only have one <h1> per <article>.
section article h1 is given position: relative and it's position: absolute children will be positioned in relation to it.
The :after is given position: absolute and width: 100% in order to create a line at the bottom of your <h1> background.
Remember that the :after and :before pseudo elements are the equivalent of:
<h1>
<span>This is the :before</span>
I am the heading
<span>This is the :after</span>
</h1>
Have an example
CSS
section article h1 {
background:#0C79CB;
padding:10px 10px 20px;
position: relative;
}
/*
-- Select the first h1 child of article and generate a pseudo element.
*/
section article h1:nth-child(1):after {
background-color:white;
content:'';
height:10px;
width: 100%;
display:block;
position: absolute;
bottom: 0;
left: 0;
}
In your example, you're trying to select the second child of the h1, but that element doesn't have any children, and so it fails. You have to select the second child of the parent of the h1
section article :nth-child(2):before
This has the advantage that you don't put any tag name in there, so it will work even if one day you'll change the h1 to an h2, for example.
That last selector could be rewritten also to
section article :first-child:after
It's not the same thing, but you can also add generated content after an element (and in your case it'll be fine and work in the same way).
Or, if you want to match something against the h1, you need to target its next sibling, using the sibling selector
section article h1 + *:before
This selector will choose the first element (whatever kind it is) that appears right after an h1.
Or, inserting generated content after the element, you can use this
section article h1:after {
background-color: white !important;
content: '';
height: 10px;
display: block;
}
Which, in my opinion, is the simplest thing to do
I have a really simple example below of what I am trying to do, and a fiddle here
<html>
<head>
<style>
#testDiv{
display:block;
}
#hiddenDiv{
display:none;
}
#testDiv:hover + #hiddenDiv{
display:block;
}
</style>
</head>
<body>
<div id="testDiv">
<h1>Hello World</h1>
<div id="hiddenDiv">
<h2>Hidden normally</h2>
</div>
</div>
</body>
</html>
Basically, when I hover over the #testDiv, I want to display #hiddenDiv. I need to put the div I wish to display inside #testDiv or else it won't be accessible when I move the mouse onto it.
Is this possible with CSS, or will I need to use Javascript?
I don't see the display changing in Google Chrome, but in case you have something else changing it in your scenario, you could try this:
http://jsfiddle.net/RDBf9/
What I did was add another style only for the testDiv hover
#testDiv:hover{
display:list-item;
}
Update
In your updated scenario, testDiv became a parent ( congratulations! ) of hiddenDiv.
So you need to select the hiddenDiv that has a hovered parent with id testDiv, like this:
#testDiv:hover #hiddenDiv{
display:block;
}
P.S: In CSS, selectors are readed from right to left. That is the order browsers read them too.
This CSS from your example:
#testDiv:hover + #hiddenDiv{
font-weight:bold;
}
is saying:
any time #testDiv is hovered, set any sibling #hiddenDivs to font-weight: bold;
The + means sibling. It works in your second example because #hiddenDiv2 is a sibling of #testDiv2.
When the target items are nested the CSS selector changes to a child selector. This:
#testDiv:hover #hiddenDiv {
display:block;
}
is saying:
any time #testDiv is hovered, set all child #hiddenDivs to display: block;
I am hiding a div with the class .text with
div.text{
visibility:hidden;
}
This div is surrounded by another div with the class .col3
<div class="col3">
<div class="image-box">
<div class="text"> test </div>
</div>
</div>
I want the visibility to change to "visible" when i hover col3
i tried
.col3:hover + div.text {
visibility:visible;
}
however it doesnt seem to work this way.
strange thing is , when i do
.image-box:hover + div.text{
visibility:visible;
}
It does show the text div when i hover the image box, but thats not what i want, i want it to show when i hover the surrounding div......
any help is welcome...
This should work:
.col3:hover div.text {
visibility:visible;
}
The use of the + selector is incorrect as it targets elements directly following the first element. More information can be found here.
+ in CSS is known as an "adjacent sibling combinator". A sibling is an element which is contained within the same parent as another element. In this case, your .image-box element is a sibling of your .text element. Both of these elements are children of your .col3 element. Your first selector will not select anything as .text isn't a sibling of .col3.
You'll need to use either a descendant combinator:
.col3:hover div.text {
visibility: visible;
}
Or a child combinator:
.col3:hover > div.text {
visibility: visible;
}
The reason why your
.col3:hover + div.text
Isn't working is because you're using an adjacent selector. What you're basically saying is "Take any div-node with the class text, that is lying on the same level as .col3, and do something with it when .col3 is hovered". But there isn't any. The div.text is not on the same level as .col3, but a direct child of it.
What you want to do is:
.col3:hover > div.text {
visibility:visible;
}
Which says "Take any div.text which is a direct child node of .col3, and do something with it, when .col3 is hovered".
I am working in web scraping. Now i am scraping one website and load into my website its working fine. But i need to add css into one div. This div does not contain any id or class.
But my main div contain class. How can i css for my inner div
For example:
<div class="ct-ui-spinner-animation">
<div>
<div> <!-- I Want to call css in this div-->
Sample
</div>
</div>
</div>
I am trying this ways
First try:
.ct-ui-spinner-animation > div
{
color:#fff !important;
}
Second try
.ct-ui-spinner-animation {
color:#fff !important;
}
But not applying css please guide me. How can i do it.
Your code was almost there.
You need to use two child selectors:
.ct-ui-spinner-animation > div > div
{
color:#fff !important;
}
Notice this will be applied for all the div elements which are the grandchildren of any element with the .ct-ui-spinner-animation class.
.ct-ui-spinner-animation > div > div
{
//Styles
}
Can be like this:
.ct-ui-spinner-animation > div > div{
color:#fff !important;
}
http://jsfiddle.net/CCnMe/
or
.ct-ui-spinner-animation div {
color:#fff !important;
}
http://jsfiddle.net/CCnMe/1/
You could give a look at the first-child CSS attribute
.ct-ui-spinner-animation:first-child {
color:#fff !important;
}
Setting a specific class or id for your div is (very) slightly better for the performance too.
Since it's two nodes down, you need to use "div" or * to in the middle.
.ct-ui-spinner-animation > * > div
/* or */
.ct-ui-spinner-animation > div > div
You need to do this to access the second child div:
.ct-ui-spinner-animation div div
{
//CSS goes here;
}