简体   繁体   English

当混合浮动和相对定位的元素时,文本被移位

[英]Text is shifted when mixing floated and relatively positioned elements

I'm looking to solve with CSS a problem that I found far trickier than expected. 我想用CSS解决一个我发现比预期更棘手的问题。

Here is the smallest (valid) html fragment I was able to build that expose the issue: 这是我能够构建的最小(有效)html片段,它揭示了这个问题:

<!doctype html>
<html>
    <head>
        <title></title>
        <style>
            body, div {margin: 0; color: white;}
        </style>
    </head>
    <body>
        <div style="float: left; background-color: #000; width: 200px; height: 200px;"></div>
        <div style="background-color: #333; position: relative; left: 200px; height: 200px;">This is a test !</div>
    </body>
</html>

Does someone know how to prevent the text "This is a test !" 有人知道如何阻止文本“这是一个测试!” To be shifted to the right by the floated element ? 要通过浮动元素向右移动?

I'm guessing that the floated element is the culprit since when I remove it the text is at the right place. 我猜测浮动元素是罪魁祸首,因为当我删除它时,文本位于正确的位置。 Also changing the order of the divs doesn't yield the expected result. 同样改变div的顺序也不会产生预期的结果。

Thank you ! 谢谢 !

Replace left: 200px; 替换left: 200px; on your right side div with margin-left: 200px; 在你的右侧div与margin-left: 200px;

Fiddle to prove here 小提琴在这里证明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM