Div with position absolute having innerwidth of div with scrollbar - html

The last two days I've been reading most questions here and a lot more about 'fill remaining width' and 'escaping overflow: hidden', but I can't get my problem solved. At the moment, I seriously doubt if it is possible at all.
I have a scrolling box with full body width. On top of that I have a absolute positioned header that I need to make the exact same width as the scrollbox. My intention is to make the header 0px or (if needed) 1px in height and let the content overflow.
Here is a fiddle.
The scrollbox has a scrollbar (always visible), the header obviously not. To compensate for that, I float a fake scrollbar to the right inside the header container, and left of that a <div> filling the remaining width (being exactly the innerwidth of the scrollbox).
HTML
//THE SCROLLBOX
<div id="scrollbox">
<div id="center2">
content<br>content<br>...
</div>
</div>
// THE HEADER
<div id="header_box">
<!--- FAKE SCROLLBAR -->
<div id="scroller">
<div></div>
</div>
// REMAINING WIDTH
<div id="container">
<div id="FIRST">
<div id="FIRST_banner"></div>
</div>
</div>
<div id="SECOND">
<div id="SECOND_banner"></div>
</div>
</div>
</div>
CSS
#header_box {
background: yellow;
position: absolute;
left: 0;
top: 0;
right: 0;
height: 25px;
width: 100%;
overflow: visible;
}
#scroller {
float: right;
overflow-x: hidden;
overflow-y: scroll;
height: 50px;
width: auto;
/* visibility: hidden; */
}
#scroller>div {
width: 0px;
height: 101%;
}
#container {
display: inline;
width: auto;
height: 50px;
overflow: visible;
}
#FIRST {
position: relative;
overflow: hidden;
height: 25px;
background: pink;
}
#FIRST_banner {
position: absolute;
top: 0;
left: 50%;
height: 220px;
width: 30px;
background: crimson;
}
#SECOND {
background: darkcyan;
position: relative;
height: 5px;
}
#SECOND_banner {
position: absolute;
top: 0;
left: 50%;
height: 220px;
width: 30px;
background: blue;
}
The problem lies in the div (#FIRST) with remaining width. From all the solutions I've read only the one with
position: relative;
overflow: hidden;
works for me. It gives the exact width, lining up the center of the header and the scrollbox nicely. But I can't break out of the overflow: hidden, so it cuts off the content.
So my second thought was: wrap #FIRST in a #container and let the child determine the width of the parent. After that, I can put another div (#SECOND) inside the container with the width of the parent. It works partially. The #container has the width intended, and the #SECOND div overflows nicely but takes on the width of #header_box, as no width is set on the parent itself.
So, my questions:
Can I somehow break out of the overflow: hidden of the FIRST div? (In that case the container and second div can be removed).
Is there a way to let the SECOND div obey the width of it's parent.
Some totally different solution.
Sadly there is a catch to this all:
css only
no javascript
no flexbox
Thanks voor any toughts.

In the end, it was the good old <table> that saved the day, much simpler than I tought. There still is a fake scrollbar, but the absolute header now aligns perfect with the contents of the scrollable div behind it, and it remains fluid.
See fiddle here
HTML:
<!--- HEADER -->
<div id="THIRD">
<div id="THIRD_A">
<div id="THIRD_B"></div>
<div id="THIRD_C"></div>
<div id="THIRD_D"></div>
</div>
</div>
<!--- FAKE SCROLLBAR -->
<div id="scroller">
<div></div>
</div>
</div>
CSS:
/* The container for the header */
#header_box{
position: absolute;
left: 0;
top: 0;
right: 0;
height: 0px;
width: 100%;
overflow: visible;
display: table;
}
/* Takes on the width of its child: the fake scrollbar */
#scroller {
display: table-cell;
float: right;
overflow-x: hidden;
overflow-y: scroll;
height: 0px;
width: auto;
}
/* This triggers a scrollbar to be shown */
#scroller>div {
width: 0px;
height: 101%;
}
/* The 'remaining width' container (= screenwidth - scrollbar, equals innerwidth of scrollbox) */
#THIRD{
display: table-cell;
width: 100%;
height: 0px;
}
/* Needed to give the children a 'width' reference */
#THIRD_A{
position: relative;
width: 100%;
height: 0px;
}
/* The actual header items */
#THIRD_B {
position: absolute;
top: 0;
left: 50%;
width: 25px;
height: 220px;
background: black;
}
#THIRD_C {
position: absolute;
top: 0;
left: 10%;
width: 125px;
height: 120px;
background: black;
}
#THIRD_D {
position: absolute;
top: 0;
right: 0%;
width: 25px;
height: 220px;
background: black;
}
NOTE:
On most handheld browser, this is 1px off. It seems webkit based browsers display a tablecell of 0px width as 1px width (see this question). The solution is to wrap the table in another div with css:
position absolute;
left: 0;
right: -1px
and setting #scroller>div to a width of 1px.
NOTE 2:
This is also a solution for a fixed div inside a scrollable div. See this fiddle

Related

How to show a child div on top parent div when the parent div's horizontal scroll is visible

I am trying to show a child div with its 100% height on top of a parent div when the parent div's horizontal scroll is visible. Although initially, it seemed really straight-forward but have been struggling with it all the while.
Here's the markup https://codepen.io/ambarbs/pen/OwwojV
.parent {
background: grey;
height: 100px;
width: 200px;
overflow-x: auto;
overflow-y: hidden;
color: white;
/* z-index: 0; */
}
.child {
width: 400px;
}
.menu {
height: 300px;
background: orange;
z-index: 500;
}
<div class='parent'>
<div class='child'>
I want to show the orange .menu DIV on top of this DIV.
Is it possible when the horizontal scroll is visible?
</div>
<div class='menu'></div>
</div>
UPDATE: The menu is actually a context-menu and should always show ideally show on top of the scrollbar as well.
Using CSS you can do it by:
Set .parent to position: relative;
Set .child to position: absolute;
Set .menu to position: sticky; left: 0;
.parent {
position: relative;
height: 100px;
width: 200px;
overflow-x: auto;
overflow-y: hidden;
}
.child {
position: absolute;
width: 400px;
}
.menu {
position: sticky;
left: 0;
width: inherit;
height: inherit;
background: rgba(255, 120, 0, 0.8);
}
<div class='parent'>
<div class='child'>
I want to show the orange .menu DIV on top of this DIV.
Is it possible when the horizontal scroll is visible?
</div>
<div class='menu'>MENU</div>
</div>
Beware that Position Sticky is not supported by any IE browser (of course)
SOLUTION FOR IE 9, 10, 11
A bit of JS could help you to handle older browsers.
Basically, on scroll it uses translateX to set the .menu position:
const transX = (ev, el) => {
ev.target.querySelector(el).style.transform = `translateX(${ev.target.scrollLeft}px)`;
};
document.querySelector(".parent").addEventListener("scroll", ev => transX(ev, ".menu"));
.parent {
position: relative;
height: 100px;
width: 200px;
overflow-x: auto;
overflow-y: hidden;
}
.child {
position: absolute;
width: 400px;
}
.menu {
position: relative; /* Set to "sticky" and remove JS if you don't care about IE */
left: 0;
width: inherit;
height: inherit;
background: rgba(255, 120, 0, 0.8);
}
<div class='parent'>
<div class='child'>
I want to show the orange .menu DIV on top of this DIV.
Is it possible when the horizontal scroll is visible?
</div>
<div class='menu'>MENU</div>
</div>
Solution
Set the position property of the desired child to fixed. That way, its position is based on the browser window instead of the parent. For a fixed <div> with no content, you must specify the width and height properties. If you want the width the same as the parent, you can use the inherit value.
.menu{
position: fixed;
height: 300px;
width: inherit;
background: orange;
left: 10px;
top: 10px;
}

Off screen div is giving body a horizontal scrollbar

I have a sidebar on the right that'll pop out from off-screen once triggered. I keep it in the body tag because it needs to always be the same height as the entire page which varies from page-to-page. If I give the body an overflow-x: hidden, it'll hide the contents on smaller browser windows and not allow them to scroll. Is there any way around this?
I need the sidebar to scroll with the page, so I can't use position: fixed
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
}
#sidebar {
position: absolute;
top: 0;
right: -100px;
width: 100px;
height: 100%;
}
<div id="sidebar"></div>
This is just a stripped down example.
One simple answer is to place the sidebar inside a position: absolute container (.hideScroll) which has overflow: hidden.
The new parent is given the entire width and height of the viewport and wont affect body scrolling.
In this example I have used viewport percentage lengths (vh) instead of percentage heights. These units get their height from the viewport and are not relative to any parents.
Example
Hover over the body in the window to trigger the sidebar.
.hideScroll {
overflow: hidden;
position: absolute;
height: 100vh;
left: 0;
top: 0;
width: 100%
}
#sidebar {
position: absolute;
top: 0;
width: 100px;
height: 100vh;
background: #F00;
right: -100px;
transition: right 1s;
}
body {
margin: 0;
/*150 height is for the example to make the body scroll*/
height: 150vh;
}
/*For example to show the sidebar on body hover*/
body:hover #sidebar {
right: 0;
}
<div class="hideScroll">
<div id="sidebar">Content</div>
</div>
Add display: none; to the sidebar:
#sidebar { display: none; position: absolute; top: 0; right: -100px; width: 100px; height: 100%; }
Then when you move it into the main window, set display: block; at the same time.

Padding not working for my text

html:
<div id="main">
<div style="position: absolute; height: 150px; width: 400px; left: 290px;"><img src="HEAD-IMAGE.jpg" /></div>
<div style="position: absolute; height: 300px; width: 233px; top: 180px;"><img src="LEFT-IMAGE.jpg" />(below head)</div>
<div style="position: absolute; top: 200px; left: 270px;">TEXT (next to left image)</div>
</div>
css:
div#main{
position: absolute;
top: 141px; left: 50%;
height: 100%; width: 960px;
padding: 10px;
margin-left: -490px;
text-align: justify;
background-color: yellow;
}
my padding from #main works for my images but not for my text (right & bottom padding).
Why is this happening?
In your example, only the text div has a top and left property. The two divs containing the images only contain one of these properties:
The header div has left: 290px;, so it gets its y-axis position moved by the top padding.
The left div has top: 180px; so it gets its x-axis position moved by the left padding.
The text div has top: 200px; left: 270px; so its x and y-axis are not affected by the padding.
To illustrate this, for this example the text div has had its left property removed. It is now affected by the left padding of its container:
("Show code snippet" and run it)
#main {
position: absolute;
top: 141px;
left: 50%;
height: 100%;
width: 960px;
padding: 50px;
margin-left: -290px;
text-align: justify;
background-color: yellow;
}
.header {
position: absolute;
height: 150px;
width: 400px;
left: 290px;
background: #F00;
}
.left {
position: absolute;
height: 300px;
width: 233px;
top: 180px;
background: #F00;
}
.text {
position: absolute;
top: 200px;
background: #F00;
}
<div id="main">
<div class="header">
<img src="http://www.placehold.it/200" />
</div>
<div class="left">
<img src="http://www.placehold.it/200" />
</div>
<div class="text">You can't handle the truth, soldier!</div>
</div>
Is position: absolute the best way to layout my elements?
Depends... position: absolute removes elements from the normal flow of the document. That is, each element is essentially invisible to the other. This is particularly problematic if you wish to create a flexible layout, which can re-size in accordance with the users browser height / width.
Can you show me another way to layout HTML elements?
Sure! There are many ways to layout a page without resorting to position: absolute. Here is a basic example using display: flex — a newer way to layout elements. It does not enjoy 100% browser support yet, so this is purely an example of one technique :)
Read more:
about vw and vh units on the MDN
about flexbox over on CSS-Tricks - A Complete Guide to Flexbox
about flexbox browser support
Flex example
Note how the elements resize when the example is made full-screen.
* {
margin: 0;
padding: 0;
}
body {
width: 80vw;
max-width: 800px;
margin: 0 auto;
background: #424242;
}
header {
background: #e91e63;
height: 20vh;
}
.wrap {
display: flex;
}
.left {
background: #fce4ec;
flex: 1;
}
.content {
background: #fafafa;
min-height: 70vh;
flex: 2;
}
footer {
height: 10vh;
background: #c51162;
}
<header>
I am header
</header>
<div class="wrap">
<div class="left">
I am sidebar
</div>
<div class="content">
I am content
</div>
</div>
<footer>
I am footer, hear me roar! RWAR!
</footer>
Define a class .child for your <div>
<div class="child">
and define style accordingly
.child { padding: 10px; }
Use position: relative; on the child divs to make them account for the parent divs padding.
problem is you give left and top to text div that why not accept padding,simply remove left to text div then it will accept the padding...

Firefox - width: 100% not working as expected

I am building a site that works fine in both Chrome and Safari, but am having difficulties in Firefox. The applicable HTML in this issue is simple, is is just three divs inside of another div. The goal is to have one div positioned at the top of the parent div, one at the bottom, and one stretching across the remaining space:
<div class="outer">
<div class="top">
<p>some junk here</p>
</div>
<div class="middle">
<img src="<?php echo(htmlspecialchars($image_url)); ?>"/>
</div>
<div class="bottom">
<p>more junk</p>
</div>
</div>
Now, the css is as follows:
.outer {
position: relative;
display: inline-block;
text-align: center;
width: 600px;
height: 600px;
}
.middle {
background-size: 100%;
top: 62px;
bottom: 62px;
right: 0;
left: 0;
margin: 0;
padding: 0;
position: absolute;
}
.middle img {
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
max-width: 95%;
max-height: 95%;
}
.top, .bottom {
width: 100%; /* THIS IS WHAT IS NOT WORKING */
height: 60px;
margin: 0;
padding: 0;
display: table;
position: absolute;
}
.top {
top: 0;
}
.bottom {
bottom: 0;
}
The issue is that the top and bottom divs are not extending to 100%. The are taking up as little space as necessary to fit their content. I have tried setting a max width on the divs, tried changing the display types, but nothing works. The kicker is, once I resize the window even the smallest amount, the top and bottom divs shoot to 100%. Strange. I am at a loss with this one so any help is greatly appreciated. Thanks.
.outer DIV cannot be display: inline-block for this scenario. inline-block means to adapt to the child widths. You need to either specify an exact width dimension, or use block display property.
.outer {
position: relative;
display: block; /* use BLOCK here instead of inline-block; */
text-align: center;
}
The reason why the top and bottom divs' widths were not working properly was because they were set to a display type of table. Removing just that line fixed the issue.
.top, .bottom {
width: 100%;
height: 60px;
margin: 0;
padding: 0;
/* REMOVE: display: table; */
position: absolute;
}

How to have a div expand to the height of his sibling div

I've been searching for a while, but couldn't find a solution.
I have a div containing 2 columns of content.
<div id="container">
<div id="content1">
Content1
</div>
<div id="content2">
<span style="font-size: 1500px;"> Stretch height</span>
</div>
</div>
and the CSS:
#container {
height: 100%;
}
#content1 {
position: absolute;
top: 0;
left: 0;
width: 50%;
float:right;
background-color: pink;
min-height: 100%;
height: auto;
}
#content2 {
position: absolute;
top: 0;
right: 0;
width: 50%;
float:left;
background-color: red;
min-height: 100%;
height: auto;
}
Currently, #content1 has the height of 100% while #content2 was expanded to fit its content.
This way the background color of #content1 appears only at the top and when scrolling down you can see it's not colored.
How can I overcome that? (without using JS)
Instead of using min-height: 100%, you can use bottom: 0. When both the top and bottom attributes are set to 0 it will fill the whole height (if the position is set to absolute, that is).