简体   繁体   English

如何将所有div放在一行?

[英]How do I align all my div's in one line?

I've looked at a bunch of websites, but none of them worked for me. 我看了很多网站,但没有一个能为我工作。 I'm probably missing something really simple. 我可能错过了一些非常简单的东西。

#container {
  width:100%;
}

#one {
  background-color:blue;
  width:20%;
  height:50%;
}

#two {
  background-color:green;
  width:20%;
  height:50%;
}

#three {
  background-color:yellow;
  width:20%;
  height:50%;
}

#four {
  background-color:orange;
  width:20%;
  height:50%;
}

#five {
  background-color:red;
  width:20%;
  height:50%;
}

This is what I want it to look like: 这就是我想要的样子:

图片

It doesn't display a lot, which I suspect is because of the height:50%... Thanks in advance :) 它没有显示很多,我怀疑是因为高度:50%...在此先感谢:)

You just need to add float left to each id in your container. 您只需要向容器中的每个id添加float。 This is a truncated version, no need to add the same css to each of your separate ids. 这是一个截断版本,无需为每个单独的ID添加相同的css。

#container #one, #container #two, #container #three, #container #four, #container #five {
 float:left;
}

or you can use display inline block 或者您可以使用显示内联块

#container #one, #container #two, #container #three, #container #four, #container #five {
     display:inline-block;
    }

To center the divs if any space is left over you can add text align center to ensure the divs in your container are centered properly. 如果任何空间遗留在div上,则可以添加文本对齐中心以确保容器中的div正确居中。 This only works when using display block inline on your container. 这仅适用于在容器上内联显示块的情况。

#container {
         text-align:center;
        }

To put all the divs in the same line use 将所有div放在同一行中使用

display:inline-block;

if want to show divs in next line, use 如果想在下一行显示div,请使用

display:block;

default is set to block; default设置为block;

 #container { width:100%; } #one,#two,#three,#four,#five{ width:20%; height:50%; } #one { background-color:blue; display:inline-block; } #two { background-color:green; display:inline-block; } #three { background-color:yellow; display:inline-block; } #four { background-color:orange; } #five { background-color:red; } 
 <div id="container"> <div id="one"> One </div> <div id="two"> Two </div> <div id="three"> Three </div> <div id="four"> four </div> <div id="five"> five </div> </div> 

Hope it helps 希望能帮助到你

我不确定这是你要问的,但也许你只是需要

div{float:right;}

Try this: 尝试这个:

 #container { width: 100%; height: 300px; border: 1px solid #000; } #container div { width: 20%; height: 50%; float: left; } #element-1 { background-color: red; } #element-2 { background-color: blue; } #element-3 { background-color: pink; } #element-4 { background-color: yellow; } #element-5 { background-color: green; } 
 <div id="container"> <div id="element-1"></div> <div id="element-2"></div> <div id="element-3"></div> <div id="element-4"></div> <div id="element-5"></div> </div> 

I have helped in some way 我以某种方式帮助过

I've modified your code a bit but this should output what you are looking for. 我已经修改了你的代码,但这应该输出你要找的东西。

You display the divs inline, and position them relatively with a slightly negative margin so that they take up 20% of the width each. 您显示内联div,并以略微负边距相对定位它们,使它们占据每个宽度的20%。

In a comment you mentioned you want to "make it exactly 50% tall", so you need to give the body 100% height, then set the divs to have 50% height: 在你提到的评论中,你想“让它正好高50%”,所以你需要给身体100%高度,然后将div设置为50%高度:

 html, body { height: 100%; } div { display: inline-block; width: 20%; height: 50%; position: relative; margin: -2px; } #one { background-color: lightblue; } #two { background-color: green; } #three { background-color: yellow; } #four { background-color: orange; } #five { background-color: red; } 
 <div id="one"> </div> <div id="two"> </div> <div id="three"> </div> <div id="four"> </div> <div id="five"> </div> 

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

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