简体   繁体   English

不同的弹性容器并排

[英]Different flex containers side by side

I'm trying to make it such that I have two half-width wrappers side by side.我正在尝试使我有两个并排的半角包装纸。 Currently the wrappers do take half the space but don't appear side to side.目前,包装器确实占用了一半的空间,但并没有出现在一边。 The display:flex seems to be taking the whole width and leaving the unused space on the side as margin. display:flex似乎占据了整个宽度,并在侧面留下了未使用的空间作为边距。

 .wrapper { display: flex; flex-direction: column; width: 40%; border: 1px solid black; }
 <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div> <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div>

I also tried adding another div outside wrapper with width 50% but it didn't help.我还尝试添加另一个宽度为 50% 的 div 外部包装器,但它没有帮助。 Any ideas?有任何想法吗?

Instead of display: flex , use display: inline-flex .代替display: flex ,使用display: inline-flex

The first is a block-level element which, by default, takes the full width of the parent.第一个是块级元素,默认情况下,它采用父元素的全宽。

The second is an inline-level element, which can co-exist with other elements on the same line.第二种是行内元素,它可以与同一行的其他元素共存。

 .wrapper { display: inline-flex; flex-direction: column; width: 40%; border: 1px solid black; }
 <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div> <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div>

Alternatively, set the parent element to display: flex which, by default, forces the children to exist in the same row.或者,将父元素设置为display: flex ,默认情况下,它会强制子元素存在于同一行中。

Add this to your code: body { display: flex } .将此添加到您的代码中: body { display: flex }

 .wrapper { display: flex; flex-direction: column; width: 40%; border: 1px solid black; } body { display: flex; }
 <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div> <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div>

I believe display: flex is similar to display: block if it's a top level element.我相信display: flex类似于display: block如果它是顶级元素。 The difference being;区别在于; the children of the flex container will be able to utilize the flex behavior. flex 容器的子容器将能够利用 flex 行为。 What you need to do is something like this:你需要做的是这样的:

 body { margin: 0; }.parent { display: flex; height: 100vh; background: #eee; }.child { display: flex; flex-direction: column; flex: 1; border: 1px dashed #ccc; }
 <div class='parent'> <div class='child column'>child 1</div> <div class='child column'>child 2</div> </div>

Wrap the wrappers with div and then display: flex .用 div 包裹包装器,然后display: flex

 .main-wrapper { display: flex; }.wrapper { display: flex; flex-direction: column; width: 50%; border: 1px solid black; }
 <div class="main-wrapper"> <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div> <div class="wrapper"> <div class="title">Test Title</div> <div class="info"> <div class="column"> <b>1</b> <span>One</span> </div> <div class="column"> <b>2</b> <span>Two</span> </div> </div> </div> </div>

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

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