2010-08-02 49 views
1

我曾将问题发布到http://doctype.com/within-same-line-have-center-element-right-most-element,但没有得到很好的答案。我贴在这里,希望能获得良好的反馈;)在同一行内,有一个中心元素和最右边的元素

<div style="float:right"> 
    right most, same line as hello world and will not push hello world! 
</div> 

<div style="text-align:center">hello world</div> 

目前,最右边的元素,从页面的中心推动的“hello world”出来的。我怎样才能避免这种情况?

我希望

  • 你好世界仍处于中心
  • 正确的元素仍然是同一条线上世界,你好
  • 正确的元素是在最右边的
  • 都是顶最

谢谢。

我尝试了一些别人的建议,但它不会工作。

<div style="text-align:center; width:100%;"> 
    Hello World 
    <div style="float:right">right most, same line as hello world and will not push hello world!</div> 
    <div style="clear:both"></div> 
</div> 

对于上面的代码,我测试了IE8。它们不会在同一行。我用firefox 3.6.8进行了测试,“Hello World”会被右侧的元素向左推。

回答

2

上的最右边的div内含div和position:absolute; right:0px; top:0px;使用position:relative

<div style="text-align:center; width:100%; position:relative;"> 
    Hello World 
    <div style="right:0px; top:0px; position:absolute;">right most, same line as hello world and will not push hello world!</div> 
    <div style="clear:both"></div> 
</div> 

position:relative使用,任何绝对定位元素内被偏移到该相对定位祖先。有关更多示例,请参见this blog post

演示 - http://jsbin.com/itene

+0

辉煌。它只是工作! – 2010-08-12 03:39:17

+0

另一个不理解CSS模型的例子,也就是不理解“position”的瘟疫。 – John 2015-04-04 20:43:28

+0

@John我一定错过了你的回答,否则我会很高兴地认可它的优越性。 – 2015-04-04 22:46:45

0

试试这个:

<div style="text-align:center; width:100%;"> 
    <div style="float:left; margin-left:400px;">Hello World </div> 
    <div style="float:right">right most, same line as hello world and will not push hello world!</div> 
    <div style="clear:both"></div> 
</div> 

See the DEMO here.

你可能想在hello world div来调整margin-left值。

0

指定居中DIV和使用位置的线高度:用于所述第二相对:

<div style="text-align:center; line-height: 50px;">hello world</div> 
<div style="line-height: 50px; position: relative; top: -50px; float:right; z-index: 1"> 
    right most, same line as hello world and will not push hello world! 
</div> 
+0

对不起。我意识到两种东西都在向下移动。是否有可能将它们保持为最高? – 2010-08-02 13:14:59

+0

是的,通过改变线条高度。尝试一个小于50px的值。 – Anax 2010-08-02 13:31:22

相关问题