OstlerDev
OstlerDev

Reputation: 542

Multiple lines of text next to image (CSS-HTML)

I am trying to put 2 lines of text next to an image, sort of like this

_________
|       | Line one of text
| image |
|       | Line two of text
---------

This is the code that I have so far

<p style="color: #fff;"><img src="assets/image.png"><span style="">Line one of text</span>
    <br>
    <span class="ban2">Line 2 of text</span></p>
 .banner p {
  font-family: "Gentium Basic";
  font-size: 19px;
  text-align: center;
  color: #aaa;
  margin-top: -10;
  display: block;
 }
.banner img {
  float: center; 
    margin: 5px;
 }
 .banner span {
  padding-top: 50px;
  font-size: 17px;
  vertical-align:top;
 }
  .banner .ban2 span {
  padding-top: 50px;
  font-size: 17px;
  vertical-align:top;
 }

But currently it does this:

_________
|       | Line one of text
| image |
|       | 
---------
Line two of text

I have looked all over the web but have not been able to figure out how to do this, any help would be very welcome.

Upvotes: 39

Views: 54266

Answers (7)

Linesofcode
Linesofcode

Reputation: 5891

I suggest using the old tables that works great. In terms of CSS it is just needed to add the vertical-align: top property.

<table>
  <tbody>
    <tr>
      <td class="img-container">
        <img src="https://img2.gratispng.com/20180324/sbe/kisspng-google-logo-g-suite-google-5ab6f1f0dbc9b7.1299911115219389289003.jpg"/>
      </td>
      <td class="content-container">
        <span class="description">This is a very long text that creates multiple lines with the image at left side</span>
      </td>
    </tr>
  </tbody>
</table>

Fiddle: https://jsfiddle.net/fypa0k4w/

Upvotes: 0

Linh
Linh

Reputation: 60923

Here is my demo which have using float and overflow with some explain

.div1 {
     border: 3px solid #0f0;
     overflow:auto; // without overflow here, if the "Line 1" and "Line 2" is short then "Next elements" will display below "Line 2" and the image will cover the "Next elements" 
}
.img {
    float: left;
    width:100px;
    height:100px;
    background: #000 
}
.div2 {
    float: left; // without float here, if "Line 1" and "Line 2" is long -> text will display both at right and bottom the image
} 
<div class="div1">
  <img class="img"/>
  <div class="div2">
    <p> Line 1 </p>
    <p> Line 2 </p>
  </div>
</div>

<p>Next elements</p>

Hope it help

enter image description here

Upvotes: 10

Ricky Levi
Ricky Levi

Reputation: 7987

I know this is old post, but still wanted to say that not only float will do it, the <img> tag has a built-in attribute called align="left" which does that as well

<p>
 <img src="smiley.gif" align="left"><span>Line one of text</span>
 <br>
 <span class="ban2">Line 2 of text</span>
</p>

Fiddle: http://jsfiddle.net/356akcoy/

Upvotes: 1

mvndaai
mvndaai

Reputation: 3831

Here is a snippet using a svg so you can test it anywhere.

.img{
    float: left;
    margin-right:1rem;
}
<div>
  <svg class="img" width="50" height="50" >
    <rect width="50" height="50" style="fill:black;"/>
  </svg>
  <p>
    Line 1
    <br>
    Line 2
  </p>
</div>

Upvotes: 11

Asraful Haque
Asraful Haque

Reputation: 1125

Check it. It is well defined css.

<!DOCTYPE html>
<html>
   <head>
      <title>Selectors</title>
      <style>
         .banner p {
             font-family: "Gentium Basic";
             font-size: 19px;
             text-align: center;
             color: #aaa;
             margin-top: -10;
             display: block;
         }
         img, span {
             float:left;
         }
         .banner img {
             float: center; 
             margin: 5px;
         }
         [class="ban1"]{
             font-size: 17px;
             position:relative;
             top:50px;
             left:11px;
         }
         [class="ban2"] {
             font-size: 17px;
             position: relative;
             left: -97px;
             top: 74px;
         }
      </style>
   </head>
   <body>
      <div class="banner">
         <div class="wrapper">
            <p><img src="span.png"><span class="ban1">Line one of text</span>
               <span class="ban2">Line 2 of text</span>
            </p>
         </div>
      </div>
   </body>
</html>

Upvotes: 1

user3491125
user3491125

Reputation: 366

I know this post is old but wrap your element in a div and apply the vertical-align:top to this div and you're done.

Upvotes: 3

Ming
Ming

Reputation: 4578

There's no such thing as float: center; You can choose either left, right, or none.

http://jsfiddle.net/vd7X8/1/

If you float: left; on the image it will do what you're after.

If you want it centered, then you're going to have to wrap the image and the text in a container, fix the width of the container and do margin: 0 auto; on it, then continue to have your image floated--except it will be constrained by the wrapper.

Upvotes: 19

Related Questions