在下面的HTML頁面中,我有一個div,其中包含兩個跨度(span1
和span2
),其中第二個包含子跨度span2a
。 span1
包含文本,span2a
也一樣。通過這種設置,文本的垂直對齊不同,我不能解決原因。跨度爲什麼會影響同級跨度的垂直位置
使事情複雜化,span2
和span2a
上有風格屬性。我無法控制這些,因爲它們是在jQuery循環插件的運行時注入的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style type="text/css">
body {margin: 0; padding: 0; font-family: arial; color: black;}
.container { height: 30px; text-align: left; padding: 8px 15px 0 15px; background-color: #fee; border: 1px solid red;}
.ticker { font-size: 12px; font-weight: bold; padding-top: 4px; float: left;}
.ticker > span:first-child { background-color: #bfb; }
.tickerscroll { display: inline-block; background-color: #bbf; }
</style>
</head>
<body>
<div class="container">
<div class="ticker">
<span class="span1">LABEL: </span>
<span class="span2 tickerscroll" style="position: relative; width: 480px; height: 15px; overflow-x: hidden; overflow-y: hidden; ">
<span class="span2a" style="position: absolute; top: 0px; opacity: 1; z-index: 3; left: 0px; ">The value (in a sub-span; note that Label is lower then this text)</span>
</span>
</div>
</div>
<div class="container">
<div class="ticker">
<span class="span1">LABEL: </span>
<span class="span2 tickerscroll" style="position: relative; width: 480px; height: 15px; ">
<span class="span2a" style="position: absolute; top: 0px; left: 0px; ">The value (this time, I've added a &nbsp; outside this inner span)</span>
</span>
</div>
</div>
<div class="container">
<div class="ticker">
<span class="span1">LABEL: </span>
<span class="span2 tickerscroll" style="position: relative; width: 480px; height: 15px; ">
The value (this time, the inner span span2a is not present at all)
</span>
</div>
</div>
</body>
</html>
正如你可以從我的代碼中看到的,我已經添加了兩個變體「修復」的問題:
- 添加一些文字(任何文字,在我來說,我已經添加了一個非
span2a
以外的span2
內。 - 完全刪除子跨度,並將文本放在第二個頂級跨度內。但是,這不是我的選擇,因爲需要使用span2a來使jQuery循環工作。
有沒有辦法讓兩段文字對齊而不必人爲拋出一個& nbsp;變成span2
?該解決方案無法更改任何span元素的樣式屬性,但可以根據需要添加CSS類。
非常感謝!我嘗試了很多東西,我很驚訝,我還沒有嘗試過。有趣的是,你的解決方案似乎沒有IE7修復工作,至少在運行IE7兼容模式的IE8中......。 – 2012-04-03 08:44:09
這一直是我的許多問題的原因。我很高興終於找到了解決方案!謝謝! – 2013-02-05 05:05:34