将边框添加到与基线对齐的行中的弹性项目

时间:2016-09-12 19:18:19

标签: html css css3 flexbox

问题:如何让边框延伸到父级和环绕子级别的高度。鉴于我正在使用flexbox并与基线对齐。

这可以用所有的css完成吗?

更多详情: 我正在使用弹性框将每行中的项目与特定基线对齐。它以每个细胞为中心,位于这里的中心位置。在下图中:

using flexbox centered on "center here"

当我当前悬停在每个项目上时,它会以实线悬停,每个单元格周围有10px的边距:

hover

我希望悬停边框在悬停时延伸到行的顶部和底部,如下所示:

enter image description here

当我将鼠标悬停在每个项目上时,他们的悬停边框就像这样排列:

enter image description here

^如果我在每个单元格上悬停,这就是我想要的样子。

codepen



/* on hover i want this to be a box around the item, and to have the top and the bottom of the border be touching the top/bottom of the row it is on */
.flex-item:hover {
  border: 1px solid darkred;
}

/* so item doesn't move when not hovering */
.flex-item {
  border: 1px solid lightblue;
}

.flex-container {
  overflow: hidden;    position: relative;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: baseline;
  align-items: baseline;
  width: 400px;
  height: 350px;
  background-color: lightblue;
  flex-wrap: wrap;
  padding: 10px;
}

.inner-wrapper {
  display: inline-block;
  text-align: center;
  width: 100px;
  margin: 10px;
  background-color: cornflowerblue;
}

.flex-body {
  border-bottom: 1px solid #fff;
}
.flex-body-more {
  float: left;
  clear: left;
  width: 100%;
}
.flex-img {
  justify-content: center;
  align-items: center;
}

  <div class="flex-container">
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1 longer title</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x90/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text more text more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x50/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
  </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要根据弹性项目调整一些规则:

.flex-item {
    border: 1px solid lightblue;
    transition: .7s;
    display: flex;
    align-items: flex-end;
}

.flex-container {
    overflow: hidden;
    position: relative;
    display: -webkit-flex;
    display: flex;
    width: 400px;
    height: 350px;
    background-color: lightblue;
    flex-wrap: wrap;
    padding: 10px;
    justify-content: flex-start;
}

请参阅下面的代码段:

&#13;
&#13;
/* on hover i want this to be a box around the item, and to have the top and the bottom of the border be touching the top/bottom of the row it is on */
.flex-item:hover {
  border: 1px solid darkred;
}

/* so item doesn't move when not hovering */
.flex-item {
    border: 1px solid lightblue;
    transition: .7s;
    display: flex;
    align-items: flex-end;
}

.flex-container {
    overflow: hidden;
    position: relative;
    display: -webkit-flex;
    display: flex;
    width: 400px;
    height: 350px;
    background-color: lightblue;
    flex-wrap: wrap;
    padding: 10px;
    justify-content: flex-start;
}

.inner-wrapper {
  display: inline-block;
  text-align: center;
  width: 100px;
  margin: 10px;
  background-color: cornflowerblue;
}

.flex-body {
  border-bottom: 1px solid #fff;
}
.flex-body-more {
  float: left;
  clear: left;
  width: 100%;
}
.flex-img {
  justify-content: center;
  align-items: center;
}
&#13;
<div class="flex-container">
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1 longer title</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x90/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text more text more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
    <div class="flex-item">
      <div class="inner-wrapper">
        <div class="flex-title">flex title1</div>
        <div class="flex-img">
          <img src='http://dummyimage.com/40x50/000/ffffff&text=hi'>
        </div>
        <div class="flex-body">center here</div>
        <div class="flex-body-more">more text</div>
      </div>
    </div>
  </div>
&#13;
&#13;
&#13;