我正在创建一个包含多个按钮的 div。
我希望按钮能出现在div中间或居中显示。多行按钮应该基本上看起来像文本,但是居中/对齐。
<div>
<button>a</button><button>b</button>
</div>
我希望按钮能出现在div中间或居中显示。多行按钮应该基本上看起来像文本,但是居中/对齐。
<div>
<button>a</button><button>b</button>
</div>
如果需要垂直显示此按钮,则使用flex-direction: column;,如果需要水平显示,则使用flex-direction: row;。并将此属性添加到您的代码中。
div{
position:relative;
display:flex;
flex-direction: column;
flex-wrap:wrap;
}
button{
display:block;
padding:1rem 2rem;
background:#d0333a;
margin-bottom:.5rem;
text-align:center;
border: none;
border-radius:6px;
color:#fff;
cursor:pointer;
}
<div>
<button>a</button><button>b</button><button>c</button><button>d</button>
</div>
请使用以下内容:
<button style="margin: 0 auto; display: block;">a</button>
<button style="margin: 0 auto; display: block;">b</button>
text-align: center;
这可以用于居中所有内联元素,如按钮、文本等...
只需使用text-align:center
<div style="text-align:center">
<button>a</button><button>b</button>
</div>
敬礼, Nimesh P.
<style>
div{
text-align: center;
}
</style>
它将会执行...
<div class="container">
<button>button</button>
</div>
和 CSS
.container { width: 100%;}
.container button { display: block; margin: 0 auto;}
html
<div>
<button>a</button><button>b</button><button>c</button><button>d</button><button>e</button><button>f</button><button>g</button><button>h</button>
</div>
CSS
div{
text-align: center;
}
button {
background: transparent;
border: none;
cursor: pointer;
padding: 5px;
}
为了在水平和垂直方向上对齐按钮,请将以下内容应用于父DIV
HTML
<div id = "parent_div">
<button>a</button>
<button>b</button>
<button>c</button>
</div>
CSS
#parent_div {
width:200px;
height:200px;
border:solid 1px blue;
display:flex;/*Requires vendor prefixes*/
justify-content:center;/*Requires vendor prefixes*/
align-items:center;/*Requires vendor prefixes*/
}
删除 justify-content
和 align-items
以去除垂直和水平对齐。
阅读此文:FLEX