Reputation: 444
I have two boxes that one is hidden and one is visible. I want to make it work in a way that when the default box is hover, it fades out and the box with display none gets visible.
Here is the code that doesnt work the way I want it because the once box one is hover, it is still visible, while i want it to go display: none
.
.box1, .box2{
width:100px;
height 100px;
border:1px solid black;
text-align:center;
}
.box1{
display: block;
}
.box2{
display: none;
}
.box1:hover ~ .box2{
display:block;
}
<div class="box1">
<p>data 1</p>
</div>
<div class="box2">
<p>data 2</p>
</div>
It could be better if it is animated once the boxes switch.
Similar questions have been asked but they all asked for JavaScript. I prefer CSS only.
Any idea? Thanks in advance.
Upvotes: 4
Views: 7100
Reputation: 3213
Referring to Mr @Swellar answer's, I noticed that you would like to add a transition effect.
hence you have to set position: absolute
for both boxses,
then onhover you will switch between opacity: 0;visibility: hidden
and opacity: 1;visibility: visible
instead of display: none
and display: block
, see the following snippet for more details:
.box1,
.box2 {
width: 100px;
height: 100px;
border: 1px solid black;
text-align: center;
position: absolute;
transition: 600ms ease;;
}
.box1 {
opacity: 1;
visibility: visible;
background-color: red;
}
.box2 {
opacity: 0;
visibility: hidden;
background-color: green;
}
.wrapper {
width: auto;
display: inline-block;
}
.wrapper:hover .box1 {
opacity: 0;
visibility: hidden;
}
.wrapper:hover .box2 {
opacity: 1;
visibility: visible;
}
<div class="wrapper">
<div class="box1">
<p>data 1</p>
</div>
<div class="box2">
<p>data 2</p>
</div>
</div>
Upvotes: 1
Reputation: 4251
Try this. I have added one extra div to wrap box1
and box2
and some css.
.box1, .box2{
width:100px;
height: 100px;
border:1px solid black;
text-align:center;
transition:0.5s;
}
.box2 {
opacity: 0;
position: absolute;
top: 0;
left: 0;
}
.box-wrap:hover .box2{
opacity:1;
transform:translateY(0px);
}
.box-wrap:hover .box1{
opacity:0;
height:0;
}
.box-wrap{
position:relative;
display:inline-block;
vertical-align: top;
}
<div class="box-wrap">
<div class="box1">
<p>data 1</p>
</div>
<div class="box2">
<p>data 2</p>
</div>
</div>
Upvotes: 3
Reputation: 6797
This will work fine for you.
I have used some transition to go with your need.
your need can only be achieved by wrapping a div
around your .box1
and .box2
.
.box1,
.box2 {
width: 100px;
height 100px;
border: 1px solid black;
text-align: center;
overflow: hidden;
max-height: 200px;
opacity: 1;
transition: all ease-in-out 0.4s;
}
.box1 {
display: block;
}
.box2 {
max-height: 0px;
opacity: 0;
}
.main_box{
display:inline-block;
}
.main_box:hover .box1{
max-height: 0px;
opacity: 0;
}
.main_box:hover .box2{
max-height: 200px;
opacity: 1;
}
<div class="main_box">
<div class="box1">
<p>data 1</p>
</div>
<div class="box2">
<p>data 2</p>
</div>
</div>
Hope this was helpfull for you.
Upvotes: 3
Reputation: 2261
Try this one.
.box1, .box2{
width:100px;
height 100px;
border:1px solid black;
text-align:center;
float: left;
}
.box1{
display: block;
background: red;
}
.box2{
display: none;
margin-left: -102px;
background: green;
}
.box1:hover{
opacity:0;
}
.box1:hover ~ .box2{
display:block;
}
<div class="box1">
<p>data 1</p>
</div>
<div class="box2">
<p>data 2</p>
</div>
Upvotes: 0
Reputation: 5401
You can add a wrapper to the two div with the following css:
.wrapper {
width: auto;
display: inline-block;
background-color: red; //for visuals
}
And you seem to forgot to hide .box1
.box1,
.box2 {
width: 100px;
height: 100px;
border: 1px solid black;
text-align: center;
}
.box1 {
display: block;
}
.box2 {
display: none;
}
.wrapper {
width: auto;
display: inline-block;
background-color: red;
}
.wrapper:hover .box1 {
display: none;
}
.wrapper:hover .box2 {
display: block;
}
<div class="wrapper">
<div class="box1">
<p>data 1</p>
</div>
<div class="box2">
<p>data 2</p>
</div>
</div>
Upvotes: 5