How to fix the position of an element inside a div with scroll( With css )

Asked

Viewed 13,356 times

6

It is possible to fix an element( div, img, p ) in a div with scroll and with position:relative and using only css?

*Currently reposition the div using the function $('elemento').scroll() and calculating the scroolTop() of the element but in IE( the worst browser in the world ) there is a kind of delay and the element gets 'tremendous' so I wonder if it is possible to do this only with CSS.

HTML:

<div class="divContainer">
    <div class="conteudoFixo">
        <h3>Objeto Fixo</h3>
    </div>
    <div class="conteudoNormal">
        <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>

CSS:

.divContainer {
    height:200px;
    width:300px;
    position:left;
    overflow:auto;
    border: 1px solid #FF0000;
    position: relative;
}

.conteudoFixo {
    position: absolute;
    top:0px;
    left:0px;
    background-color:#FF00FF;
    width:100%;
}

Example link: http://jsfiddle.net/uQLzv/

  • 2

    Maybe this question may have already answered your question

  • Caputo thanks for the answer is that already used it (javascript) to reposition my div, but in IE( damned ) occurs a delay then the div is tremendous. But finally thanks for the reply.

3 answers

1

Put this in your CSS:

.conteudoFixo {
    position: fixed;
    width: 300px;
    background-color:#FF00FF;
}
  • Thanks for your help. So...is that when I use position:Fixed the divFixa loses the reference of divPai( is above the scroll ) and I can’t control with z-index; But Valew still, I’ll keep trying now I have a base.

  • What really is your goal? we can find an alternative to position:Fixed and Absolute.

  • So it is put the divFixo over all content without counting spacing is type the Fixed relative to window but in my case it would be relative to div( with scroll ). Ahh, my goal is to duplicate and fix the header and columns of a table( later I will create a plugin, the ones I found are not so flexible ); *Please do not relate table to this question, Valew XD

1

I would change your css by adding this code to the class conteudoNormal.

.conteudoNormal{
    overflow:auto;
    height:200px;
}

And leave the class divContainer thus:

.divContainer {
    width:300px;
    position:left;
    border: 1px solid #FF0000;
    position: relative;
}
  • So it is that in my case the divFixed cannot 'push' the normal content and would be on top even, in my case it would be the equivalent of the position Fixed in the window( browser ) only the divFixa would have as reference to divContainer unlike the whole window. Valew at once

1


So friend the solution is simple, it makes no sense to leave the .conteudoFixo inside a scroll div which in the case of your code is the parent div .divContainer ... the wisest would be to let the div q vc want fixed off the scroll, causing only the list items to roll along with the div, so do as I did in your code that I edited.

http://jsfiddle.net/uQLzv/5/

Explaining the issue:

1- The div .divContainer no longer owns the property height, remains relative, and within it follow 2 Ivs, a .conteudoFixo and .conteudoNormal ... both own the property float:left along with width:100%, in this way one is below the other in the order of its HTML markup.

2- The div .conteudoFixo just has a css styling to give beauty, style as you prefer.

3- Already the div .conteudoNormal, pay attention ... THIS DIV WHO MUST OWN THE PROPERTY overflow:auto along with height: <Valor_que_vc_preferir>, this div will contain the items that will scroll in the box

  • Valew, I 'manjo' AS3 and Javascript, starting Java needing something we are there!

Browser other questions tagged

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