CSS: Putting H2 on left and link on right of same line

Firstly, the solution using flex which handles the issue of vertically centering the link:

...
    <style>
      div {
        background: #dbdbdb;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    </style>
...
  <body>
    <div>
      <h2>H2</h2>
      <a href="#">Some link</a>
    </div>
...
---------------
 
My earlier attempts were using float for the link element. [I wanted to use float to get some exposure to it as the CSS tutorial I went through did not cover it IFIRC or maybe had very short coverage of it. Note that the tutorial has extensive coverage of flex and grid.]

Putting link on same line with H2: Code that partially worked for me:
        <div>
          <h2>Blogger Blog Feed to HTML Book</h2>
          <a
            href="https://raviswdev.blogspot.com/2024/04/barebones-blogger-blog-feed-to-html.html"
            target="_blank"
            class="float-right vertically-center"
            >About</a
          >
        </div>
----
IFIRC, above code worked without the div element too.
But the issue was that the About link was not vertically centered.


....

Vertically centering the span in same line with h2:


The above example works as is but when float:right; CSS is specified, it does not work.


Comments

Archive