如何垂直居中这个跨度等于 h1

How to vertically center this span equal to h1

到目前为止我已经知道了:

Jsfiddle

如何更改跨度的 css 使其垂直居中于其左侧的 h1

希望不要太复杂!

h1span 周围添加包装器,然后将其设置为 inline-flex。由于带有标题的 text-align,包装纸将居中。

Fiddle

.title {
  width: 100%;
  margin: 30px auto;
  text-align: center;
}

.wrapper {
  display: inline-flex;
  justify-content: flex-start;
  align-items: center;
}

.title i {
  color: var(--grey-500);
}

.title span {
  font-size: var(--caption);
  line-height: 40px;
  color: #9E9E9E;
}

.title h1 {
  color: var(--black);
  font-size: var(--h1);
  text-align: center;
  font-weight: 500;
  margin: 65px auto;
}
<div class="title">
  <div class="wrapper">
    <h1>Title 1</h1> <i class="material-icons separateTitleType">remove</i> <span>Page</span>
  </div>
</div>

将此代码添加到 .title

display: flex;
justify-content:center;
align-items:center;

此外,我从 h1 中删除了 margin: 65px auto,因此它不会占据 flex-container 中的所有位置。

/* Titles */
.title {
    width: 100%;
    margin: 30px auto;
    text-align: center;
    display: flex;
    justify-content:center;
    align-items:center;
}
.title i {
    color: var(--grey-500);
}
.title span {
    font-size: var(--caption);
    line-height: 40px;
    color: #9E9E9E;
}
.title h1 {
    color: var(--black);
    font-size: var(--h1);
    text-align: center;
    font-weight: 500;
}
:root {
    --black: #000000;
    --h1: 2.125em;
    --caption: 0.875em;
    --grey-500: #9E9E9E;
}
/* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: local('Material Icons'), local('MaterialIcons-Regular'), url(https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2) format('woff2');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -webkit-font-feature-settings: 'liga';
  -webkit-font-smoothing: antialiased;
}
body {
  font-family: 'Roboto', sans-serif;
  background-color: #fff;
}
<div class="title">
    <h1>Title 1</h1> <i class="material-icons separateTitleType">remove</i> <span>Page</span>
</div>

最好的选择是使用 Flex 样式。

  1. 删除“.title i”、“.title span”、“.title h1”的所有样式
  2. 编辑标题如下:

Flex 标题样式:

.title {
    width: 100%;
    margin: 30px auto;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

justify-content:居中; - 这个 re-aligns 你的 H1、破折号和跨度在中间。

align-items:居中; - 这为您提供了垂直对齐方式。