如何在没有定义宽度的情况下显示另一个div旁边的div?

时间:2015-01-23 04:33:32

标签: html css width

我正在横向滚动网站上工作。我有一个封面背景,然后是一个带有文本列的黑色div,最后,我想在前一个div的右边有一个绿色div。

我的页面结构如下:

<body>
<div id="content">
  <div id="post-content">
    <div id="cover"></div>
    <div id="text"><!-- columns --></div>
    <div id="theend"><p>THE END</p></div>
  </div>
</div>
</body>

CSS如下:

body { margin:0; padding:0; background:black; }
#content { position:absolute; height:100%; top:0; left:0; }
#post-content { position:relative; background:transparent; overflow-x: scroll; overflow-y: auto; height: 100%; top:0; left:0; }
#cover { left:0; top:0; position:absolute; height:100%; width:100%; background: url('http://www.inveralmondchs.org/wp-content/uploads/2014/02/waterfall-godafoss-iceland.jpg') center no-repeat fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background-attachment: scroll; }
#text { float:left; width: auto; height:100%; left:100%; position: relative; box-sizing: border-box; padding:0px; margin:0px; font-size:15px; text-align: left; color:white; font-family:'open sans'; -webkit-column-width: 300px; -webkit-column-gap: 40px; -moz-column-width: 300px; -moz-column-gap: 40px; -moz-column-count: auto; column-width: 300px; column-gap: 40px; }
#theend { float:left; height:100%; position: relative; width:300px; text-align:center; background: green; color:white; }

您可以在此JSFiddle中看到演示:http://jsfiddle.net/kz5ch49w/47/

我认为问题与width的{​​{1}}有关。因为我的网站是动态的,所以我无法准确定义它。然后,绿色div #text出现在左下角,而不是显示在带有列的黑色div的右侧。

如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

你可以让第三个div的位置绝对:

#theend { float:left; height:100%; position: absolute; top:0; left: 0; width:300px; text-align:center; background: green; color:white; }

然后通过Javascript设置位置:

var w1 = document.getElementById("cover").scrollWidth + document.getElementById("text").scrollWidth; document.getElementById("theend").style.left = w1 + "px";

我分叉你的小提琴here

答案 1 :(得分:0)

只需将其添加到您的CSS:

#cover, #text, #theend {
    display: inline-block;
}

然后通过在所有三个最里面的div中放置一些可显示的文本来测试排列。

答案 2 :(得分:0)

  • 首先关闭 - 位置覆盖内容,因为它们都是绝对的。
  • 将#theend改为右移
  • 最后尝试更改/删除-moz-column-width:300px;和webkit-column。我删除了它,它看起来不错。

这有助于获得右侧的绿色div和背景静态,但我仍然看不到显示的文本。如果我找到了,我会告诉您。