简体   繁体   English

HTML CSS在屏幕宽度之外并排显示div

[英]HTML CSS make divs side by side, out of screen width

My goal is to put div with width=100vw, after that div there should be second div with width for example 300px (so that second div should be out of screen). 我的目标是将div设置为宽度= 100vw,在该div之后应该有第二个div宽度,例如300px(因此第二个div应该在屏幕之外)。 I tried many things with float, display inline and so on, now I don't have any more ideas. 我尝试了浮动,显示内联等许多东西,现在我没有更多的想法。

<div id="div1"></div>
<div id="div2"></div>

Here is fiddle with example code 这里是示例代码的小提琴

https://jsfiddle.net/kg5ea4sc/5/ https://jsfiddle.net/kg5ea4sc/5/

You can use white-space: nowrap on parent element and display: inline-block on two inner elements. 您可以在父元素上使用white-space: nowrap并在两个内部元素上display: inline-block Also maybe you want to add vertical-align: top so it will look like this Fiddle 也许你想添加vertical-align: top所以它看起来像这个Fiddle

 .element { white-space: nowrap; } #div1{ background: green; display: inline-block; width:100vw; height: 80px; } #div2{ background: red; display: inline-block; width:300px; height: 100px; } 
 <div class="element"> <div id="div1"></div> <div id="div2"></div> </div> 

https://jsfiddle.net/guanzo/kg5ea4sc/18/ https://jsfiddle.net/guanzo/kg5ea4sc/18/

The second div is outside of the screen. 第二个div在屏幕之外。 You'll have to manipulate either it's position or the overflow:hidden property on the container if you want to see it though. 如果你想看到它,你必须操纵它的位置或者容器上的overflow:hidden属性。

HTML HTML

<div id="container">
  <div id="div1"></div>
  <div id="div2"></div>
</div>

CSS CSS

#div1{
  background: green;
  width:100vw;
  height: 80px;
}

#div2{
  background: red;
  width:300px;
  height: 100px;
}

div{
    display:inline-block;
}

#container{
    width:100vw;
   white-space:nowrap;
   overflow:hidden;
}

Here is my fork of your fiddle: https://jsfiddle.net/nyzvbvo7/1/ 这是我的小提琴: https//jsfiddle.net/nyzvbvo7/1/

You can scoll to the right to see the second div 你可以右边看到第二个div

What I changed: 我改变了什么:

I added 我补充道

body {
  width: calc(100vw + 300px);
  margin: 0;
}
#div1, #div2 {
      display: inline-block;
      vertical-align: top;
}

So I made the body wide enough to hold both containers and set the container's display to inline-block . 因此,我将主体设置得足够宽,以容纳两个容器,并将容器的display设置为inline-block vertical-align: top; can be left out, the the containers will be algned at their baseline (which can vary depending on the content) 可以省略,容器将在其基线处(根据内容而有所不同)

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

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