如何始终让两个div并排显示?

6

我有两个向左浮动的div。虽然我不想使用绝对定位,但有没有其他方法可以让它们并排而不使用绝对定位?或者这是唯一的方法?

<div class="moreinfo" id="darkgray">
            <p>
                Today, hate speech continues to profilerate throughout the Internet, normalized in the form of YouTube comments, animated GIFs, and tweets. Online anonymity affords users a sense of security that fosters a culture of cruelty and bigotry. Our goal is to create a conversation about the consequences of hateful speech that rethinks how we communicate online. Social media is full of positive potential; we can tap into it by holding each other accountable.
            </p>
        </div>
        <div class="moreinfo" id="lightgray">
            <h2>
                "WE NEED TO TEACH OUR CHILDREN NOT TO STAND SILENTLY BY WHILE OTHERS ARE BEING TORMENTED. IN THE END, THEY WILL BE SAFER ONLINE &amp; OFFLINE."
                <a href="#">READ ARTICLE BY WIRED SAFETY</a>
            </h2>
        </div>
        <div class="clear"></div>

css

.moreinfo{
    width:715px;
    height:250px;
    float:left;
    color:white;
}

1
display: inline-block 可能会对您有所帮助。 - Konsole
5个回答

10

感谢您的文章,非常棒。然而,添加"display:inline-block;"并没有解决问题,它只能与百分比一起使用吗?我不想让它们缩放,我宁愿让窗口将其裁剪掉。 - Naomi K

2

我注意到最好的方法是使用百分之五十的宽度来同时适用于两个元素。


1
请将得票最高的答案标记为正确答案。那是一个更好、更完整的答案。 - Nilanshu Jaiswal

0
最佳解决方案是使用 display:tabledisplay:table-cell,以确保它们并排放置。

这个存在兼容性问题。虽然受影响的比例不是很大,但那些使用IE7及以下版本的用户会受到影响。http://caniuse.com/css-table - Deryck
哈哈,我刚才在脑海中阅读那段文字时感觉非常有趣。标点符号让人兴奋不已。 - Deryck

0

你写的 CSS 正确地使 div 并排,但是你必须注意内部浮动 div 的宽度不应大于父元素的宽度。
试试这个(缩小 moreinfo 的宽度作为演示):

.moreinfo{
width:150px;
height:300px;
float:left;
color:black;}

0

将包含元素的宽度设置为足够大,以容纳您想要的方式。

body {
  min-width: 1450px;
}

这里有一个小例子


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接