【问题标题】:Image in flex container not centering after defining width and height定义宽度和高度后,flex容器中的图像未居中
【发布时间】:2019-10-17 21:57:41
【问题描述】:
我正在使用 css flex box 做一些个人作品集。我想要做的是将图像水平居中,它确实居中,但问题是在我将width 和height 定义为img 之后,图像位于开头或左侧现在不在中心。
这是居中而不定义 width 和 height 时的样子
.center-img{
border-radius: 50%;
display:flex;
flex-direction:column;
justify-content:center;
text-align:center;
}
<div class="center-img">
<img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png" alt="user">
<h1>User</h1>
</div>
这就是定义width和height后的样子
.center-img{
width:100px;
height:100px;
border-radius: 50%;
display:flex;
flex-direction:column;
justify-content:center;
text-align:center;
}
<div class="center-img">
<img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png" alt="user">
<h1>User</h1>
</div>
我希望在定义图像的width 和height 之后不会影响图像的位置。所以它应该在中心。
谢谢!
【问题讨论】:
标签:
html
css
flexbox
centering
【解决方案1】:
需要考虑的几件事:
-
您从未将图像居中。
在您的第一个代码示例中,图像占据了整个视口宽度,因此它只是居中显示。当您使用width 和height 减小图像的大小时,它会按照默认值左对齐。使用 align-items 在列方向容器中水平居中 flex 项。
-
不要将图像用作弹性项目。有很多错误。
众所周知,作为弹性项目的图像在不同浏览器中存在渲染问题。相反,将图像包装在 div 中,并使 div 成为 flex 项。
-
将嵌套容器内的图像与 flex 属性对齐。
使用嵌套的 flex 容器使图像居中。
.center-img {
display: flex;
flex-direction: column;
align-items: center;
background-color: lightgray;
}
.center-img > div {
width: 100px;
height: 100px;
border-radius: 50%;
display: flex;
align-items: center;
border: 1px dashed red;
}
.center-img > div > img {
width: 100%;
}
h1 {
margin: 0;
}
<div class="center-img">
<div>
<img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png"
alt="user">
</div>
<h1>User</h1>
</div>
【解决方案2】:
仅将高度和宽度应用于 img 并添加 align-items: center,如下所示:
.center-img {
border-radius: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
img {
width: 100px;
height: 100px;
}
<div class="center-img">
<img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png"
alt="user">
<h1>User</h1>
</div>
【解决方案3】:
你需要纠结里面物品的尺寸。
可以使用max-width 和max-height 相对于容器来控制图像。我添加了红色边框,以便您可以更轻松地查看容器。
此外,h1 元素默认带有大的font-size 和margin,因此也需要为它们设置值。
.center-img {
width: 100px;
height: 100px;
border-radius: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center; /* change text-align to align-items */
border: 1px red solid; /* this is just so you can easily see the container */
}
.center-img img {
max-width: 100%; /* dimensions relative to the container size */
max-height: 40%; /* dimensions relative to the container size */
height: auto; /* keep aspect ratio */
width: auto; /* keep aspect ratio */
}
.center-img h1 {
margin: 5px 0 0; /* h1 has default margin-top and margin-bottom */
font-size: 18px; /* Set this to a reasonable size */
}
<div class="center-img">
<img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png"
alt="user">
<h1>User</h1>
</div>