Skip to content Skip to sidebar Skip to footer

How To Replace On Div With Another On Hover (css Only)

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.

Solution 1:

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;
}
<divclass="wrapper"><divclass="box1"><p>data 1</p></div><divclass="box2"><p>data 2</p></div></div>

Solution 2:

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;
  height100px;
  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;
}
<divclass="main_box"><divclass="box1"><p>data 1</p></div><divclass="box2"><p>data 2</p></div></div>

Hope this was helpfull for you.

Solution 3:

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;
}
<divclass="box-wrap"><divclass="box1"><p>data 1</p></div><divclass="box2"><p>data 2</p></div></div>

Solution 4:

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;
}
<divclass="wrapper"><divclass="box1"><p>data 1</p></div><divclass="box2"><p>data 2</p></div></div>

Solution 5:

Try this one.

.box1, .box2{
  width:100px;
  height100px;
  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;
}
<divclass="box1"><p>data 1</p></div><divclass="box2"><p>data 2</p></div>

Post a Comment for "How To Replace On Div With Another On Hover (css Only)"