如何将css :: before伪元素排成一个网格

时间:2015-12-07 14:20:03

标签: html css

有没有办法,只使用css,排列一个多线元素(和我的例子中的锚),以便"之前" bit和" anchor"位并排出现,就好像在网格中一样。

>   i am a test link
    which goes over
    multiple lines

与此结果相反(希望将其展示在前面的内容之下)。



a::before {
  content: ">";
  padding-right: 20px;
}

div {
  width: 150px;
}

<div>
   <a href="#">i am a test link which goes over multiple lines</a>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:7)

display:table/table-cell效果很好。

a::before {
  content: ">";
  padding-right: 20px;
}
div {
  width: 150px;
  margin: 1em;
  border: 1px solid grey;
}

/* relevant stuff */
a {
  display: table;
  text-decoration: none;
}
a::before {
  display: table-cell;
  vertical-align: middle;
}
<div>
  <a href="#">i am a test link which goes over multiple lines</a>
</div>

或者受到Nenad Vracar最初答案的启发,Flexbox

a::before {
  content: ">";
  padding-right: 20px;
}
div {
  width: 150px;
  border: 1px solid grey;
  margin: 1em;
}

/* relevant stuff */
a {
  display: flex;
  align-items: center;
  text-decoration: none;
}
<div>
  <a href="#">i am a test link which goes over multiple lines</a>
</div>

答案 1 :(得分:3)

您可以像这样使用 Flexbox ,也可以垂直对齐项目

<强> DEMO

div {
  width: 150px;
}

a {
  display: flex;
  flex-direction: row;
  align-items: center;
}

a:before {
  content: ">";
  margin: 5px;
}
<div>
   <a href="#">i am a test link which goes over multiple lines</a>
</div>

答案 2 :(得分:2)

一种方法是将padding-left值添加到父锚元素,然后是display: inline-block / position: relative。然后绝对相对于父元素定位伪元素:

&#13;
&#13;
a {
  position: relative;
  padding-left: 20px;
  display: inline-block;
}
a::before {
  content: ">";
  position: absolute;
  left: 0;
}

div {
  width: 150px;
}
&#13;
<div>
   <a href="#">i am a test link which goes over multiple lines</a>
</div>
&#13;
&#13;
&#13;