使锚标签填充100%父母flex元素的高度和宽度,同时垂直和放大水平居中

时间:2017-01-22 12:36:35

标签: html css css3 sass flexbox

我有一个使用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

感谢您的帮助

2 个答案:

答案 0 :(得分:9)

不要害怕,也要固定一个弹性箱......

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

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

答案 1 :(得分:4)

align-items: center移除.content .panel--link并使.content .panel--link a.link成为flexbox并使用以下对象垂直

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

见下面的演示:

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;