简体   繁体   English

彼此相邻2格

[英]2 div next to each other

First off all, I've searched on StackOverflow, but I couldn't find a solution which fully works for me. 首先,我搜索了StackOverflow,但是找不到适合我的解决方案。

I have a div element and in there I have 2 other div elements that needs to be positioned next to each other. 我有一个div元素,在这里我还有两个其他的div元素,它们需要彼此相邻放置。

The first div has a fixed with, while the second div should take the remaining screen space. 第一个div具有固定的,而第二个div应该占用剩余的屏幕空间。

Thanks to StackOverflow already, I've found the following: 由于已经有了StackOverflow,我发现了以下内容:

.element {
    margin-bottom:15px;
    height: 40px;
}
.element div {
    vertical-align: top;
    height: 40px;
}
.element div:first-child {
    float: left;
    width: 100px;
    background-color: red;
}
.element div:last-child {
    margin-left: 100px;
    background-color: green;
}
<div class="container">
    <div class="element">
        <div>col 1</div>
        <div>col 2</div>
    </div>
</div>

This gives me the following output for the HTML: 这为HTML提供了以下输出:

在此处输入图片说明

However, when I place an input element in the "col2", the input element is pushed outside the boundaries, see image below: 但是,当我将输入元素放置在“ col2”中时,输入元素被推到边界之外,请参见下图:

在此处输入图片说明

It's driving me nuts that I don't find a solution here. 令我发疯的是我在这里找不到解决方案。

I've creates a JsFiddle also. 我也创建了一个JsFiddle

You can set the css box-sizing to border-box for removing padding that is added to width: 您可以将css box-sizingborder-box以删除添加到width的填充:

box-sizing : border-box;

Working Demo 工作演示

Set box-sizing: border-box; 设置box-sizing: border-box; to .element INPUT - DEMO .element INPUT - DEMO

.element INPUT {
    -webkit-box-sizing : border-box;‌
    -moz-box-sizing : border-box;
    box-sizing:border-box;
}

In order to make it work in all browsers, Change your css to this : 为了使其在所有浏览器中都能正常工作,请将CSS更改为:

.element INPUT {width: 100%; box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;}

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

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