简体   繁体   English

CSS:图片标题逐渐褪色

[英]CSS: Fading image caption

I have a picture that when you hover over it, a fading caption would appear 我有一张图片,当您将鼠标悬停在它上面时,将会出现淡淡的字幕

Here is the jfiddle 这是jfiddle

https://jsfiddle.net/e9dwbdyn/4/ https://jsfiddle.net/e9dwbdyn/4/

I want it to look like this however: 我希望它看起来像这样:

我希望它看起来像这样

I think it has to do with this part but I'm not sure how to exactly format it. 我认为这与这部分有关,但我不确定如何准确格式化它。 Any advice/help would be appreciated. 任何建议/帮助将不胜感激。 Thanks! 谢谢!

figcaption {
  position: absolute;
  top:35%;
  width: 80%;
    height:50%;
  left:10%;
  font-size: 14px;
  color: white;
  background-color: #9F8F53;
  opacity: 0;
  -webkit-transition: opacity .5s ease-in-out;
  -moz-transition: opacity .5s ease-in-out;
  -o-transition: opacity .5s ease-in-out;
 transition: opacity .5s ease-in-out;
}

Try this one https://jsfiddle.net/e9dwbdyn/6/ 试试这个https://jsfiddle.net/e9dwbdyn/6/

figure {
  position: relative;
  display: block;
  margin: 5px 0 10px 0;
    width:350px;
}

figcaption {
  position: absolute;
  top:30%;
  width: 80%;
  height:40%;
  left:10%;
  font-size: 20px;
  font-family: "Arial, Helvetica, sans-serif";
  text-align: center;
  color: white;
  background-color: #000;
  opacity: 0;
  -webkit-transition: opacity .5s ease-in-out;
  -moz-transition: opacity .5s ease-in-out;
  -o-transition: opacity .5s ease-in-out;
  transition: opacity .5s ease-in-out;
}

figure:hover figcaption {
  opacity: 0.5;
}

.product-name a {
    color: #fff;
}

.product-name a:hover {
    color: #fff
}

.product-name, .desc_grid, .price {
 padding: 0px;
    margin: 0px;
}
}

You would still need to play around with some margins, text fonts and sizes to get the exact match. 您仍然需要使用一些边距,文本字体和大小来获得完全匹配。

you may use figcaption as flex container https://jsfiddle.net/e9dwbdyn/5/ 您可以将figcaption用作flex容器https://jsfiddle.net/e9dwbdyn/5/

 figure { position: relative; display: block; margin: 5px 0 10px 0; width:350px; } figcaption { position: absolute; top:0; left:0; bottom:0; right:0; display:flex; font-size: 14px; color: white; } figcaption>div { background-color: #9F8F53; opacity: 0; -webkit-transition: opacity .5s ease-in-out; -moz-transition: opacity .5s ease-in-out; -o-transition: opacity .5s ease-in-out; transition: opacity .5s ease-in-out; margin:auto; text-align:center; width:80%; } figure:hover figcaption div { opacity: 0.7; } .product-name 
 <figure> <img src="https://goodnessofgodministries.files.wordpress.com/2012/03/bugia_candlestick_.jpg" alt="Candlesticks" style="width:350px" /> </a> <figcaption> <div class="product-shop"> <h3 class="product-name"><a href="" title="Candlesticks">Candlesticks</a><span class="over"></span></h3> <p class="desc_grid">lorem ipsum</p> <div class="price-box"> <span class="regular-price" id="product-price-3-new"> <span class="price">$50.00</span></span> </div> </div> </figcaption> </figure> 

When positioning elements absolutely it is always a good idea to incorporate a bit of flexibility. 绝对定位元素时,最好加入一点灵活性。 The issue with your code, is that you try to vertically center the element by estimating the top and left value in percentages, which isn't that flexible: What if the images inside the figure element have different sizes and aspect ratios? 你的代码的问题,是你试图通过估计垂直居中元素topleft百分比值,这是不是灵活:如果内部的图像figure元素具有不同的尺寸和长宽比? If so, these estimated percentages will not work in every instance and would potentially require you to manually change the value with each image. 如果是这样,这些估计的百分比将无法在每个实例中都起作用,并且可能需要您手动更改每个图像的值。

In the example you present, it looks as if the height of the transitioned element is determined by its own content, rather than having set a specific height as in your code. 在您呈现的示例中,过渡元素的height似乎由其自身的内容决定,而不是像在代码中那样设置特定的height

Example 1 ( height determined by the content inside) works with browsers from IE9 and up: 示例1height由内部内容确定)适用于IE9及更高版本的浏览器:

figcaption {
  position: absolute;
  top: 50%; /* Always 50% from the top */
  transform: translateY(-50%); /* Extracting half of the content height to vertically center */
  width: 80%;
  left: 0;
  right: 0;
  opacity: 0;
  margin: 0 auto;
  font-size: 14px;
  padding: 1em;
  color: white;
  background: rgba(194, 145, 57, 0.7); /* Use semitransparent background instead of opacity for readability reasons */
  transition: opacity .5s;
}

figure:hover figcaption {
  opacity: 1;
}

Example 2 (fixed height ) should work in all browsers: 示例2 (固定height )在所有浏览器中均应适用:

figcaption {
  position: absolute;
  height: 50%; /* Fixed height */
  width: 80%;
  top: 0; /* Filling the whole space with top, left, bottom, right */
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  margin: auto; /* Using margin: auto; the space around is distributed evenly */
  font-size: 14px;
  padding: 1em;
  color: white;
  background: rgba(194, 145, 57, 0.7);
  transition: opacity .5s;
}

In the not-too-distant future Flexbox has to be the preferred method, as it does all the calculations for you. 在不久的将来,Flexbox必须成为首选方法,因为它可以为您完成所有计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM