Logo image and H1 heading on the same line

529,990

Solution 1

As example (DEMO):

HTML:

<div class="header">
  <img src="img/logo.png" alt="logo" />
  <h1>My website name</h1>
</div>

CSS:

.header img {
  float: left;
  width: 100px;
  height: 100px;
  background: #555;
}

.header h1 {
  position: relative;
  top: 18px;
  left: 10px;
}

DEMO

Solution 2

Try this:

  1. Put both elements in a container DIV.
  2. Give that container the property overflow:auto
  3. Float both elements to the left using float:left
  4. Give the H1 a width so that it doesn't take up the full width of it's parent container.

Solution 3

If your image is part of the logo why not do this:

<h1><img src="img/logo.png" alt="logo" /> My website name</h1>

Use CSS to style it better.

And it is also best practice to make your logo a hyperlink that take the user back to the home page.

So you could do:

<h1 id="logo"><a href="/"><img src="img/logo.png" alt="logo" /> My website name</a></h1>

Solution 4

Try this:

<img style="display: inline;" src="img/logo.png" alt="logo" />
<h1 style="display: inline;">My website name</h1>

Solution 5

Just stick the img tag inside the h1 tag as part of the content.

Share:
529,990
Six Quads
Author by

Six Quads

Updated on June 25, 2020

Comments

  • Six Quads
    Six Quads about 4 years

    I want to create my first web page but I encountered a problem.

    I have the following code:

    <img src="img/logo.png" alt="logo" />
    <h1>My website name</h1>
    

    I'd like to know how to make the logo and the H1 to be in the same line. Thanks!

  • Mr. Alien
    Mr. Alien almost 12 years
    @Speransky Got almost same.. ;)
  • Mr. Alien
    Mr. Alien almost 12 years
    @SperanskyDanil You deserve it too am upvoting you, you missed out the line-height and clear I guess
  • Alex Alksne
    Alex Alksne over 8 years
    @Kermit It's pretty self explanatory and worked for me!
  • vaku
    vaku about 5 years
    Welcome to Stack Overflow @Richa It is always better to include the source or references in support of your answer and If possible provide why your answer works...
  • Pawel Cioch
    Pawel Cioch over 4 years
    Is is "legal" per HTML specification?