gpt4 book ai didi

html - 带有边框的 h2 中的文本溢出

转载 作者:行者123 更新时间:2023-11-28 15:07:12 24 4
gpt4 key购买 nike

我想要一个带有双边框的 h2 元素。目前我有这个片段,HTML+CSS:

h2.titulo {
width: 100%;
margin: 10px 0;
padding: 10px 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
border-bottom: 1px solid #ccc;
font-size: 25px;
}

h2.titulo span {
padding: 11px 0;
font-size: 19px;
font-size: 25px;
border-bottom: 1px solid #8c2638;
}
<h2 class="titulo"><span>My title</span></h2>

我希望这个标题是响应式的,当我有一个很长的标题时,我认为最好的方法是 text-overflow: ellipsisoverflow:hidden。我试过了,但是隐藏在 h2 上的溢出使红色边框底部消失了:

h2.titulo {
width: 100%;
margin: 10px 0;
padding: 10px 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
border-bottom: 1px solid #ccc;
font-size: 25px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}

h2.titulo span {
padding: 11px 0;
font-size: 19px;
font-size: 25px;
border-bottom: 1px solid #8c2638;
}
<h2 class="titulo"><span>A very very very very very very very very long long title</span></h2>

最接近的方法是这样的,但是红色边框并没有与另一个边框完全折叠:

h2.titulo {
border-bottom: 1px solid #ccc;
font-size: 25px;
width: 100%;
margin: 10px 0;
padding: 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
overflow-x: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

h2.titulo span {
display: inline-block;
line-height: 100%;
padding-bottom: 10px;
border-bottom: 1px solid #8c2638;
}
<h2 class="titulo"><span>Normal title</span></h2>

和 margin-bottom:-1px; on span 由于溢出而无法解决问题。有什么想法吗?

最佳答案

display: inline-block 添加到您的 span 中,并从 h2 中删除 bottom-padding。

您的 margin-bottom:-1px 也不起作用,因为 span 默认是 inline 元素,它不会提供任何视觉变化本身就像 paddingmargin

堆栈片段

h2.titulo {
width: 100%;
margin: 10px 0;
padding: 10px 0 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
border-bottom: 1px solid #ccc;
font-size: 25px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}

h2.titulo span {
padding: 11px 0;
font-size: 19px;
font-size: 25px;
border-bottom: 1px solid #8c2638;
display: inline-block;
}
<h2 class="titulo"><span>A very very very very very very very very long long title</span></h2>

<h2 class="titulo"><span>NORMAL TITLE</span></h2>

更新:如果您希望折叠边框,请使用 box-shadow 而不是 border

堆栈片段

h2.titulo {
width: 100%;
margin: 10px 0;
padding: 10px 0 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
box-shadow: 0px -1px 0px 0px #ccc inset;
font-size: 25px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}

h2.titulo span {
padding: 11px 0;
font-size: 19px;
font-size: 25px;
box-shadow: 0px -1px 0px 0px #8c2638 inset;
display: inline-block;
position: relative;
}
<h2 class="titulo"><span>A very very very very very very very very long long title</span></h2>

<h2 class="titulo"><span>NORMAL TITLE</span></h2>

关于html - 带有边框的 h2 中的文本溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155585/

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