Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

两个行块宽度50%却不在同一行

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding:0;
            margin: 0;
            border: none;
        }
        .wrapper {
            width: 500px;
            overflow: hidden;
            background-color: grey;
        }
        .cell {
            display: inline-block;
            width: 50%;
        }
        .left {
            background-color: red;
        }
        .right{
            background-color: blue;
            
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="cell left">left</div>
        <div class="cell right">right</div>
    </div>
</body>
</html>

clipboard.png

直觉认为红块和蓝块应该是在同一行,然而并不是
设置float:left属性后两块并排了,width:49%也并排了,width:250px还是两排。造成这个问题的关键点在哪?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

在于 两个子div之间有一个 空白文本节点!去掉后就好了

<div class="cell left">left</div><div class="cell right">right</div>

或者

   <div class="cell left">left</div><!--
--><div class="cell right">right</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...