为了账号安全,请及时绑定邮箱和手机立即绑定

为什么带有“display:table-cell;”的div不受保证金的影响?

为什么带有“display:table-cell;”的div不受保证金的影响?

森林海 2019-08-26 19:03:45
为什么带有“display:table-cell;”的div不受保证金的影响?我有div彼此相邻的元素display: table-cell;。我想margin在它们之间设置,但margin: 5px没有效果。为什么?我的代码:<div style="display: table-cell; margin: 5px; background-color: red;">1</div><div style="display: table-cell; margin: 5px; background-color: green;">1</div>
查看完整描述

3 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

原因

MDN文档

[margin属性]适用于除table-caption,table和inline-table之外的表显示类型的元素以外的所有元素

换句话说,该margin物业是适用display:table-cell的元素。

请考虑使用该border-spacing属性。

请注意,它应该应用于具有display:table布局和的父元素border-collapse:separate

例如:

HTML

<div class="table">
    <div class="row">
        <div class="cell">123</div>
        <div class="cell">456</div>
        <div class="cell">879</div>
    </div></div>

CSS

.table {display:table;border-collapse:separate;border-spacing:5px;}.row {display:table-row;}.cell {display:table-cell;padding:5px;border:1px solid black;}

请参阅jsFiddle演示


水平和垂直不同的边距

正如DiegoQuirós所提到的,该border-spacing属性还接受两个值来设置水平轴和垂直轴的不同边距。

例如

.table {/*...*/border-spacing:3px 5px;} /* 3px horizontally, 5px vertically */


查看完整回答
反对 回复 2019-08-26
?
炎炎设计

TA贡献1808条经验 获得超4个赞

您可以使用内部div来设置边距。

<div style="display: table-cell;">
   <div style="margin:5px;background-color: red;">1</div></div><div style="display: table-cell; ">
  <div style="margin:5px;background-color: green;">1</div></div>

JS小提琴


查看完整回答
反对 回复 2019-08-26
?
动漫人物

TA贡献1815条经验 获得超10个赞

表格单元格不考虑边距,但您可以使用透明边框:

div {
  display: table-cell;
  border: 5px solid transparent;}

注意:你不能在这里使用百分比...... :(


查看完整回答
反对 回复 2019-08-26
  • 3 回答
  • 0 关注
  • 563 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信