gpt4 book ai didi

css - 无法将我的 Logo 垂直居中

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:56 25 4
gpt4 key购买 nike

关于 my page左上角的标志没有居中。这是我的代码:

.header_col1 {
float: left;
width: 20%;
background: #009240;
text-align: center;
position: relative;
}
.header_col1 a { /* my logo */
display: inline-block;
vertical-align: middle;
}

然而,根据我读过的文章,这种技术应该有效。为什么不在这种情况下?我不想使用绝对定位,因为 Logo 周围的绿色区域在较小的分辨率下会缩小,而 Logo 也必须这样做。

最佳答案

这是一种方法。

display: table-cell 应用于包装图像的 a 元素,并从 .header-col1 祖先继承宽度和高度 block 。

然后您可以使用 vertical-align: middletext-align: center 来获得您想要的对齐方式。如果您需要在左右边缘留一些空白,请添加一些左右填充。

最后,为了让图像响应,设置 width: 100% 并使用 vertical-align: top 来处理下面的空白区域-基线问题。

.header_col1, .header_col2 {
height: 110px;
}
.header_col1 {
float: left;
width: 20%;
background: #009240 none repeat scroll 0% 0%;
text-align: center;
position: relative;
}
.header_col1 a {
vertical-align: middle;
display: table-cell;
text-align: center;
width: inherit;
height: inherit;
padding: 0 10px; /* optional if you need left/right whitespace */
}
.header_col1 a img {
width: 100%;
vertical-align: top;
}
<div class="header_col1">
<a id="logo" href="#">
<img src="http://placehold.it/262x78">
</a>
</div>

关于css - 无法将我的 Logo 垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30461664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com