当悬停在 children 上时,防止 flexbox 列表项更改 parent 的宽度

Prevent flexbox list items changing width of parent when hover over children

我有一系列列表项,有些有一个 child 列表,其中包含子导航链接。我正在使用 CSS 通过在 child <ul> 上设置 display: none 来创建悬停效果,然后在悬停时设置为 display: block

然而,这会导致 parent <li> 在悬停期间变大。有没有办法防止这种情况?将鼠标悬停在没有 child 列表的 <li> 项上时,它们的宽度保持不变。

<ul class="flex unstyled-list">
  <li><a href="/">link</a></li>
  <li><a href="/" target="_blank">link 2</a></li>
  <li>
    <a href="/">parent 1</a>
    <ul class="unstyled-list">
      <li><a href="/">Hgdfgdf</a></li>
      <li><a href="/">Hgdfgdf</a></li>
    </ul>
  </li>
  <li>
    <a href="/">parent 2</a>
    <ul class="unstyled-list">
      <li><a href="/">Hgdfgdf gdf ggf dfs</a></li>
      <li><a href="/">Hgdfgdf gdf gdf gfd</a></li>
    </ul>
  </li>
</ul>


.unstyled-list {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

.flex {
  width: 970px;
  height: 37px;
  display: flex;
  flex: 1 0 auto;
  flex-basis: 100%;
  background-color: #000;
}

.flex li {
  flex-grow: 1;
  height: 37px;
  line-height: 37px;
  color: #fff;
  text-align: center;
}

#nav-strip li:hover {
  background-color: #000;
}

.flex li:hover ul {
  display: block;
}

.flex li a {
  display: block;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
}

.flex li ul {
  display: none;
  width: 200px;
  padding: 0;
}

.flex li ul li {
  width: 185px;
  height: 30px;
  line-height: 30px;
  padding: 0 0 0 15px;
  background: #000;
  text-align: left;
}

.flex li ul li:hover {
  background: #1e1d3f;
}

.flex li ul li a {
  display: block;
  font-size: 12px;
  font-weight: normal;
}

可运行示例:

https://jsfiddle.net/ndp1y79b/1/

感谢@Temani Afif 和@Pete - 添加

positon: absolute;
top: 100%

在 child <ul> 上已修复此问题。谢谢 - 比我想象的要简单得多!