Floating Div Over An Image

HtmlCssCss FloatZ Index

Html Problem Overview


I'm having trouble floating a div over an image. Here is what I am trying to accomplish:

    .container {
       border: 1px solid #DDDDDD;
       width: 200px;
       height: 200px;
    }
    .tag {
       float: left;
       position: relative;
       left: 0px;
       top: 0px;
       z-index: 1000;
       background-color: #92AD40;
       padding: 5px;
       color: #FFFFFF;
       font-weight: bold;
    }

    <div class="container">
       <div class="tag">Featured</div>
       <img src="http://www.placehold.it/200x200">
    </div>

In this image:

enter image description here

I want the "Featured" box to float over top of the image but instead it seems to "clear" the float and cause the image to wrap to the next line, as though it was displaying as a block element. Unfortunately, I can't figure out what I am doing wrong. Any ideas?

Html Solutions


Solution 1 - Html

Never fails, once I post the question to SO, I get some enlightening "aha" moment and figure it out. The solution:

    .container {
       border: 1px solid #DDDDDD;
       width: 200px;
       height: 200px;
       position: relative;
    }
    .tag {
       float: left;
       position: absolute;
       left: 0px;
       top: 0px;
       z-index: 1000;
       background-color: #92AD40;
       padding: 5px;
       color: #FFFFFF;
       font-weight: bold;
    }

<div class="container">
       <div class="tag">Featured</div>
       <img src="http://www.placehold.it/200x200">
</div>

The key is the container has to be positioned relative and the tag positioned absolute.

Solution 2 - Html

Change your positioning a bit:

.container {
    border: 1px solid #DDDDDD;
    width: 200px;
    height: 200px;
    position:relative;
}
.tag {
    float: left;
    position: absolute;
    left: 0px;
    top: 0px;
    background-color: green;
}

jsFiddle example

You need to set relative positioning on the container and then absolute on the inner tag div. The inner tag's absolute positioning will be with respect to the outer relatively positioned div. You don't even need the z-index rule on the tag div.

Solution 3 - Html

Actually just adding margin-bottom: -20px; to the tag class fixed it right up.

http://jsfiddle.net/dChUR/7/

Being block elements, div's naturally have defined borders that they try not to violate. To get them to layer for images, which have no content beside the image because they have no closing tag, you just have to force them to do what they do not want to do, like violate their natural boundaries.

.container {
  border: 1px solid #DDDDDD;
  width: 200px;
  height: 200px;
  }
.tag {
  float: left;
  position: relative;
  left: 0px;
  top: 0px;
  background-color: green;
  z-index: 1000;
  margin-bottom: -20px;
  }

Another toue to take would be to create div's using an image as the background, and then place content where ever you like.

<div id="imgContainer" style="
         background-image: url("foo.jpg"); 
         background-repeat: no-repeat; 
         background-size: cover; 
         -webkit-background-size: cover; 
         -mox-background-size: cover; 
         -o-background-size: cover;">
  <div id="theTag">BLAH BLAH BLAH</div>
</div>





Solution 4 - Html

You've got the right idea. Looks to me like you just need to change .tag's position:relative to position:absolute, and add position:relative to .container.

Solution 5 - Html

you might consider using the Relative and Absolute positining.

`.container {  
position: relative;  
}  
.tag {     
position: absolute;   
}`  

I have tested it there, also if you want it to change its position use this as its margin:

top: 20px; left: 10px;

It will place it 20 pixels from top and 10 pixels from left; but leave this one if not necessary.

Solution 6 - Html

You can achieve this with relative position.

But why isn't your code working?

An element with position:relative keeps it's position and also still affects all other following elements. That's the reason why your div won't overlap the image by just using z-index.

You'll still need to position the div element with, for example: top:-28px where the amount would be the height of the element with tag class.

> Note: top has no effect on non-positioned elements. It works with absolute, relative and sticky.

If you add top:-28px to the tag element it will only overlap the image if the z-index it has a higher number. This is the importance of z-index in this case.

.container {
  width: 200px;
  height: 200px;
}
.tag {
   position: relative;
   z-index: 1;
   float: left;
   padding: 5px;
   color: #FFFFFF;
   font-weight: bold;
   background-color: #92AD40;
}
img{
  position:relative;
  z-index:0;
  top:-28px;
}

<div class="container">
  <div id='tag' class="tag">Featured</div>
  <img id='img' src="https://i.stack.imgur.com/rUDax.png">
</div>

If you want to play a bit with this concepts

I added some JS code to toggle between different styles

const tag = document.getElementById('tag')
const img =  document.getElementById('img')
const label1 = document.getElementById('label1')
const label2 = document.getElementById('label2')

function togglePosition(){
  if(!tag.style.position){
    tag.style.position = 'relative'
    img.style.position = 'relative'
    label1.innerHTML = 'Relative position added'
  }
  else{
    tag.style.position = null
    img.style.position = null
    label1.innerHTML = 'Add relative position'
  }
}

function toggleZindex(){
   if(!tag.style.zIndex){
    tag.style.zIndex = '1'
    img.style.zIndex = '0'
    label2.innerHTML = 'z-index  (1 and 0) added to elements'
  }
  else{
    tag.style.zIndex = null
    img.style.zIndex = null
    label2.innerHTML = 'Add z-index to elements'
  }
}

.container {
  margin-top:20px;
  width: 200px;
  height: 200px;
}
.tag {
   float: left;
   padding: 5px;
   color: #FFFFFF;
   font-weight: bold;
   background-color: #92AD40;
}
img{
  top:-28px;
}

<input type='checkbox' onclick='togglePosition()'/> 
<label id='label1'>Add relative position</label>
<br/>
<input type='checkbox' onclick='toggleZindex()'/> 
<label id='label2'>Add z-index to elements</label>

<div class="container">
  <div id='tag' class="tag">Featured</div>
  <img id='img' src="https://i.stack.imgur.com/rUDax.png">
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremy HarrisView Question on Stackoverflow
Solution 1 - HtmlJeremy HarrisView Answer on Stackoverflow
Solution 2 - Htmlj08691View Answer on Stackoverflow
Solution 3 - HtmlThomas CheneyView Answer on Stackoverflow
Solution 4 - Htmluser1618143View Answer on Stackoverflow
Solution 5 - Htmluser2690583View Answer on Stackoverflow
Solution 6 - HtmlGassView Answer on Stackoverflow