I want my page elements to stay in the same position regardless of whether there's a scrollbar on the right edge or not. I've managed to accomplish this for the main content by giving the body 100vw and by giving the main content a margin-right of the width of the scrollbar. (Assume, for simplicity's sake, that the width of the scrollbar is fixed at 16px.) But I can't figure out how to make content on the right stick to where the scrollbar's left edge will be.
In the example below, the "Right" text should not change position when the scrollbar appears, and all of its text should remain visible without hard-coding its left (since that would have to be changed every time the width of its content changed).
const { style } = main;
setInterval(() => {
// simulating lots of main content
style.height = style.height ? '' : '1000px';
}, 2000);
body {
margin-top: 40px; /* because the "Result" gets in the way */
width: 100vw;
margin: 0;
}
#right {
position: absolute;
right: 0;
}
#right > div {
margin-right: 16px; /* SCROLLBAR WIDTH */
}
#main {
padding-top: 60px;
margin-right: 16px; /* SCROLLBAR WIDTH */
}
#main > div {
text-align: center;
margin: 0;
}
<div id=right>
<div>
Right
</div>
</div>
<div id=main>
<div>
Main content (does not change position)
</div>
</div>
I've tried lots of combinations of float: right, position: absolute, margin-right and right properties on the elements, and tried nesting another container on the right, but nothing gave me the desired result. How can this be achieved?
Use left not right with vw unit and rectify with a translation:
const { style } = main;
setInterval(() => {
// simulating lots of main content
style.height = style.height ? '' : '1000px';
}, 2000);
body {
margin-top: 40px; /* because the "Result" gets in the way */
width: 100vw;
margin: 0;
}
#right {
position: absolute;
left: 100vw; /* all the way to the left */
transform:translateX(calc(-1*(100% + 16px))); /* move back considering 100% of its width and scrollbar width*/
}
#right > div {
margin-right: 16px; /* SCROLLBAR WIDTH */
}
#main {
padding-top: 60px;
margin-right: 16px; /* SCROLLBAR WIDTH */
}
#main > div {
text-align: center;
margin: 0;
}
<div id=right>
<div>
Right
</div>
</div>
<div id=main>
<div>
Main content (does not change position)
</div>
</div>
Related
I'm new to css and not sure how to do this.
I have a parent div that has a fixed height and a child div that has a fixed width with horizontal scroll overflow. The problem is that the child divs horizontal scroll is hidden on the bottom of the div. Is there a way so that the child divs horizontal scroll can be displayed as in the attached picture?
Thanks
My approach with jQuery, take a look at this fiddle!
Explanation
This version is desktop & mobile (Touch) compatible. Here, #scroller a hidden div with a horizontal scroll-bar to capture the scrolling and adjust the #inner div's scroll position.
Maximum horizontal scroll ranges can be different between #scroller & #inner, so I'm mapping from one range to the other.
HTML
<div id="outer">
<div id="scroller">
<div id="expander"></div>
</div>
<div id="inner">
<h1>This is looong text for testing</h1>
</div>
</div>
CSS
#outer {
width: 100%;
height: 300px;
border: 3px solid red;
overflow-y: scroll;
position: relative;
}
#inner {
width: 50%;
height: 700px;
border: 3px solid blue;
overflow-x: hidden;
}
#scroller {
height: 300px;
width: 50%;
position: absolute;
top: 0;
left: 0;
overflow-x: scroll;
}
#expander {
width: 200%;
height: 1px;
}
h1 {
width: 700px;
}
JQuery
$("#outer").on('scroll', function() {
var oTop = $("#outer").scrollTop();
$('#scroller').css('top', oTop);
});
$("#scroller").on('scroll', function() {
var scrollerMax = $(this)[0].scrollWidth - $(this).width();
var innerMax = $('#inner')[0].scrollWidth - $('#inner').width();
$('#inner').scrollLeft(mapRange($("#scroller").scrollLeft(), 0, scrollerMax, 0, innerMax))
});
function mapRange(x, in_min, in_max, out_min, out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
try using position:absolute for the child div.
if you could post the code i can show you how on your specific example
I have this piece of code:
<body>
<div class="page-top">
*
</div>
<div class="page-bottom">
<div class="contentbox">
<h1>CONTENTBOX</h1>
</div>
</div>
</body>
And here is the stylesheet:
body {
margin-left: 0;
margin-right: 0;
margin-top: 0;
margin-bottom: 0;
}
div.page-top {
padding-bottom: 20%;
}
div.page-bottom {
padding-bottom: 80%;
background-image: url('http://hd.wallpaperswide.com/thumbs/fog_at_the_pink_house-t2.jpg');
}
div.contentbox {
background-color: red;
margin-top: 10%; <!PROBLEM>
}
The problem is: if I add to the contentbox a margin at the top (see code), instead of just go ten percent beneath the top line of the 'parent' div (.page-bottom), it just creates empty space above both div's.
Why does this occur? What I actually want is that the content div has just a margin of about 20% at all sides so it is a smaller div (contentbox) in a fullscreen div (page-bottom).
To clear things up:
click here for the image
Thanks for your help, and if you need more information I will provide you with that!
Because adding a margin will pushes material into the opposite direction, even if nested. You should add a padding-top to your .page-bottom instead.
You should add padding-top: 10%; to .page-bottom to get more room between the .contentbox and the parent div. See the example below. Cleaned up some code as well.
body {
margin: 0;
}
.page-top {
background-color: blue;
padding-bottom: 20%;
}
.page-bottom {
padding-top: 10%;
padding-bottom: 80%;
background-image: url('http://hd.wallpaperswide.com/thumbs/fog_at_the_pink_house-t2.jpg');
}
.contentbox {
background-color: red;
}
<div class="page-top">
*
</div>
<div class="page-bottom">
<div class="contentbox">
<h1>CONTENTBOX</h1>
</div>
</div>
I have a banner that I am trying to add a text to the bottom portion of it. I got the text centered and how I want to be, but when I want to move the text to the bottom of the page, the picture moves too.
HTML
<div class="col_one art-banner">
<div class="art-banner-text">
<h2>what do <span>you</span> want to learn?</h2>
</div>
</div>
CSS
.art-banner { background-image: url("graphics/art_banner.jpg"); height: 150px;}
.art-banner-text { width: 940px; height: 50px; background-color: rgba(0,0,0,0.5); }
.art-banner-text h2 { text-align: center; padding-top: 10px; font-family: "Bender";}
.art-banner-text span { color: #eb6623; }
JSFiddle
Presuming you're trying to use margin-top to move the art-banner-text down, you're running into the collapsing margin problem: the margin is shared between the inner div and the outer one, meaning the outer one gets the margin too.
So the solution is not to use margins, but position:relative for the outer div and position:absolute for the inner one, with bottom:0 to position it at the bottom of the outer one.
.art-banner {
background-image: url("https://photos-2.dropbox.com/t/2/AAAtS4UXAnyf0x4vH0ty5lE779vFfS2smjUWyJFsFwnMPg/12/18401260/jpeg/32x32/1/1437685200/0/2/art_banner.jpg/COyP4wggASACIAMgBCAFIAYgBygBKAIoBw/L9JVtmzn-g-n3CMbDujkZkXxzuwR9ntwvtEoBLNl_4g?size=1024x768&size_mode=2");
height: 150px;
position: relative;
}
.art-banner-text {
width: 940px;
height: 50px;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
bottom: 0;
}
.art-banner-text h2 {
text-align: center;
padding-top: 10px;
font-family: "Bender";
margin: 0;
}
.art-banner-text span {
color: #eb6623;
}
<div class="col_one art-banner">
<div class="art-banner-text">
<h2>what do <span>you</span> want to learn?</h2>
</div>
</div>
(Note that I had to change the URI for the image, to make it show up. What you had was the URI for the dropbox page that displays the image, not the image itself.)
You need to have the outer container ( which is .art-banner-text) set to position:relative; and set the inner div or element to absolute to place it where you want. https://jsfiddle.net/2ryrnxz7/
<div class="col_one art-banner">
<div class="art-banner-text">
<h2>what do <span>you</span> want to learn?</h2>
</div>
</div>
css
.art-banner { background-image: url("https://www.dropbox.com/s/migdkqlmse8ym0t/art_banner.jpg?dl=0"); height: 150px;}
.art-banner-text { width: 940px; height: 50px; position: relative; background-color: rgba(0,0,0,0.5); }
.art-banner-text h2 { font-family: "Bender"; margin: auto 0; padding:0px; bottom:0px; position:absolute; left:35%}
.art-banner-text span { color: #eb6623; }
You can set the left to whatever % you want to push towards the middle. This won't work for mobile as it is set and won't reposition itself with the page. But if you just need it to work for desktop, this is how to do it.
It sounds like you might want to use CSS positioning. For example .art-banner {position: relative;} .art-banner-text {position: absolute;} You can then position, move, or animate the text in the inner div without affecting the outer div.
I have been trying to determine the cause of the following issue on a web page.
With the code below, when any browser is resized and the page becomes horizontally scrollable, a white space appears on the right.
How can I remove this white space? Thanks for all help given!
body {
margin: 0;
padding: 0;
}
.wrap {
background-color: #2a2c67;
}
.main {
height: 50px;
text-align: center;
width: 1300px;
margin-right: auto;
margin-left: auto;
color: #fff;
}
<div class="wrap">
<div class="main">Content Goes Here</div>
</div>
the container "wrap" has no width set so, as a block element it will take 100% of the ACTUAL window size. when you horizontal scroll because the child has a fixed width bigger than his parent and the actual window, you will scroll the child, but the parent will remain with whatever window size it's atm and scrolling out of the window. It won't dinamically change his current width (as browsers understand) to fill the child width.
Imo you just have to change the background color to the children to fix it (not that it's broken).
Edited: Or as other people said... set the width to the wrap and not to the "main"
body {
margin: 0;
padding: 0;
}
.wrap {
}
.main {
height: 50px;
text-align: center;
width: 1300px;
margin-right: auto;
margin-left: auto;
color: #fff;
background-color: #2a2c67;
}
<div class="wrap">
<div class="main">Content Goes Here</div>
</div>
I have a DIV measuring 400px wide, containing two DIVs side-by-side, each with width of 400px and height of 600px. The width of both DIVs is fixed, however the height can vary. I'd like to hide the second DIV and show the first completely, with no scrolling inside the DIV.
My solution, I thought, was to hide the overflow-x. This seems to also hide the y overflow too.
Here's my code:
#schools-sub-nav {
}
#schools-container {
width: 400px; /* Set the width of the visible portion of content here */
background-color: fuchsia;
position: relative;
overflow-x: hidden;
}
#schools-list {
width: 400px; /* Set the width of the visible portion of content here */
height: 600px; /* Delete the height, let the content define the height */
background-color: purple;
position: absolute;
top: 0;
left: 0;
}
#boards-list {
width: 400px; /* Set the width of the visible portion of content here */
height: 600px; /* Delete the height, let the content define the height */
background-color: green;
position: absolute;
top: 0;
left: 400px;
}
<div id="schools-sub-nav"> Schools List // Boards List </div>
<div id="schools-container">
<div id="schools-list"> One </div>
<div id="boards-list"> Two </div>
</div>
I expect #schools-list to be visible, but for some reason overflow-x: hidden in #schools-container hides it.
The way you made the two divs (with an absolute position) void the overflow rule!
You need to change the position type (to normal/not absolute) and I suggest using floats, finally, the container div that you want to apply the overflow, needs to have a way to fit it, like placing a div at the end with clear: both (in the case of using floats).
EDIT: I just tried it and you can hide the second div by following the upper suggestion and adding another surrounding div inside with a very large width and change the overflow-x to overflow for the main container div.
Like this:
<div id="schools-container">
<div id="schools-container-inside">
<div id="schools-list"> One </div>
<div id="boards-list"> Two </div>
</div>
</div>
And then the CSS (I commented the original not used CSS and added the new div class at the end):
#schools-container {
width: 400px; /* Set the width of the visible portion of content here */
background-color: fuchsia;
position: relative;
/*overflow-x: hidden;*/
overflow: hidden;
}
#schools-list {
width: 400px; /* Set the width of the visible portion of content here */
height: 600px; /* Delete the height, let the content define the height */
background-color: purple;
/*
position: absolute;
top: 0;
left: 0;
*/
float: left;
}
#boards-list {
width: 400px; /* Set the width of the visible portion of content here */
height: 600px; /* Delete the height, let the content define the height */
background-color: green;
/*
position: absolute;
top: 0;
left: 400px;
*/
float: left;
}
#schools-container-inside {
width: 10000px;
overflow: hidden;
}
JsFiddle here: http://jsfiddle.net/MbMAc/
I think you need this
#schools-container {
width: 400px; /* Set the width of the visible portion of content here */
background-color: fuchsia;
position: relative;
overflow-x: hidden;
overflow-y:auto;
height:600px;
}
You need to define height of main div as well.