我有不同宽度的图片,需要将它们保持在中心位置 他们各自的divs 。
我需要考虑所有边距 - 顶部,底部,左侧,右侧。他们应该在div的中心而不是顶部中心。因此class="row text-centre"
和class="center-block"
无法提供帮助。
我尝试更改边距但每张图片的宽度不同使其无法实现。我想知道是否有任何方法可以保持任何宽度在其div中心的图像。
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<style>
.col-md-3.img-container {
background-color: green;
height: 400px;
display: flex;
-webkit-flex-flow: column;
-ms-flex-flow: column;
flex-flow: column;
margin-right: 2%;
}
.img-div {
-webkit-box-flex: 2;
-webkit-flex: 2;
-ms-flex: 2;
flex: 2;
//position: relative;
margin-left:20%;
margin-right:20%;
// margin:auto;
}
</style>
</head>
<body>
<div id="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-3 img-container">
<div class="img-div">
<a
href="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQiWGXo4U6CCvNItlDYFgEQz4d3T-YjLj13nqUZ-crpAr3qMPx-"
title="" data-gallery rel="nofollow"> <img
src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQiWGXo4U6CCvNItlDYFgEQz4d3T-YjLj13nqUZ-crpAr3qMPx-"
width="20%" class="img-thumbnail img-responsive img-adv" />
</a>
</div>
</div>
<div class="col-md-3 img-container">
<div class="img-div">
<a
href="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSqUIWjfSNC02M5Yjo-7nLBoeSJSEcOZCy0uRdF7Z8HgZRxGWB_Lg"
title="" data-gallery rel="nofollow"> <img
src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSqUIWjfSNC02M5Yjo-7nLBoeSJSEcOZCy0uRdF7Z8HgZRxGWB_Lg"
width="40%" class="img-thumbnail img-responsive img-adv" />
</a>
</div>
</div>
<div class="col-md-3 img-container">
<div class="img-div">
<a
href="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTP96P2l57b9znJ60v1gYS695LfH9K0bt8lB38Yi0McCdtq_dtC"
title="" data-gallery rel="nofollow"> <img
src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTP96P2l57b9znJ60v1gYS695LfH9K0bt8lB38Yi0McCdtq_dtC"
width="80%" class="img-thumbnail img-responsive img-adv" />
</a>
</div>
</div>
<div class="col-md-3 img-container">
<div class="img-div">
<a
href="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQYUsrUfG7vmoT61NjkXL1o-Xk-I032GQI1wuZ_QIcen399srHimA"
title="" data-gallery rel="nofollow"> <img
src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQYUsrUfG7vmoT61NjkXL1o-Xk-I032GQI1wuZ_QIcen399srHimA"
width="90%" class="img-thumbnail img-responsive img-adv" />
</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
如果您使用的是bootstrap
class="center-block"
.Absolute-Center {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
这个类必须在两个维度中居中。看一下,您必须将此类添加到标记内的图像中:
答案 1 :(得分:0)
text-align:center;
将其添加到.img-div
答案 2 :(得分:0)
我相信你的答案就在这里:
http://jsfiddle.net/4wz688o1/5/
.img-div:before { /* create a full-height inline block pseudo=element */
content: ' ';
display: inline-block;
vertical-align: middle; /* vertical alignment of the inline element */
height: 100%;
}
.img-div {
height: 400px;
-webkit-box-flex: 2;
-webkit-flex: 2;
-ms-flex: 2;
flex: 2;
position: relative;
text-align:center;
}
.img-div img {
vertical-align: middle; /* vertical alignment of the inline element */
}
基本上img-div需要适当的高度来操作。
答案 3 :(得分:0)
这对我来说很好。你不需要任何:: before和img样式。
.img-container{
position: relative;
/* set some width and height, I test with width: auto, 100%, 800px and fixed height */
display: table;
}
.img-container .img-div{
display: table-cell;
vertical-align: middle;
text-align: center;
}
答案 4 :(得分:-1)
我已移除margin
并添加了text-align:center;
试试这个
.img-div {
-webkit-box-flex: 2;
-webkit-flex: 2;
-ms-flex: 2;
flex: 2;
position: relative;
text-align:center; // added this
}
小提琴:http://jsfiddle.net/4wz688o1/1/
更新了代码
.img-div {
-webkit-box-flex: 2;
-webkit-flex: 2;
-ms-flex: 2;
flex: 2;
position: relative;
}
.img-div img
{
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translateY(-50%) translateX(-50%);
}