调整标题元素底部边框的大小以适应文本宽度并响应
Resize bottom border on a heading element to fit the text width and also be responsive
我想使底部边框更窄且更灵敏,同时保持文本居中。我怎样才能做到这一点?我尝试明确使用宽度 属性 但这显然不起作用。
演示:https://www.bootply.com/jEB0e4Ao61
h1 {
border-bottom: 1px solid orange;
padding-bottom: 5px;
width: 400px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container text-center features">
<h1>Features</h1><br>
</div>
你需要做两件事:
- 使用
display:inline-block
;
- 设置
width: auto
从块元素更改为边框将调整为文本的宽度:
<h1>
是一个 block
元素,它会自动拉伸到容器宽度的 100%。将宽度设置为自动或百分比将应用于默认宽度,即 50% 将是容器宽度的 50%。
使用 inline-block
和宽度 auto
将使标题仅与标题本身一样宽,这意味着您不需要针对不同屏幕尺寸的媒体查询。这将使边框与标题本身的宽度完全相同。
要使 "underline" 比文本宽度更宽:
如果您想要在标题的两侧添加额外的 space,您可以使用填充来做到这一点。例如:
padding-left:20px; padding-right:20px;
将在标题的任一侧添加 20px,扩展元素宽度,同时将底部边框在两侧扩展 20px。
工作演示:
h1 {
border-bottom: 1px solid orange;
display: inline-block;
width: auto;
text-align: center;
margin: auto;
padding: 0 20px 5px; /* this adds 20px to the sides, extending the border */
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container text-center features">
<h1>Features</h1><br>
</div>
<p>The "underline" for this heading extends 20px either side of the text by setting the left and width padding to 20px</p>
我想使底部边框更窄且更灵敏,同时保持文本居中。我怎样才能做到这一点?我尝试明确使用宽度 属性 但这显然不起作用。
演示:https://www.bootply.com/jEB0e4Ao61
h1 {
border-bottom: 1px solid orange;
padding-bottom: 5px;
width: 400px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container text-center features">
<h1>Features</h1><br>
</div>
你需要做两件事:
- 使用
display:inline-block
; - 设置
width: auto
从块元素更改为边框将调整为文本的宽度:
<h1>
是一个 block
元素,它会自动拉伸到容器宽度的 100%。将宽度设置为自动或百分比将应用于默认宽度,即 50% 将是容器宽度的 50%。
使用 inline-block
和宽度 auto
将使标题仅与标题本身一样宽,这意味着您不需要针对不同屏幕尺寸的媒体查询。这将使边框与标题本身的宽度完全相同。
要使 "underline" 比文本宽度更宽:
如果您想要在标题的两侧添加额外的 space,您可以使用填充来做到这一点。例如:
padding-left:20px; padding-right:20px;
将在标题的任一侧添加 20px,扩展元素宽度,同时将底部边框在两侧扩展 20px。
工作演示:
h1 {
border-bottom: 1px solid orange;
display: inline-block;
width: auto;
text-align: center;
margin: auto;
padding: 0 20px 5px; /* this adds 20px to the sides, extending the border */
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container text-center features">
<h1>Features</h1><br>
</div>
<p>The "underline" for this heading extends 20px either side of the text by setting the left and width padding to 20px</p>