How to make a DIV not wrap?

2019-01-10 06:07发布

I need to create a container DIV style that contains multiple other DIV's. It is asked that these DIV's wouldn't wrap if the browser window is resized to be narrow.

I tried to make it work like below.

<style>
   .container
   {
      min-width: 3000px;
      overflow: hidden;
   }
   .slide
   {
      float: left;
   }
</style>
<div class="container">
   <div class="slide">something</div>
   <div class="slide">something</div>
   <div class="slide">something</div>
   <div class="slide">something</div>
</div>

This works in most cases. However, in some special cases, the rendering is incorrect. I found the container DIV change to 3000px width in RTL of IE7; and it turns to be messy.

Is there any other way to make a container DIV not to wrap?

12条回答
贪生不怕死
2楼-- · 2019-01-10 06:33

Try using white-space: nowrap; in the container style (instead of overflow: hidden;)

查看更多
走好不送
3楼-- · 2019-01-10 06:39

The <span> tag is used to group inline-elements in a document.
(source)

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-10 06:42

you can use

display: table;

for your container and therfore avoid the overflow: hidden;. It should do the job if you used it just for warpping purpose.

查看更多
虎瘦雄心在
5楼-- · 2019-01-10 06:45

This worked for me:

.container {
  display: inline-flex;
}

.slide {
  float: left;
}
<div class="container">
  <div class="slide">something1</div>
  <div class="slide">something2</div>
  <div class="slide">something3</div>
  <div class="slide">something4</div>
</div>

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

查看更多
淡お忘
6楼-- · 2019-01-10 06:46

If I don't want to define a minimal width because I don't know the amount of elements the only thing that worked to me was:

display: inline-block;
white-space: nowrap;

But only in Chrome and Safari :/

查看更多
Anthone
7楼-- · 2019-01-10 06:46

The combo you need is

white-space: nowrap

on the parent and

display: inline-block; // or inline

on the children

查看更多
登录 后发表回答