使用按钮居中内容块

时间:2017-12-22 08:23:01

标签: html css twitter-bootstrap twitter-bootstrap-3

The center-block class of Bootstrap在图片中有效,在调整大小的块元素(如div s)中,显然在按钮中。

按钮类应用于链接(变为伪按钮)时,不会水平居中



<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<button type="button" class="btn btn-default center-block">Pure button</button>
<a href="#" class="btn btn-default center-block">Link button</a>
&#13;
&#13;
&#13;

appearance具有button值的CSS属性不起作用。设置固定维度(widthmax-widthmin-width)不是一个好的解决方案。

This trouble was ignored in GitHub

1 个答案:

答案 0 :(得分:3)

Display: blockbuttons和其他元素(例如a)上以其他方式工作。您需要将display: table添加到链接而不是display: block;

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<style>
a.btn-default {display: table}
</style>

<button type="button" class="btn btn-default center-block">Pure button</button>
<a href="#" class="btn btn-default center-block">Link button</a>