简体   繁体   English

获取div水平包装

[英]Get divs to wrap horizontally

I'm making a blog in which the results of a search for articles will be contained in divs. 我正在制作一个博客,其中搜索文章的结果将包含在div中。 The layout of my website is all horizontal (ie the articles scroll horizontally). 我的网站布局都是水平的(即文章水平滚动)。

Making a single line of divs is easy but that not what I want. 制作单行div很容易,但这不是我想要的。 It will be easier if I explain with a diagram. 如果我用图解释会更容易。 Here's how I want the divs to be: 这是我想要div的方式:

 ______________   ______________   ______________
|    Div 1     | |    Div 4     | |    Div 7     |
|______________| |______________| |______________|
 ______________   ______________   ______________
|    Div 2     | |    Div 5     | |    Div 8     |
|______________| |______________| |______________|
 ______________   ______________ 
|    Div 3     | |    Div 6     |
|______________| |______________|

But if the window is taller then there's more space vertically, which should give: 但如果窗口较高,则垂直空间更大,这应该给出:

 ______________   ______________ 
|    Div 1     | |    Div 5     |
|______________| |______________|
 ______________   ______________ 
|    Div 2     | |    Div 6     |
|______________| |______________|
 ______________   ______________ 
|    Div 3     | |    Div 7     |
|______________| |______________|
 ______________   ______________
|    Div 4     | |    Div 8     |
|______________| |______________|

This is what I mean by horizontal wrapping. 这就是我所说的水平包装。 In short, the divs take as much vertical space as then can before occupying a new column. 简而言之,在占用新列之前,div可以占用尽可能多的垂直空间。

I was hoping this was possible with pure html/css but I'm having a feeling that it's not possible without a little bit of javascript. 我希望这可以用纯html / css,但我有一种感觉,没有一点点javascript就不可能。

You can use Flexbox for this. 您可以使用Flexbox。 You need to set fixed height on parent element or 100vh and flex-direction: column . 您需要在父元素或100vhflex-direction: column上设置固定高度。

 * { box-sizing: border-box; } ul { display: flex; height: 100vh; flex-direction: column; align-items: flex-start; align-content: flex-start; flex-wrap: wrap; list-style: none; padding: 0; } li { padding: 20px 60px; border: 1px solid black; } 
 <ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul> 

I think in pure css this can only be done with flexbox: 我认为在纯CSS中这只能用flexbox来完成:

  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-content: flex-start;

Live-Example (change the height of the container): 实例 - 改变容器的高度:

https://codepen.io/MattDiMu/pen/zdBxad https://codepen.io/MattDiMu/pen/zdBxad

edit: Yes, using 100vh for the size of the container is the much more elegant solution :) 编辑:是的,使用100vh的容器大小是更优雅的解决方案:)

Who told you this was not possible in CSS/HTML. 谁告诉你这在CSS / HTML中是不可能的。 It is possible. 有可能的。 Here is a sample using simple css and not even a flexbox. 这是一个使用简单的CSS而不是flexbox的示例。 You just need to know the right area to put CSS properties to. 您只需要知道将CSS属性放入的正确区域。 In your case you need the box to be float:left in order to consume the width of the container which can be dynamic like 50vw to consume 50% of the viewable screen width or in percentage if you like. 在你的情况下,你需要盒子是float:left是为了消耗容器的宽度,它可以像50vw一样动态消耗50%的可视屏幕宽度或者你想要的百分比。

https://codepen.io/Nasir_T/pen/rzLavm https://codepen.io/Nasir_T/pen/rzLavm

Hope this helps. 希望这可以帮助。

[EDIT] [编辑]

As the question had horizontal in the last line of explanation when it was asked so i miss understood it. 由于这个问题在最后一行解释时是横向的,所以我很想知道它。 Anyway this answer can help someone who requires a pure css solution for horizontal stacking. 无论如何,这个答案可以帮助那些需要纯css解决方案进行水平堆叠的人。

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

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