代码之家  ›  专栏  ›  技术社区  ›  Scoob

在flexbox中不工作的锚点元素上变换比例

  •  1
  • Scoob  · 技术社区  · 7 年前

    我使用flexbox作为我页面顶部的导航栏。我只包括这一部分的代码,因为该项目是一个完整的网站。我网站上的所有锚标签样式都相同,使用相同的 transform: scale(1.2)

    此外,在这个代码笔上,flexbox似乎不尊重 justify-content: space-around

    enter image description here

    https://codepen.io/colesam/pen/YxLPVW

    a {
      color: #646c84;
      font-weight: 500;
      line-height: 1.7vw;
      transition: all 0.2s;
    }
    
    a:hover {
      color: #ffaf54;
      cursor: pointer;
      transform: scale(1.2);
      text-decoration: none;
    }
    
    a:focus {
      color: #646c84;
      text-decoration: none;
    }
    
    a:focus:hover {
      color: #ffaf54;
      cursor: pointer;
    }
    
    .active-indicator {
      background: #ffaf54;
      border-radius: 25px;
      height: 2px;
      margin-top: -2px;
      margin-bottom: 5px;
      margin-left: auto;
      margin-right: auto;
      opacity: 0;
      transition: all 0.2s;
      width: 25px;
    }
    
    .active-indicator.active {
      opacity: 1;
    }
    
    #menu {
      background: whitesmoke;
      border: 1px solid rgba(0, 0, 0, 0.1);
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      padding: 0 25vw;
      position: fixed;
      left: -1;
      right: -1;
      top: 0;
      transition: all 0.2s;
      z-index: 1;
    }
    
    #menu a {
      font-size: 0.9vw;
    }
    
    #menu.inactive {
      opacity: 0;
    }
    <div id="menu">
      <div id="landing-nav">
        <a>Home</a>
        <div class="active-indicator active"></div>
      </div>
      <div id="about-nav">
        <a>About</a>
        <div class="active-indicator"></div>
      </div>
      <div id="portfolio-nav">
        <a>Portfolio</a>
        <div class="active-indicator"></div>
      </div>
      <div id="resume-nav">
        <a>Resume</a>
        <div class="active-indicator"></div>
      </div>
      <div id="contact-nav">
        <a>Contact</a>
        <div class="active-indicator"></div>
      </div>
    </div>
    2 回复  |  直到 7 年前
        1
  •  2
  •   Michael Benjamin William Falcon    7 年前

    您的导航菜单没有额外的宽度,因此弹性项打包在一起。

    #menu { padding: 0 25vw; }
    

    但这只会在容器的左右两侧添加填充。物品仍然打包在一起,并且 justify-content 没有工作空间。

    不要使用填充,尝试使用类似 width: 50vw .

    问题在于 transform: scale() 是将其应用于内联级元素( a

    display: block 或将变换应用于父div。

    参考文献:

        2
  •  1
  •   sol    7 年前

    您对固定导航使用了无效的属性值。

    代替

    left: -1;
    right: -1;
    

    具有

    left: 0;
    right: 0;
    

    transform: scale 不工作,但可以通过更改 div 将定位标记包装到 flex

    Codepen