Zinan Nadeem
Zinan Nadeem

Reputation: 199

element hover not working properly in css3 fade effects

I am working on a simple project where a single block element hover will show 4x zoom element. I did it through pure css and css3 transition. See the jsfiddle demo . There will be four element , each has different hover element. But when I hover on it only one hover element is showing though it is not associate with that block or element.

Check the demo to make yourself an opinion.

.main {
  position: relative;
  width: 300px;
  overflow: hidden
}

.main a {
  width: 50%;
  height: 50%;
  float: left;
}

.main a .child {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: gray;
  filter: alpha(opacity=0);
  opacity: 0;
  -webkit-transition: opacity 0.5s ease-out;
  -moz-transition: opacity 0.5s ease-out;
  -ms-transition: opacity 0.5s ease-out;
  -o-transition: opacity 0.5s ease-out;
  transition: opacity 0.5s ease-out;
}

.main a:hover .child {
  -webkit-transition: opacity 0.2s ease-in;
  -moz-transition: opacity 0.2s ease-in;
  -ms-transition: opacity 0.2s ease-in;
  -o-transition: opacity 0.2s ease-in;
  transition: opacity 0.2s ease-in;
  zoom: 1;
  filter: alpha(opacity=100);
  opacity: 1.0;
}
<div class="main">
  <a href="">
    <img src="http://placehold.it/150x150">
    <div class="child">
      <h4>1.Text</h4>
    </div>
  </a>
  <a href="">
    <img src="http://placehold.it/150x150">
    <div class="child">
      <h4>2.Text</h4>
    </div>
  </a>
  <a href="">
    <img src="http://placehold.it/150x150">
    <div class="child">
      <h4>3.Text</h4>
    </div>
  </a>
  <a href="">
    <img src="http://placehold.it/150x150">
    <div class="child">
      <h4>4.Text</h4>
    </div>
  </a>

</div>

Demo project on JSFiddle

Important note: if i use display none or block in hover instate the css3 transition then it is working fine, But i need the fade effect.

Upvotes: 5

Views: 126

Answers (3)

Ferrmolina
Ferrmolina

Reputation: 2771

You can try using ::before

HTML

<ul id="content-ul">
    <li>
        <a href="#" target="_blank" titulo="Lorem 1">
            <img src="http://placehold.it/150x150" />
        </a>
    </li>
    <li>
        <a href="#" target="_blank" titulo="Lorem 2">
            <img src="http://placehold.it/150x150" />
        </a>
    </li>
    <li>
        <a href="#" target="_blank" titulo="Lorem 3">
            <img src="http://placehold.it/150x150" />
        </a>
    </li>
    <li>
        <a href="#" target="_blank" titulo="Lorem 4">
            <img src="http://placehold.it/150x150" />
        </a>
    </li>
</ul>

CSS:

#content-ul{
    display: block;
    width: 340px;
    margin: auto;
    overflow: hidden;
}
#content-ul li,#content-ul li a{
    display: block;
    position: relative;
    width: 150px;
    height: 150px;
    float: left;
    overflow: hidden;
}
#content-ul li{
    margin: 10px 10px ;
}
#content-ul li a{
    text-decoration: none;
    color: #fff;
    font-size: 14px;
    text-shadow: 0 1px 0 #000;
    font-weight: bold;
    border-radius: 4px;
}
#content-ul li a img{
    border-radius: 4px;

}
#content-ul li a:before{
    visibility: hidden;
    content: attr(titulo);
    position: absolute;
    line-height: 250px;
    float: left;
    width: 150px;
    height: 150px;
    background: rgba(0,0,50,.5);
    border-radius: 4px;
    overflow: hidden;
    text-align: center;
    font-size: 25px;

    filter: alpha(opacity=0);
    opacity: 0;
    -webkit-transition: opacity 0.5s ease-out;
    -moz-transition: opacity 0.5s ease-out;
    -ms-transition: opacity 0.5s ease-out;
    -o-transition: opacity 0.5s ease-out;
    transition: opacity 0.5s ease-out;

}
#content-ul li a:hover::before{
    -webkit-transition: opacity 0.2s ease-in;
    -moz-transition: opacity 0.2s ease-in;
    -ms-transition: opacity 0.2s ease-in;
    -o-transition: opacity 0.2s ease-in;
    transition: opacity 0.2s ease-in; 
    zoom: 1;
    filter: alpha(opacity=100);
    opacity: 1.0;
    visibility: visible;
}

Working Demo


Here an update based on your code:

HTML:

 <div class="main">
    <a href="" content="1Lorem Ipsum is simply dummy text of the printing and typesetting industry.">
        <img src="http://placehold.it/150x150">
    </a>
    <a href="" content="2Lorem Ipsum is simply dummy text of the printing and typesetting industry.">
        <img src="http://placehold.it/150x150">
    </a>
    <a href="" content="3Lorem Ipsum is simply dummy text of the printing and typesetting industry.">
        <img src="http://placehold.it/150x150">
    </a>
    <a href="" content="4Lorem Ipsum is simply dummy text of the printing and typesetting industry.">
        <img src="http://placehold.it/150x150">
    </a>
</div>

CSS:

.main{
    position: relative;
    width: 300px;
    overflow:hidden
}
.main a{
    width: 50%;
    height: 50%; 
    float:left;
}       
.main a:before{
    visibility: hidden;
    content: attr(content);
    position: absolute;
    line-height: 250px;
    float: left;
    width: 150px;
    height: 150px;
    background: rgba(0,0,50,.5);
    border-radius: 4px;
    overflow: hidden;
    text-align: center;
    font-size: 25px;
    filter: alpha(opacity=0);
    opacity: 0;
    -webkit-transition: opacity 0.5s ease-out;
    -moz-transition: opacity 0.5s ease-out;
    -ms-transition: opacity 0.5s ease-out;
    -o-transition: opacity 0.5s ease-out;
    transition: opacity 0.5s ease-out;
}
.main a:hover::before{
    -webkit-transition: opacity 0.2s ease-in;
    -moz-transition: opacity 0.2s ease-in;
    -ms-transition: opacity 0.2s ease-in;
    -o-transition: opacity 0.2s ease-in;
    transition: opacity 0.2s ease-in; 
    zoom: 1;
    filter: alpha(opacity=100);
    opacity: 1.0;
    visibility: visible;
}

Working Demo

Upvotes: 0

xzegga
xzegga

Reputation: 3149

the problem here is that the four .child element is showed when hover on a element, but only the last .child is visible, to resolve this isue use visibility property instead display:

https://jsfiddle.net/f9m1mnce/

.main{
	position: relative;
	width: 300px;
	overflow:hidden	
}
.main a{
	width: 50%;
	height: 50%; 
	float:left;
}
.main a > .child{
	position: absolute; 
	left:0;
	right:0;
	bottom:0;
	top: 0;
    visibility: hidden;
	background: gray;
	filter: alpha(opacity=0);
	opacity: 0;
	-webkit-transition: opacity 0.5s ease-out;
	-moz-transition: opacity 0.5s ease-out;
	-ms-transition: opacity 0.5s ease-out;
	-o-transition: opacity 0.5s ease-out;
	transition: opacity 0.5s ease-out;					
}
.main a:hover > .child{
	-webkit-transition: opacity 0.2s ease-in .2;
	-moz-transition: opacity 0.2s ease-in .2 ;
	-ms-transition: opacity 0.2s ease-in .2;
	-o-transition: opacity 0.2s ease-in .2;
	transition: opacity 0.2s ease-in .2; 
    top: 0;
    visibility: visible;
	zoom: 1;
	filter: alpha(opacity=100);
	opacity: 1.0;
}
<div class="main">
	<a href="">
		<img src="http://placehold.it/150x150">
		<div class="child">
			<h4>1 Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h4>
		</div>
	</a>
	<a href="">
		<img src="http://placehold.it/150x150">
		<div class="child">
			<h4>2 Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h4>
		</div>
	</a>
	<a href="">
		<img src="http://placehold.it/150x150">
		<div class="child">
			<h4>3 Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h4>
		</div>
	</a>
	<a href="">
		<img src="http://placehold.it/150x150">
		<div class="child">
			<h4>4 Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h4>
		</div>
	</a>
	
</div>

Upvotes: 2

Ashish Bhagat
Ashish Bhagat

Reputation: 420

A absolute element is associated to its parent relative element, in your demo in your demo you need to set position relative to a tag

.main a{
    width: 50%;
    height: 50%; 
    float:left;
    position:relative;
}

Upvotes: 0

Related Questions