2009-09-21 192 views
3

要做到这一点,我已经做到了这一点:HTML左,右对齐元素

<table style="width:100%;"> 
<tr> 
    <td style="width:50%;text-align: left;">left</td> 
    <td style="width:50%;text-align: right;">right</td> 
    </tr> 
</table> 

我怎么能以最简单的方式(至少标记)做到这一点,而不使用表?我只想将2个元素对齐到左右最大值。

这里有数百个2列布局,但它们更符合页面布局,似乎矫枉过正。

回答

8

一些HTML代码:

<div class="left">left</div> 
<div class="right">right</div> 

一些CSS:

.left, .right { 
    width: 50%; /* Floated elements technically need a width specified. */ 
} 

.left { 
    float: left; 
} 

.right { 
    float: right; 
} 
+0

添加文本对齐:右;对.right类做了我所需要的,thanx。 – mxmissile 2009-09-21 15:49:05

3

事情是这样的:

CSS

<style type="text/css"> 
#primary { 
    width: 100%; 
} 

.secondary { 
    width: 50%; 
} 

.leftcolumn { 
    float: left; 
    text-align: left; 
} 

.rightcolumn { 
    float: right; 
    text-align: right; 
} 
</style> 

HTML

<div id="primary"> 
    <div class="secondary leftcolumn"> 
     <p>This will be on the left</p> 
    </div> 
    <div class="secondary rightcolumn"> 
     <p>This will be on the right</p> 
    </div> 
</div> 

虽然我会改变leftcolumnrightcolumn具体到每一个将包含(赞成semantic的命名方法来CSS,而非结构性的)内容的东西。