Home » Html » Logo image and H1 heading on the same line

Logo image and H1 heading on the same line

Posted by: admin November 30, 2017 Leave a comment

Questions:

I wanna create my first web page but encountered a problem.

I have the following code:

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

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

Answers:

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

Questions:
Answers:

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.
Questions:
Answers:

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>

Questions:
Answers:

Try this:

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

Questions:
Answers:

You can do it as Billy Moat told you, wrap your <img> and <h1> in a <div> and use float: left; to float your image to the left, set the <div> width and than set a line-height for your h1 and use <div style="clear: float;"></div> to clear your floating elements.

Fiddle

Questions:
Answers:

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

Questions:
Answers:

in your css file do img { float: left; } and h1 {float: left; }

Questions:
Answers:

Check this.

 .header{width:100%;
    }

    .header img{ width: 20%; //or whatever width you like to have

    }

    .header h1{

    display:inline; //It will take rest of space which left by logo.
}

Questions:
Answers:

I’d use bootstrap and set the html as:

<div class="row">
    <div class="col-md-4">
        <img src="img/logo.png" alt="logo" />
    </div>
    <div class="col-md-8">
        <h1>My website name</h1>
    </div>
</div>