在下面的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