控制flexbox中的链接包装项

时间:2018-02-04 23:39:26

标签: html css flexbox

我在此容器中设置为display: flex的容器是用链接包装的项目。我希望使用div类名称而不是a属性来控制项目。这是我的代码:



.container {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  width: 100%;
  margin-bottom: 2rem;
  justify-content: space-between;
  flex-flow: wrap;
}

.container a {
  background-color: red;
  width: 48%;
  margin-bottom: 2%;
}

<div class="container">
  <a href="google.com"><div class="item">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div></a>
  <a href="google.com"><div class="item">It is a long established fact that a reader will be distracted by the readable content of a page.</div></a>
</div>
&#13;
&#13;
&#13;

这完全有效,但是,我想用div类名称项替换.container a。这样做的原因是在维护代码时我认为它会让人看到我的CSS。如何制作,以便我可以用[{1}}替换选择器.container a,它仍然可以正常工作?注意:整个div块必须是链接。这是jsFiddle

3 个答案:

答案 0 :(得分:1)

您可以将类添加到A标记中。甚至完全放弃div。

.container {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  /*width: 100%; by default*/
  margin-bottom: 2rem;
  justify-content: space-between;
  flex-flow: wrap; /* has no effect since you're using % for flex-items width */
}

.item {
  background-color: red;
  width: 48%;
  margin-bottom: 2%;
}
<div class="container">
  <a class="item" href="google.com">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </a>
  <a class="item" href="google.com">It is a long established fact that a reader will be distracted by the readable content of a page.</a>
</div>

答案 1 :(得分:0)

为什么不尝试这样的操作,方法是移动\n内的a标记,这样您就可以使用divdiv

来定位它们
.item

答案 2 :(得分:0)

您可以将类名应用于a标签而不是div s(如果DIV需要保留类,则可以应用于两者)