简体   繁体   English

CSS图像叠加使引导程序响应

[英]Css image overlay made responsive with bootstrap

I have an image that I created an overlay for, however I can't figure out how to make the height responsive. 我有一个为其创建覆盖图的图像,但是我不知道如何使高度响应。 I either have to define it in pixels, or it doesn't work. 我要么以像素为单位定义它,否则它不起作用。 I am hoping to have both the height and width adjust responsively, keeping the same image aspect ratio. 我希望可以同时调整高度和宽度,并保持相同的图像纵横比。 Here is my HTML: 这是我的HTML:

<ul class="img-list">
  <li>
    <a href="http://nataliemac.com"><br>
       <img class="imgrd" src="http://127.0.0.1:8080/wordpress/wp-content/uploads/2016/03/PIC_6436-300x199.jpg" width="900" height="597" class="alignnone size-medium wp-image-189"><br>
       <span class="text-content"><span>Place Name</span></span><br>
    </a>
  </li>
<p>
  ...
</p>

And my CSS 还有我的CSS

ul.img-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  text-align: center;
}

ul.img-list li {
  display: inline-block;
  margin: 0 1em 1em 0;
  position: relative;
  width: 100%;
  height: 100%;

}

span.text-content span {
  display: inline-block;
  text-align: center;
  vertical-align: middle;
}

ul.img-list li:hover span.text-content {
  opacity: 1;
}
span.text-content {
  display: inline-block;
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  left: 0;
  position: absolute;
  top: 25px;
  width: 100%; 
  height: 100%;
  border: 5px solid #fff;
  border-radius: 10px 125px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

img.imgrd {
  border-radius: 10px 125px;
}

Help is much appreciated! 非常感谢帮助!

try that : 尝试:

ul.img-list li {
  display: inline-block;
  margin: 0 1em 1em 0;
  position: relative;
  width: 100%;
  height: auto;
}

Alternatively, get down and dirty and grab the height of the image with jquery, something like: 或者,变脏并用jquery抓取图像的高度,例如:

var imgheight = $('.imgrd').height();
$('.text-content').height(imgheight);

I would avoid this if any CSS methods fit your use though :) 如果有任何CSS方法适合您,我会避免这种情况:)

You want to make the li span act as a table cell in order to properly get the text in the middle of the overlay: 您要使li span充当表格单元格,以便正确地在覆盖层的中间获取文本:

span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

Then, you need to make the IMG responsive. 然后,您需要使IMG响应。 Right now it's not, so the overlay is 100% width and height of the image, but the image won't change when the container shrinks. 现在还没有,因此叠加层是图像的宽度和高度的100%,但是当容器缩小时图像不会改变。 So: 所以:

img.imgrd {
  border-radius: 10px 125px;
  width: 100%;
  max-width: 100%;
  height: auto;
}

After that, you have some <br> tags which break the layout... so remove them. 之后,您有一些<br>标签会破坏布局...因此将其删除。 And update your span.text-content to make sure it starts at the top of the image (0px and not 25px), and also set the box-sizing property to accommodate the 5px border: 并更新您的span.text-content以确保它从图像的顶部开始(0px而不是25px),并设置box-sizing属性以容纳5px边框:

span.text-content {
  display: inline-block;
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%; 
  height: 100%;
  border: 5px solid #fff;
  border-radius: 10px 125px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
  box-sizing: border-box;
}

See the JSFiddle here: 在这里查看JSFiddle:

https://jsfiddle.net/xeqhepn4/2/ https://jsfiddle.net/xeqhepn4/2/

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

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