html - 在 div 中居中图像而不裁剪图像和 overflow hidden

标签 html css overflow center

我必须在每个 div 的中心设置每个图像,并 overflow hidden ,而不裁剪图像。我试过

  • margin: auto 0;
  • background-position: center;

但没有任何作用。

.main {
  width: 100%;
  height: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.column {
  width: 16.7% !important;
  max-width: 16.4% !important;
  height: 100%;
  display: inline-block;
  margin-right: -1px;
  overflow: hidden;
  position: relative;
}

.column .picture:before {
  height: 100%;
  transition: all .7s;
  display: none;
}

.picture {
	background-position: center;
  height: 90%;
  overflow: hidden;
  transition: all .5s;
}

.column:hover .picture {
  filter: blur(1.2px);
  overflow: hidden;
	height: 100%;
  transition: all .5s;
  -webkit-backface-visibility: hidden;
  -webkit-transform: translateZ(1px) scale(1.1, 1.1);
}

.column:hover .picture:before {
  display: block;
  transition: all .7s;
}

.label{
  color: #E7E7E7;
  font-size: 2em !important;
  font-weight: 900;
  line-height: 60px;
  text-shadow: 3px 3px #953163;
  text-align: center;
  text-decoration: none;
}
<div class="main">
			<div class="column">
				<a href="http://link1.com/">
					<img class="picture" src="https://i.imgur.com/u5vuO6M.jpg" alt="">
					<div class="label"> Link 1</div>
				</a>
			</div>
			<div class="column">
				<a href="http://link2.com/">
					<img class="picture" src="https://i.imgur.com/u5vuO6M.jpg" alt="">
					<div class="label"> Link 2</div>
				</a>
			</div>
			<div class="column">
				<a href="http://link3.com/">
					<img class="picture" src="https://i.imgur.com/u5vuO6M.jpg" alt="">
					<div class="label"> Link 3</div>
				</a>
			</div>
</div>

enter code here

最佳答案

将图像 -50% 向左平移,并将左边距设置为 50%

要防止悬停转换移除 translateX,请将其更新为:

transform: translateX(-50%) translateZ(1px) scale(1.1, 1.1);

.main {
  width: 100%;
  height: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.column {
  width: 16.7% !important;
  max-width: 16.4% !important;
  height: 100%;
  display: inline-block;
  margin-right: -1px;
  overflow: hidden;
  position: relative;
}

.column .picture:before {
  height: 100%;
  transition: all .7s;
  display: none;
}

.picture {
  display: inline-block;
  height: 90%;
  transition: all .5s;
  transform: translateX(-50%);
  margin-left: 50%;
}

.column:hover .picture {
  filter: blur(1.2px);
  overflow: hidden;
  height: 100%;
  transition: all .5s;
  -webkit-backface-visibility: hidden;
  transform: translateX(-50%) translateZ(1px) scale(1.1, 1.1);
}

.column:hover .picture:before {
  display: block;
  transition: all .7s;
}

.label {
  color: #E7E7E7;
  font-size: 2em !important;
  font-weight: 900;
  line-height: 60px;
  text-shadow: 3px 3px #953163;
  text-align: center;
  text-decoration: none;
}
<div class="main">
  <div class="column">
    <a href="http://link1.com/">
      <img class="picture" src="https://i.imgur.com/u5vuO6M.jpg" alt="">
      <div class="label"> Link 1</div>
    </a>
  </div>
  <div class="column">
    <a href="http://link2.com/">
      <img class="picture" src="https://i.imgur.com/u5vuO6M.jpg" alt="">
      <div class="label"> Link 2</div>
    </a>
  </div>
  <div class="column">
    <a href="http://link3.com/">
      <img class="picture" src="https://i.imgur.com/u5vuO6M.jpg" alt="">
      <div class="label"> Link 3</div>
    </a>
  </div>
</div>

关于html - 在 div 中居中图像而不裁剪图像和 overflow hidden ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44619426/

相关文章:

javascript - 如何在页面加载/使用 Javascript 时自动打开纯 CSS 弹出窗口?

html - 如何确保动态表为每一列提供适当百分比的空间?

html - Css hover image transition 行为怪异

css - 水平滚动中断 overflow-x :hidden in safari and firefox

html - 如何使用 flexbox 显示具有不同规则的列?

javascript - 垂直对齐 float div 内的 img

jquery - 我该如何修复即选择宽度?

html - 溢出 :hidden is not working in my slideshow project

c++ - 为什么 C++ 流使用 char 而不是 unsigned char?

html - 纯CSS解决方案添加一些文字