Display: table-cell, contents and padding

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

I have a very straightforward HTML page that displays content in two columns. To format the columns I'm using <div> as the outer container (display: table-row) and two inner <div> as the actual columns (display: table-cell). One of these columns has a padding at the top. The markup looks like the following - extra markup and styles omitted for clarity:

<style> .row { display: table-row } .cell { display: table-cell; border: 1px solid black; width: 150px } .content { background-color: yellow } </style>  <div class="row">    <div class="cell">       <div class="content">Some content; this is not padded.</div>    </div>      <div class="cell">         <div class="content">More content; padded at the top.</div>     </div> </div> 

I'm getting this:


But I expected this:


The behavior is the same whether the padding-top is applied to the cell or the content. Am I missing anything? Thanks.

回答1:

You can achieve your two desired results just by using padding and margin on your div with the class content. Remember, padding will contribute to the overall width and height of an object, so that will extend the background color.

First screen shot:

<div class="content" style="margin-top: 10px">More content; padded at the top.</div> 

Second screen shot:

<div class="content" style="padding-top: 10px">More content; padded at the top.</div> 


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