How to align these buttons in a row? [duplicate]

夙愿已清 提交于 2019-11-28 08:36:06

问题


https://jsfiddle.net/2L9mznzu/

There are two empty text button, how to align them into a row?

.button {
  display: inline-block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  background: gray;
  margin: 0 4px;
  text-align: center;
}
<div class="button">
</div>
<div class="button">Button
</div>
<div class="button">
</div>

回答1:


Use vertical-align: top property to your .button.

The vertical-align CSS property specifies the vertical alignment of an inline or table-cell box. Source: MDN

See demo below:

.button {
  display: inline-block;
  vertical-align: top;
  width: 80px;
  height: 30px;
  line-height: 30px;
  background: gray;
  margin: 0 4px;
  text-align: center;
}
<div class="button">
</div>
<div class="button">Button
</div>
<div class="button">
</div>



回答2:


just add vertical-align: middle;

.button {
display: inline-block;
width: 80px;
height: 30px;
line-height: 30px;
background: gray;
margin: 0 4px;
text-align: center;
vertical-align: middle;

}




回答3:


You can wrap them inside a container div and use display:flex; this way they will always be aligned to the vertical center of the container div.

.button {
  display: inline-block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  background: gray;
  margin: 0 4px;
  text-align: center;
}

.container{
  display:flex;
  flex-direction:row;
  align-items:center;
}
<div class="container"><div class="button">
</div>
<div class="button">Button
</div>
<div class="button">
</div>
</div>


来源:https://stackoverflow.com/questions/41375341/how-to-align-these-buttons-in-a-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!