相对于彼此对齐元素

Align elements relative to each other

基本上我想知道您是否可以相对于彼此定位 2 个元素。 我在div中有一个h1和h2,我想将h2对齐到h1

的右侧

html

<header>
  <div>
    <h1>Header with some text</h1>
    <h2>Other header</h2>
  </div>
  <div>
  </div>
  <div>
  </div>
</header>

css

header { 
  width: 960px;
}

div {
  width: 318px;
  float: left;
  border: 1px solid red;
  min-height: 200px;
}

h1, h2 {
  font-size: 16px;
}

最简单的解决方案是 将标题 包裹在 extra inline-block div 中,然后应用 text-align:right.

.parent {
  width: 80%;
  border: 1px solid red;
}
.wrap {
  display: inline-block;
  text-align: right;
}
<div class="parent">
  <div class="wrap">
    <h1>
I'm a really long h1 tag
</h1>
    <h2>
 Short h2 tag
</h2>
  </div>
</div>

使用此代码

.some_class > * {
  display:inline-block;

  }
<div class="some_class">
  <h1>some text</h1>
  <h2> Some other text </h2>
  
  </div>