Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding div which children position is absolute

Tags:

css

Is there any option to prevent parent element be overflowed by children element when children element position is absolute and hight of children is higher than parent? I don't want to hide overflowing part by overflow: hidden, I want to force parent div.alert height to be not lower than its children. As you can see I am using position:absolute and transform:tranlateY(-50%) to put the content of div in the middle independently of the content length. Unfortunately when content is higher than minimum height of parent it goes outside of parent.

<div class="alert">
    <span>
        text text text text text text text text text text text text 
    </span>
</div>
<div class="alert">
    <span>
        text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
    </span>
</div>
<div class="alert">
    <span>
        text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
    </span>
</div>

CSS:

body{
    background: #000;
}
.alert{
    position: relative;
    margin: 35px 5px;
    background: #4679BD;
    color: #fff;
    padding: 10px 15px;
    min-height: 40px;
}
span{
    position: absolute;
    top: 50%;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}

JS FIDDLE

Thanks in advance.

like image 859
LJ Wadowski Avatar asked Jul 21 '15 08:07

LJ Wadowski


People also ask

Can a div be position absolute and relative?

Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.

What is an example of absolute position?

When you need something to be positioned in an very specific spot you would use absolute positioning. For instance you may want to have an image with an overlapping caption that always sits at the top of the picture (say 20px from the top).

Should I use position relative or absolute?

If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. Position Absolute: When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.


2 Answers

Absolute positioning is used for exactly the opposite of what you're trying to achieve. You never clarified whether it has to be and why does it need to be absolute.

You can easily achieve the same effect using padding on the inside element:

body{
    background: #000;
}
.alert{
    position: relative;
    margin: 35px 5px;
    background: #4679BD;
    color: #fff;
    padding: 10px 15px;
    min-height: 40px;
}
span{
    padding: 12px 0;
    display: inline-block;
}
<div class="alert">
    <span>
        text text text text text text text text text text text text 
    </span>
</div>
<div class="alert">
    <span>
        text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
    </span>
</div>
<div class="alert">
    <span>
        text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
    </span>
</div>

http://jsfiddle.net/rnq046wg/4/


Using the flex approach (be careful with older browsers):

body{
    background: #000;
}
.alert{
    position: relative;
    margin: 35px 5px;
    background: #4679BD;
    color: #fff;
    padding: 10px 15px;
    min-height: 40px;
    display: flex;
    align-items: center;
}
span{
    display: inline-block;
}
<div class="alert">
    <span>
        text text text text text text text text text text text text 
    </span>
</div>
<div class="alert">
    <span>
        text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
    </span>
</div>
<div class="alert">
    <span>
        text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
    </span>
</div>
like image 197
Shomz Avatar answered Sep 27 '22 18:09

Shomz


There is no easy way to deal with this because position: absolute; is intended to "go out" from the parent's model.

What you can do to solve your problem is using another approach to vertically center:

.alert{
    margin: 35px 5px;
    background: #4679BD;
    color: #fff;
    padding: 10px 15px;
    min-height: 40px;

    display: table;
    table-layout: fixed;
    width: 100%;
    box-sizing: border-box;
}
span{
    display: table-cell;
    vertical-align: middle;
}

Fiddle

like image 42
smnbbrv Avatar answered Sep 27 '22 18:09

smnbbrv