html - margin-top 因内容而异
问题描述
我想让所有按钮的按钮容器的边距顶部为 0 或其他值。但不知何故,这似乎取决于内容。按钮内容换行越多,margin-top 的差异就越大。如何使它们全部对齐?
.buttoncontainer {
height: 100px;
width: 400px;
background-color: lightblue;
display: inline-block;
}
button {
margin-top: 0;
height: 100%;
width: 60px;
display: inline-block;
}
<div class="buttoncontainer">
<button></button>
<button>test</button>
<button>test test test </button>
</div>
解决方案
这与保证金无关。如果您vertical-align: middle
为按钮设置,它们将正确对齐。
.buttoncontainer {
height: 100px;
width: 400px;
background-color: lightblue;
display: inline-block;
}
button {
margin-top: 0;
height: 100%;
width: 60px;
display: inline-block;
vertical-align: middle;
}
<div class="buttoncontainer">
<button></button>
<button>test</button>
<button>test test test </button>
</div>
因为 inline-block 元素vertical-align: baseline
默认具有。如果你不设置它middle
,它会像这样对齐。
.buttoncontainer {
height: 100px;
width: 400px;
background-color: lightblue;
display: inline-block;
}
button {
margin-top: 0;
height: 100%;
width: 60px;
display: inline-block;
/* vertical-align: baseline; this is default */
}
<div class="buttoncontainer">
<button>a</button>
<button>test</button>
<button>test test test </button>
</div>
推荐阅读
- python - 如何解析选定输出的配置文件?
- python - 用 pandas.join() 赋值
- json.net - Newtonsoft.Json 反序列化失败
- ruby-on-rails - Simple_form 和 Accepts_nested_attributes_for 的问题
- angularjs - AG Grid - cellEditingStopped 中的 isCancelAfterEnd 状态?
- r - 如何将不同但相同类型的值存储到 R 中的数据框中?
- javascript - 如果值相等,则嵌套数组将对象添加到状态中的数组
- r - R中威布尔曲线下的着色子区域
- c# - 消毒异步方法c#的最佳方法
- php - 在codeigniter中创建php矩阵表