Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have two headings on the same line in html

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.

like image 248
Aditya Sastry Avatar asked Dec 31 '12 00:12

Aditya Sastry


People also ask

How do you make two headings in one line in HTML?

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. Show activity on this post. Add a span with the style="float: right" element inside the h1 element.

How do I have three headings on the same line in HTML?

So, the solution to place multiple blocks elements in on line, is to embed them in another container, where the <header> tag could advantageously be used to semantically organize your page: <header id="my_header"> <div> <h2>ELEMENT 1</h2> </div> <div> <h2>ELEMENT 2</h2> </div> <div> <h2>ELEMENT 1</h2> </div> </header> ...

How do I add a second heading in HTML?

Add sub-heading using HTML: The most often way we add sub-headings in HTML is either by using only HTML Headings or using HTML Headings and HTML div. Let's explore both ways of adding sub-heading. Adding Sub Heading using HTML Headings: The HTML heading tag (<h1> , <h2> . . . . <h6>) defines the heading of a page .


2 Answers

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.

like image 73
twilson Avatar answered Sep 30 '22 03:09

twilson


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 />​ 
like image 35
Jon Newmuis Avatar answered Sep 30 '22 01:09

Jon Newmuis