Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position absolute not inside parent

Tags:

html

css

I have a problem with my CSS regarding div positions.

I have a parent div which is set to position:relative and a child div set to position:absolute

But for some reason the child div is displaying below and outside the borders of the parent div...

This is my CSS:

.big{
    position:relative;
    width:40%;
    border:1px solid black;
    display:inline-block;
}

.small{
    position:absolute;
    width:75px;
    height:75px;
    border:1px solid green;
}

The HTML:

<div class="big">        
    <p align="center">Test</p>
    <div class="small"></div>        
</div>
<div class="big">
    <p align="center">Test</p>
    <div class="small"></div>    
</div>

I have provided a JSFiddle to show you it in action:

http://jsfiddle.net/j6VLc/1/

How do i fix it to make the child div be inside the parent div whilst using position:absolute for it?

like image 393
Sir Avatar asked Jun 29 '14 03:06

Sir


People also ask

Is position absolute relative to parent?

In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.

Why does position absolute not working?

If you are placing an element with absolute position, you need the base element to have a position value other than the default value. In your case if you change the position value of the parent div to 'relative' you can fix the issue.

What can I use instead of position absolute?

We can use CSS absolute positioning to overlap elements, but there's some advantages to using CSS grid instead. Let's explore using CSS grid as an alternative to position absolute.


2 Answers

You can't do this using position: absolute as it removes the element from the normal document flow. position: relative on the parent will change where the position: absolute is positioned relative to, but it will not expand to contain the position: absolute. You will need to set a fixed height or using position: relative instead.

Note, if using position: relative in your example, you will need to add a margin-bottom equal to the value of top to make it expand to contain the position: relative.

.big {
    position: relative;
    width: 40%;
    border: 1px solid black;
    display: inline-block;
}

.small {
    position: relative;
    width: 75px;
    height: 75px;
    border: 1px solid green;
    top: 50px;
    left: 40px;
    margin-bottom: 50px;
    margin-right: 40px;
}
<div class="big">
    <p align="center">Test</p>
    <div class="small"></div>
</div>
<div class="big">
     <p align="center">Test</p>
    <div class="small"></div>
</div>
like image 50
Alexander O'Mara Avatar answered Sep 23 '22 01:09

Alexander O'Mara


As you have given a height of 75px to the child div and inside the parent div you have also given <p> which is a block element so the <p> tag is making its space and after that your child div is appearing....Make the div height of parent element larger than child and style the <p> tag to display: inline;

.big {
    position: relative;
    width: 40%;
    height: 100px;
    border: 1px solid black;
    display: inline-block;
}

.small {
    position: absolute;
    width: 75px;
    height: 75px;
    border: 1px solid green;
}

p {
  display: inline;
}

Hope this will get you to what you want.

like image 36
user15479861 Avatar answered Sep 22 '22 01:09

user15479861