Reputation: 19453
I want to have the div to always fix floating top appear within a div and my code looks something like this:
HTML:
<p>Some on page content</p>
<div id="cover">
<div id="floatdiv">floating fix top bar</div>
<p>first contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p>
</div>
<p>Other content ..</p>
CSS:
#cover{
border: 1px solid red;
width: 500px;
height: 300px;
overflow: auto;
}
#floatdiv{
background-color:black;
color:white;
padding:10px;
}
How can it be done?
Upvotes: 2
Views: 112
Reputation: 1
HTML page:
<p>some content page</p>
<div class="outerCovering">
<div id="cover">
<div id="floatdiv">floating fix top bar</div>
<div><p>first contents</p> <p>second contents</p> <p>third contents</p></div>
</div>
</div>
css page:
#cover{
border: 1px solid red;
width: 500px;
height: 300px;
overflow: auto;
#floatdiv{
background-color:black;
color:white;
padding-top:10px;
padding-bottom:10px;
position: absolute;`enter code here`
width: inherit;
}
`
Upvotes: 0
Reputation: 4525
you can use position: absolute
to remain the div. Then use width: inherit
to get the parent's width, also specify the padding
to top
and bottom
to avoid the overflowing of div
#floatdiv{
background-color:black;
color:white;
padding-top:10px;
padding-bottom:10px;
position: absolute;
width: inherit;
}
UPDATE:
added <p> </p>
to display the first value
Upvotes: 3
Reputation: 451
I have done it with following html and css, Hope it helps.
HTML
<p>Some on page content</p>
<div id="cover" class="cover">
<div id="floatdiv">floating fix top bar</div>
<div class="scrollableContent">
<p>first contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p>
</div>
</div>
<p>Other content ..</p>
CSS
#cover{
border: 1px solid red;
width: 500px;
height: 300px;
overflow: hidden;
position:relative;
}
#floatdiv{
overflow:hidden;
position: absolute;
top: 0;
left: 0;
width:100%;
background-color:black;
color:white;
padding:10px;
}
.scrollableContent{
height: 250px;
margin-top:30px;
overflow-y:scroll;
}
JS Fiddle Working with first option visible
Upvotes: 4
Reputation: 688
A little Bit Modified Your existing Code
<p>Some on page content</p>
<div class="outerCovering">
<div id="cover">
<div id="floatdiv">floating fix top bar</div>
<div><p>first contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p></div>
</div>
</div>
Here is your Css code
#cover{
border: 1px solid red;
width: 500px;
height: 300px;
overflow: auto;
}
#floatdiv{
background-color:black;
color:white;
padding:10px;
position: absolute;
top: 0px;
width: 100%;
}
Upvotes: 0
Reputation: 9593
If I understand your question, you can give it a position: absolute
and width: 100%
:
#floatdiv {
background-color:black;
color:white;
padding:10px;
position: absolute;
width: 100%;
}
Upvotes: 1
Reputation: 17471
If you don't set left/top properties, it will be fixed
/absolute
against the closest relative
/absolute
parent div.
#floatdiv {
background-color:black;
color:white;
padding:10px;
//I would go with absolute
position: absolute;
width: 465px;
display: block;
}
If you use fixed instead absolute, when you scroll the page, the fixed div will be also visible. Also, add padding-top: 45px
to #cover
or the first paragraph will be hidden behind #floatdiv
Upvotes: 1