Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align H1 Header and Normal Text in Same Line

Tags:

html

css

I'm trying to have a H1 header and regular text on the same line, with a line under it, like so:

enter image description here

I have tried this, but have been unsuccessful

<div style="border-bottom:1px;">  <div align="left"><h1>Header</h1></div> <div align="right">Regular Text Goes Here</div> </div> 

What am I doing wrong?

like image 228
Mike Avatar asked Nov 02 '12 11:11

Mike


People also ask

How do I put text on the same line as a header in HTML?

The idea is to make the <h1> inline to allow the second text to be at the same line.

How do you get h1 and P on the same line?

Create a new tag in CSS with the desired attributes and values or edit an existing one (ex: . address). Then put the text you want to be inline under that tag and that's it.

How do I make text appear on the same line in HTML?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.


1 Answers

Original answer (still working just fine)

See this CodePen.

The idea is to make the <h1> inline to allow the second text to be at the same line.

HTML:

<header>   <h1>Text</h1>   <span>text2</span> </header> 

CSS:

header { border-bottom: 1px solid #000; } header > h1 { display: inline-block; } header span { margin-left: 100px; } 

2020 Update

See this alternative CodePen that makes use of flexbox.

Instead of setting the h1 to an inline-block, you can make the header a flex container. A flex container will (by default) layout its children on a horizontal axis. Note that you also need align-items: center to keep the h1 and span on the same vertical axis.

Also, note that you might want align-items: baseline if you want the texts to appear on the same baseline (like my original answer).

header {   display: flex;   align-items: center;    /* Remove the next line if you want the span to appear next to the h1 */   justify-content: space-between;    border-bottom: 1px solid #000;   padding: 10px 30px; } 
like image 80
Matteus Hemström Avatar answered Sep 22 '22 21:09

Matteus Hemström