使用:: before作为创建空间的方法的目的?

时间:2018-12-07 21:17:39

标签: css css3

我最近在Google的Material Design中看到使用:before的伪元素在对话框标题之前生成垂直空间。

<h2>
  :before { content:''; height: 40px; width:0; display:inline-block; vertical-align: 0 }
  Some Text
</h2>

代替padding/margin-top或带有必要空间的包装器。我很想知道这样做背后的原因是什么,以及这种方法是否有好处?

  /* as seen on mdc's alertdialog headings: https://material-components.github.io/material-components-web-catalog/#/component/dialog */

.title {
  /* prefixes & repeat styles removed for easier reading */
  line-height: normal;
  font-family: Roboto, sans-serif;
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  font-size: 1.25rem;
  line-height: 2rem;
  font-weight: 500;
  letter-spacing: .0125em;
  text-decoration: inherit; /* none */
  text-transform: inherit; /* none */
  display: block;
  position: relative;
  flex-shrink: 0;
  box-sizing: border-box;
  margin: 0;
  padding: 0 24px 9px;
  border-bottom: 1px solid transparent;
}

.title:before{
  content:'';
  height:40px;
  width:0;
  display:inline-block; /* is left of the element */
  vertical-align: 0; /* baseline */
}
<h2 class="title">Heading</h2>

0 个答案:

没有答案