Shrink second child to always fit a parent [duplicate] - html

This question already has answers here:
Make div (height) occupy parent remaining height
(9 answers)
Closed 6 years ago.
Assume we have following markup
<div id="parent">
<div id="header">
</div>
<div id="content">
</div>
</div>
header div is variable-sized, so we don't know what exact offset gets the second one. We want to accomplish two following objectives:
The second div should never overflow its parent. If overflow occurs, the scrollbar appears.
It should be done in pure-CSS manner, for IE9+ if possible.
I have been thinking that this task is pretty easy, really, I just want my div to be non-overflowing, that's all, but I found that it's not possible even with flexbox.
When i say overflow I mean vertical overflow, because they all are of the same width.

It's very simple to do this with Flexbox, just set flex-direction: column on parent and overflow-y: auto on content.
#parent {
height: 300px;
border: 1px solid black;
display: flex;
flex-direction: column;
width: 200px;
}
#header {
background: lightblue;
}
#content {
flex: 1;
overflow-y: auto;
background: lightgreen;
}
<div id="parent">
<div id="header"><br><br><br></div>
<div id="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque culpa consequuntur repellat ex, ut cumque fugit minus, itaque omnis, cum quidem in debitis. Consequuntur perspiciatis corporis nostrum similique eveniet voluptates cumque molestias rerum, distinctio accusamus? Quas explicabo, ea praesentium ad velit rem accusamus officia quibusdam ut accusantium possimus dignissimos dolores ipsum quia placeat tenetur omnis veritatis molestias voluptatibus consequuntur odit consectetur vero, voluptas corporis laudantium! Rem laborum excepturi quia temporibus, veniam blanditiis tempore eaque nostrum suscipit, quam laboriosam ratione provident obcaecati iste magni molestiae explicabo quibusdam expedita veritatis officia, debitis officiis sed. Neque veniam eius saepe. Quo doloremque, repellat mollitia!</div>
</div>

Related

Bootstrap 4 Equal height columns not working

I have two columns next to each other but the content inside them is different but the columns are different I was lead to believe this was default it bootstrap 4 or it was in one of the alphas at one point.
Currently I have tried.
.equal {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
}
.equal > [class*='col-'] {
display: flex;
flex-direction: column;
}
and added that to my css.
and my html looks like.
<div class="row equal">
<div class="col-md-8"></div>
<div class="col-md-4"></div>
</div>
but it doesnt seem to affect it in any way.
edit seems like its not the column but the content inside said column that isnt 100% of its parent.
but if i do height: 100% it makes it 100vh for some reason and not 100% of the column height.
https://codepen.io/anon/pen/KBEegO
in this i have made divs inside it i need them divs 100% of the parent column
that is the current view of the row and column the border is on the inner div of the column.
I need it so if there is more content in the left column the right column is the same height as it and if the right column is as it is on the image the left column gets its height.
For div inside that, you can use position absolute with CSS transform and translate property.
.innerdiv {
top: 50%;
position:absolute;
left: 50%;
transform: translate(-50%, -50%);
}
This will make your inner div verticle and horizontal center.
I have updated the codepen please check https://codepen.io/anon/pen/KBEejz?editors=1100
.equal div div {
border: 1px solid green;
height: 100%;
display: flex;
flex: 1;
}
.equal>[class*='col-'] {
display: flex;
flex-direction: column;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" />
<div class="row equal">
<div class="col-md-8">
<div>
<p style="background-color:pink;">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis quibusdam nobis praesentium hic vero quo quia error sed commodi cumque, illo porro eaque ad ipsa. Error maxime consequatur officiis alias fuga mollitia quas, magnam delectus necessitatibus
illum, quos aperiam doloremque eaque explicabo architecto dolor? Placeat quos nemo natus velit aliquam?</p>
</div>
</div>
<div class="col-md-4">
<div>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci blanditiis commodi sapiente totam sequi consectetur provident. Voluptatibus corrupti odit reiciendis, aliquid temporibus dolores tempore optio delectus nesciunt ipsam veritatis quisquam
facilis voluptatem voluptates inventore aliquam, praesentium, assumenda ducimus ratione quasi nisi sunt consequatur corporis soluta. Deleniti quam harum soluta alias minima voluptas iusto voluptates, iure necessitatibus assumenda ex. Minus numquam
beatae vero aperiam cupiditate assumenda ipsam hic ad corporis placeat facere consequuntur consectetur sint, aspernatur mollitia optio odit eos officia nostrum? Totam asperiores quia sequi autem. Ex nostrum fuga, et, illo iusto inventore officiis
facere minima hic sint obcaecati consectetur.</p>
</div>
</div>
</div>

Want to make contentless div responsive to height of adjacent div

Making a Reactjs app. Referring to the code, I would like to make the height of the colorTab div, equal and responsive to that of the content div. The height of content must be dynamic given that I would like it to be defined by the amount of text in tile + description, which is variable, and the width of the window.
Currently, when I omit min-height from colorTab's CSS and simply have height: 100%; defining colorTab's height, colorTab disappears. Adding the min-height gives it that height but then it becomes unresponsive to the height of content which is the goal. How do I solve this issue?
JSX:
<div className="wrapper">
<div className="colorTab" style={color}>
</div>
<div className="content">
<tr>
<td className="title">
<a href={link}>{title}</a>
</td>
</tr>
<tr>
<td className="description">
{description}
</td>
</tr>
</div>
</div>
CSS:
.wrapper {
min-height: 48px;
overflow: hidden;
}
.colorTab {
float: left;
position: relative;
width: 5px;
min-height: 48px;
height: 100%;
border-radius: 5px;
margin-left: 15px;
}
.title {
padding-top: 0 !important;
padding-bottom: 0 !important;
}
.description {
padding-top: 0 !important;
padding-bottom: 0 !important;
}
Flexbox will offer the functionality you need.
Put display: flex on your container class. And flex: 1 on your content div. No matter how much content you place in the content div the colorTab div will match its height.
Example in pure HTML/CSS (no React):
.wrapper {
overflow: hidden;
display: flex;
}
.colorTab {
position: relative;
width: 5px;
border-radius: 5px;
background: red;
}
.content {
flex: 1;
}
<div class="wrapper">
<div class="colorTab">
</div>
<div class="content">
<div class="title">
<a>Your Title</a>
</div>
<div class="description">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem nam perspiciatis aperiam mollitia obcaecati molestiae, consequuntur saepe repellendus cumque aliquid. Ullam reiciendis praesentium repellendus ipsam, qui illum. At, aliquid quidem. Reprehenderit eligendi voluptatem maiores deleniti id nulla, pariatur ipsa ducimus accusantium! Unde ea nostrum eligendi suscipit impedit, laborum adipisci accusamus ducimus temporibus eius inventore optio officia reiciendis porro eos assumenda numquam velit obcaecati. Perferendis, ipsum! Facilis fuga dolorum nobis nihil illo nam, voluptate suscipit excepturi sunt non. Modi perferendis ex illum eaque pariatur laudantium saepe accusantium vel, blanditiis, aperiam odit! Suscipit ullam, necessitatibus est distinctio obcaecati, odio ipsa blanditiis consequatur.
</div>
</div>
Now, I would absolutely recommend the flexbox mentioned in the other answer but...
but for some other outdated browsers which do not support it (cough
cough looking at you, grandpa, using that same old version of IE)... it
might be good idea to provide extra version support.
So, for answer's completion sake:
The other option is to utilize the tables.
.wrapper {
display: table;
}
.description {
display: table-cell;
}
.colorTab {
display: table-cell;
width: 5px;
border-radius: 5px;
background: red;
}
// not necessary, but for esthetic reasons
.content {
position: relative;
left: 10px;
}
<div class="wrapper">
<div class="colorTab">
</div>
<div class="content">
<div class="title">
<a>Your Title</a>
</div>
<div class="description">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem nam perspiciatis aperiam mollitia obcaecati molestiae, consequuntur saepe repellendus cumque aliquid. Ullam reiciendis praesentium repellendus ipsam, qui illum. At, aliquid quidem. Reprehenderit eligendi voluptatem maiores deleniti id nulla, pariatur ipsa ducimus accusantium! Unde ea nostrum eligendi suscipit impedit, laborum adipisci accusamus ducimus temporibus eius inventore optio officia reiciendis porro eos assumenda numquam velit obcaecati. Perferendis, ipsum! Facilis fuga dolorum nobis nihil illo nam, voluptate suscipit excepturi sunt non. Modi perferendis ex illum eaque pariatur laudantium saepe accusantium vel, blanditiis, aperiam odit! Suscipit ullam, necessitatibus est distinctio obcaecati, odio ipsa blanditiis consequatur.
</div>
</div>

Scrolling and Non-Scrolling versions of a single page app

I am writing a single-page app in React. All pages in this app have a header and some content. The header is fixed and the content is scrollable. To do this, I am setting the height to 100% starting from the <html> tag down to the <Page> component. The <Page> component itself is a flexbox with a fixed-height header and the content area is set up with flex: 1 and overflow: auto to allow for scrolling. Here's a simplified structure of the app using only HTML and CSS:
* {
box-sizing: border-box;
}
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
font-family: Arial, Verdana, Helvetica, sans-serif;
}
.scrolling-page {
height: 100%;
display: flex;
flex-direction: column;
}
.header {
background-color: #eeeeee;
height: 50px;
font-size: 24px;
line-height: 34px;
padding: 8px;
}
.content {
flex: 1;
overflow: auto;
padding: 8px;
}
<div class="scrolling-page">
<header class="header">
Header
</header>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur
mollitia maxime facere quae cumque perferendis cum atque quia
repellendus rerum eaque quod quibusdam incidunt blanditiis possimus
temporibus reiciendis deserunt sequi eveniet necessitatibus maiores quas
assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque
dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa
cupiditate cum architecto! Facilis deleniti unde suscipit minima
obcaecati vero ea soluta odio cupiditate placeat vitae nesciunt quis
alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui
nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur
vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui
nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur
vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
</div>
</div>
I would like to now create a version of the same page that does not scroll and occupies only the height needed by the page, not 100% of the page. This requires the removal of 100% heights starting from the <html> tag down. However since this is a single-page app, I can't have it both ways. There is only one instance of <html>, <body> etc. Do you see any way around this?
I'm not sure if I'm following what you intend to do.. If it's just displaying the "fluid" height version when content is not enough to overflow the viewport and a fixed 100% of the viewport version with scroll bars when content is larger, then max-height 100vh and overflow auto would do the trick

adjust margin in just 1 div [duplicate]

This question already has answers here:
Margin on child element moves parent element
(18 answers)
Closed 6 years ago.
Well, now I've this problem, I want to adjust just the margin on the "c2" but when I set it to X, it changes the "c1" div margin :S
Here's the code I'm using:
<header>
<div class="jumbotron">
<center><h1>Bienvenidos a JVasconcelos.me</h1></center>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-md-12 col-centered">
<div class="c1">
<div class="c2">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis inventore illum quaerat laboriosam eos, vel sed suscipit cumque laborum est animi aliquid tempora iusto beatae quam quisquam porro dolore! Ullam tenetur doloribus ducimus, totam voluptatum, deleniti vero voluptatem eius architecto velit neque voluptas aliquam quidem sed eveniet! Nobis ex eos iste dolorum tempora doloremque non deleniti, aperiam quibusdam corrupti officia consequatur, impedit. Exercitationem debitis iste voluptatum, illo nulla iure culpa ex fugit, aliquid dolorem excepturi, impedit voluptates quae quidem error earum natus, provident eum vitae. Tempore ducimus laborum voluptates, qui aspernatur odit dolorum modi quas cupiditate unde quam earum amet!
</p>
</div>
</div>
</div>
</div>
</div>
CSS
div.c1 { height: 100vh; background: #417ba1; margin-top: -30px; padding: 0px 30px; }
div.c2 { height: 90%; background: #fff; margin-top: 0px; padding: 60px 30px; }
.jumbotron { background: url("../img/header_bg.png") no-repeat; height: 100%; }
Ah. You're looking for
.c1 {
overflow: auto; // or hidden or overlay
}
This behavior is due to the collapsing margins part of the box model spec. Putting overflow: auto|hidden|overlay on the parent will establish a new block formatting context and stop the margins from collapsing.
This is because the border of the c1 and c2 are collapsing. You have to hide the overflow (with overflow: hidden or any overflow different that default, which is visible) of the container to avoid that the c1 also get the margin of c2.
div.c1 { height: 100vh; background: #417ba1; overflow: hidden; margin-top: -30px; padding: 0px 30px; }
div.c2 { height: 90%; background: #fff; margin-top: 20px; padding: 60px 30px; }
.jumbotron { background: url("../img/header_bg.png") no-repeat; height: 100%; }
<header>
<div class="jumbotron">
<center><h1>Bienvenidos a JVasconcelos.me</h1></center>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-md-12 col-centered">
<div class="c1">
<div class="c2">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis inventore illum quaerat laboriosam eos, vel sed suscipit cumque laborum est animi aliquid tempora iusto beatae quam quisquam porro dolore! Ullam tenetur doloribus ducimus, totam voluptatum, deleniti vero voluptatem eius architecto velit neque voluptas aliquam quidem sed eveniet! Nobis ex eos iste dolorum tempora doloremque non deleniti, aperiam quibusdam corrupti officia consequatur, impedit. Exercitationem debitis iste voluptatum, illo nulla iure culpa ex fugit, aliquid dolorem excepturi, impedit voluptates quae quidem error earum natus, provident eum vitae. Tempore ducimus laborum voluptates, qui aspernatur odit dolorum modi quas cupiditate unde quam earum amet!
</p>
</div>
</div>
</div>
</div>
</div>
I assume you mean when you change the margin-top on the div.c2 your div.c1 also shifts down. This is due to the default definition of a div+div construct.
To achieve what you want you will need to create the following extra css definition on your div.c1:
display: inline-block;
Have a look at this codepen: http://codepen.io/anon/pen/beNjbW
Difference of or greater margin of c1 or c2 will be rendered due to collapsible margins.
In this case your total distance will be 0 (they cancel each other):
div.c1 {
margin-top: -30px;
}
div.c2 {
margin-top: 30px;
}
In this case your distance will be 10px from the top:
div.c1 {
margin-top: -30px;
}
div.c2 {
margin-top: 40px;
}
One of top margins can be omitted in this case. You can control the distance to header by adjusting margin-top of either one
codePen example

Can I make height 100% work like width 100%?

Fiddle
I have two <div>s floated left inside a <section>. I am looking for the simplest way to get whichever <div> is shorter, in this case <div id="one"> to stretch its height to match the other (<div id="two">). In the fiddle, you will see that <div id="one"> has a gray background, which I want to continue down to the bottom of the <section>. Any workarounds or whatever to get this done is great. But I don't want to use javascript to do this. Thanks.
HTML:
<section>
<div id="one"><p>Lorem</p></div>
<div id="two">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore</p>
</div>
<div class="clear"></div>
</section>
CSS:
section {
width: 80%;
margin: 0 auto;
border: 1px solid #ccc;
border-radius: 5px;
}
#one, #two {
float: left;
width: 50%;
}
#one {
background: rgb(230,230,230);
}
.clear {
clear: both;
}
load jquery and add the following javascript
$(document).ready( function() {
var x = $("#one").css('height');
var y = $("#two").css('height');
if(x > y) {
$("#two").css('height', x);
} else {
$("#one").css('height', y);
}
});
but incase you are allergic to JavaScript ... and would accept CSS3 you could try
section {
display: flex;
width: 80%;
margin: 0 auto;
border: 1px solid #ccc;
border-radius: 5px;
}
#one, #two {
display: flex;
width: 50%;
display: table-cell;
}
Here is a pure HTML and CSS solution for you. It uses a div with a class of "inner" to wrap your divs you want to match up. Some CSS is also added to make it behave like you want it to.
jsFiddle solution - pure HTML and CSS
HTML code:
<section>
<div class="inner">
<div id="one">
<p>Lorem</p>
</div>
<div id="two">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium
doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore
veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim
ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia
consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque
porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,
adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore</p>
</div>
</div>
<div class="clear"></div>
You can hack around the padding on the p in the #one div, but this will throw your text off in this div. I'm not sure how relevant it is with aligning to the other text, but it may help you out.
#one p {
padding: 9em;
}