简体   繁体   English

如何使导航项适应小屏幕尺寸

[英]How to make a nav items to adapt to small screen size

在此处输入图像描述

I am trying to make nav items adaptive to the smaller screens.我正在尝试使导航项适应较小的屏幕。 I have tried to give to parent element max-width 100% but it doesn't work.我试图给父元素 max-width 100% 但它不起作用。 It only works when I turn display: flex;它仅在我打开 display: flex 时有效; off.离开。 I need nav items to be restricted by a parent widht.我需要导航项受父宽度限制。

I have the following code:我有以下代码:

HTML HTML

<nav class="tabs">
    <li>
        <a href="#" class="tab active">
            All
            <span class="badge grey">151</span>
        </a>
    </li>

    <li>
        <a href="#" class="tab">
            Started
            <span class="badge grey">128</span>
        </a>
    </li>

    <li>
        <a href="#" class="tab">
            On Hold
            <span class="badge grey">15</span>
        </a>
    </li>

    <li>
        <a href="#" class="tab">
            Completed
            <span class="badge grey">8</span>
        </a>
    </li>
</nav>

CSS: CSS:

    .tabs {
      display: flex;
      border-bottom: 1px solid var(--color-border);
      gap: 1.5rem;
      flex-wrap: nowrap;
      overflow-x: auto;
    }
    
    .tabs > * {
      flex-shrink: 0;
    }
    
    .tabs .tab {
      padding: 1rem 0;
      display: flex;
      align-items: center;
      gap: 0.5rem;
      position: relative;
    }
    
    .tabs .tab.active {
      color: var(--color-heading);
    }
    
    .tabs .tab.active::after {
      content: "";
      width: 100%;
      height: 2px;
      background-color: var(--color-primary);
      position: absolute;
      bottom: 0;
      border-radius: 2px 2px 0 0;
    }

So in this image you may see the result I am trying to achieve but without turning the flex off:因此,在此图像中,您可能会看到我试图实现但没有关闭 flex 的结果:

在此处输入图像描述

You should try using @media (max-width: ...) and there decrease padding , font-size , etc to get wanted result您应该尝试使用@media (max-width: ...)并减少paddingfont-size等以获得想要的结果

The flex will force all elements to be on the same row. flex 将强制所有元素位于同一行。 You can use flex-wrap to let items optionally go to the next row.您可以使用flex-wrap让项目可选 go 到下一行。

.tabs {
  display: flex;
  border-bottom: 1px solid var(--color-border);
  gap: 1.5rem;
- flex-wrap: nowrap;
+ flex-wrap: wrap;
  overflow-x: auto;
}

You can also choose to optionally set flex-direction: column for small screens using MichaelLearner's answer.您还可以选择有选择地设置flex-direction: column for small screens using MichaelLearner's answer。

Assuming that you want the flex items to show in a different way on a big streen than on a small screen, consider changing the direction of the boxes from row (default to column:假设您希望弹性项目在大屏幕上以不同于在小屏幕上的方式显示,请考虑将框的方向从行更改(默认为列:

Desktop:桌面:

.tabs { /* for desktop monitors with lots of with */
  display: flex;
  flex-direction: row;  
}

Mobile:移动的:

.tabs { /* for thin tall mobile phones */
  display: flex;
  flex-direction: column;  
}

 .tabs { display: flex; flex-direction: column; /* setting this to row or column will change the flow of boxes */ border-bottom: 1px solid var(--color-border); gap: 1.5rem; flex-wrap: nowrap; overflow-x: auto; }.tabs > * { flex-shrink: 0; }.tabs.tab { padding: 1rem 0; display: flex; align-items: center; gap: 0.5rem; position: relative; }.tabs.tab.active { color: var(--color-heading); }.tabs.tab.active::after { content: ""; width: 100%; height: 2px; background-color: var(--color-primary); position: absolute; bottom: 0; border-radius: 2px 2px 0 0; }
 <nav class="tabs"> <li> <a href="#" class="tab active"> All <span class="badge grey">151</span> </a> </li> <li> <a href="#" class="tab"> Started <span class="badge grey">128</span> </a> </li> <li> <a href="#" class="tab"> On Hold <span class="badge grey">15</span> </a> </li> <li> <a href="#" class="tab"> Completed <span class="badge grey">8</span> </a> </li> </nav>

PS.附言。 It appears you were trying to insert an image?看来您正在尝试插入图像? (first line) please copy paste it again, directly into the editor. (第一行)请再次复制粘贴,直接进入编辑器。 Stack Overflow allows for that, Also. Stack Overflow 也允许这样做。 please add what you exactly want to see in the answer.请在答案中添加您真正想看到的内容。

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

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