2016-05-12 62 views
1

我正在做垂直对齐,所以我把我的<p>放在表格单元格上,但是当我试图居中文本时,text-align:center不会影响它们,但是我可以看到第四个盒子上的div居中?也许这是因为display:table-cell?这是我的SCSS代码:无法居中对齐表格单元格中的p?

div { 
 
    text-align: center; 
 
    display: inline-block; 
 
    vertical-align: top; 
 
} 
 
div.top { 
 
    padding: 1%; 
 
    margin: 1%; 
 
    background: #ff99b3; 
 
    height: auto; 
 
    width: 20%; 
 
    text-align: center; 
 
} 
 
div.top p { 
 
    height: 200px; 
 
    display: table-cell; 
 
    vertical-align: top; 
 
} 
 
div.middle { 
 
    padding: 1%; 
 
    margin: 1%; 
 
    background: #ffff99; 
 
    height: auto; 
 
    width: 20%; 
 
} 
 
div.middle p { 
 
    height: 200px; 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
} 
 
div.bottom { 
 
    padding: 1%; 
 
    margin: 1%; 
 
    background: #eb99ff; 
 
    height: auto; 
 
    width: 20%; 
 
} 
 
div.bottom p { 
 
    height: 200px; 
 
    display: table-cell; 
 
    vertical-align: bottom; 
 
} 
 
div.length { 
 
    padding: 1%; 
 
    margin: 1%; 
 
    background: #99ffe6; 
 
    height: auto; 
 
    width: 20%; 
 
} 
 
div.length div { 
 
    background: #ff99b3; 
 
    vertical-align: -425px; 
 
}
<div class="top" style="text-align: center;"> 
 
    <p style="text-align: center;"> 
 
    <strong>TOP</strong> 
 
    </p> 
 
</div> 
 

 
<div class="middle"> 
 
    <p> 
 
    <strong>MIDDLE</strong> 
 
    </p> 
 
</div> 
 

 
<div class="bottom"> 
 
    <p style="text-align:center;"> 
 
    <strong>BOTTOM</strong> 
 

 
</div> 
 

 
<div class="length"> 
 
    <div> 
 
    <strong>ELEMENT</strong> 
 
    </div> 
 
</div>

而且我JsFiddle

我已经把CSS的内联样式上divp但仍然不居中文本

+1

粘贴HTML代码也将有助于得到完美的答案 – Rahul

+0

我把表格单元格上监守垂直对齐不会,如果工作,我删除表格单元格?我想要发生在每个盒子上,他们被分配顶部,中间,底部 –

+0

@dippas谢谢。您的代码正常工作 –

回答

0

使用table-celldiv和删除p

body { 
 
    margin: 0 
 
} 
 
.table { 
 
    display: table; 
 
    width: 100% 
 
} 
 
.table > div { 
 
    text-align: center; 
 
    display: table-cell; 
 
    height: 200px; 
 
    width: 25%; 
 
    padding: 1%; 
 
} 
 
.top { 
 
    background: #ff99b3; 
 
    vertical-align: top 
 
} 
 
.middle { 
 
    background: #ffff99; 
 
    vertical-align: middle 
 
} 
 
.bottom { 
 
    background: #eb99ff; 
 
    vertical-align: bottom 
 
} 
 
.length { 
 
    background: #99ffe6; 
 
} 
 
.length > div { 
 
    background: #ff99b3; 
 
}
<div class="table"> 
 
    <div class="top"> 
 
    <strong>TOP</strong> 
 
    </div> 
 
    <div class="middle"> 
 
    <strong>MIDDLE</strong> 
 
    </div> 
 
    <div class="bottom"> 
 
    <strong>BOTTOM</strong> 
 
    </div> 
 
    <div class="length"> 
 
    <div> 
 
     <strong>ELEMENT</strong> 
 
    </div> 
 
    </div> 
 
</div>

0

如果右键单击“中间”一词并选择“检查元素”,那么这将在您的页面中打开一个元素树。你可以很快发现<p>不是全角。那就是问题所在。

通常这可以通过使用width:100%margin:0 auto来解决。但是这在<p>元素上不起作用。

我觉得你的问题的一部分是你有一个table-cell元素(您<p>),但它不是table-rowtable元素中包含。想想旧式<table>,以及它们通常如何设置。

<table> 
    <tr> 
    <td></td> 
    </tr> 
</table> 

如果你曾在此设置的元素(使用实际<table><tr><td>或元素的CSS display: tabletable-rowtable-cell),定位会更容易定制。

具体,我建议你保持你的<div> S,并更换<p><table><tr><td>,然后你可以在tablewidth: 100%使预期text-align会工作。

+0

我试图删除表格单元格,但垂直对齐不会在每个方块上工作 –

+1

您的原则是正确的,但由于CSS在必要时创建了匿名表格对象,因此解决方案比您想象的要简单。实际上,一种解决方案就是将'div'显示属性从'inline-block'更改为'inline-table'。 – Alohci

+0

为了澄清,*“匿名表对象”*是指自动添加的“”和“”...是否正确? –