定义宽度和高度后,flex 容器中的图像不居中
Image in flex container not centering after defining width and height
我正在使用 css 弹性框做一些个人作品集。我想要做的是将图像水平居中,它确实居中但问题是在我定义 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
后不会影响图像的位置。所以它应该在中心。
谢谢!
仅将高度和宽度应用于 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>
您需要考虑内部物品的尺寸。
图像可以用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>
需要考虑的几件事:
您从未将图像居中。
在您的第一个代码示例中,图像占据了整个视口宽度,因此它只是居中显示。当您使用 width
和 height
缩小图像大小时,它按照默认值左对齐。使用 align-items
在 column-direction 容器中水平居中弹性项目。
不要将图像用作 flex 项目。有很多错误。
众所周知,作为 flex 项目的图像在不同浏览器中存在渲染问题。相反,将图像包裹在 div 中,并使 div 成为弹性项目。
在具有 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>
我正在使用 css 弹性框做一些个人作品集。我想要做的是将图像水平居中,它确实居中但问题是在我定义 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
后不会影响图像的位置。所以它应该在中心。
谢谢!
仅将高度和宽度应用于 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>
您需要考虑内部物品的尺寸。
图像可以用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>
需要考虑的几件事:
您从未将图像居中。
在您的第一个代码示例中,图像占据了整个视口宽度,因此它只是居中显示。当您使用
width
和height
缩小图像大小时,它按照默认值左对齐。使用align-items
在 column-direction 容器中水平居中弹性项目。不要将图像用作 flex 项目。有很多错误。
众所周知,作为 flex 项目的图像在不同浏览器中存在渲染问题。相反,将图像包裹在 div 中,并使 div 成为弹性项目。
在具有 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>