CSS overflow scrollbar is positioned wrong when bottom CSS attribute is set - html

I have a div that has a footer image taking up 26px of space. The CSS is set to display a vertical scrollbar when needed, however I need to make sure the scrollbar doesn't overlap into my footer area, so I use bottom:26px; to bring it up. When that happens though the scrollbar is shifted upwards and I can't see the top of the content or the top arrow of the scrollbar. I am not sure what to change for the css to fix it so the scrollbar is at the very top, and leaves a 26px spacing at the bottom for my image. Any help is appreciated.
HTML
<div id="channel-container">
<div id="channel">
</div></div>
CSS
#channel {
color: #FFFFFF;
text-align: left;
width: 100%;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
position: absolute;
bottom: 26px;
}
#channel-container {
float: right;
width: 31%;
height: 100%;
}

Think about restructuring your html. If the div is supposed to scroll, but the footer is not then I wouldn't group them together. Set margin/padding to 0 on footer and same for bottom of scrollablediv. They should seamlessly mash together. Also obviates the need for using position absolute and a bottom value.
Here is a fiddle of what I think you are after. http://jsfiddle.net/vdZ6R/
<div id="container">
<div id="scrollablediv"></div>
<div id="footer"><img src="" /></div>
</div>

Related

How do I lock a sidebar to the height of a window even when a user scrolls?

I'm running into a minor issue with one of the elements on my page. I have a sidebar which I am attempting to have span the height of the page by using the following CSS:
#sidebar {
width: 180px;
padding: 10px;
position: absolute;
top: 50px;
bottom: 0;
float: left;
background: #eee;
color: #666;
}
The corresponding CSS is pretty much what you'd expect:
<div id="header">
The header which takes up 50px in height
</div>
<div id="main-container">
<div id="sidebar">
The sidebar in question
</div>
<div id="main-content">
The rest of my page
</div>
</div>
The code works as expected for the most part. When the page renders it spans 100% of the height (minus the 50px from the top). The problem is that it essentially assigns the box to the exact height of the window so as I scroll down the box scrolls away instead of staying locked to the bottom of the window. Any ideas how to resolve this?
You have to use position:fixed if you want for the sidebar to be fixed on some position:
#sidebar {
width: 180px;
padding: 10px;
position: fixed;
top: 50px;
bottom: 0;
background: #eee;
color: #666;
}
JSFiddle
Another way would be to give to the parent container position:relative, and on his child position:absolute - but then the parent must have some height so the child element takes its height.
html,body{
position:relative;
height:100%; /* some height */
}
#sidebar{
width: 180px;
padding: 10px;
position: absolute;
top: 50px;
bottom: 0;
background: #eee;
color: #666;
}
JSFiddle
Check learnlayout to read more about positioning.
use css position:fixed to make the sidebar fixed.
in order to lock the height according to screen height i would use javascript/jquery:
$(function(){
// assign to resize
$(window).resize(set_height);
});
function set_height() {
$('#sidebar_id').height($(window).height());
}
hope that helps
First of all, I don't understand how it's spanning 100% of the height when no height has been defined.
Secondly use position: fixed instead of absolute.
On a second note, I'd like to recommend what seems a more proper way of going about positioning this. At the end of the main-container div, before it's closing tag, put this
<div style="clear: both;"></div>
and make the main container also float left, or float right if that doesnt give you what you want. It's suprising how such a common layout can feel tricky to do properly. (at least for newbies like us). I might be wrong, this might not be a better way, but it's the way I'd do it. The extra div you add is so that floated divs take up space, apart from that if it doesn't work, give the sidebar a height of 100%, or if you think it will overflow, tell me I'll add to my answer.

Make an object stick to the top-right side of the page

I added the famous "Fork me on Github" ribbon to one of my projects. The tag looks like this:
<a href="https://github.com/Nurdok/htmlify">
<img style="position: absolute; top: 0; right: 0; border: 0;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"
alt="Fork me on GitHub">
</a>
It looks great, but some of the divs on my webpage have minimum length, so when the window is small, one has to horizontally scroll the screen. When that happens, I want the "Fork me on Github" link to stick to the top-right side of the page, not the window. This is how it looks right now:
Scrolled all the way to the left:
Scrolled all the way to the right:
It seems that the ribbon is placed on the top-right side of the initial window, and stays static.
What I want is for it to be out of sight in the first case and top-right in the second case (when I scroll to the right).
Edit: Thanks for the quick answers, people. However, most of the answers made the ribbon scroll horizontally and vertically with the page. What I want is for it to be fixed on the top-right side of the page (not the browser view), and only be seen if I scroll to where its position is.
You can do a little trick and put your image into a div which has minimal-width.
<div style="position:relative;min-width:960px">
<img src="..." style="position: absolute;right:0;top:0" />
</div>
and put that div at the beginning of <body> section.
position:relative makes that all children of that elements that have position:absolute are positioned absolute according to that div, not whole page. When viewport is bigger than min-width, the div is the same width as the viewport. When the viewport is smaller, the div will have the min-width and the image stays at the corner of the div.
Two alternatives
Sticking to the Viewport: To stick it to the viewport you should position your element "fixed" instead of "absolute"
<img style="position: fixed; top: 0; right: 0; border: 0;"
Sticking to a Container: And if you want it to be sticked to a container (so youn dont see it when you browse left) use absolute but do that container position:relative so its containing block is targeted
If you dont want to see the image when scrolling left then use a explicit width for this container I am talking about
Here is a JSFiddle example.
I used a squared div instead of an image. CSS code as follows:
#container {
width: 700px;
height: 700px;
background: #55ff90;
position: relative;
}
#image {
width: 70px;
height: 60px;
background: #ffff90;
position: absolute;
top: 0px;
right: 0px;
}
In case it's supposed to stick to the right top on horizontal scroll only, you can't accomplish this with basic CSS. Your requirement is stick to the right top for horizontal scroll but not vertical scroll. The first part of the requirement can be accomplished using position: fixed; though this breaks the second part.
How about always sticking to the right top of the website using a relative float: Fiddle
<div id='container'>
<div id='sticky'>x</div>
</div>
#sticky {
width: 100px;
height: 100px;
background: red;
float: right;
}
#container {
width: 100%;
height: 200px;
background: blue;
}
You should use float:right, adjusting margin if you need, e.g.: margin-right: 5px. Cheers :)
If I understand what you want correctly, you'd like for the image to stick to the top corner of the window UNTIL the window gets to a certain size (horizontally) and then stick.
If so, here is a plausible solution:
body{
min-width:1000px; /* or whatever you need it to be */
}
#ribbon{
position:relative;
float:right;
}
DEMO FIDDLE
DEMO FULLSCREEN
You can also use a container div with min-width, your choice.
Change position: absolute; to position: fixed.
As side note, put the style on the a instead of the image and add some z-index to make sure it stays on top of everything else:
<a href="https://github.com/Nurdok/htmlify" style="position: fixed; top: 0; right: 0; border: 0; z-index: 999; display: block;">
<img src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"
alt="Fork me on GitHub">
</a>

Horizontally Centering Absolute Positioned Div

As of now, I can horizontally center the image, but once I try to vertically center it (adding top-margin), the parent div also moves down as well (which is what I don't want).
Here is an image of what I am talking about: Screenshot
I think the best option would be to set it to an absolute position, but then I am having issues horizontally centering it.
<div id="header">
<div id="container">
<div id="logo">
<img src="images/logo.png" />
</div>
</div>
</div>
#container {
width: 960px;
margin: 0 auto;
position: relative;
}
#logo {
height: 96px;
width: 484px;
margin: 50px auto;
}
Help would be greatly appreciated! Thank you
You could put overflow: hidden on the #container. It will introduce a new block formatting context, so the margins are not collapsed anymore.
jsFiddle Demo
The magic of overflow: hidden
Can overflow:hidden affect layout?
From what I see, the container is not going lower (nor it shouldn't), it's just the logo (due to the margin) that it's going lower.
Where do you want to center the image, on the header?
Give the header a fixed height and use margin or padding to center your image.
If the header's height needs to be fluid, then you could go with
#container {
width: 960px;
margin: 0 auto;
position: relative;
vertical-align: middle;
display: table-cell
}
hope that helps!
R

html image position forcing window width too wide

I'm trying to make a website with a header that repeats along the entire page width, with the website's name centered in the header, and a logo slightly offset from the website name. If the screen width is too small to display the entirety of the logo, I just want it to cut off the logo to the right, otherwise the entire logo will be displayed (i.e. the monitor/window is big enough. What I don't want is to float the image to the right. I want it to, more or less, be absolutely positioned near the title.
However, I can't figure out a way to do this. I can't use overflow-x or overflow-y (because of browser support) and the mark-up i currently have just widens the window with a scroll bar to accomodate the entire image (see screenshot).
Here is the screen shot
http://img691.imageshack.us/img691/3188/screenshot03062011.jpg
Here is the mark up and CSS:
<div id="header-wrap">
<div id="header">
<img src="title-card.png" />
<img id="this-chick-logo" src="this-chick-logo.png" />
</div>
</div>
--
body
{
background: #dfb1e4;
padding: 0px;
margin: 0px;
}
#header-wrap
{
background: url('header-bg.png') repeat-x;
width: 100%;
height: 291px;
}
#header
{
margin: 0 auto;
width: 1000px;
text-align: center;
}
#header img
{
margin-top: 105px;
}
#header img#this-chick-logo
{
margin-top: -75px;
margin-left: 680px;
overflow: hidden;
}
Thanks.
Add
overflow: hidden; to #header-wrap
Dont put it on the image.
overflow: hidden; either needs to be added to #header-wrap and possible removed from the img#this-check-logo. Also if the girl likes to push off to the right and some of it gets cut off in the overflow and you don't want that I'd move her a few pixels at a time back towards the left until you get it a way you like it. If you don't mind a little cut off just leave it and only add the overflow: hidden.

Help making the HTML/CSS layout work in IE

Need some help with making the following layout work in IE:
Light gray is a browser window. Dark gray is the main content area, centered against the window. To its left is a fixed width yellow box, and to its right is a variable width green box. The yellow+blue+green triplet is then repeated down to the bottom (it's basically a simple blog layout).
I got this working in Firefox/Chrome by using negative margin-left and floating all three colored boxes. The IE does not understand it. Tried padding dark gray area on both sides by the width of the yellow box (and then do the overflow: visible, white-space: nowrap in the green box) - still no go.
Any ideas or pointers? What the hell does the IE understand?
Thanks
Alex, the trick here is pretty simple. Position those two *fixed_size* and *var_size* containers absolutely within #main. Give #main relative positioning. Then given the two absolutely positioned containers negative left and right margins, respectively.
Should certainly work in IE
Good luck and let me know if you need additional help
EDIT: this is the code that is also visible in the Fiddle:
<div id="main">
lorem ipsum
<div id="left">
</div>
<div id="right">
</div>
</div>
and the CSS:
#main {
margin: 0 auto; position: relative; height: 300px; width: 400px; background: gray;
}
#left {
position: absolute; width: 100px; height: 75px; left: -100px; background: red;
}
#right {
position: absolute; width: 100px; height: auto; right: -100px; background: blue;
}
Obviously, use the appropriate ways to center a div in IE with:
body {
text-align: center;
}
#main {
text-align: left; margin: 0 auto;
}
EDIT2: Check out the updated jsFiddle.. Hopefully that is something like what you wanted: http://www.jsfiddle.net/2avM7/3/
You should start with a master container, that is wide enough to visit all 3 containers from left to right, so like this:
<div id="container" style="margin: 0 auto;">
<div id="fixed_size>Content goes here</div>
<div id="main_content" style="margin: 0 auto;">Center content</div>
<div id="variable_size_container">Content for that goes here!<div>
</div>
margin: 0 auto; does the trick here, it centers a div in the center of its parent div.