Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap container with position:absolute loses layout inside

I am developing website against latest Bootstrap version 3.3.2. My task is to create navigation, that is positioned on top of other content (simple roll over menu on hover). For this menu I want to use Bootstrap columns.

To position .container-fluid on top of other containers, I need to remove it from standard flow. So need to use position:absolute. As soon as I apply this to .container-fluid (or wrappers around it), it loses 100% width and whole layout inside gets lost.

If I remove position:absolute from .container-fluid (#menu in my case), it gets back layout, but is not removed from standard flow.

JSFiddle with only this case: http://jsfiddle.net/q6v9wv31/

HTML:

<div class="container first">
    <div class="row">
        <div class="col-xs-12">
            <p>Content here</p>
        </div>
    </div>
</div>
<div class="container ontop">
    <div class="row">
        <div class="col-xs-6">
            <p>Menu Item 1</p>
        </div>
        <div class="col-xs-6">
            <p>Menu Item 2</p>
        </div>
    </div>
</div>

CSS:

body {
    margin: 10px;
}
.first {
    height: 200px;
    background-color: #ddd;
}
.ontop {
    height: 100px;
    background-color: #d00;
    position: absolute;
    top: 100px;
}

Current version of project: http://html.accuraten.com/ssc-html-dev/public/

Please help me understand, how to solve this task.

like image 832
dandaka Avatar asked Jan 26 '15 03:01

dandaka


People also ask

Why you shouldn't use position absolute?

IF you were to use all absolute div tags you could easily have your tags overlap and hide each other when the data within them changed. Secondly, many good sites allow for the information to wrap depending on the screen resolution and browser size that a user uses to access the web page.

How do you keep the absolute position inside a div?

position : absolute Inside Other Positioned ElementsThe inner div element has position set to absolute and top to 0px and right to 0px . This places the inner div at the top right corner inside its parent element (because the parent element has position: relative set).

How do you fix a position absolute?

Absolutely positioned elements are positioned with respect to a containing block, which is the nearest postioned ancestor. If there is no positioned ancestor, the viewport will be the containing block. Elements with fixed positioning are fixed with respect to the viewport—the viewport is always their containing block.

Is position absolute relative to viewport?

absolute has no parent that is relatively positioned. Therefore it stays relative to the viewport and appears all the time even when you scroll.


1 Answers

If you want to set the position to absolute and want it to have 100% width, you should set the left and right CSS styles:

HTML:

<div class="container first">
    <div class="row">
        <div class="col-xs-12">
            <p>Content here</p>
        </div>
    </div>
</div>
<div class="ontop container">
    <div class="row">
        <div class="col-xs-4">
            <p>Menu Item 1</p>
        </div>
        <div class="col-xs-4">
            <p>Menu Item 2</p>
        </div>
        <div class="col-xs-4">
            <p>Menu Item 2</p>
        </div>
    </div>
</div>

CSS:

body {
    margin: 10px;
}
.first {
    height: 200px;
    background-color: #ddd;
}
.ontop {
    height: 100px;
    background-color: #d00;
    position: absolute;
    top: 100px;
    right: 0;
    left: 0;
}

Change left: 0 and right: 0 to 10px if you want it to have the same margin as the first container.

An element which is positioned absolute does not obtain any space in the document. This means that it does not leave an empty space after being positioned. You can subsequently use the properties left, right, top, and bottom to place the box.

http://html.net/tutorials/css/lesson14.php#s2

like image 171
jrarama Avatar answered Sep 18 '22 17:09

jrarama