简体   繁体   English

设置 width: auto 有什么不同吗? 和宽度:适合内容;?

[英]Is there any difference setting width: auto; and width: fit-content;?

From what I know, setting width: fit-content;据我所知,设置 width: fit-content; to the element simply means that the element width will take up the available space but never less than the min-content within the element.到元素只是意味着元素宽度将占用可用空间,但绝不会小于元素内的最小内容。 But, isn't the default width: auto;但是,默认宽度不是:auto; behaves the same as well?行为也一样吗?

Just wondering when should I use width: fit-content;?只是想知道什么时候应该使用 width: fit-content;?

width: auto is the default width for all elements, so the width will rely on display types (block, inline, etc.) to show respectively. width: auto是所有元素的默认宽度,所以宽度会根据显示类型(block、inline等)分别显示。

To make you understand better about width:auto , I put 2 code snippets below为了让您更好地了解width:auto ,我在下面放了 2 个代码片段

Case 1: Using div with display: block as default案例 1:默认使用带有display: blockdiv

 .container { border: 2px solid #ccc; padding: 10px; width: 20em; }.item { width: auto; background-color: lightblue; padding: 5px; margin-bottom: 1em; }
 <div class="container"> <div class="item">Item</div> <div class="item">Item with more text in it.</div> <div class="item">Item with more text in it, hopefully it's lengthy enough for the demo</div> </div>

Case 2: Using span with display: inline as default案例 2:默认使用span with display: inline

 .container { border: 2px solid #ccc; padding: 10px; width: 20em; }.item { width: auto; background-color: lightblue; padding: 5px; margin-bottom: 1em; }
 <div class="container"> <span class="item">Item</span> <span class="item">Item with more text in it.</span> <span class="item">Item with more text in it, hopefully it's lengthy enough for the demo</span> </div>

So basically, display: block will take up the whole width for element display, and display: inline and display: inline-block will align with content width.所以基本上, display: block会占据元素显示的整个宽度,而display: inlinedisplay: inline-block会与内容宽度对齐。 The next question is why we don't use display: inline or display: inline-block instead of width: fit-content ?下一个问题是为什么我们不使用display: inlinedisplay: inline-block而不是width: fit-content

Let's check the below code snippet for div with width: fit-content让我们检查下面的div代码片段width: fit-content

 .container { border: 2px solid #ccc; padding: 10px; width: 20em; }.item { width: fit-content; background-color: lightblue; padding: 5px; margin-bottom: 1em; }
 <div class="container"> <div class="item">Item</div> <div class="item">Item with more text in it.</div> <div class="item">Item with more text in it, hopefully it's lengthy enough for the demo</div> </div>

You can see that each content line has been shown with an individual color box, and it does not consume other lines' space可以看到每个内容行都用单独的颜色框显示,并且不占用其他行的空间

As your question作为你的问题

Just wondering when should I use width: fit-content;?只是想知道什么时候应该使用 width: fit-content;?

In my personal experience, I've been usually using width: fit-content;以我个人的经验,我通常使用width: fit-content; when I want to show tags with fit borders当我想显示带有合适边框的标签时

在此处输入图像描述

Source来源

All my tags can be shown in different lines but borders will be displayed respectively by content, so width: fit-content is a good choice for me.我所有的标签都可以显示在不同的行中,但边框将按内容分别显示,所以width: fit-content对我来说是个不错的选择。

P/s: Actually, we can achieve this design in other ways like using a wrapper, using flexbox, etc. But I personally like width: fit-content because it's simpler. P/s: 其实我们可以通过其他方式来实现这个设计,比如使用wrapper,使用flexbox等等。但是我个人比较喜欢width: fit-content ,因为这样更简单。 One thing I'd like to note is that this approach does not work well with Inte.net Explorer (check browser compatibility here )我想指出的一件事是这种方法不适用于 Inte.net Explorer(在此处检查浏览器兼容性)

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

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