CSS - 居中和圆形的 Img Inside Div
CSS - center and rounded Img Inside Div
我在我的网页上的 Div 中有一个 Img,图像显示在 div 中,但对齐方式因浏览器宽度而异。
这是我的代码:
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="" src="../images/Joanne.jpg" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>
我想知道是否有一种方法可以垂直和水平对齐 Div 中的图像,并且也许还有带圆角的图像。
我搜索了一下,但没有任何效果,我认为通过将左右边距设置为 'auto' 可以解决我的问题,但是当我调整浏览器大小时图像仍然关闭。
如有任何帮助或建议,我们将不胜感激。
对于圆形图像 - 使用 border-radius 属性
对于垂直居中的图像 - 在具有指定高度的图像的父元素上使用 display:flex 和 align-items:center。
对于水平居中的图像 - display:flex 和 justify-content:center 在具有指定宽度的图像的父元素上。
但是,对于您的示例,我使用了margin:auto,因为它实际上更简单。
有关 flex 内容的更多信息,请单击此处:http://www.w3schools.com/css/css3_flexbox.asp
它应该是这样的:
.image {
border-radius: 50px;
}
.w3-col.w3-container.w3-green {
display: flex;
align-items: center;
margin: auto;
}
.w3-row {
border: 1px solid #aaa;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="image" src="http://s33.postimg.org/vnc0xbztb/Joanne.jpg
" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<!--<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>-->
</div>
为了清晰起见,我添加了 1px 边框 属性。
在任何事情之前,内联编写都不是好方法CSS,但无论如何你可以在 sudo 元素(之后)上使用垂直对齐方式,就像这样
.image {
border-radius: 50px; //adjust yourself
display: inline-block;
vertical-align: middle;
}
.w3-col.w3-container.w3-green{
text-align:center;
height:100px;
border:1px solid;
width:100%;
}
.w3-col.w3-container.w3-green:after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; margin:auto;">
<img class="image" src=" http://s33.postimg.org/vnc0xbztb/Joanne.jpg" alt="Chania" style="height: 80px; ">
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>
使用 transform: scale()
在 DIV 中完美居中您的图像
我在里面创建了一个方框,我在其中放置了您的图片,以展示如何将图片居中 div(class="box").
body{
width: 100%;
height: 100vh;
margin: 0;
}
.w3-row {
display: flex; //flexBox
align-items: center;
justify-content: center;
flex-direction: column; //Works as a Stack i.e Image at the Top and Description at the bottom
}
使用 CSS 变换缩放图像
.image{
width: 100%;
height: auto;
transform: scale(.5); //change the scale value to change size of the Image 0<scale<1
}
对于圆形图像
.img-rounded{
border-radius: 50%
}
我在我的网页上的 Div 中有一个 Img,图像显示在 div 中,但对齐方式因浏览器宽度而异。
这是我的代码:
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="" src="../images/Joanne.jpg" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>
我想知道是否有一种方法可以垂直和水平对齐 Div 中的图像,并且也许还有带圆角的图像。
我搜索了一下,但没有任何效果,我认为通过将左右边距设置为 'auto' 可以解决我的问题,但是当我调整浏览器大小时图像仍然关闭。
如有任何帮助或建议,我们将不胜感激。
对于圆形图像 - 使用 border-radius 属性
对于垂直居中的图像 - 在具有指定高度的图像的父元素上使用 display:flex 和 align-items:center。
对于水平居中的图像 - display:flex 和 justify-content:center 在具有指定宽度的图像的父元素上。 但是,对于您的示例,我使用了margin:auto,因为它实际上更简单。
有关 flex 内容的更多信息,请单击此处:http://www.w3schools.com/css/css3_flexbox.asp
它应该是这样的:
.image {
border-radius: 50px;
}
.w3-col.w3-container.w3-green {
display: flex;
align-items: center;
margin: auto;
}
.w3-row {
border: 1px solid #aaa;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="image" src="http://s33.postimg.org/vnc0xbztb/Joanne.jpg
" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<!--<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>-->
</div>
为了清晰起见,我添加了 1px 边框 属性。
在任何事情之前,内联编写都不是好方法CSS,但无论如何你可以在 sudo 元素(之后)上使用垂直对齐方式,就像这样
.image {
border-radius: 50px; //adjust yourself
display: inline-block;
vertical-align: middle;
}
.w3-col.w3-container.w3-green{
text-align:center;
height:100px;
border:1px solid;
width:100%;
}
.w3-col.w3-container.w3-green:after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; margin:auto;">
<img class="image" src=" http://s33.postimg.org/vnc0xbztb/Joanne.jpg" alt="Chania" style="height: 80px; ">
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>
使用 transform: scale()
在 DIV 中完美居中您的图像我在里面创建了一个方框,我在其中放置了您的图片,以展示如何将图片居中 div(class="box").
body{
width: 100%;
height: 100vh;
margin: 0;
}
.w3-row {
display: flex; //flexBox
align-items: center;
justify-content: center;
flex-direction: column; //Works as a Stack i.e Image at the Top and Description at the bottom
}
使用 CSS 变换缩放图像
.image{
width: 100%;
height: auto;
transform: scale(.5); //change the scale value to change size of the Image 0<scale<1
}
对于圆形图像
.img-rounded{
border-radius: 50%
}