Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text over Image html css [duplicate]

I'm trying to set an image in HTML and want text to run over it. But when I use the following code the text will run under the image.

HTML

    <div id="titel">
    <img src="images/titel.jpg" alt="titel">
    <h3>Titel</h3>
    </div>

CSS

    #titel
    {
        float:left;
        width:220px;
        height:100px;
        margin:0px 5px 5px;
    }

    h2
    {
        position:absolute;
    }
like image 501
user2988642 Avatar asked Aug 17 '26 03:08

user2988642


1 Answers

You need to add position:relative to the titel div and a top value to the image.

  #titel {
      float:left;
      width:220px;
      height:100px;
      margin:0px 5px 5px;
      position:relative;
  }
  h3 {
      position:absolute;
      top:0;
  } 

jsFiddle example

Another option is to set the image as the background image of the div, and dispense with the positioning altogether.

jsFiddle example

(note also that in your example you have a h3 element but in your CSS you were targeting a h2 element.)

like image 66
j08691 Avatar answered Aug 18 '26 16:08

j08691



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!