how to have two headings on the same line in html
Asked Answered
P

7

30

I want to have two headings h2 and h3 on the same horizontal rule one on the left and the other on the right. They have a HR underneath them and I want them to be at the same distance from this HR.

I tried making them both inline and have one float right and the other left. The problem with doing so was with h3 as it is smaller than h2 vertically it was centered at half the h2's length.

h2 was kinda like sitting on the hr and h3 kinda looked like floating in mid air.

I kinda wanted them to be like both sitting on the hr.

h2{
display:inline;
float:left;
}
h3{
display:inline;
float:right;
}

I was talking about visually describing the situation.

Paediatrician answered 31/12, 2012 at 0:49 Comment(2)
The best way you can describe is with code.Disestablish
Show us some code, what have you tried?Christos
M
39

You'd need to wrap the two headings in a div tag, and have that div tag use a style that does clear: both. e.g:

<div style="clear: both">
    <h2 style="float: left">Heading 1</h2>
    <h3 style="float: right">Heading 2</h3>
</div>
<hr />

Having the hr after the div tag will ensure that it is pushed beneath both headers.

Or something very similar to that. Hope this helps.

Merchandise answered 31/12, 2012 at 0:53 Comment(0)
O
13

You should only need to do one of:

  • Make them both inline (or inline-block)
  • Set them to float left or right

You should be able to adjust the height, padding, or margin properties of the smaller heading to compensate for its positioning. I recommend setting both headings to have the same height.

See this live jsFiddle for an example.

(code of the jsFiddle):

CSS

h2 {
  font-size: 50px;
}

h3 {
  font-size: 30px;
}

h2, h3 {
  width: 50%;
  height: 60px;
  margin: 0;
  padding: 0;
  display: inline;
}​

HTML

<h2>Big Heading</h2>
<h3>Small(er) Heading</h3>
<hr />​
Obedience answered 31/12, 2012 at 0:52 Comment(1)
I tried the margin,padding everything but the float I guess keeps on overriding themPaediatrician
U
6

Display property 'inline-block' will place both headers next to each other. You can run this code snippet to see it

<h1 style="display: inline-block" >Text 1</h1>
<h1 style="display: inline-block" >Text 2</h1>
Unchristian answered 31/12, 2020 at 1:26 Comment(1)
Please add some explanation about the code.Upswell
L
4

The Css vertical-align property should help you out here:

vertical-align: bottom;

is what you need for your smaller header :)

Vertical-Align

Loafer answered 31/12, 2012 at 0:54 Comment(1)
tried it doesnt work .. vertical align works without the float:rightPaediatrician
A
4

Check my sample solution

<h5 style="float: left; width: 50%;">Employee: Employee Name</h5>
<h5 style="float: right; width: 50%; text-align: right;">Employee: Employee Name</h5>

This will divide your page into two and insert the two header elements to the right and left part equally.

Affairs answered 23/5, 2014 at 10:0 Comment(0)
C
2

The following code will allow you to have two headings on the same line, the first left-aligned and the second right-aligned, and has the added advantage of keeping both headings on the same baseline.

The HTML Part:

<h1 class="text-left-right">
    <span class="left-text">Heading Goes Here</span>
    <span class="byline">Byline here</span>
</h1>

And the CSS:

.text-left-right {
    text-align: right;
    position: relative;
}
.left-text {
    left: 0;
    position: absolute;
}
.byline {
    font-size: 16px;
    color: rgba(140, 140, 140, 1);
}
Climacteric answered 18/2, 2018 at 5:57 Comment(0)
E
2

Add a span with the style="float: right" element inside the h1 element. So you can add a "goto top of the page" link, with a unicode arrow link button.

<h1 id="myAnchor">Headline Text
<span style="float: right"><a href="#top" aria-hidden="true">↥</a></span>
</h1>
Expressivity answered 8/1, 2020 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.