我想要一个带有双边框的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>
我希望这个标题具有响应性,当我有一个很长的标题时,我认为最好的方法是文本溢出:省略号,溢出:隐藏。我尝试了这个,但隐藏在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>
保证金底部:-1px;由于溢出,在span上没有做到这一点。好吗?
答案 0 :(得分:1)
将display: inline-block
添加到您的范围内,并从h2
删除底部填充。
此外,您的margin-bottom:-1px
无效,因为默认情况下span
是inline
元素,其自身不会发生任何视觉变化,例如padding
或margin
< / p>
Stack Snippet
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
Stack Snippet
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>
答案 1 :(得分:0)
将div放在一对div中可以起到作用,因为div是高度自动和全宽度默认值,但你可以设置第二个将它显示为内联块,然后它将正确应用边框。 / p>
div.titleBox{
border-bottom: 1px solid #ccc;
}
div.border{
border-bottom: 1px solid #8c2638;
display: inline-block;
}
h2.titulo {
width: 100%;
margin: 10px 0;
padding: 10px 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
font-size: 25px;
}
h2.titulo span {
padding: 11px 0;
font-size: 19px;
font-size: 25px;
word-break: break-all;
}
&#13;
<div class="titleBox"><div class="border"><h2 class="titulo"><span>This is a short title</span></h2></div></div>
<div class="titleBox"><div class="border"><h2 class="titulo"><span>This is a very very very very very vbery very large one!</span></h2></div></div>
&#13;
使用H2(默认情况下为块)可能会让您感到头疼。
h2.titulo {
width: 100%;
margin: 10px 0;
text-transform: uppercase;
font-weight: 400;
font-size: 30px;
display: block;
color: #8c2638;
font-size: 25px;
border-bottom: 1px solid #ccc;
}
h2.titulo span {
padding: 11px 0;
font-size: 19px;
font-size: 25px;
word-break: break-all;
border-bottom: 1px solid #8c2638;
display: inline-block;
margin: -1px;
}
&#13;
<h2 class="titulo"><span>This is a short title</span></h2>
<h2 class="titulo"><span>This is a very very very very very vbery very large one!</span></h2>
&#13;
但你可以解决它删除h2填充并将范围显示为内联块
答案 2 :(得分:0)
使用:: after或:: before添加exstra边框怎么样?
我认为这是&#34; h2&#34;元件