Search code examples
csspositioncss-position

Align elements relative to each other


Basically I'm wondering if you can position 2 elements relative to each other. I have a h1 and h2 inside a div, I want to align the h2 to the right side of the h1

enter image description here

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;
}

Solution

  • The simplest solution is to wrap the headings in an extra inline-block div and the apply 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>