无法在flex div容器内的两行中显示h3标记

时间:2016-12-07 10:45:07

标签: html css flexbox

我正在使用以下示例,我需要在页面中为每个项目使用两个标题。 <div>内的li div定义为flex,如果我添加多个元素,则将其显示为两列,但我希望在两行中居中对齐水平&amp;垂直。

如果我将flex更改为阻止或其他属性,则标题将移至顶部,而我希望它们位于中心

不确定如何用最小的CSS修复它

http://codepen.io/anon/pen/XNqYWd

<ul class="cbp-rfgrid">
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
             <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
             <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
         <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
</ul>

1 个答案:

答案 0 :(得分:1)

使用flex-directionjustify-content属性,例如:

.cbp-rfgrid li a div {
  flex-direction: column; /* Flex Direction Column */
  justify-content: center; /* Vertically Aligns Content (on flex-direction: column) */
}

查看下面的代码段(使用全屏):

&#13;
&#13;
.cbp-rfgrid {
	margin: 35px 0 0 0;
	padding: 0;
	list-style: none;
	position: relative;
	width: 100%;
}

.cbp-rfgrid li {
	position: relative;
	float: left;
	overflow: hidden;
	width: 16.6666667%; /* Fallback */
	width: -webkit-calc(100% / 6);
	width: calc(100% / 4);
  margin:1px;
}

.cbp-rfgrid li a,
.cbp-rfgrid li a img {
	display: block;
	width: 100%;
	cursor: pointer;
}

.cbp-rfgrid li a img {
	max-width: 100%;
}

/* Flexbox is used for centering the heading */
.cbp-rfgrid li a div {
	position: absolute;
	left: 20px;
	top: 20px;
	right: 20px;
	bottom: 20px;
	background: rgba(71,163,218,0.2);
	display: -webkit-flex;
	display: -moz-flex;
	display: -ms-flex;
	display: flex;
    flex-direction: column;
    justify-content: center;
	-webkit-align-items: center;
	-moz-align-items: center;
	-ms-align-items: center;
    align-items: center;
    text-align: center;
    opacity: 0;
}

.cbp-rfgrid li a:hover div {
	opacity: 1;
}

.cbp-rfgrid li a div h3 {
	width: 100%;
	color: #fff;
	text-transform: uppercase;
	font-size: 1.4em;
	letter-spacing: 2px;
	padding: 0 10px;
}

/* Example for media query: change number of items per row */

@media screen and (max-width: 1190px) {
	.cbp-rfgrid li {
		width: 20%; /* Fallback */
		width: -webkit-calc(100% / 5);
		width: calc(100% / 5);
	}
}

@media screen and (max-width: 945px) {
	.cbp-rfgrid li {
		width: 25%; /* Fallback */
		width: -webkit-calc(100% / 4);
		width: calc(100% / 4);
	}
}

@media screen and (max-width: 660px) {
	.cbp-rfgrid li {
		width: 33.3333333%; /* Fallback */
		width: -webkit-calc(100% / 3);
		width: calc(100% / 3);
	}
}

@media screen and (max-width: 660px) {
	.cbp-rfgrid li {
		width: 33.3333333%; /* Fallback */
		width: -webkit-calc(100% / 3);
		width: calc(100% / 3);
	}
}

@media screen and (max-width: 400px) {
	.cbp-rfgrid li {
		width: 50%; /* Fallback */
		width: -webkit-calc(100% / 2);
		width: calc(100% / 2);
	}
}

@media screen and (max-width: 300px) {
	.cbp-rfgrid li {
		width: 100%;
	}
}
&#13;
<ul class="cbp-rfgrid">
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
             <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
             <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
         <h3>Heading One</h3>
      <h3>Date</h3>
      </div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="http://placehold.it/600x400">
      <div>
        <h3>Felis catus</h3></div>
    </a>
  </li>
</ul>
&#13;
&#13;
&#13;

希望这有帮助!