How to block scrolling without "jumping" - css

Well i know about overflow-y: hidden, but I dont like that my site "jumps" to the right side like for 20px every time i use overflow-y: hidden.
Is it possoble to block scrolling without "jumping"?
here is example http://jsfiddle.net/wjyb8tzw/6/

Just give a margin or padding of 20px at the right side.
padding-right:20px;

The line overflow-y disables the scrollbar on the right, this changes the maximum width of the webpage.
The line margin: auto centers the div based on the maximum width of the webpage, therefore disabling the scrollbar moves the div a little to keep it centered.
If you specifically don't want the div to be centered then use margin-left: -17px; as the width of the of the scrollbar is 17 pixels as stated here.

Related

CSS: Align div to center when viewport's large enough, otherwise to negative margin?

Let's assume the following html markup:
<div id="container">
<div id="inner"></div>
</div>
I would like the inner div to be centered in its parent (container) while if there is not enough space in the viewport, it should gradually go to negative left margin upto -XY px.
The situation is:
I have a container div that always has a plain background. In this div I have another div that has a ribbon as a background and contains the navigation. If the viewport is wide enough, I want to show everything (=from the inner div) centered on the page. So far no problem by setting margin: 0 auto to the inner. However, when the viewport is less than the ribbon's width, I would like to "cut off" the ribbon on the left side (logically, on the right it is automatic) to the point where it "bends", that's the point where the buttons of the navigation begin (169px from left). I have come up with this:
#container {
height: 240px;
position: relative;
background: url("../images/top_noise_bg.png") top left repeat #222;
margin-left: -169px;
margin-right: -169px;
}
The margin works as intended when the viewport is large enough. When it is too small, the left side works just right, however the negative right margin cuts the *top_noise* background off on the right side when I scroll there. Also it creates another undesired effect: the page will always have horizontal scrollbars. Any advices how to solve this? Thank you!
jsfiddle example -> resize the area to very small, then scroll to right of the red div.

Stretch / shrink parent div to fit content's width

I am trying trying to make a div's width as wide as it's content. Here's a fidle to show what I mean:
http://jsfiddle.net/djxpU/
I want the blue area to be as wide as the white. I tried float:left and display:inline-block, however they won't work with position:absolute;. Any workarounds?
If you want the white area to fit the blue parent, you'd set the width of the white to 100% #X{
width:100%;
}
Block-level elements actually do this naturally. The problem you have is, absolute positioned elements are taken out of the normal flow, so the block can't wrap around your white boxes.
Is there a reason you need them positioned absolute?
EDIT: If you just wanted the white boxes to be centered, here you go: http://jsfiddle.net/Marconius/djxpU/1/
Code (because I have to): margin: 0 auto;
By default a div will be the width of its parent and will display as block. Here is an example of the divs filling the available space while still maintaining the left margin.
Apply this to your 'X' divs: { margin-left: 120px; height: 40px; background-color: white;}
http://jsfiddle.net/yz3Dk/

centre div horizontally

On this page, I want the main content div - which has an id value of container - to be horizontally centred on the grey background. However I want the black login panel to remain stretch across the entire width of the screen.
In an effort to achieve this, I added the rule:
#container {
margin: 0 auto;
}
But it doesn't work, what am I doing wrong?
Update
Thanks for the answers. It was suggested that I fix the problem by removing the max-width from the body and setting a width on the container.
This centres the container, but causes it to occupy all the available horizontal space. What I want is for the container to be centred with a width of (say) 900px, and the grey background should appear in the "empty" space on the left and right of the container.
you need to specify a width, otherwise the margin won't know how to centre...
like this:
#container {
width: 960px;
margin: 0 auto;
}
EDIT:
Also, remove the max-width on your body!!
The issue is that you have max-width: 960px; on your body element. Non-absolute elements will not size past the boundaries of their parent element.
You should instead be setting max-width (or better width) on the #container element, otherwise the div will automatically size to 100% as it is a block-level element.

HTML: I want to create a DIV thats horizontal centered and reaches from the top to the bottom

I want to create a page with a horizontal centered content block that reaches from teh top to the bottom of the browser window. I already figured out that tables are not the right way to design a layout. A block that reaches from top to bottom is not the problem:
<div style="position:absolute;top:0px;width:800px;height:100%;background-color: #fff;">
</div>
But I'm not able to make this Div centered. I tried
"margin:auto"
But no effect. Th centers the text in the Div, but not the Div itself on th screen.
To center a div you need two things, a width, and automatic horizontal margins. Like this:
#myDiv {
width:800px; /* or whatever */
margin:0 auto;
}
There is no need for absolute positioning, just these two rules will do the trick.
to center an Absolutely Positioned div add left: 50%; margin-left: -400px;
where the negative margin value is half the width of the div
Try not to use position:absolute for layouts unless necessary. This sample shows best practice for horizontally centering your content.
If you need a solution that will continuously work to restrain the content area height within the viewable area, try my jQuery solution: http://jsfiddle.net/BumbleB2na/Z75hA/

Absolute div shifts when scrollbar is present

i have a problem with the entire content of my page.
The problem being without a scrollbar present my content is about 20px to the right, but when a scrollbar is present it shifts to the left.
I have to compensate for this for an absolute postioned div by positioning it over the content by 20px until a scrollbar is present as it rests at the right hand side of the page.
This is a crappy fault on my behalf but i just want an easy way to fix this. Any quick and easy suggestions? Would i be better off making the main content div an absolute one?
One quick and dirty way is to always force the scrollbar to be visible with:
html { overflow-y: scroll; }
Not ideal, but it standardizes the appearance if the lack of scrollbar offset is breaking your design.
If I'm understanding your problem correctly, your absolute div is 20px off when a scrollbar is present? If that is the case what you can do is set a parent div that wraps around your content and absolute div.
Be sure to set this wrapper div to position: relative; so now your absolute div will be positioned inside the relative div instead of the document level. If there is a scrollbar, the wrapper div will be offset to the left by 20px (the width of the scrollbar) and the absolute div will also.
<div class="wrapper">
your content goes here
<div class="absoluteDiv"></div>
</div>
.wrapper { position: relative; }
.absoluteDiv { position: absolute; }
I don't think your content is actually shifting in any sort of buggy way; it's just that the presence of the scroll bar makes the viewport narrower. If you're using a layout that depends on viewport width (e.g. fluid layout, or fixed-width with centered content), this will cause everything to move by half the width of the scroll bar when it appears.
AFAIK, there's no sure-fire way to compensate for that, since the width of the scroll bar isn't known.

Resources