简体   繁体   English

在CSS中使用适合内容自动调整宽度

[英]Auto-sizing width with fit-content in css

I'm trying to prevent a parent division to be smaller than it's children using min-width and fit-content . 我正在尝试使用min-widthfit-content防止父分区小于子分区。

I first setup a division .parent with a min-width: fit-content . 我首先用min-width: fit-content设置了一个.parent分区。 I, then added, a child with width: 100px and min-width: fit-content . 然后,我添加了一个width: 100pxmin-width: fit-content的孩子。 Finaly, I added enough characters to the children to bust the 100px ; 最后,我给孩子们添加了足够的字符来破坏100px

 .parent { border: 1px solid red; width: fit-content; } .children { border: 1px solid green; min-width: fit-content; width: 100px; } 
 <div class="parent"> <div class="children"> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa </div> </div> 

DEMO: https://codepen.io/osasseville/pen/NadrgB?editors=1100 演示: https : //codepen.io/osasseville/pen/NadrgB? editors =1100

I would expect the parent to fit the content of the children, which fit the content of the characters. 我希望父母适合孩子的内容,也适合角色的内容。

Strangely, if I change the children's width to 1%, the min-width is respected . 奇怪的是,如果我将孩子们的宽度更改为1%, 则必须遵守最小宽度

The code is working as it should. 该代码可以正常工作。 The parent div will only stretch its width up to the contents inside it, ie the child div. 父div只会将其宽度扩展到其内部内容,即子div。

And here you have defined the width of child div as 100px, so the parent div width will also expand only till 100px, and about the text, which is overflowing outside child div, is not considered as content for parent div. 并且在这里您已将子div的宽度定义为100px,因此父div的宽度也只会扩展到100px,并且大约在子div外部溢出的文本不被视为父div的内容。

If you want the parent to fit the content of the children, you should change the width of child div to fit-content 如果希望父项适合孩子的内容,则应将子div的宽度更改为适合内容

like this 像这样

 .parent { border: 1px solid red; width: fit-content; } .children { border: 1px solid green; width: fit-content; } 
 <div class="parent"> <div class="children"> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa </div> </div> 

you can also use display:inline-block; 您还可以使用display:inline-block; with Anupam Raut's answer: 与Anupam Raut的答案:

    <style>
        .parent {
          border: 1px solid red;
          width: fit-content;
          display: inline-block;
        }

        .children {
          border: 1px solid green;
          min-width: fit-content;
        }
    </style>

and html: 和html:

<div class="parent">
      <div class="children">
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      </div>
    </div>

Ok, so I am not sure I totaly understand what you want to do but we can start from this answer. 好的,所以我不确定我是否完全理解您想要做什么,但是我们可以从这个答案开始。 I dont't know how much you know about CSS so dont't be offended by my answer. 我不知道您对CSS有多少了解,所以不要被我的回答所冒犯。

  • First, in HTML most elements have, by default, two types of rendering (this is really simplified) : block-level or inline . 首先,在HTML中,大多数元素默认都有两种渲染类型(实际上是简化的): 块级inline A block-level element will take the width of its parent . 块级元素将采用其父的宽度 An inline element will take the width of its content . 内联元素将采用其内容的宽度

  • So if you understand that principle you'll see that having the parent element to be as wide as its children which is as wide as its content is pretty simple. 因此,如果您了解该原理,您会发现,将父元素的宽度与其子元素的宽度以及其内容的宽度一样简单。 Here is an exemple: 这是一个例子:

 .parent { border: 1px solid red; /* This will make the parent as wide as its content */ display: inline-block; } .children { border: 1px solid green; /* This is just so that we see if it's working */ max-width: 100px; } 
 <div class="parent"> <div class="children"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </div> 

Now, of course there are other ways to do it, but this is the simplest solution. 现在,当然还有其他方法可以做到这一点,但这是最简单的解决方案。 The best solution will depend on your context. 最佳解决方案将取决于您的上下文。

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

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