gpt4 book ai didi

css - 垂直对齐没有高度或宽度的内联对象

转载 作者:行者123 更新时间:2023-11-28 12:18:27 25 4
gpt4 key购买 nike

给定以下 html:

<div class="body">    
<div class="banner">
<div class="name">
<h2>
<a href="http://www.example.com">
<span class="bold">Test Link</span><br/>
</a>
</h2>
</div>
<div class="title">
<h3>A Connections Learning Partner Program</h3>
<p>Quality online learning for high school students in Oakland County and surrounding counties.
</p>
</div>
<div class="link">
<a href="http://www.example.com">Learn More</a>
</div>
</div>
</div>

如何在 .link 中垂直对齐 .link a (按钮)而不给出高度或宽度?像这样...

enter image description here

这是我的 fiddle

最佳答案

这是您可以做到的一种方法。您的 HTML 很好,无需更改任何内容。

对于 CSS:

.body { width: 920px; }

.banner {
background-color: #454545;
border-bottom: 3px solid #F9F9F9;
height: 100px;
margin: 0 0 5px;
padding: 0;
display: table;
}

.banner > div {
outline: 1px dotted yellow; /* optional to show cell edges... */
display: table-cell;
}

.banner .name {
width: 25%;
vertical-align: top;
padding-top: 25px; /* control top white space */
text-align: center;
}

.banner .name h2 {
color: #F9F9F9;
max-height: 55px;
text-transform: uppercase;
margin: 0;
padding: 0;
}

.banner .title {
width: 50%;
vertical-align: top;
padding-top: 25px;
}

.banner .title h3 {
font-size: 15px;
font-weight: bold;
line-height: 15px;
margin: 0px 0 0 0;
padding: 0;
}

.banner .title p {
font-size: 12px;
max-height: 35px;
overflow: hidden;
margin: 0;
padding: 0;
}

.banner .link {
width: 25%;
vertical-align: middle;
text-align: left; /* set to left, center or right as needed */
}

.banner .link a {
margin-left: 25px; /* controls left offset */
background-color: #FA9800;
border-radius: 5px 5px 5px 5px;
cursor: pointer;
display: inline-block; /* use inline-block if you want to center element */
font-size: 12px;
font-weight: bold;
height: 23px;
line-height: 23px;
text-align: center;
text-decoration: none;
width: 100px;
}

参见 fiddle :http://jsfiddle.net/audetwebdesign/jsG8F/

这是如何运作的

诀窍是使用 display: table在你的 .banner容器然后display: table-cell在你的 child 身上div元素,并将 .name 的 % 宽度分别设置为 25%、50%、25% , .title , .link .

然后您可以使用 vertical-aligntext-align控制各种文本 block 的垂直和水平位置。

我添加了与使用 padding-top 相关的评论控制横幅顶部的空白区域。

对于 .link a元素,您可以根据需要调整左边距(或右边距)。

这些 CSS 规则使您可以很好地控制横幅中各种元素的放置。

向后兼容性

display: table-cell属性向后兼容 IE8。

引用:https://developer.mozilla.org/en-US/docs/Web/CSS/display

关于css - 垂直对齐没有高度或宽度的内联对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868002/

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