How to use Fixed position within a div?

Asked

Viewed 1,882 times

2

I have a div who has yours header and its content that has a height fixed and this has scroll.
How to put position: fixed in the header (where is the title), without it moving when the scroll go with the page?

Example:

HTML

<div class="main">
    <div id="conversation">
        <div class="header">
            <h3>Título</h3>
        </div>
        <div class="content">
            <ul>
                <li>Primeira conversa</li>
                <li>Segunda conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
                <li>Terceira conversa</li>
            </ul>
        </div>
    </div>
</div>

CSS

body, html{
    height: 150%
}    
#conversation{
    width: 400px;
    border: 1px solid #eee;
    height: 400px;
    overflow-x: hidden;
} 
.header{
    background: #4ECDC4;
    color: #fff;
    text-align: center;
    height: 40px;
    position: fixed;
    width: 385px;
}
.content{
    margin-top:35px;
}    
ul{
   margin: 0;
   padding: 0;
}
li{
    list-style: none;
    padding: 20px 10px;
    border-bottom: 1px solid #eee;
}

Jsfiddle

  • That is not the answer to your question, but it is much simpler: http://jsfiddle.net/7aQYh/1/

  • I do not believe that it is possible to solve this problem by maintaining the position: fixed. When using this property, you highlight the page element, and it becomes independent of any hierarchy (that is, you cannot limit it in a div). So I think @Bacco’s solution is the way.

  • Jefferson, is this what you’re looking for? http://jsfiddle.net/sF95J/

  • @Sergio, almost. The problem is that my div is not glued to the top. add for example margin-top: 100px; and test there for you to see.

  • @Jeffersonalison, okay. So just join the position of the .main? So? http://jsfiddle.net/r95R8/

  • @Jeffersonalison you want to make chat windows in facebook style?

  • @Jeffersonalison the same as I originally proposed, but with margin-top: http://jsfiddle.net/7aQYh/3/ - I took the margin of H3

Show 2 more comments

3 answers

6

When I give position: fixed then the element behaves as a free and independent element of the position of its relative. If you want to prevent this "free" behavior with jQuery, you can do so but I must point out that this code I leave below does the same as removing the position: fixed, as @Bacco suggested and is lower in performance.

But I’m gonna assume you really need to give position: fixed and then my suggestion is to add a scroll "spy" to run a function that adjusts the position of the element taking into account the initial position and the scroll at the time.

var header = $('#conversation .header');
var mainPosition = $('.main').position();
$(document).on('scroll', function () {
    header.css('top', -$(document).scrollTop() + mainPosition.top + 'px');
});

http://jsfiddle.net/45B7H/

  • Only one detail here. When an element receives position:fixed, he is free/independent not only of his relative, but of the document as a whole.

4

Try using position Absolute in the header. For example:

.header{
    background: #4ECDC4;
    color: #fff;
    text-align: center;
    height: 40px;
    position: absolute;
    width: 385px;
}

2

The position fixed does not fit very well to your need, the position that would best suit position:absolute, but I preferred to better organize your Css.
To better understand how the positions you can read the article Position property of css.

Fixed Position

The position: Fixed; will fix the position of the element in the coordinate you set. As the page is scrolled, the element remains fixed at the position you set and the page content normally scrolls.

It is usually used to fix elements such as headers or sidebars.

Namely the position fixed always keeps fixed to the window and not to the element in which it is inserted, so I made modifications to its css to work exactly as you need.

Css

body, html{
    height: 150%
}

#conversation{
    width: 400px;
    border: 1px solid #eee;
    overflow: hidden;
}

.header {
    background: #4ECDC4;
    color: #fff;
    text-align: center;
    height: 40px;
}

.header h3 {
    margin: 0;
    padding: 0;
    line-height: 40px;
}

.content{
    overflow-x: hidden;
    height: 400px;
}

ul{
   margin: 0;
   padding: 0;
}

li{
    list-style: none;
    padding: 20px 10px;
    border-bottom: 1px solid #eee;
}

You can see the example working on

jsfidle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.