【问题标题】:Make anchor tag fill 100% height and width of parent flex element, whilst being vertically & horizontally centered使锚标记填充父 flex 元素的 100% 高度和宽度,同时垂直和水平居中
【发布时间】:2017-06-07 01:17:58
【问题描述】:

我有一个使用 flexbox 构建的布局,但有一个方面我似乎无法开始工作。

我的面板中有锚标签,我希望它们垂直和水平居中,但我希望锚标签是面板宽度和高度的 100%,这样当您单击面板中的任何位置时,它将链接您,而不是仅单击链接文本。

这是面板的 HTML:

<div class="panel--link panel--one">
    <a href="#" class="link">
        Panel 1
    </a>
</div>

还有 SCSS:

.panel {
    box-sizing: content-box;
    flex: 1;
    border: 3px solid #fff;
    padding: 0px;
    text-align: center;
}

.panel--link {
    @extend .panel;
    display: flex;  
    flex: 1;
    justify-content: center;
    align-items: center;

    a.link {
        text-decoration: none;
        color: #fff;
        text-transform: uppercase;
        font-weight: 600;
        font-size: 1rem;
        letter-spacing: 3px;
        flex: 1;
    }
}

查看我的 Codepen 以了解整个布局,以便更好地理解它!

http://codepen.io/zauber/pen/BpRJQG

感谢您的帮助

【问题讨论】:

    标签: html css sass flexbox


    【解决方案1】:

    别害怕,也可以制作锚 flexbox:

    a {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    

    并且您应该将align-items: stretch 添加到锚的父级。

    【讨论】:

    • 您好,感谢您的回答 - 这在 Chrome 中有效,但在 Safari 中无效。我认为原因是在 Safari 中,它查看父元素(面板)的高度并没有看到,因为面板的高度是由 flex 设置的。这就是我遇到的问题!
    【解决方案2】:

    .content .panel--link 中删除align-items: center 并将.content .panel--link a.link 也设为flexbox 并将其垂直 对齐,使用:

    display: flex;
    justify-content: center;
    align-items: center;
    

    请看下面的演示:

    body {
      height: 800px;
    }
    .main {
      height: 100%;
    }
    .content {
      height: 100%;
      border: 20px solid #fff;
      display: flex;
      box-sizing: border-box;
    }
    .content .column {
      flex-direction: column;
      width: 33.33333%;
      background: #374550;
      display: flex;
    }
    .content .panel,
    .content .panel--link {
      box-sizing: content-box;
      flex: 1;
      border: 3px solid #fff;
      padding: 0px;
      text-align: center;
    }
    .content .panel .logo,
    .content .panel--link .logo {
      margin: 7.5% 0px 0% 0%;
      width: 80%;
    }
    .content .panel .blurb,
    .content .panel--link .blurb {
      width: 80%;
      margin: 7.5% auto 0% auto;
    }
    .content .panel .blurb h1,
    .content .panel--link .blurb h1 {
      text-decoration: none;
      color: rgba(255, 255, 255, 0.6);
      font-weight: lighter;
      line-height: 220%;
      font-size: 0.85rem;
      margin: 0px;
    }
    .content .panel .tel,
    .content .panel--link .tel {
      margin: 7.5% auto 5% auto;
    }
    .content .panel .tel h2,
    .content .panel--link .tel h2 {
      text-decoration: none;
      color: #fff;
      font-weight: 500;
      font-size: 1.2rem;
      margin: 0px;
    }
    .content .panel--link {
      display: flex;
      flex: 1;
      justify-content: center;
      /*align-items: center;*/
      text-align: center;
    }
    .content .panel--link a.link {
      text-decoration: none;
      color: #fff;
      text-transform: uppercase;
      font-weight: 600;
      font-size: 1rem;
      letter-spacing: 3px;
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    @media (max-width: 768px) {
      .content .column--two {
        order: -1;
      }
      .content .column--two .panel--two {
        order: -1;
      }
    }
    .content .column--two .panel--one {
      flex-grow: 1;
    }
    .content .column--two .panel--two {
      flex-grow: 2;
      background: #374550;
    }
    .content .column--two .panel--three {
      flex-grow: 1;
    }
    <main class="content">
      <div class="column column--one">
    
        <div class="panel--link panel--one">
          <a href="#" class="link">
                            Panel 1
                        </a>
        </div>
    
        <div class="panel--link panel--two">
          <a href="#" class="link">
                            Panel 2
                        </a>
        </div>
      </div>
      <div class="column column--two">
        <div class="panel--link panel--one">
          <a href="" class="link">
                            Panel 3
                        </a>
        </div>
        <div class="panel panel--two">
    
          <div class="blurb">
            <h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lectus orci, imperdiet ac auctor non, tristique eget augue. Curabitur quis gravida lorem, sed maximus purus. Nunc sit amet mollis turpis.</h1>
          </div>
          <div class="tel">
            <h2>123 456 789</h2>
          </div>
        </div>
        <div class="panel--link panel--three">
          <a href="#" class="link">
                            Panel 4
                        </a>
        </div>
      </div>
      <div class="column column--three">
        <div class="panel--link panel--one">
          <a href="#" class="link">
                            Panel 5
                        </a>
        </div>
        <div class="panel--link panel--two">
          <a href="#" class="link">
                            Panel 6
                        </a>
        </div>
      </div>
    </main>

    【讨论】:

      猜你喜欢
      • 2014-07-12
      • 1970-01-01
      • 2011-05-16
      • 2019-05-04
      • 2018-02-28
      • 2011-11-24
      • 2017-05-28
      • 2018-09-11
      • 2022-12-31
      相关资源
      最近更新 更多