Position:absolute causes horizontal scrollbar - html

Absolutely positioned (side yellow advertisements) div's cause unwanted horizontal scrollbar when window is resized (size decreased) beyond them. Scrollbar should appear only when window is smaller than main #container and these advertisement div's should not affect the layout. It doesnt matter if they get covered.
HTML:
<div id='topbar'>
<div id='menu'> <a href='#'>Link1</a>
<a href='#'>Link2</a>
<a href='#'>Link3</a>
<a href='#'>Link4</a>
</div>
</div>
<div id='container'>
<div id='pushfix'></div>
<div id='ad_container'>
<div id='ad1'>ad</div>
<div id='ad2'>ad</div>
</div>
Lorem ipsum placeholder text
</div>
CSS:
body, html {
height:100%;
}
body {
margin:0;
}
#topbar {
width:100%;
background-color:#DCDCDC;
height:40px;
position:absolute;
}
#menu {
width:250px;
background-color:#B3B3B3;
margin:0 auto;
height:100%;
text-align:center;
line-height:40px;
}
#menu a {
color:#fff;
}
#container {
height:100%;
background-color:#808080;
width:240px;
padding:0 5px;
margin:0 auto;
}
#pushfix {
height:40px;
}
#ad_container {
position:relative;
width:240px;
}
#ad_container div {
width:100px;
background-color:yellow;
height:300px;
position:absolute;
}
#ad1 {
left:-105px;
}
#ad2 {
right:-105px;
}
Exact layout replica: http://jsfiddle.net/8UkQA/

Absolutely-positioned elements that expand beyond the boundaries of the body seem to cause scrollbars to appear, for some reason. You can remedy this by simply wrapping everything inside the body tag in a relatively-positioned div styled with overflow: hidden;. The absolutely positioned content that expands beyond the boundaries of this container won't cause scrollbars on the window.
Here's a working example: http://jsfiddle.net/8UkQA/1/

I may like to further add, if the same problem is being faced and by using the solution suggested by #Aaron the page seems to not scroll then you can use axis specific version of the "overflow" attribute, as following,
overflow-x: hidden;
This will only hide the content protruding on the right hand side (or left hand side if website is RTL) and not the vertical content.
Also to further enhance this method if the protruding content is appearing only at a certain resolution (as in my case), you can use css media query to restrict the behaviour.
#media (min-width: 1500px) {
body {
overflow-x: hidden;
}
}

You need to give the child coordinates a.k.a. top: 0; left: 0;

// need to disable AutoScroll first, otherwise disabling the horizontal scrollbar doesn't work
flowLayoutPanel.AutoScroll = false;
// disable horizontal scrollbar
flowLayoutPanel.HorizontalScroll.Enabled = false;
// restore AutoScroll
flowLayoutPanel.AutoScroll = true;
Hope this will resolve your issue.

Related

Fixed div width is wider than the screen

I am trying to fix a div with a 50vw width. However, when I fix the div, 50vw acts as if it's 100vw.
In the example below, to get the effect I want, I have to make the target 25vw instead of 50vw. 100vw is wider than the screen.
Here is the jsfiddle. the blue .target container should be half the width of the yellow container.
<div class="main">
.
<div class="content">
<div class="content-wrapper">
<div class="target-containers">
<div class="target">. </div>
</div>
</div>
</div>
</div>
CSS
html, body {
margin: 0;
width:100%;
height: 100%;
}
body {
background-color:gray;
}
.main {
background-color: yellow;
min-height:100vw;
position:relative;
margin-left: 25%;
margin-right:25%;
}
.content-wrappers {
position:relative;
}
.target-containers {
position:relative;
}
.target {
min-width:50%;
width:50%;
position:fixed;
float:left;
background-color:blue;
}
Please read about position: fixed.
It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none
Who is the initial containing block? (EDIT to clearify this comment)
Please read about identifying the containing block
Note: The containing block in which the root element () resides is a rectangle called the initial containing block. It has the dimensions of the viewport (for continuous media) or the page area (for paged media).
Please read about position: sticky
A stickily positioned box is positioned similarly to a relatively positioned box, but the offset is computed with reference to the nearest ancestor with a scrolling box, or the viewport if no ancestor has a scrolling box.
You could change the position value to sticky like this
.main {
background-color: yellow;
max-height:100vh;
min-height: 100vh;
margin-left: 25%;
margin-right:25%;
overflow-y: auto
}
.target{
min-width:50%;
width:50%;
position:sticky;
background-color:blue;
top: 0;
}
Fiddle Example
If you make an element position:fixed it will break it out of the document flow.
If you remove position: fixed it works as expected as it is inheriting from the parent element.
https://jsfiddle.net/k460abmv/

Cannot hide scroll, though overflow is hidden?

<div class="main">
<div class="content">
<div class="left_container">
</div>
</div>
</div>
In the code above, I have added a scroll to the left_container div.
But I want to hide it! Referring to other questions and answer, I found out to set overflow:hidden in the parent div class.
But still the scroll is not hidden in the child class?
CSS:
.content
{
background-color: white;
width:100%;
height:auto;
margin-top:10%;
position: absolute;
overflow: hidden;
}
.left_container
{
background-color:;
margin-left:5%;
margin-top:5%;
width:70%;
height:1000px;
overflow:auto;
}
Well I have also tried to set it hidden in the body! still not working..?
There is actually a simple way.
.left_container { overflow: hidden }
Now give left_container a child div and apply the following styles
.left_container-child {
height: 100%;
overflow-x: scroll;
width: 101%;
}
What this does is push the scrollbar out of sight.
You really want to check this over on differnet browsers and make sure you get the sweet spot that your content doesnt get cut off.
You can see a working example here (change the width on left_container-child to experiment pushing the scrollbar out).
Example
You have not used overflow:hidden in .left_container.
Try:
.left_container {
background-color:;
margin-left:5%;
margin-top:5%;
width:70%;
height:1000px;
overflow:hidden;
}
I think can't hide scroll bar for overflow hidden content.
you can try using scroll bar plugin http://nicescroll.areaaperta.com/demo.html

Getting rid of space under relative div with negative margin-top

I have some kind of animated javascript slider. I wan't some items to stick out a little of the top of my slider but to be hidden when they stick out of the bottom of the wrapper, so i made a div with overflow:hidden; with a little extra space over it. I now need to get rid of that extra-space in the flow.
It wasn't a problem before because i'd just set the div as absolute but now i'm making my layout fluid and i can't have absolute divs because the slider's height is relative to the document's width. Is there any way to get rid of those extra-pixels ? Thanks in advance.
http://jsfiddle.net/ySg6f/
Here's the actual website if it can help : http://pa3com.a3net.fr/
<header>
<div id="slider_wrapper">
<div class="overflow">
<div id="slider">
</div>
</div>
</div>
header
{
padding-top:20px;
background-color:blue;
}
#slider_wrapper
{
position:relative;
background-color:green;
}
.overflow
{
padding-top:12px;
overflow:hidden;
}
#slider
{
height:0;
padding-bottom:25%;
background-color:red;
top:-12px;
position:relative;
}
.overflow {
padding-top: 0;
}
#slider {
top: -12px; /* remove this */
}
Finally solved it by using negative margin and adding 1px padding on the parent.

Not centered horizontally because of position absolute

I made this:
HTML:
<body>
<div id="header" >
</div>
<div id="main" >
</div>
<div id="footer" >
</div>
</body>
CSS:
body
{
margin:0px;
}
#header
{
width:100%;
background-color:black;
height:60px;
}
#main
{
width:300px;
border:1px dotted black;
margin:0 auto;
}
#footer
{
width:100%;
background-color:black;
height:40px;
position:absolute;
bottom:0px;
}
http://jsfiddle.net/VpwQQ/2/
But as you can see, the main div doesn't have a height.
Then I replaced my css by that:
body
{
margin:0px;
}
#header
{
width:100%;
background-color:black;
height:60px;
}
#main
{
width:300px;
border:1px dotted black;
position:absolute;
margin:0 auto;
bottom:60px;
top:80px;
}
#footer
{
width:100%;
background-color:black;
height:40px;
position:absolute;
bottom:0px;
}
http://jsfiddle.net/VpwQQ/1/
But then, the horizontal center doesn't work.
How can I do this design (div centered and that takes all the page in height between the header and footer with a 20 px magin) ?
I'm not sure what you're trying to do, but I'll give my explaination of what's going to happen with your code:
Your #main div doesn't have a height because it doesn't have a height CSS property, nor does it have any content.
You should add either a height: 100px or just add some content and you will see it gets a height.
The reason why I ask what you want to do is because you're not very clear as to what you want your final product to look like.
You're going to have another problem with the footer. If you use position absolute it sticks to the bottom at the moment. Set the height of the #main div to something ridiculously high and you'll see that when you have to scroll down the page the footer stays where it is. See http://jsfiddle.net/VpwQQ/3/
You should use position: fixed but this will keep it on the bottom of the WINDOW and not the DOCUMENT. So then you get into the problem of having to use Javascript in order to measure the document height and setting positions appropriately. Not sure what you're trying to do, but if you're just trying to lay out a website then use standard relative positioning to push the footer down naturally below the #main div.
Edit:
See http://jsfiddle.net/VpwQQ/4/ if you're just trying to set up a normal website layout.
If you want the footer to "stick" to the bottom of the page all the time then you will need to use position: fixed but I don't think this works across all browsers. See http://jsfiddle.net/VpwQQ/6/
Lastly, to get both footer and header to "stick" see http://jsfiddle.net/VpwQQ/8/
I added a div inside #main.
Main now has a 100% width.
Inside, put a div of 300px, with no absolute position.
I forked your fiddle: http://jsfiddle.net/8U9P6/
Personnally I prefer the javascript solution and not using the absolute position. But this solution seems to work.
Add and overflow to contain the content in the inside div: http://jsfiddle.net/M2nZc/
Note that the page will not grow as it is absolute position.
You can't use automatic margins on an absolutely positioned element, as it's not in the document flow any more.
Use width: 100% on the #main div, then put another element inside it that you center using automatic margins.
Demo: http://jsfiddle.net/Guffa/VpwQQ/9/
Note: You may need to use height: 100% on the body and html elements for the bottom sizing to work on the #main element.
Once you fill your #main div with content, it will automatically gain height according to the content. You can simply fill it with a few paragraphs of lorem ispum to simulate content. You can now remove the absolute position and positioning CSS.
Centering a div using the "0 auto" shorthand only works when the parent element (which, for the #main div, is the body element) has a defined width. To do this, try giving your body element a width of 100%. Doing this is something that you might want to make a habit of in you CSS.
To have your #main div always be 20px below the #header div, simply add 20px of margin-bottom to your #header div. Do the same below the #main div to space the footer.
Summed up (without the footer at the bottom, for now) your CSS might read something like this:
body {
width: 100%
margin: 0px;
}
#header {
width: 100%;
height: 60px;
margin-bottom: 20px; /*here we space the header 20px from the next element*/
background-color: black;
}
#main {
width: 300px;
margin: 0 auto 20px auto; /*we append the margin to include 20px of spacing at the bottom*/
border:1px dotted black;
}
#footer {
width:100%;
height:40px;
background-color:black;
}
Example: http://jsfiddle.net/WEx3j/
If you want the footer to be 'sticky' (always be at the very bottom of your website), I advise you to employ this method.
I hope this clarified a few things.

CSS Centering Slideshow Images

I am having issues horizontally centering my slideshow images so that they are central on all screen sizes / resolutions.
The HTML looks as such
<div id='banner'>
<div class='slides_container'>
<div>
<a href='#'><img src='images/banner.png'></a>
</div>
<div>
<a href='#'><img src='images/banner2.png'></a>
</div>
</div>
</div>
And the CSS to match this is:
#banner {
width:100%;
margin-bottom:50px;
}
.slides_container {
width:100%;
height:500px;
}
.slides_container div {
width:1100px;
height:500px;
text-align:center;
}
I am really struggling here to get the image to center on all screen sizes since padding and margins don't work I am in need of a different method!
Any replies are extremely appreciated.
You should make sure the .slides_container div is centered within its parent, i.e.
.slides_container div {
margin: 0px auto; // center
width:1100px;
height:500px;
text-align:center;
}
If that doesn't work, you need to make sure the parent container is width 100% of the page.
If the parent is not width 100% of the page, the parent needs to have this property also:
.slides_container {
margin: 0px auto;
}
If that doesn't work, then you need to make sure its parent is 100% width of the page.
Hope this helps.
Edit
I took a look at it in FireBug, and it was immediately apparent that the slide container is set to 3800px wide, and the div inside doesn't have a width set. If you set the div inside the slide container to 100% width, it will cause it to become 3800px wide, so that won't work.
By the nature of the script you are using, it is using an abolute-positioned div to work. So margin: 0px auto won't work here.
The solution is a bit of javascript to run onload, and on window resize, to set that div which holds the image to the width of your browser window, and text-align: center. So for example, since I have 1280px wide monitor, this centers the image for me:
.slides_control div {
width: 1280px;
text-align: center;
}
Add .slides_container img and margin:0 auto
#banner { width:100%; margin-bottom:50px; }
.slides_container {
width:100%;
height:500px;
}
.slides_container div, .slides_container img {
width:1100px;
height:500px;
text-align:center;
margin:0 auto; }
Normally they use margin:0 auto; to handle this. text-align won't do you good for div.