position:absolute and fill remaining space in div - css

Please have a look at the following configuration:
http://codepen.io/anon/pen/mJYLpe?editors=110
I would like my #right div to stay at the right of the screen with a pre-defined width and I would like my #left div to fill the remaining space. Note that the #imagetag needs to stay at the bottom right of the filler div without needing a manual positioning.
Please know that this is not a duplicate - all of the provided solutions here require the "filler" div to have no position property - however I need the position:absolute property to make overflow:hidden work properly for my case (as you can see contained image is bigger than the container - it needs to be hidden)
Open to suggestions.

Add
position: absolute;
right: 0;
on the #right and then use calc(100% - 200px) on #left
http://codepen.io/anon/pen/oXRdPv?editors=110

You need to use positioning for this case. This is a case of:
+-------+-----------+
| FIXED | FLUUUUUID |
+-------+-----------+
Or
+-------+-----------+
| FIXED | FLUUUUUID |
| | FLUUUUUID |
+-------+-----------+
Fixed-Fluid Model. In my snippet, I have demonstrated two kinds of examples. In the first case, the fluid is less in size. And the next has too long content.
Snippet
.parent {position: relative; margin: 0 0 15px; border: 1px solid #999; padding: 5px; padding-left: 100px;}
.parent .fixed {position: absolute; left: 5px; width: 90px; background-color: #99f;}
.parent .fluid {background-color: #f99;}
<div class="parent">
<div class="fixed">Fixed</div>
<div class="fluid">Fluid</div>
</div>
<div class="parent">
<div class="fixed">Fixed</div>
<div class="fluid">Fluid Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque animi placeat, expedita tempora explicabo facilis nulla fuga recusandae officia, maiores porro eaque, dolore et modi in sapiente accusamus id aut.</div>
</div>

Edit from a previous answer, so comments might not be up to date.
Changes:
#left {
position: absolute;
height: 100%;
overflow: hidden;
width: calc(100% - 200px);
}
#right {
background-color: pink;
width: 200px;
height: 100%;
position: absolute;
right: 0;
z-index: 100;
}
And moved the image tag to be a container for the "left" and "right" boxes
See Fiddle:
http://codepen.io/anon/pen/NqVzNY?editors=110

I don't know what browsers you want to support but my go for this would be flexbox
Browser support of flexbox http://caniuse.com/#feat=flexbox
In flexbox you can set a min and max width to a div and next to it it will be responsive
http://codepen.io/anon/pen/NqVzpz?editors=110
set the carry to flex
#carry {
display: flex;
};
and the left to flex and only adding a width to the right one
#left {
flex: 1;
}
#right {
background-color: pink;
max-width: 200px;
min-width: 200px;
height: 100%;
flex: 1;
}
if you wane know what more awesome stuff flexbox is capable of check this https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Add float: right on your #right element and add float: left on your #left element.
Also, the width of #left and #right elements should share a total of 100%.
Set the width and height to 100% on the #image element. Then apply the size of the image on background-size: 130%.
http://codepen.io/anon/pen/NqVMOv?editors=110

Related

Remove unwanted space with position sticky

I'm trying to use position: sticky on a rotated element but I get extra space at the top. Also where the sticky element has to stops (at the end of parent) it goes outside.
Notice I need to have control to choose how many pixels put between sticky element and the left window side.
Check the 2 screenshot to understand the 2 problems, and what I want to achieve.
Problem 1: extra space at top
Problem 2: sticky element goes outside at the end of section
I'm using this code:
HTML
<section class="section">
<h1 class="section__title">STICKY</h1>
<div class="container">
<div class="row">
<div class="col [ col-lg-8 offset-lg-2 ]">
<div class="h4">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam quos illum aperiam officia provident, mollitia at, tempore atque, blanditiis sit optio esse harum officiis voluptas iusto sequi. Magni, reiciendis quidem.
</div>
</div>
</div>
</div>
</section>
CSS
.section__title {
display: inline-block;
transform: rotate(-90deg) translatex(-100%);
transform-origin: 0% 0%;
margin-bottom: 0;
position: sticky;
top: 0;
left: 50px;
}
Here is a Codepen with the entire code: https://codepen.io/anon/pen/KLqJGG
How can I solve this?
Thanks
Problem 1: extra space at top
The stickily positioned element stays in the DOM flow - just like a relative positioned element does. So, hence the space there, which is occupied by the h1.section__title element.
Problem 2: sticky element goes outside at the end of section
It is because, the original height of the h1 element is still considered there, even after rotation.
So, you need to determine the exact width of the sticky header (which then becomes the height of this element after rotation) first and then set this width value for the rotated element's height, as follows:
$section-sticky-header-height: 145px;
.section__title {
display: inline-block;
margin-bottom: 0;
transform-origin: 0% 0%;
position: sticky;
top: 0;
left: 50px;
/* solves problem 1 */
float: left;
/* solves problem 2 */
transform: rotate(-90deg) translatex(-$section-sticky-header-height);
height: $section-sticky-header-height;
}
Codepen: https://codepen.io/anon/pen/arybWZ
Edit:
The problem is that I cannot determine the exact width of the sticky header because the h1 text is variable (the client will insert that text via a CMS). Is there a way to handle this? If possible without Javascript
Got it. You can try this instead, if the height is variable:
<h1 class="h1 mb-0 section__title">
<div class="rotation-outer">
<div class="rotation-inner">
<div class="rotation">
STICKY
</div>
</div>
</div>
</h1>
.section__title {
border: 1px solid; // for demo purpose
position: sticky;
top: 0;
float: left;
.rotation-outer {
display: table;
position: relative;
left: 50px;
.rotation-inner {
padding: 50% 0;
height: 0;
.rotation {
transform-origin: 0 0;
transform: rotate(-90deg) translate(-100%);
margin-top: -50%;
white-space: nowrap;
}
}
}
}
See in action: https://codepen.io/anon/pen/BedREm
There's a very good explanation here for how this works: Rotated elements in CSS that affect their parent's height correctly
Edit 2:
At that link I also discovered the writing-mode: vertical-rl; property (in this answer stackoverflow.com/a/50406895/1252920). Do you think could be a better solution? I applied it in this Codepen: codepen.io/anon/pen/JqyJWK?editors=1100 What do you think?
Yes, another sweet alternative, you can use. :)
Here I changed/optimized it a little bit: https://codepen.io/anon/pen/qGXPPe?editors=1100
However, please note that vertical-lr or vertical-rl is not widely supported. Apparently only on desktop version of Chrome/Firefox/Opera. See here.
So, it's up to you, which one to use. Personally, I wouldn't use writing-mode due to lack of browser support.
Seems to me that your Codepen does not have that padding you mention, therefore I wonder if you removed the default padding and margin of html and body elements.
html, body {
margin: 0;
padding: 0;
}
Let me know if it works or in which browser the padding you mention appears.

My position: sticky element isn't sticky when using flexbox

I was stuck on this for a little bit and thought I'd share this position: sticky + flexbox gotcha:
My sticky div was working fine until I switched my view to a flex box container, and suddenly the div wasn't sticky when it was wrapped in a flexbox parent.
.flexbox-wrapper {
display: flex;
height: 600px;
}
.regular {
background-color: blue;
}
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
background-color: red;
}
<div class="flexbox-wrapper">
<div class="regular">
This is the regular box
</div>
<div class="sticky">
This is the sticky box
</div>
</div>
JSFiddle showing the problem
Since flex box elements default to stretch, all the elements are the same height, which can't be scrolled against.
Adding align-self: flex-start to the sticky element set the height to auto, which allowed scrolling, and fixed it.
Currently this is supported in all major browsers, but Safari is still behind a -webkit- prefix, and other browsers except for Firefox have some issues with position: sticky tables.
.flexbox-wrapper {
display: flex;
overflow: auto;
height: 200px; /* Not necessary -- for example only */
}
.regular {
background-color: blue; /* Not necessary -- for example only */
height: 600px; /* Not necessary -- for example only */
}
.sticky {
position: -webkit-sticky; /* for Safari */
position: sticky;
top: 0;
align-self: flex-start; /* <-- this is the fix */
background-color: red; /* Not necessary -- for example only */
}
<div class="flexbox-wrapper">
<div class="regular">
This is the regular box
</div>
<div class="sticky">
This is the sticky box
</div>
</div>
JSFiddle showing the solution
In my case, one of the parent containers had overflow-x: hidden; applied to it, which will break position: sticky functionality. You'll need to remove that rule.
No parent element should have the above CSS rule applied to it. This condition applies to all parents up to (but not including) the 'body' element.
If you are using flex in the parent element use align-self: flex-start for the element which you want to make sticky.
position: sticky;
align-self: flex-start;
top: 0;
overflow-y: auto;
You can also try adding a child div to the flex item with the contents inside and assign position: sticky; top: 0; to that.
That worked for me for a two column layout where the contents of the first column needed to be sticky and the second column appear scrollable.
For my situation, the align-self: flex-start (or justify-self: flex-start) solution does not work. I need to keep overflow-x: hidden as well since some containers swipe horizontally.
My solution required nested display: flex with overflow-y: auto to get the desired behaviors:
header can adjust height dynamically, which prevents playing with position: absolute or position: fixed
content scrolls vertically, constrained horizontally to the view width
sticky element can be anywhere vertically, sticking to the bottom of the header
other elements can slide horizontally
looks like the SO snippet tool can't render width on child elements to properly to demonstrate the horizontal slide, or maybe there's some other setting on my actual layout that makes it work...
note that a wrapper element that does nothing else is required to allow overflow-x: auto to work correctly in elements under a parent with overflow-x: hidden
body {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
body>header {
background-color: red;
color: white;
padding: 1em;
}
.content {
overflow-x: hidden;
overflow-y: auto;
display: flex;
flex-direction: column;
}
article {
position: relative;
display: flex;
flex-direction: column;
overflow-y: auto;
}
.horizontal_slide {
display: flex;
overflow-x: auto;
background-color: lightblue;
padding: .5em;
}
.horizontal_slide>* {
width: 1000px;
}
.toolbar {
position: sticky;
top: 0;
z-index: 10;
background-color: lightgray;
padding: .5em;
display: flex;
}
<header>Fancy header with height adjusting to variable content</header>
<div class="content">
<article class="card">
<h1>One of several cards that flips visibility</h1>
<div class="overflow_x_wrapper">
<div class="horizontal_slide">
<div>Reason why `overflow-x: hidden` on the parent is required
</div>
<div>Reason why `overflow-x: hidden` on the parent is required
</div>
<div>Reason why `overflow-x: hidden` on the parent is required
</div>
</div>
<div class="toolbar">Sticky toolbar part-way down the content</div>
<p>Rest of vertically scrollable container with variable number of child containers and other elements</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</article>
</div>
Make sure flex-wrapper has assigned a height, and set the overflow value to auto.
Then add "align-self: flex-start;" to the sticky element.
.flexbox-wrapper {
display: flex;
height: 600px; //<- nessary,and don't assign with %
overflow:auto; //<-fix
}
.regular {
background-color: blue;
}
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
background-color: red;
align-self: flex-start; // <-fix
}
<div class="flexbox-wrapper">
<div class="regular">
This is the regular box
</div>
<div class="sticky">
This is the sticky box
</div>
</div>
I made a makeshift flexbox table and had this problem. To solve it, I simply put the sticky header row outside of the flexbox, just before it.
Global solution/rule from my experience:
Do not put structures with sticky elements directly inside { display : flex } containers.
Instead (for flex layouts) put your tables/divs with sticky elements inside a flex sub-container (e.g. with { flex: 1 1 auto }, but without { display : flex } ), then everything should work as intended.

Anchor to cover entire DIV - but with challenges and no javascript

I'm working on a little design challenge, and it's getting the better of me right now.
Essentially, it's a material design card, which means when I click it it takes me somewhere else.
The easy route would be (and as it is now) is to surround the content with an anchor. However, in this case I ONLY want the anchor text to be "My keyword".
Here's the simple html output:
<a class="post-card md-card">
<div class="md-card-title aspect-16x9">
<div class="title-large"></div>
</div>
<div class="md-card-content">
<div class="sup-text"></div>
</div>
</a>
So, the 2 things I want to do are:
Only have the keyword inside the anchor
Be able click the whole thing (the link covers the entire outer div)
Here's the stuff that make it more difficult:
The blue box on top has an aspect ratio set, which means its not a
constant height
The text inside the blue box is centered using Flexbox
The white box isn't a fixed height either
Here's how the aspect ratio is calculated:
.AspectRatio(#widthRatio:16; #heightRatio:9; #useableWidth:100%) {
&:extend(.clearfix all);
overflow:hidden;
max-width:#useableWidth;
&::before {
content:"";
float:left;
padding-top:percentage(#heightRatio / #widthRatio);
}
}
So I need to keep the keyword text where it is but make the whole thing clickable.
I've been playing around with the idea an absolutely positioned anchor on top, which I can do but I can't get it to stretch to the bottom without moving the text.
Any CSS gurus got some ideas?
This should give you a good starting place...
.post-card {
background-color: #63d9ff;
box-sizing: border-box;
position: relative;
width: 200px;
}
.post-card * {
box-sizing: inherit;
}
.post-card .md-card-title {
bottom: 0;
left: 0;
position: absolute;
right: 0;
text-align: center;
top: 0;
}
.post-card .md-card-title .content-prop,
.post-card .md-card-title .content {
display: inline-block;
vertical-align: middle;
}
.post-card .md-card-title .content-prop {
padding-top: 56%;
width: 0;
}
.post-card .md-card-title .content {
padding: 1rem;
width: 100%;
}
.post-card .space-prop {
display: block;
padding-top: 56%;
}
.md-card-content {
background-color: grey;
padding: 1rem;
}
<div class="post-card md-card">
<a class="md-card-title aspect-16x9" href="#">
<span class="content-prop"></span><!--
--><span class="content">My Keyword</span>
</a>
<span class="space-prop"></span>
<div class="md-card-content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis mauris ut eros consectetur efficitur vitae at leo.
</div>
</div>
JSFiddle
So the explaination...
The whole post card is positioned relative, the anchor is then positioned absolute within it. The anchor is given a top, left, bottom, and right value of 0 which makes it cover it's parent container.
The content-prop and space-prop are given no height but have a top padding of 56%. This means that their top-padding value is 56% of their width which works out at a 16:9 ratio. The space banner is used here to add a empty gap at the top of the post card to make room for the anchor.
Both the content-prop and the content elements are set to display inline-block and vertical aligned to middle. Because the prop is taller than the content, the content floats in the centre. The HTML comment between these two elements eliminates white space so that the content div can be set to 100% width even when the prop is on the same horizontal row.

Getting a DIV to affect its previous element height?

I currently have something like the following:
Basically it's just three divs contained in one container_div which has its width and height specified. CSS code for the container and the top div looks like:
.container_div{
width: 800px;
height: 500px;
}
.top_div{
width:100%;
height:100px;
}
What I am now trying to do is come up with CSS code for the center_div and bottom_div elements in a way that:
Bottom div has no overflow
Bottom div can grow/shrink without causing its parent element to change its size ( something like bottom:0 absolute positioning )
Whenever bottom div grows, center div shrinks and vice-versa.
This is what should happen when bottom div grows:
I am looking for a pure CSS solution. Firefox support is enough.
This can be easily achieved by a css table layout. In your case, involving table rows, that will (by default) automatically fill the space of it's display: table container.
In your case, just set:
The top div to be a fixed height
The middle div to be 100% height. This will squize the bottom div to its own content height.
The bottom div to be zero height.
body { margin: 0; }
#container {
display: table;
position: absolute;
height: 100%;
width: 100%;
color: white;
}
#container > div:nth-child(1) {
display: table-row;
height: 50px;
background: red;
}
#container > div:nth-child(2) {
display: table-row;
height: 100%;
background: green;
}
#container > div:nth-child(3) {
display: table-row;
height: 0;
background: blue;
}
<div id="container">
<div>div 1 (fixed 100px)</div>
<div>div 2 (expand to fill remaining space) </div>
<div>
div 3 (fit its own content)
loren ipsum dolor sit amet... loren ipsum dolor sit amet... loren ipsum dolor sit amet... loren ipsum dolor sit amet...
</div>
</div>

How to set minimum height for a div?

I have a page in which expanding content flows out of the holding div, the relevant CSS is below as well. Simply removing the height:510px line will allow the div to expand as needed. However, new users who have no content will not have any height and the page will look unblanced. How do I set a minimum height?
.Bb1
{
width:680px;
height:510px;
background-color:#ffffff;
float:left;
border-right:3px solid #edefed;
border-radius: 10px;
}
CSS allows a "min-height" property. This can be used to set the minimum height of the div you're talking about.
#div-id { min-height: 100px; }
Incase you want to set a minimum/maximum height and minimum/maximum width to a div, CSS allows the specific feature.
To set the minimum width of the div or any other class/id/element, use;
min-width: 150px;
To set the minimum height of the div or any other class/id/element, use;
min-height: 300px;
Similarly for setting maximum width and height of a div or any other class/id/element, use;
max-width: 600px;
max-height: 600px;
Important:
For your div to expand freely in height and width after data is available for users; you will have to set the width/height to auto immediately after you have set the min-width or min-height.
min-width: 300px;
width: auto;
min-height: 100px;
height: auto;
min-height:510px;
css has a min-height attribute
Here is d way through which you can set min height of a div
<div id="bb1" style="min-height:200px;>
.....
</div>
or apply
#bb1{
min-height:200px;
}
if you have used class
like
<div class="bb1">
in div then use
.bb1{
min-height:200px;
}
.comments { min-height:650px;height:auto; }
<div class="comments">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi.</div>
Do this:
.minHeight {
min-height: 100px;
height: auto;
border: 1px solid red;
/* IMPORTANT -- always set 'height: auto;' immediately after the min-height attribute */
}
<div class="minHeight">
this div has a min-height attribute
</div>

Resources