提问者:小点点

将列表宽度调整为包含链接而不是包含子列表


我有一个水平的flex列表,每个项目都包含一个链接和另一个子列表。

我希望第一代项目li的宽度与包含链接中的单词一样宽,子列表的宽度与可点击和不换行单词所需的宽度一样宽。

尝试了选项1:我将第一个li的宽度设置为40px,将子ul的宽度设置为200px-->;子链接不再可点击。

尝试了选项2:我将子ul的宽度设置为10px,并设置white-space:nowrap;position:relative;z-index:5;指向最终链接-->;子链接是可点击的。。。但是,这是最好的做法吗?

<ul id="menu-mobile-en" class="menu">
    <li id="menu-item-20858" class="retail-only-element menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-20858 focus"><a href="#">SHOP</a><button aria-expanded="false" class="dropdown-toggle"><span class="screen-reader-text">Expand child menu</span></button>
        <ul class="sub-menu toggled-on">
            <li id="menu-item-9404" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9404"><a href="https://staging.mysite.com/all/">All</a></li>
            <li id="menu-item-39924" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-39924"><a href="https://staging.mysite.com/eshop2/new/">New</a></li>
            <li id="menu-item-4352" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4352"><a href="https://staging.mysite.com/eshop2/boxers-for-men/">Boxers</a></li>
            <li id="menu-item-4357" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4357"><a href="https://staging.mysite.com/eshop2/briefs/">Briefs</a></li>
            <li id="menu-item-4358" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4358"><a href="https://staging.mysite.com/eshop2/tank-tops/">Tank Tops</a></li>
            <li id="menu-item-4355" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4355"><a href="https://staging.mysite.com/eshop2/t-shirts/">T-shirts</a></li>
            <li id="menu-item-4353" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4353"><a href="https://staging.mysite.com/eshop2/henleys/">Henleys</a></li>
            <li id="menu-item-4354" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4354"><a href="https://staging.mysite.com/eshop2/longjohns/">Longjohns</a></li>
            <li id="menu-item-4356" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4356 focus"><a href="https://staging.mysite.com/eshop2/accessories/">Accessories</a></li>
            <li id="menu-item-34091" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-34091"><a href="https://staging.mysite.com/eshop2/last-chance/">Last chance</a></li>
        </ul>
    </li>
</ul>

CSS:

/* Inner toggled menu */
.handheld-navigation {
    color: black;
    position: fixed;
    width: 100%;
    top: 85px;
    bottom: 0;
    background-color: white;
    font-size: 15px;
    letter-spacing: 3px
}
.handheld-navigation ul.menu {
    display:flex;
    margin-left: 7px;
    margin-top: 0px;
}
.handheld-navigation ul.menu a {
    font-weight: 900;
    display: inline-block;
}
.handheld-navigation ul.menu li.menu-item a {
    padding: 8px 0;
}
.handheld-navigation ul.menu li.menu-item ul.sub-menu {
    display:flex;
    flex-direction: column;
    margin-left: 0;
}
.handheld-navigation ul.menu li.menu-item ul.sub-menu li a {
    font-weight: normal;
    color: black;
    text-transform: uppercase;
}

共1个答案

匿名用户

这看起来有帮助吗?

注意:运行代码段并检查元素。

null

/* Inner toggled menu */

ul.menu {
  display: flex;
  margin-left: 7px;
  margin-top: 0px;
}

ul.menu a {
  font-weight: 900;
  display: inline-block;
}

ul.menu button {
  position: absolute;
  right: 10px;
  top: 10px;
}

ul.menu li.menu-item a {
  padding: 8px 0;
}

ul.menu li.menu-item ul.sub-menu {
  display: flex;
  position: absolute;
  flex-direction: column;
  margin-left: 0;
}

ul.menu li.menu-item ul.sub-menu li a {
  font-weight: normal;
  color: black;
  text-transform: uppercase;
}
<ul id="menu-mobile-en" class="menu">
  <li id="menu-item-20858" class="retail-only-element menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-20858 focus"><a href="#">SHOP</a><button aria-expanded="false" class="dropdown-toggle"><span class="screen-reader-text">Expand child menu</span></button>
    <ul class="sub-menu toggled-on">
      <li id="menu-item-9404" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9404"><a href="https://staging.mysite.com/all/">All</a></li>
      <li id="menu-item-39924" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-39924"><a href="https://staging.mysite.com/eshop2/new/">New</a></li>
      <li id="menu-item-4352" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4352"><a href="https://staging.mysite.com/eshop2/boxers-for-men/">Boxers</a></li>
      <li id="menu-item-4357" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4357"><a href="https://staging.mysite.com/eshop2/briefs/">Briefs</a></li>
      <li id="menu-item-4358" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4358"><a href="https://staging.mysite.com/eshop2/tank-tops/">Tank Tops</a></li>
      <li id="menu-item-4355" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4355"><a href="https://staging.mysite.com/eshop2/t-shirts/">T-shirts</a></li>
      <li id="menu-item-4353" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4353"><a href="https://staging.mysite.com/eshop2/henleys/">Henleys</a></li>
      <li id="menu-item-4354" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4354"><a href="https://staging.mysite.com/eshop2/longjohns/">Longjohns</a></li>
      <li id="menu-item-4356" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4356 focus"><a href="https://staging.mysite.com/eshop2/accessories/">Accessories</a></li>
      <li id="menu-item-34091" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-34091"><a href="https://staging.mysite.com/eshop2/last-chance/">Last chance</a></li>
    </ul>
  </li>
</ul>