简体   繁体   English

将两个100%宽度的div并排放置

[英]Put two 100% width divs side-by-side

So if I want to have two divs, each of 100% of the entire page, side by side, given that the wrapper has overflow:hidden , how should I go about implementing it? 因此,如果我想要两个div,每个100%的整个页面,并排,假设包装器有overflow:hidden ,我应该如何实现它?

I have tried using inline-block but it did not work. 我尝试过使用inline-block但它没有用。

I have tried using float too but it caused errors. 我已经尝试使用float 太多 ,但它造成的错误。

I want the second div to be hidden so I can change it's left as an animation, sort of like a slide. 我希望第二个div被隐藏,所以我可以把它改成左边的动画,有点像幻灯片。

Thanks in advance! 提前致谢!

If I've understood you correctly, you can achieve what you're after using inline-block . 如果我理解正确,你就可以实现使用inline-block You just have to be a little careful with white space (ie you need to make sure you've got no white space between the two child div elements). 你只需要小心一点白色空间(即你需要确保两个子div元素之间没有空白区域)。 I've stopped the div s from wrapping by setting white-space: nowrap; 我通过设置white-space: nowrap;来阻止div包装white-space: nowrap; .

<div class="foo">
    <div> woo woo !</div><div> woo woo !</div>
</div>

.foo {
    overflow: hidden;
    white-space: nowrap;        
}
    .foo > div {
        display: inline-block;
        vertical-align: top;
        width: 100%;
        background: aqua;
    }
    .foo > div +  div {
        background: lime;
    }

Try it out at http://jsfiddle.net/8Q3pS/2/ . http://jsfiddle.net/8Q3pS/2/上试一试。

Edit: Here's an alternative implementation using position: absolute; 编辑:这是使用position: absolute;的替代实现position: absolute; : http://jsfiddle.net/8Q3pS/5/ . http//jsfiddle.net/8Q3pS/5/ That way you'll be able to animate the second one into view using left . 这样你就可以使用left将第二个动画显示到视图中。 Note that you'll need to set a height on the parent div . 请注意,您需要在父div上设置高度。

.foo {
    position: relative;
    width: 100%;
    height: 1.5em;
    overflow: hidden;
}
    .foo > div {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        background: aqua;
    }
    .foo > div +  div {
        left: 100%;
        background: lime;
    }

Could you not set max-width to 100%, not set the actual width and float them side by side? 你能不能将max-width设置为100%,不设置实际宽度并将它们并排浮动? With both overflow:hidden, as they expand it should create horizontal scrollbars. 使用overflow:hidden,当它们展开时应该创建水平滚动条。

This should be purely a matter of positioning one of the divs off the page using absolute positioning and transitioning the left property using either hover state or javascript. 这应该纯粹是使用绝对定位将一个div定位在页面之外,并使用悬停状态或javascript转换left属性。

Hover the red div. 悬停红色div。

Codepen Example Codepen示例

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

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