html - 图片高度匹配容器

标签 html css

如何使图像始终完美地包含在容器内,因为高度和宽度完全相同?

.container {
  width: 500px!important;
  height: 800px!important;
}

.container:before {
  content: "";
  background: rgba(0, 0, 0, 0.2);
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  width: 500px;
  height: 800px;
}

.content_holder {
  position: absolute;
  bottom: 0;
  padding: 5%;
}

.custom_container .content_holder h3 {
  color: #fff!important;
}

.custom_container .content_holder p {
  color: #fff!important;
}
<div class="container">
  <img src="http://wrighthand.uk.w3pcloud.com/wp-content/uploads/2018/03/shutterstock_1016869756.jpg" />
  <div class="content_holder">
    <p>Subject</p>
    <h3>Container Title</h3>
    <a class="button">Learn more</a>
  </div>
</div>

由于某种原因,图像没有像我尝试将其添加到的网站那样位于容器中。

http://wrighthand.uk.w3pcloud.com

最佳答案

试试这个:

.container {
  width: 500px!important;
  height: 800px!important;
}

.container:before {
	content: "";
	background: rgba(0,0,0,0.2);
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	position: absolute;
  width: 500px;
  height: 800px;
}

.content_holder {
	position: absolute;
	bottom: 0;
	padding: 5%;
}

.custom_container .content_holder h3 {
	color:#fff!important;
}

.custom_container .content_holder p {
	color:#fff!important;
}
<div class="container">
<img src="http://wrighthand.uk.w3pcloud.com/wp-content/uploads/2018/03/shutterstock_1016869756.jpg" width="100%" height="100%"/>
<div class="content_holder">
<p>Subject</p>
<h3>Container Title</h3>
<a class="button">Learn more</a>
</div>
</div>

它现在非常适合。 (不过有点失真……)

关于html - 图片高度匹配容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49674590/

相关文章:

html - 如何在 html/css 中更改多色文本框底部边框的颜色?

html - 在导航栏内垂直居中一个圆形 div

jquery - 如何在浏览器验证具有 'required' 属性的字段后禁用提交按钮?

html - 浏览器之间背面可见性的行为不一致

javascript - 使用 JSON 文件动态更新 HTML 内容?

Javascript 预加载回调

html - 在 Firefox 中使用列时右侧 float 元素消失

html - 带有固定警报的固定标题

css - 如何删除内联 block 产生的额外边距空间?

jquery - 使用 jQuery 使内容可见