简体   繁体   English

如何在不改变内部 div 位置的情况下包装多个 div

[英]How to wrap multiple div without changing inner div posittion

So I'm having this issue, I'm coding a basic website using NextJS, this is an example of my code所以我遇到了这个问题,我正在使用 NextJS 编写一个基本网站,这是我的代码示例

I have a set of div like this with relative positioning我有一组这样的具有相对定位的 div

<div>one</div>
<div>two</div>
<div>three</div>

output it looks something like this output 它看起来像这样

one two three

Now I want to wrap all 3 divs inside another div with absolute positioning so I can change its position freely across the screen现在我想用绝对定位将所有 3 个 div 包装在另一个 div 中,这样我就可以在屏幕上自由更改它的 position

<div>
  <div>one</div>
  <div>two</div>
  <div>three</div>
</div>

But doing that will change inner div layout to this但是这样做会将内部 div 布局更改为此

one
two
three

So basically, how do I wrap multiple divs inside another div without changing inner div position?所以基本上,如何在不更改内部 div position 的情况下将多个 div 包装在另一个 div 中?

Try this in your CSS code for the inner divs:内部div 的 CSS 代码中试试这个:

display: inline-block;

and this for the outer div:这对于外部div:

width: fit-content;

See this jsfiddle demonstration看到这个 jsfiddle 演示

Something like this:像这样的东西:

 .parent{ position: absolute; }.child{ display: inline-block }
 <div class="parent"> <div class="child">One</div> <div class="child">Two</div> <div class="child">Three</div> </div>

I think the problem is that div is not the right tag for your purpose.我认为问题在于div不适合您的目的。 You might want to use span instead.您可能想改用span

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

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