简体   繁体   English

如何垂直对齐div文本?

[英]How to vertically align div text?

I have 2 div floating side by side. 我有2 div并排漂浮。

The div on the left is a part number, only one line. 左侧的div是部件号,只有一行。 The div on the right is a product description and can have multiple lines. 右边的div是产品说明,可以有多行。

How can I vertically align the left div text when the right div grows to 2 or more line. 当右div增长到2或更多行时,如何垂直对齐左div文本。

Here is the code. 这是代码。

<td>
   <div class="prodNumber left partNumSizexSmall">
      <a href="link" style="color:blue;text-decoration:underline;"><b>PartNum</b></a>
   </div>
   <div class="prodDesc xsmallDesc left">
      ProductDescription1
   </div>
</td>

Here is the CSS 这是CSS

.left {
    float: left;
}
.prodNumber {

}

.prodDesc {
    padding-left: 8px;
    padding-right: 8px;
}
.partNumSizexSmall {
    min-width: 50px;
}
.xsmallDesc {
    max-width: 550px;
}

The div to have it's content centered needs the display: table-cell 要使其内容居中的div需要display: table-cell

.center_me {
    height: 200px;
    vertical-align: middle; 
    display:table-cell;
}

The display:table-cell is what allows an element to have children vertically aligned, so it has to be on the parent to be able to have the children aligned vertically. display:table-cell是允许元素的子元素垂直对齐的元素,因此必须在父元素上才能使子元素垂直对齐。

Here's a working JSFiddle demo . 这是一个正在运行的JSFiddle演示

Use line-height. 使用行高。

$("document").ready(function(){
    var newheight = $(".prodDesc").height();
    var newheight2 = $(".prodNumber").height();
   if( newheight > newheight2 ){
        $(".prodNumber").css("line-height", newheight);
    }else if( newheight2 > newheight ){
         $(".prodDesc").css("line-height", newheight2);
    }
});

Should work, but I didnt test it. 应该可以,但是我没有测试。 sorry. 抱歉。

the most proven solution is to apply main container as display:table; 最成熟的解决方案是将主容器用作display:table; and the children as display:table-cell. 子级显示为:table-cell。 Since as per ur requirement you want the both prodNumber & prodDesc to be dependent on each other hence please add one outside wrap which holds both these elements. 由于根据您的要求,您希望prodNumber和prodDesc都相互依赖,因此请添加一个包含这两个元素的外部包装。 See working file here: url http://jsfiddle.net/wZ3n3/ 在此处查看工作文件: url http://jsfiddle.net/wZ3n3/

PS: append more texts. PS:追加更多文字。

Hope you got answer. 希望你能回答。

Use table instead of div 使用表格代替div

         <td>
            <table>
                <tr>
                    <td class="prodNumber left partNumSizexSmall">
                        <a href="link" style="color: blue; text-decoration: underline;"><b>PartNum</b></a>
                    </td>
                    <td class="prodDesc xsmallDesc left">
                        ProductDescription1 ProductDescription1 ProductDescription1 ProductDescription1
                        ProductDescription1 ProductDescription1 ProductDescription1 ProductDescription1
                        Product Description1 Product Description1 Product Description1 Product Description1
                    </td>
                </tr>
            </table>
        </td>

Use somthing like this 用这样的东西

<div align="center">
This is some text!
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM