So in FireFox / IE for some reason, my hover keeps blinking, I'm not quite sure why. Is it just better to do my hovers in javascript or is there an easier fix in CSS? Here's a JSFiddle to show what i mean - http://jsfiddle.net/eRBCa/
HTML
<div>
<div id="div1"></div>
<div id="div2">Test Div</div>
</div>
CSS
#div1{
width: 300px;
height: 275px;
background-color: yellow;
}
#div1:hover + #div2{
display: block;
}
#div2{
background-color: grey;
width: 300px;
height: 275px;
margin-top: -275px;
opacity: .9;
display: none;
}
It seems (without getting in to much technical details), that the :hover selector works differently in Chrome than in Firefox or IE. Namely, when #div2 gets visible, it becomes the "hovered" element and #div1 loses the 'hover' "attribute" (in FF or IE). That's what causes the flickering.
You could fix that by changing your CSS like this:
#div1:hover + #div2,
#div2:hover {
display: block;
}
See, also, this short demo.
The jitter effect is created because once you display the overlay, your mouse is now hovering the overlay instead of the original (#div1). You can fix this by looking at whether the parent element is hovered instead.
/* instead of #div1:hover + #div2, where .container is a class on the parent */
.container:hover #div2 {
display: block;
}
http://jsfiddle.net/eRBCa/1/
You can do something like this:
http://jsfiddle.net/eRBCa/4/
HTML
<div>
<div id="div1">
<div class="content">
content here
</div>
</div>
</div>
CSS
#div1{
width: 300px;
height: 275px;
background-color: yellow;
position:relative;
}
#div1:hover{ background-color:red; }
#div1:hover .content {display:block; }
.content {display:none; position:absolute; top:0; left:0}
You should call action earlier in html.
Once you hover div1, div2 comes on top, so you hover div2 and they are adjacent.
http://jsfiddle.net/GPCh3/
<div id="call">
<div id="div1"></div>
<div id="div2">Test Div</div>
</div>
#div1{
width: 300px;
height: 275px;
background-color: yellow;
}
#call:hover #div2{
display: block;
}
#div2{
background-color: grey;
width: 300px;
height: 275px;
margin-top: -275px;
opacity: .9;
display: none;
}
Related
I got two div's and I want to change the color of the first by hovering the second one. I found solutions when the "hovered " come before the objective that its css should be changed, what if the "hovered" come after? What could be done without javascript?
.box, .box-2 {
display: block;
height: 100px;
width: 100px;
margin: 20px;
}
.box {
background-color: red;
}
.box-2 {
background-color: blue;
}
.box-2:hover + .box {
background-color: green;
}
<body>
<div class="wrapper">
<div class="box"></div>
<div class="box-2"></div>
</div>
</body>
A solution is to inverse the order visually and keep the order in the DOM so that you can still use the + selector.
Here is an example with flex:
.wrapper {
display:flex;
flex-direction:column;
}
.box, .box-2 {
display: block;
height: 100px;
width: 100px;
margin: 20px;
}
.box {
background-color: red;
}
.box-2 {
background-color: blue;
order:2; /* this will make box-2 goes after */
}
.box-2:hover + .box {
background-color: green;
}
<body>
<div class="wrapper">
<div class="box-2"></div>
<div class="box"></div>
</div>
</body>
Some related question to get more ideas:
Is there a "previous sibling" CSS selector?
Previous adjacent sibling selector workaround?
While Temani's answer is a great technique, I have an alternative suggestion if you need this to work both ways, using the :not() selector, though it's a tad bit more hit-or-miss because of your margins.
If you check for the hover on the .wrapper element, you can then style your box when it isn't hovered, like so:
.wrapper:hover > .box:not(:hover) {
background-color: green;
}
I have two divs next to each other. The div on the right is 300px x 335px. The div on the left goes all the way down the page. I want the width of the left div to go all the way until the right div. Then under the right div, it takes up the whole width of the page. Is this possible?
div elements are block level elements. So they are like square blocks. No, they can't work as you ask. However, you might Google for CSS Shapes to see if it can do what you wish but it's not available in all browsers and still isn't exactly the same as you request.
Here is some option either you can add min-width to the short div and long div to extend it. or you can add a background-color body to fake the illusion of it. but like Rob said there is no good way that can work out.
.short {
width: 100px; height: 100px;
background:red;
float:left;
//min-height: 500px;
}
.long {
width: 100px; height: 500px;
background:blue;
float:left;
//min-height: 500px;
}
.width {
width: 100%;
height: 200px;
background:yellow;
}
.clearfix {
overflow: auto;
zoom: 1;
}
body {
// background-color: red;
}
<div class="clearfix">
<div class="short"></div>
<div class="long"></div>
</div>
<div class="width"></div>
That is not possible, although you could always put another div under the one on the right and set the margin so that it looks like it's part of the one on the left.
This is one of the method to achieve what you want
CSS
#left1 {
margin-right: 300px;
height: 335px;
background: #aaa;
}
#right {
width: 300px;
height: 335px;
float: right;
}
#left2 {
background: #aaa;
border: 1px soild #000;
min-height: 300px;
}
<div id="right"></div>
<div id="left1"></div>
<div id="left2"></div>
I've been trying to figure this out for a long time and I can't seem to get it. I have the following HTML:
<div class="b">
<button>Show when I hover</button>
</div>
<div class="A">When I hover over this the background should change</div>
with the corresponding CSS:
.b {
float: right;
display: none;
}
.A {
float: left;
display: inline;
width: 1000px;
}
.A:hover {
background: gray;
}
.A:hover + .b {
display: block;
}
What I'm trying to do is whenever I hover over A the b div and corresponding button should show. In addition, I want it such that when my mouse is on the button, the background of A is still gray as if I was hovering over it. I can't seem to figure this out. Any ideas?
Relevant JSFiddle: http://jsfiddle.net/sn19k1wz/3/
You can do this by changing position of A and B
<div class="A">When I hover over this the background should change</div>
<div class="b">
<button>Show when I hover</button>
</div>
Change the div positions, hovering div tag should be the first one
Like this :
<div class="A">When I hover over this the background should change</div>
<div class="b">
<button>Show when I hover</button>
</div>
Demo URL
Try like this: Demo
.A {
display: inline-block;
width: 1000px;
position: relative;
}
.b {
display: none;
position: absolute;
z-index: 999;
right: 0;
top:6px;
}
.A:hover {
background: gray;
}
.A:hover + .b {
display: block;
background: red;
cursor:pointer;
}
I have 2 separate div tags. I want div2 to appear when someone hovers over div1.
This is what I am trying to achieve..
HTML
<div class="div1">
HOVER TO ADD DETAILS
</div>
<div class="div2">
<input type="image" src="img1.png" name="btn1" value="btn1">
<input type="image" src="img2.png" name="btn1" value="btn1">
<input type="image" src="img3.png" name="btn1" value="btn1">
</div>
CSS
.div1{ background-color:#bcbcbc; width: 400px; height:45px;}
.div2{ display:none; position:relative; width: 50px; margin:0 auto;}
.div1 > .div2 {display:block; }
This is what I am trying.. It is not working. I am not able to show div2 when I hover div1. I've searched over the net but not able to find what I want. any idea how to do this. I would appreciate a css way of doing it. Thanks in advance..
Make .div1 the child of .div2 like:
<div class='div1'><div class='div2'>...</div></div>
And then the CSS:
.div2 {
display: none;
}
.div1:hover > .div2 {
display: block;
}
.div2:hover {
display: block;
}
And also add your CSS.
Try:
.div1:hover + .div2 {display:block; }
Try this
.div2 {
display: none;
}
.div1:hover ~ .div2 {
display: block;
}
.div2:hover {
display: block;
}
Also it is better to use IDs like #div1 and #div2 as classes are to specify a style for more than one elements.
.div1 {
background-color:#bcbcbc;
width: 400px;
height:45px;
}
.div2 {
display:none;
position:relative;
width: 50px;
margin:0 auto;
}
.div1:hover~.div2{
display: block;
}
Try this(it fixes the flickering issue and you can interact with the second div as well):
.div1 {
z-index:1;
}
.div1:hover {
z-index:0;
}
.div2 {
z-index: 0;
}
.div2:hover {
z-index:1;
}
DEMO
(this basically switches the z-indices of the two divs when you hover over .div1)
You should get the effect by using ~ like so:
.div1:hover ~ .div2 { display: block; }
<div id="home">
<div id="logo"></div>
<div id="foot">
<div id="one">
<span id="aaa" class="test">aaa</span>
</div>
<div id="two">
<span id="bbb" class="test">bbb</span>
</div>
</div>
</div>
#home {
width: 400px;
height: 400px;
}
#logo {
height: 200px;
background-color: green;
}
#foot {
height: 200px;
}
#one {
height: 200px;
width: 200px;
background-color: red;
}
#two {
height: 200px;
width: 200px;
background-color: blue;
float: left;
}
.test {
margin-top: 50px;
margin-left: 50px;
background-color: yellow;
}
why in this example float: left doesnt working? and why margin-top set position of #home and not of parents?
LIVE: http://jsfiddle.net/tLuTS/10/
Floating doesn't work in your example because you need to float both elements that you want on the same line.
So I've updated your example with #one and #two floated left. Also added some IE float fixes. http://jsfiddle.net/tLuTS/11/
I'm not sure what you're trying to achieve using margin-top.
Both elements need to be floated, and the second one should have clear:both set.
Example
Just add this
#one {
height: 200px;
width: 200px;
background-color: red;
float:left;
}
Here is the updated version on jsfiddle.net.
This is because your inline CSS "test" . For Span Id="bbb", there are two CSS define one is "test" and other is "two". Priority of "test" is more so float is not working .
I'm assuming you want one and two both in the footer. So you have to apply
float: left;
to one as well. About the margins: I can only see margins applied to the text with yellow background. These are <span>s, so inline elements. Make these block level elements, like <p>, for the margin to have effect.