2013-06-28 29 views
1

我希望将以下文本和链接放在同一行上,文本“由XYZ生成”居中,右侧返回顶部链接。我究竟做错了什么?文本和链接不会在同一行上正确对齐

<div id="footer_container"> 
    <span style="text-align: center">Made by XYZ</span> 
    <span style="float: right"><a href="#top">Return to Top</a></span> 
</div> 

这里是div容器代码:

#footer_container 
{ 
    background: #7fc041; 
    bottom: 0; 
    height: 1.9em; 
    left: 0; 
    position: fixed; 
    width: 100%; 
} 
+2

你只能申请['文本align'(http://www.w3.org/TR/CSS21/text.html#alignment-prop )在块级元素上,所以在'#footer_container'上使用它。 – Adrift

+1

你不需要用span来包装你的链接来浮动它。 –

回答

3

应用text-align:center到您的页脚,而不是跨度(only block level elements support this property )。此外,你并不需要一个跨度浮动的锚标记:

HTML

<div id="footer_container"> 
    <span>Made by XYZ</span> 
    <a href="#top" style="float: right">Return to Top</a> 
</div> 

CSS

#footer_container 
{ 
    background: #7fc041; 
    bottom: 0; 
    height: 1.9em; 
    left: 0; 
    position: fixed; 
    width: 100%; 
    text-align:center 
} 

这里的a working fiddle

+0

谢谢,这工作。但是,当我将margin-right应用于返回顶部链接时,它也会移过由xyz文本创建的边界。我如何防止这种情况发生? –

+0

@DavidTunnell,这是预期的(正常)行为。为了适应,只需在“xyz”文本中添加一个匹配的负边距即可。 http://jsfiddle.net/Ge465/ –

2

试试这个:

#footer_container 
{ 
    background: #7fc041; 
    bottom: 0; 
    height: 1.9em; 
    left: 0; 
    position: fixed; 
    width: 100%; 
    text-align: center 
} 

<div id="footer_container"> 
    <span>Made by XYZ</span> 
    <span style="float: right"><a href="#top">Return to Top</a></span> 
</div> 

检查the fiddle

+0

谢谢,这工作。但是,当我将margin-right应用于返回顶部链接时,它也会移过由xyz文本创建的边界。我如何防止这种情况发生? –

+0

有点乱七八糟,但看看[这些行](http://jsfiddle.net/KtEhK/2/)。更简单的方法(这也是一种攻击)是为第一个跨度添加负边距。 – karthikr