I have two divs on my page; side panel and main panel.
In the side panel I will always have a bunch of blocks on content, the number with change all the time. And the main panel will have some block of content. Generally the side panel will have bigger height than main panel.
I am looking for the main panel height to equal the content in that panel and I am looking for a way to have the height of the side panel equal to the size of the main panel. While the the overflow of the side panel scroll. Im not looking to set a static height for either, because the amount of content changes.
Currently I'm using grid but all for changing that if there is a way to do this.
Here is a Codepen mockup of what I currently have:
https://codepen.io/aculbreth/full/VMjRyx
.box {
display: grid;
grid-template-columns: 1fr 4fr;
grid-gap: 20px;
grid-auto-columns: max-content;
background: #efefea;
margin: 50px auto;
padding: 20px;
box-sizing: border-box;
.side_panel {
background:#fff;
padding:20px;
box-sizing: border-box;
.side_panel_box {
text-align:center;
border: 1px solid #000;
margin-bottom:20px;
}
}
.main_panel {
background:#fff;
padding:20px;
box-sizing: border-box;
display:block;
grid-auto-columns: max-content;
}
}
Here is a rough mockup of what i'm looking for:
As you've tagged your post with jQuery, it can be done via jquery pretty painlessly. As your CSS was questioned, I remade it to be simple css, and the jQuery simply sets the side pane height to the main content height. Hope it helps!
*** Please note, to see either of these work well, view them full-page.
$(document).ready(function() {
var maxHeight = $(".main_panel p").height();
$(".side_panel").height(maxHeight);
})
.box {
display: grid;
grid-template-columns: 1fr 4fr;
grid-gap: 20px;
grid-auto-columns: max-content;
background: #efefea;
margin: 50px auto;
padding: 20px;
box-sizing: border-box;
}
.side_panel {
background: #fff;
padding: 20px;
box-sizing: border-box;
overflow: auto;
}
.side_panel_box {
text-align: center;
border: 1px solid #000;
margin-bottom: 20px;
}
.main_panel {
background: #fff;
padding: 20px;
box-sizing: border-box;
display: block;
grid-auto-columns: max-content;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
<div class="side_panel">
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
</div>
<div class="main_panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi explicabo autem cupiditate, facere quas recusandae itaque voluptates sint accusantium amet illo veniam totam placeat odio magni, quaerat eligendi. Distinctio et cupiditate nemo fugit,
praesentium, ea obcaecati dolores non amet autem sequi laboriosam labore vero fugiat dolorem qui voluptatibus omnis eos architecto, recusandae corporis rerum quod. Ea minus et odio voluptate quaerat facere incidunt, impedit repellat eos nihil quis
modi vel molestiae, eum vitae nam nemo quasi sit aperiam unde ex fugiat. Magnam dolores, enim consectetur veniam illum error eaque ea necessitatibus nam ipsam maxime dolore temporibus animi odio eum molestiae iure. Ut quos cupiditate ad perferendis
dolorum nemo deserunt exercitationem magni ipsa iste ea fugit expedita sapiente numquam reprehenderit dolore, quo minus excepturi asperiores sit voluptatibus accusamus necessitatibus, ipsam. Delectus iste cupiditate asperiores vero repellendus atque
aliquid temporibus perspiciatis reiciendis quibusdam similique, nesciunt accusantium tempora voluptatem optio qui odit ullam amet eveniet illum incidunt quis quia repudiandae animi. Recusandae, consequuntur laboriosam praesentium fugit possimus
nemo ut porro reiciendis quo aperiam et aliquam fuga consectetur fugiat veritatis necessitatibus officiis, officia repellendus, rem. Voluptatem itaque, dolores autem at, culpa cum quam ratione laborum quo totam, nemo sed dolor, hic nisi nobis illo.</p>
</div>
</div>
Doing the same thing in pure CSS, here's the following. Note that this is applied pretty much directly from here, and applied to this particular use case.
.box {
display: flex;
}
.flex-item {
flex: 0 0 50%;
position: relative;
}
.flex-item-inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: auto;
}
.side_panel {
background: #fff;
padding: 20px;
box-sizing: border-box;
overflow: auto;
}
.side_panel_box {
text-align: center;
border: 1px solid #000;
margin-bottom: 20px;
}
.main_panel {
background: #fff;
padding: 20px;
box-sizing: border-box;
display: block;
grid-auto-columns: max-content;
}
<div class="box">
<div class="flex-item side_panel">
<div class="flex-item-inner">
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
<div class="side_panel_box">
<h1>Title</h1>
<p>Some text here</p>
</div>
</div>
</div>
<div class="flex-item main_panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi explicabo autem cupiditate, facere quas recusandae itaque voluptates sint accusantium amet illo veniam totam placeat odio magni, quaerat eligendi. Distinctio et cupiditate nemo fugit,
praesentium, ea obcaecati dolores non amet autem sequi laboriosam labore vero fugiat dolorem qui voluptatibus omnis eos architecto, recusandae corporis rerum quod. Ea minus et odio voluptate quaerat facere incidunt, impedit repellat eos nihil quis
modi vel molestiae, eum vitae nam nemo quasi sit aperiam unde ex fugiat. Magnam dolores, enim consectetur veniam illum error eaque ea necessitatibus nam ipsam maxime dolore temporibus animi odio eum molestiae iure. Ut quos cupiditate ad perferendis
dolorum nemo deserunt exercitationem magni ipsa iste ea fugit expedita sapiente numquam reprehenderit dolore, quo minus excepturi asperiores sit voluptatibus accusamus necessitatibus, ipsam. Delectus iste cupiditate asperiores vero repellendus atque
aliquid temporibus perspiciatis reiciendis quibusdam similique, nesciunt accusantium tempora voluptatem optio qui odit ullam amet eveniet illum incidunt quis quia repudiandae animi. Recusandae, consequuntur laboriosam praesentium fugit possimus
nemo ut porro reiciendis quo aperiam et aliquam fuga consectetur fugiat veritatis necessitatibus officiis, officia repellendus, rem. Voluptatem itaque, dolores autem at, culpa cum quam ratione laborum quo totam, nemo sed dolor, hic nisi nobis illo.</p>
</div>
</div>
Related
.box1, .box2 {
display: flex;
}
img {
height: 200px;
width: 100%;
}
.box2 .left, .box1 .right, .box2 .right, .box1 .left {
width: 100%
}
.box2 .left {
padding: 25px 10px 25px 75px
}
.box1 .right {
padding: 25px 75px 25px 10px
}
<div class="container">
<div class="box1">
<div class="left"><img src="https://cdn.wallpaperhub.app/cloudcache/1/b/5/8/e/f/1b58ef6e3d36a42e01992accf5c52d6eea244353.jpg" alt=""></div>
<div class="right">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam, non. Molestias voluptas velit quae itaque natus nam odio obcaecati minima recusandae enim eos mollitia est animi incidunt, beatae iure perspiciatis.
Reprehenderit sapiente numquam totam possimus necessitatibus. Enim nostrum rem quasi voluptates. Consectetur, est aliquid. Iste voluptas laborum provident harum! Ipsam pariatur velit iste quam, dolorum inventore rem molestias modi nemo?
</div>
</div>
<div class="box2">
<div class="left">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam, non. Molestias voluptas velit quae itaque natus nam odio obcaecati minima recusandae enim eos mollitia est animi incidunt, beatae iure perspiciatis.
Reprehenderit sapiente numquam totam possimus necessitatibus. Enim nostrum rem quasi voluptates. Consectetur, est aliquid. Iste voluptas laborum provident harum! Ipsam pariatur velit iste quam, dolorum inventore rem molestias modi nemo?
</div>
<div class="right"><img src="https://cdn.wallpaperhub.app/cloudcache/1/b/5/8/e/f/1b58ef6e3d36a42e01992accf5c52d6eea244353.jpg" alt=""></div>
</div>
</div>
I want to achieve to change this and to get both images connected with their corners, like this:
How to achieve the result from image? Also, i want to keep the paddings of the text.
Was it necessary? The indents are preserved. In css, I marked the edits. Also, to improve the visualization of your images, you can add rules - height: 100% and object-fit: cover for img tag (It's not obligatory).
.box1, .box2 {
display: flex;
}
img {
height: 200px;
width: 100%;
}
.box2 .left, .box1 .right, .box2 .right, .box1 .left {
width: 100%;
flex-basis: 50%; /*add this it*/
}
.box2 .left {
padding: 25px 10px 25px 75px;
box-sizing: border-box; /*add this it*/
}
.box1 .right {
padding: 25px 75px 25px 10px;
box-sizing: border-box; /*add this it*/
}
<div class="container">
<div class="box1">
<div class="left"><img src="https://cdn.wallpaperhub.app/cloudcache/1/b/5/8/e/f/1b58ef6e3d36a42e01992accf5c52d6eea244353.jpg" alt=""></div>
<div class="right">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam, non. Molestias voluptas velit quae itaque natus nam odio obcaecati minima recusandae enim eos mollitia est animi incidunt, beatae iure perspiciatis.
Reprehenderit sapiente numquam totam possimus necessitatibus. Enim nostrum rem quasi voluptates. Consectetur, est aliquid. Iste voluptas laborum provident harum! Ipsam pariatur velit iste quam, dolorum inventore rem molestias modi nemo?
</div>
</div>
<div class="box2">
<div class="left">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam, non. Molestias voluptas velit quae itaque natus nam odio obcaecati minima recusandae enim eos mollitia est animi incidunt, beatae iure perspiciatis.
Reprehenderit sapiente numquam totam possimus necessitatibus. Enim nostrum rem quasi voluptates. Consectetur, est aliquid. Iste voluptas laborum provident harum! Ipsam pariatur velit iste quam, dolorum inventore rem molestias modi nemo?
</div>
<div class="right"><img src="https://cdn.wallpaperhub.app/cloudcache/1/b/5/8/e/f/1b58ef6e3d36a42e01992accf5c52d6eea244353.jpg" alt=""></div>
</div>
</div>
change your image to a background image. In this case, you need not specify the height and width of an image, which means truly responsible.
Just change the container height and width for a different viewport and it will always take 50% height and width..
* {
box-sizing: border-box;
}
.container{
display:flex;
flex-direction: column;
width: 600px;
height: 600px;
}
.image{
background-repeat: no-repeat;
background-size: 100% 100%;
background-image: url('https://cdn.wallpaperhub.app/cloudcache/1/b/5/8/e/f/1b58ef6e3d36a42e01992accf5c52d6eea244353.jpg')
}
.box-1, .box-2{
display:flex;
flex-direction: row;
flex-basis: 50%;
}
.image, .content{
border: 1px solid black;
flex-basis: 50%;
}
.content{
padding: 30px;
}
<div class="container">
<div class="box-1"><div class="image"></div>
<div class="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam, non. Molestias voluptas velit quae itaque natus nam odio obcaecati minima recusandae enim eos mollitia est animi incidunt,</div></div>
<div class="box-2"><div class="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam, non. Molestias voluptas velit quae itaque natus nam odio obcaecati minima recusandae enim eos mollitia est animi incidunt, beatae iure perspiciatis.</div>
<div class="image"></div></div>
</div>
This question already has answers here:
Child with max-height: 100% overflows parent
(11 answers)
Closed 3 years ago.
I'm trying to create equal card-like columns, using flex to fix the buttons to the bottom of the content is not aligned. Unfortunately, the button is being pushed out of the column. I know it's because the height of the card-body is set to 100%, but I'm not sure why it's setting the height to expand past its parent.
Any help would be appreciated!
Codepen:
https://codepen.io/anon/pen/wLjrzw
HTML:
<!--
.container-fluid>.row>.col-12.col-sm-6.col-md-3.ja-card*4>a>img[src="http:lorempixel.com/400/300/cats"]^.ja-counter{$}+.ja-card-body.d-flex.flex-column>h4>lorem4^p>lorem^a.mt-auto.btn.btn-lg.btn-primary>lorem2
-->
<div class="container-fluid">
<div class="row">
<div class="col-12 col-sm-6 col-md-3 ja-card">
<img src="http:lorempixel.com/400/300/cats" alt="">
<div class="ja-counter">1</div>
<div class="ja-card-body d-flex flex-column">
<h4>Lorem ipsum dolor sit.</h4>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, impedit qui voluptates porro vero incidunt, aliquid dignissimos quae a tempora nisi quis consequatur atque. Officiis laudantium laborum ab sequi pariatur?</p>
Lorem, ipsum.
</div>
</div>
<div class="col-12 col-sm-6 col-md-3 ja-card">
<img src="http:lorempixel.com/400/300/cats" alt="">
<div class="ja-counter">2</div>
<div class="ja-card-body d-flex flex-column">
<h4>Optio excepturi inventore reprehenderit?</h4>
<p>Sapiente quis, commodi fugiat architecto temporibus accusantium veniam quasi nisi tempore facere sint similique magni, quae suscipit rem molestias. Officia quidem sint sapiente odit nisi sit pariatur repellat eaque consequuntur.Sapiente quis, commodi fugiat architecto temporibus accusantium veniam quasi nisi tempore facere sint similique magni, quae suscipit rem molestias. Officia quidem sint sapiente odit nisi sit pariatur repellat eaque consequuntur.</p>
Quidem, perferendis!
</div>
</div>
<div class="col-12 col-sm-6 col-md-3 ja-card">
<img src="http:lorempixel.com/400/300/cats" alt="">
<div class="ja-counter">3</div>
<div class="ja-card-body d-flex flex-column">
<h4>Aperiam porro quae deserunt?</h4>
<p>Doloremque tenetur aut adipisci dolore sed aspernatur! Praesentium vitae ut, quisquam sed nesciunt odit! Tempora incidunt, laborum impedit ratione maiores ipsum, laboriosam odit aspernatur ex reiciendis necessitatibus similique id optio?</p>
Nobis, eum.
</div>
</div>
<div class="col-12 col-sm-6 col-md-3 ja-card">
<img src="http:lorempixel.com/400/300/cats" alt="">
<div class="ja-counter">4</div>
<div class="ja-card-body d-flex flex-column">
<h4>Aspernatur mollitia commodi ipsum!</h4>
<p>Quia veniam recusandae ut, cum porro assumenda. Aut magnam quo rem sapiente vitae sequi, distinctio error repudiandae consequatur ut doloribus commodi rerum? Cumque iure quis pariatur, consequatur dolore consectetur doloribus?</p>
Odit, consectetur?
</div>
</div>
</div>
</div>
CSS:
.ja-card {
background-color: #d6d6d6;
padding: 0;
}
img {
width: 100%;
}
.ja-card:nth-child(even) {
background-color: #f6f6f6;
}
.ja-card-body {
height:100%;
padding: 2em;
}
.ja-counter {
position: relative;
top: 0;
left: 50%;
margin: -35px 0 0 -35px;
display: block;
width: 70px;
height: 70px;
border: 5px solid #fff;
border-radius: 50%;
color: #fff;
font-family: "Helvetica", sans-serif;
font-size: 36px;
font-weight: bold;
line-height: 60px;
text-align: center;
background-color:red;
}
The better way to approach this is to use flexbox for the card. And then make space between its children.
So
add display: flex and flex-direction: column to .ja-card
Set justify-content: space-between for .ja-card-body
Also, you don't need to set 100% height for .ja-card-body. it is useless.
Working Demo
Add box-sizing:border-box to your .ja-card-body CSS definition.
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>
I have two columns set to 50%, left with text and right with image that should fill whole column vertically. The problem is that when text is a little bigger, there is a gap below the image. Is it possible to always have the image occupy the full height (but in same ratio)? For example, the image could get wider than its column, in that case I could just set overflow hidden.
.flex {
display: flex;
border: 3px solid black;
padding: 10px;
}
.col {
border: 1px dashed #aaa;
}
.left {
padding: 30px;
}
<div class="flex">
<div class="col left">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eos fugiat deserunt ullam tempore? Aspernatur eligendi dolores explicabo officiis adipisci, incidunt distinctio tempore culpa, esse cumque atque repellendus eius delectus fugit quia odit
ut porro laborum alias. Aliquam et est neque ut, rem ab omnis? Culpa rerum, vel ad magnam iusto explicabo at consequatur deserunt quo repellendus. Sequi nulla nemo a magni voluptates. Nemo mollitia, ut ex temporibus voluptatem incidunt nostrum quo,
quod reprehenderit omnis! Sequi nulla nemo a magni voluptates.
</p>
</div>
<div class="col right">
<img src="http://placehold.it/550x250" alt="">
</div>
</div>
You have to set following CSS properties to image
height:100%;
width:auto;
.flex {
display: flex;
border: 3px solid black;
padding: 10px;
}
.col {
border: 1px dashed #aaa;
overflow-x: hidden;
}
.left {
padding: 30px;
}
.right img {
height: 100%;
width: auto;
}
<div class="flex">
<div class="col left">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eos fugiat deserunt ullam tempore? Aspernatur eligendi dolores explicabo officiis adipisci, incidunt distinctio tempore culpa, esse cumque atque repellendus eius delectus fugit quia odit
ut porro laborum alias. Aliquam et est neque ut, rem ab omnis? Culpa rerum, vel ad magnam iusto explicabo at consequatur deserunt quo repellendus. Sequi nulla nemo a magni voluptates. Nemo mollitia, ut ex temporibus voluptatem incidunt nostrum quo,
quod reprehenderit omnis! Sequi nulla nemo a magni voluptates.
</p>
</div>
<div class="col right">
<img src="http://placehold.it/550x250" alt="">
</div>
</div>
The quickest way to get the image to fill the vertical height of the container would be to make the parent a flex container. This automatically applies align-items: stretch to the children (in this case, the image).
.right { display: flex; }
From that point, you could use various methods to set the correct aspect ratio to the image. You could use percentage height, if you have defined heights on the parent / ancestors.
Or you could use the object-fit property.
.flex {
display: flex;
border: 3px solid black;
padding: 10px;
}
.col {
border: 1px dashed #aaa;
}
.left {
padding: 30px;
}
.right {
display: flex;
}
img {
object-fit: cover;
}
<div class="flex">
<div class="col left">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eos fugiat deserunt ullam tempore? Aspernatur eligendi dolores explicabo officiis adipisci, incidunt distinctio tempore culpa, esse cumque atque repellendus eius delectus fugit quia odit
ut porro laborum alias. Aliquam et est neque ut, rem ab omnis? Culpa rerum, vel ad magnam iusto explicabo at consequatur deserunt quo repellendus. Sequi nulla nemo a magni voluptates. Nemo mollitia, ut ex temporibus voluptatem incidunt nostrum quo,
quod reprehenderit omnis! Sequi nulla nemo a magni voluptates.
</p>
</div>
<div class="col right">
<img src="http://placehold.it/550x250" alt="">
</div>
</div>
Note that object-fit is not natively supported in Internet Explorer. For more details and a workaround see: Why isn't object-fit working in flexbox?
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