css - 减少列表项宽度以占据 Internet Explorer 11 中文本内容的确切大小

标签 css internet-explorer

我一直在寻找一种方法来删除通常在链接元素的换行期间创建的空白,并提出了 flex-basis: 0 解决方案,该解决方案适用于主要版本IE11 以外的浏览器。

这是我上面提到的空白的例子:enter image description here

如果有人能提出同样支持 IE11 的类似可能性,我将不胜感激

.menu {
  display: flex;
}

.menuItem {
  list-style: none;
  padding-right: 30px;
}

.menuItem a {
  position: relative;
  padding-right: 20px;
  display: block;
  background-color: #ccc;
}

.menuItem a:after {
  content: '\25be';
  position: absolute;
  right: 0;
  top: 0;
}

@media (max-width: 1100px) { 
  .noWhiteSpace .menuItem {
    flex-basis: 0;
  } 
}
<ul class='menu'>
  <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some MoreText</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
</ul>

<h2>No White Space</h2>
<ul class='menu noWhiteSpace'>
  <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some MoreText</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
</ul>

更新 flex-basis: auto: 下面的方法确实恢复了列表项的宽度,但又带回了元素在分成两行时创建空白的问题。 enter image description here

最佳答案

我建议您为 Internet Explorer 使用 flex-basis: auto;

Reason is, when a non-auto flex-basis is specified, Internet Explorer 10 and 11 always uses a content-box box model to calculate the size of a flex item.

引用:

Browser compatibility for flex-basis

修改后的代码:

<!DOCTYPE html>
<html>
<head>
<style>
.menu {
  display: flex;
}

.menuItem {
  list-style: none;
  padding-right: 30px;
}

.menuItem a {
  position: relative;
  padding-right: 20px;
  display: block;
  background-color: #ccc;
}

.menuItem a:after {
  content: '\25be';
  position: absolute;
  right: 0;
  top: 0;
}

@media (max-width: 1100px) { 
  .noWhiteSpace .menuItem {
    flex-basis: auto;
  } 
}
</style>
</head>
<body>
<ul class='menu'>
  <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some MoreText</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
</ul>

<h2>No White Space</h2>
<ul class='menu noWhiteSpace'>
  <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some MoreText</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
    <li class='menuItem'>
    <a target='_' href='https://www.google.com'>Some Text</a>
  </li>
</ul>


</body>
</html>

IE 11 中的输出:

enter image description here

关于css - 减少列表项宽度以占据 Internet Explorer 11 中文本内容的确切大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202768/

相关文章:

html - div 溢出和 z-index

javascript - 用JS给div添加自动刷新

css - 如何修复我的网站以使其在移动设备上运行?

css - 悬停在 IE 中不起作用

internet-explorer - 为什么 MS 过滤器(dropShadow、glow 或 shadow)对 IE 中的 <span> 标签不起作用?

delphi - 检测 IE 代理设置并与 TIdHTTP 一起使用

php - 在 WordPress 用户配置文件中隐藏 WooCommerce 账单和运输字段

html - 可滚动内容到百分比大小的 div 中

jquery - 在 IE7 和 iE8 中的输入框中的任意位置单击时占位符文本不会消失

CSS Calc 似乎破坏了 IE10 中的整个浏览器?虽然适用于 IE9&11