简体   繁体   English

为什么我的div不在线?

[英]Why are my divs not staying in line?

I am trying to get an Input and a Link element to sit side by side inside a Div element. 我试图让InputLink元素并排放置在Div元素内。

I wanted to have the Div and the Link to completely fill the Div element so you cannot see the containing Div around the edges of the Input and Link elements. 我想让DivLink完全填充Div元素,以便在InputLink元素的边缘看不到包含的Div

However if I set the width of my Link element any wider than 27px is falls to the next line even though I should have up to 31px left of the container Div to fit the Link element in. Relevent HTML and CSS below... 但是,如果我将Link元素的宽度设置为大于27像素,则即使下一个行距容器Div最多剩下31像素才能适合下一个链接 ,下一行仍会落入下一行。请在下面删除 HTML和CSS ...

HTML HTML

<section>
        <img src="images/logo11w.png">
        <div name="search">
            <input>
            <a href="#"></a>
        </div>
    </section>

CSS CSS

div[name="search"]{

display: block;
margin: auto;
height: 27px;
width: 572px;
background-color: green;}

input{

display: inline-block;
width: 541px;
height: 27px;
border-style: none;
background-color: yellow;
padding: 0;
margin: 0;}

div[name="search"]>a{
display: inline-block;
width: 27px;
height: 27px;
background-color: red;
margin: 0;}

Could someone explain why my link element cannot be wider that 27px without falling to the next line? 有人可以解释为什么我的link元素不能宽到27px而不下降到下一行吗? I am OK with having the width be only 27px I just wanted to understand why this is happening from a technical standpoint. 我可以将宽度设为27px,我只是想从技术角度理解为什么会发生这种情况。

inline-block displays the whitespace in the actual layout in the rendered html... one whitespace is 4px . inline-block在呈现的html中以实际布局显示空白...一个空白为4px。 so if you want your link to have 31px width, you need to give margin-left:-4px or remove the whitespace in the actual layout. 因此,如果您希望链接的宽度为31px,则需要给margin-left:-4px或删除实际布局中的空白。

<section>
        <img src="images/logo11w.png">
        <div name="search">
            <input><a href="#"></a>
        </div>
    </section>

updated fiddle with no white space: http://jsfiddle.net/x6Mq5/ 更新的小提琴,没​​有空格: http : //jsfiddle.net/x6Mq5/

updated fiddle with margin-left:-4px; margin-left:-4px;更新小提琴margin-left:-4px; http://jsfiddle.net/x6Mq5/1/ http://jsfiddle.net/x6Mq5/1/

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

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