【问题标题】:Difficulty aligning h1 and h2 with CSS难以将 h1 和 h2 与 CSS 对齐
【发布时间】:2016-06-04 12:04:16
【问题描述】:

我很难将“h1”红色通知气泡与旁边的“h2”标签垂直对齐。 我是在正确的轨道上尝试使用display: inline-block; 还是完全关闭。 我也尝试过使用vertical-align:middle,但这似乎不起作用。 任何帮助将不胜感激,谢谢。

body {
  font-family: sans-serif;
}
.steps h2 {
  font-weight: 400;
  text-transform: uppercase;
  font-size: 20px;
  margin-left: 10px;
  margin-bottom: 10px;
  letter-spacing: 1px;
  display: inline-block;
}
h1 {
  color: #fff;
  font-size: 40px;
  font-weight: 100;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.steps {
  width: 100%;
  height: 405px;
  position: absolute;
  top: 9%;
  left: 0px;
  right: 0px;
  margin: 0px auto;
}
label {
  background: #F2F2F2;
  height: 69px;
  width: 100%;
  display: inline-block;
  border-bottom: 1px solid #2C3E50;
  color: #fff;
  text-transform: capitalize;
  font-weight: 900;
  font-size: 11px;
  letter-spacing: 1px;
  position: relative;
  padding: 5px 5px 5px 20px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}
label h2 span {
  display: block;
  font-size: 12px;
  text-transform: capitalize;
  font-weight: normal;
  color: #bdc3c7;
  padding-top: 5px;
}
.new {
  width: 30px;
  height: 30px;
  display: inline-block;
  border-radius: 50%;
  font-size: 20px;
  color: #fff;
  line-height: 30px;
  text-align: center;
  background: #FF4447;
}
<div class="steps">
  <h2>Your Matches</h2>
  <ul id="matches">
    <li>
      <h1 class="new">1!</h1>
      <label for="label-1">

        <h2>Steve, 28 <span>Matched 2 days ago</span></h2>
      </label>
    </li>
    <li>
      <label for="label-2">
        <h2>Tony, 30 <span>Matched 1 hour ago</span></h2> 
      </label>
    </li>

    <li>
      <label for="label-3">
        <h2>Bruce<span>Matched 4 days ago</span></h2> 
      </label>
    </li>
  </ul>
</div>

【问题讨论】:

    标签: html css alignment vertical-alignment


    【解决方案1】:

    像这样?

    如果是这样,那么您的想法是正确的。前面提到的解决方案的另一种替代方法是将h1 移动到label 内,您希望它旁边的h1(基本上是标签的内容)在实际的label 元素内,作为一个孩子.您还需要将label h2 span 更改为display: inline;

    (HTML)

    <li>
      <label for="label-1">
        <h1 class="new">1!</h1>
        <h2>Steve, 28 <span>Matched 2 days ago</span></h2>
      </label>
    </li>
    

    (CSS)

    label h2 span {
      display: inline;
      font-size: 12px;
      text-transform: capitalize;
      font-weight: normal;
      color: #bdc3c7;
      padding-top: 5px;
    }
    .new {
      width: 30px;
      height: 30px;
      display: inline-block;
      border-radius: 50%;
      font-size: 20px;
      color: #fff;
      line-height: 30px;
      text-align: center;
      background: #FF4447;
    }
    

    【讨论】:

    • 这正是我想要实现的,谢谢!
    • 没问题! :)
    【解决方案2】:

    我不确定我是否理解你想要做什么,但这是我的快速解决方案:

    您可以通过将其添加到您的 CSS 来设置元素位置:

    h1 {
      position: relative;
      z-index: 9999;
      top: 4rem;
      left: 10rem;
    }
    

    DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多