简体   繁体   English

宽度:适合内容; 在 Chrome 上工作但不在资源管理器上工作

[英]width: fit-content; working on Chrome but not explorer

I built an app and everything is displayed perfectly in Chrome, but if I open the app in Windows Explorer the containers are smaller than they should be.我构建了一个应用程序,所有内容都在 Chrome 中完美显示,但如果我在 Windows Explorer 中打开该应用程序,容器比它们应该的要小。

I'm using width: fit-content .我正在使用width: fit-content Is this something that only works with Chrome.这是只适用于 Chrome 的东西吗?

How can I make it so that it works with all browsers?我怎样才能使它适用于所有浏览器?

width: fit-content is still in experimental stages, and is currently supported on Chrome 46.x and above (without vendor prefix), FF 3.x and above (with vendor prefix). width: fit-content仍处于试验阶段,目前支持 Chrome 46.x 及以上(无供应商前缀)、FF 3.x 及以上(有供应商前缀)。 Not supported on either IE or edge. IE 或 Edge 都不支持。
You can refer to compatibility chart here: Browser compatibility for width:fit-content您可以在此处参考兼容性图表: 宽度的浏览器兼容性:适合内容

One alternative is to use display: table , which has the same effect as width: fit-content .一种替代方法是使用display: table ,它与width: fit-content具有相同的效果。 Of course, there are other ways that you can try depending on what your requirements are.当然,根据您的要求,您还可以尝试其他方法。

 #fit-content { width: fit-content; background: pink; } #table { display: table; background: lightblue; } #normal { background: green; }
 <div id="fit-content"> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"> fit-content </div> <div id="table"> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"> table </div> <div id="normal"> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"> normal </div>

It can be done with width:auto but only when an element is display: inline-block as well as setting the value as right:inherit or left:inherit .它可以使用width:auto完成,但仅当元素display: inline-block以及将值设置为right:inheritleft:inherit

Example:示例:

.my_div {
    width: auto;
    display: inline-block;
    right:inherit; /* align to left */
}

Have similar problem.有类似的问题。 In one div, have others with display:inline-block在一个 div 中,让其他人使用display:inline-block

For parent div use white-space : nowrap;对于父 div 使用white-space : nowrap;

Don't forget to set to child's div white-space : normal;不要忘记设置为孩子的 div white-space : normal;

.parent {
    width: max-content;
    white-space : nowrap;
}
.parent .child {
    display: inline-block;
    white-space : normal;
}

HTML example HTML 示例

<div class='parent'>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
</div>

another alternative is to use width:auto;另一种选择是使用width:auto; This works sometimes but it depends on your specific case.这有时有效,但这取决于您的具体情况。

I had a similar problem, fixed it this way.我有一个类似的问题,以这种方式修复它。 Instead of using width: fit-content;而不是使用width: fit-content; you can use the snippet below.您可以使用下面的代码段。

inline-flex is well supported (even on IE11). inline-flex得到很好的支持(即使在 IE11 上)。

In the example below, flex: none;在下面的例子中, flex: none; is only useful if the parent is set as a display: flex;仅当父项设置为display: flex;时才有用display: flex; . .

.element {
  display: inline-flex;
  flex: none;
  width: auto;
}

ops.操作。 my answer came in 4yrs later.我的答案是在 4 年后出现的。

I was building mobile page and had the issue with width: fit-content;我在构建移动页面时遇到了width: fit-content; . . the code below worked for me:下面的代码对我有用:

header nav ul {
   display: table;
   margin: 0 auto;
}

display: inline-table对我有用

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

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