Set the same height for two div in CSS - html

I'm trying to align two div on the same line. No problem for that, but now I'm looking for a solution to have the same height on each div, the problem is that the first one contain only an icon and the second contain text that will probably take more space than the icon. I'm looking for a css property than can help me to do this...
.zone-info {
background-color: #e0f1f5;
line-height: 1.363em;
margin-bottom: 3px;
padding: 5px 0;
}
.zi-icon {
display: inline-block;
float: left;
text-align: center;
vertical-align: center;
height: 100%;
width: 10%;
}
.zi-text {
width: 90%;
display: inline-block;
float: left;
}
<div class="zi-icon zone-info">icone</div>
<div class="zi-text zone-info">Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur.<br>
# Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.<br>
# Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit.</br>
# Sed ut perspiciatis unde omnis iste natus error sit.
</div>
Here is a Jsfiddle of what I do for the moment.
http://jsfiddle.net/nc6L227z/

Wrap the two divs in a .wrapper element and give display: table. Then give display: table-cell to the inner divs and remove float:left and display: inline-block from the other elements:
EDIT: (vertical align added)
Check the Updated DEMO
.wrapper { display: table }
.zone-info {
background-color: #e0f1f5;
display: table-cell;
line-height: 1.363em;
vertical-align: middle;
}
.zi-icon {
text-align: center;
width: 10%;
}
.zi-text {
}

You can use table-cell or a new css property flex
div{display:flex} //you don't need float , inline-block, table cell anythng
DEMO

You could have the right one be contained in the left one.
.col1 {
float: left;
border: 1px solid green;
margin-right: 200px;
background: red;
}
.col1-content {
float: left;
width: 50px;
}
.col2 {
float: left;
background: green;
}
.col2-content {
width: 200px;
}
.clearfix:after {
content: ".";
clear: both;
display: block;
visibility: hidden;
height: 0px;
}
<div class="outer">
<div class="inner clearfix">
<div class="col1">
<div class="col1-content">
Lorem ipsum dolor sit amet.
</div>
<div class="col2">
<div class="col2-content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magni excepturi animi vitae saepe ratione cumque nostrum eius impedit delectus quibusdam eaque earum et iusto quam soluta. Expedita eius perspiciatis necessitatibus facilis dignissimos quis velit maiores incidunt sequi odio non reprehenderit ut cum perferendis enim. Eligendi fugit nesciunt dolore aliquam numquam.
</div>
</div>
</div>
</div>
</div>

Check Fiddle
I have added a parent div and applied height on it.

Related

How to make the div takes the full height of the page when the browser height shrink in CSS

When we set a block level element height to 100% it will take the full height min-height: 100%; of the page or at least the full height of its parent, however what is strange for me as I'm currently learning CSS is that when I shrink the height of the browser window
Here is a few seconds video to show what I mean. Video
As you can already see from the video, when I make the height of the browser very short and then scroll down I see that the content section no longer takes the full height until I make the browser height a little bit longer, however the sidebar and the profile sections don't do that since their width are a less than the content section.
So the question is: How to make the div to keep its full height when the browser height shrinks and then scroll down as you can see from the video?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 90%;
}
body {
position: relative;
overflow-y: scroll;
height: 100%;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar {
position: absolute;
width: 15%;
left: 0;
top: 0;
background-color: darkblue;
color: white;
padding: 2em;
margin: 2em;
min-height: 100%;
}
.content {
position: absolute;
width: 60%;
left: calc(15% + 2em);
top: 0;
background-color: green;
color: white;
padding: 2em;
margin: 2em;
min-height: 100%;
}
.profile {
position: absolute;
width: 15%;
top: 0;
left: calc(75% + 4em);
padding: 2em;
margin: 2em;
background-color: firebrick;
color: black;
min-height: 100%;
}
button {
display: block;
margin-top: 2em;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
My code on codepen: Code
CSS-Grid solution
One easy way is to use CSS-Grid. I cut your code down to the part that is actually "usefull" and removed all unecessary or non-working code.
display: grid activates CSS-Grid
grid-template-columns: 15% auto 15% creates your 3 column layout while auto occupies the remaining space.
min-height: 100vh sizes the boxes to be at least as heigh as the viewport.
body {
display: grid;
grid-template-columns: 15% auto 15%;
grid-gap: 2em;
padding: 2em;
min-height: 100vh;
}
/* original CSS cut down */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar,
.content,
.profile {
padding: 2em;
}
.sidebar {
background-color: darkblue;
color: white;
}
.content {
background-color: green;
color: white;
}
.profile {
background-color: firebrick;
color: black;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
Flexbox solution:
To use Flexbox you have to use display: flex. Same as the CSS-Grid solution you also give the body a min-height: 100vh to fill up at least the entire viewport.
To get your intended 3 column layout, you have to define your left and right side as width: 15%.
To have the middle column fill up the remaining space, you just need to add: flex-grow: 1;
body {
display: flex;
gap: 2em;
padding: 2em;
min-height: 100vh;
}
.sidebar,
.profile {
width: 15%;
}
.content {
flex-grow: 1;
}
/* original CSS cut down */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar,
.content,
.profile {
padding: 2em;
}
.sidebar {
background-color: darkblue;
color: white;
}
.content {
background-color: green;
color: white;
}
.profile {
background-color: firebrick;
color: black;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>

how can I remove small gap vertically

If I remove the space between the block code it only removes a small gap horizontally,
but vertically it's still has a small gap i know i can use flexbox instead but, I'm trying to understand why it behaves like this, any idea why it still has a small gap? vertically it can be removed with font size set to 0 but there is no text or letter below the image
/* Video Wrapper */
.video-wrapper {
background-color: royalblue;
width: 60%;
margin: 15px auto 15px auto;
box-sizing: border-box;
}
.video-wrapper > h2 {
text-indent: 150px;
background-color: lightblue;
margin: 0px;
height: 50px;
line-height: 50px;
}
.video-content {
display: inline-block;
background-color: red;
width: 40%;
position: relative;
height: 400px;
box-sizing: border-box;
}
iframe {
width: 100%;
outline: 0;
position: absolute;
height: 100%;
box-sizing: border-box;
}
.video-wrapper > p {
display: inline-block;
width: 50%;
box-sizing: border-box;
vertical-align: top;
}
No Space Removed:
<section class="video-wrapper" id="video-section">
<h2>Video</h2>
<div class="video-content"></div>
<p>Carefully Haa Lorem ipsum dolor sit amet consectetur, adipisicing elit. Non perspiciatis veritatis veniam hic dignissimos! Et quas magnam doloremque, sapiente quae error ut repellendus esse aspernatur doloribus mollitia sunt iste cupiditate quisquam, aliquam dolorem labore dolore nobis repellat consectetur vel, nulla harum alias incidunt. Odit, dolorem. Maiores maxime quidem quis odit?</p>
</section>
Space Removed :
<section class="video-wrapper" id="video-section"><h2>Video</h2><div class="video-content"></div><p>Carefully Haa Lorem ipsum dolor sit amet consectetur, adipisicing elit. Non perspiciatis veritatis veniam hic dignissimos! Et quas magnam doloremque, sapiente quae error ut repellendus esse aspernatur doloribus mollitia sunt iste cupiditate quisquam, aliquam dolorem labore dolore nobis repellat consectetur vel, nulla harum alias incidunt. Odit, dolorem. Maiores maxime quidem quis odit?</p></section>
By reducing the margin-Bottom you can hide or remove the gap
to do that CSS will be
.video-content {
display: inline-block;
background-color: red;
width: 40%;
position: relative;
margin-bottom: -4px;
height: 400px;
box-sizing: border-box;
}
Yes, as answered #Jvs there is a way like to reduce margin-botttom and
there is more one way is reducing height of parent tag/class
in your case CSS :
.video-wrapper {
background-color: royalblue;
width: 60%;
margin: 15px auto 15px auto;
box-sizing: border-box;
height: 450px;
}
Try this one also :p

Set the same width of a “Position: fixed” div as parent div(flexbox item)

How can I make the same width of the NavWrapper as parent?
I want these links at a fixed position even the main section overflows.
I know how to do this without Flex. Is there any pure CSS way to do that?
body {
padding:0;
margin:0
}
.wrapper {
display: flex;
}
nav {
flex: 1 1 150px;
background: gray;
}
.nav-wrapper {
width: 100%;
position: fixed;
top: 0; left: 0;
display:flex;
flex-direction: column;
}
.nav-wrapper a {
flex: 1 1;
border: 1px solid red;
}
section {
flex: 5 1 500px;
padding: 20px;
}
<div class="wrapper">
<nav role="navigation">
<div class="nav-wrapper">
Home
About
</div>
</nav>
<section>
<p>Lorem</p>
</section>
</div>
You don't need fixed position- you can see why I say this after looking at the example below:
Remove the fixed positioning and add height: 100vh to nav:
nav {
flex: 1 1 150px;
background: gray;
height: 100vh;
}
Now wrap the contents on a section into an inner div that is positioned absolute like this:
section {
flex: 5 1 500px;
padding: 20px;
position: relative;
overflow-y: auto;
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
This will allow the section to remain at 100vh of the nav-wrapper and the extra height will overflow.
body {
padding: 0;
margin: 0
}
.wrapper {
display: flex;
}
nav {
flex: 1 1 150px;
background: gray;
height: 100vh;
}
.nav-wrapper {
width: 100%;
display: flex;
flex-direction: column;
}
.nav-wrapper a {
flex: 1 1;
border: 1px solid red;
}
section {
flex: 5 1 500px;
padding: 20px;
position: relative;
overflow-y: auto;
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="wrapper">
<nav role="navigation">
<div class="nav-wrapper">
Home
About
</div>
</nav>
<section>
<div class="inner">
<p>Lorem</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
<p>Lorem</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
<p>Lorem</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
</div>
</section>
</div>
Check this out and let me know your feedback. Thanks!

How can I align 2 or more "row" divs to the bottom of a div, without using any kind of "absolute" positioning? Needs IE9+ support

I need to position several div rows to the bottom of a container, similar to the image here:
My problem lies in that almost every solution on SO requires either absolute positioning or some method which requires modification every time a new element is added. I tried using a display:table-cell and vertical-align:middle, but this broke my row layout (all rows had display:block;). Is there a way to get this done in a way I can keep adding html rows to the layout and it will grow from the bottom to the top without modifying the CSS?
Edit: The answer NEEDS to still work after adding a new row without modifying any CSS. IE9+ support is highly preferable. CSS ONLY solution is also highly preferred. If no answers with such criteria appear by tomorrow I'll tag the next most useful one as right.
(I'm using foundation in case that helps)
JSFiddle to play with:
https://jsfiddle.net/o47xeze7/
<div class="parent">
<div class="child">abcdfg</div>
<div class="child">abcdfg</div>
</div>
.parent {
width: 100%;
height: 20rem;
border: 1px solid black;
display: table;
}
.child {
border: 1px solid red;
display: block;
vertical-align: bottom;
}
UPDATE: I'm an idiot... All I had to do was create a container with absolute bottom positioning and let it grow updwards. When I said no absolute positioned elements I said it because I don't want anything with the likes margin-top: x-pixels, because it requires updating that value every time I add a new row, but doing an absolute bottom placed container doesn't. Sorry guys. Here is the working solution in case anyone wants it.
https://jsfiddle.net/b6akcdso/
<div class="parent">
<div class="bottom-aligned-contanier">
<div class="child">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus expedita praesentium aperiam, eveniet in, dolore iusto excepturi quibusdam accusantium delectus aut atque assumenda quaerat recusandae perferendis repellat labore, explicabo maiores.</div>
<div class="child">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis deleniti minima nostrum, tenetur autem, debitis magni vel facere laudantium incidunt asperiores aliquam cupiditate cum perferendis cumque inventore, dignissimos ad in.</div>
<div class="child">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cum impedit deleniti, id voluptatum est! Quibusdam ea fugit obcaecati minima soluta, quis voluptate aspernatur corrupti, minus tempore ipsa adipisci porro. Ab.</div>
</div>
</div>
.parent {
width: 100%;
height: 20rem;
background-color: lightgray;
position: relative;
}
.bottom-aligned-contanier {
position: absolute;
bottom: 0;
}
.child {
display: block;
width: 100%;
background-color: darkgray;
color: white;
}
.child:nth-child(2n) {
background-color: gray;
}
Awarding right answer to the guy that gave me the idea to do this.
If you can use jQuery, then this solution works. Here is a fiddle: https://jsfiddle.net/o47xeze7/3/
HTML
<div class="parent">
<div class="bottom">
<div class="child">abcdfg</div>
<div class="child">abcdfg</div>
</div>
</div>
CSS
.parent {
width: 100%;
height: 20rem;
border: 1px solid black;
display: block;
}
.child {
border: 1px solid red;
display: block;
}
.bottom {
display: block;
position: relative;
}
jQuery
$(function() {
var parentHeight = $(".parent").height();
var bottomHeight = $(".bottom").height();
var difference = parentHeight - bottomHeight;
$(".bottom").css("margin-top", difference);
});
flexbox can do that.
.parent {
width: 100%;
height: 10rem;
border: 1px solid black;
display: flex;
flex-direction: column;
justify-content: flex-end;
margin-bottom: 1em;
}
.content {
align-self: flex-start;
margin-bottom: auto;
}
.child {
width: 100%;
border: 1px solid red;
margin: 0;
}
<div class="parent">
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt ipsam nihil vel doloribus maxime sed animi repellat consequatur, earum, eum sit. Repellendus fugit dolorem dolorum facere quo odit numquam autem, qui commodi accusantium hic. Omnis.</p>
</div>
<div class="child">top</div>
<div class="child">bottom</div>
</div>
<div class="parent">
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt ipsam nihil vel doloribus maxime sed animi repellat consequatur, earum, eum sit. Repellendus fugit dolorem dolorum facere quo odit numquam autem, qui commodi accusantium hic. Omnis.</p>
</div>
<div class="child">top</div>
<div class="middle">middle</div>
<div class="child">bottom</div>
</div>
JSfiddle Demo
If you're ready to ditch support for IE8 and IE9 then this might be the best solution for you since you don't want to use absolute/table-cell positioning.
You can achieve what you're trying to do using flexbox. Here's how it's done in your case:
.parent {
width: 100%;
height: 20rem;
border: 1px solid black;
display: flex;
flex-direction: column-reverse;
}
.child {
border: 1px solid red;
}
<div class="parent">
<div class="child">abcdfg</div>
<div class="child">abcdfg</div>
</div>

How to make all sibling DIVs inside a container DIV have equal height?

So I have 2 divs in a container. One floated left and one floated right. The one on the right is text. The one on the left is an image. How can I set the image height so it's equal to the unknown height of the right text?
The container of both divs should also be the same height as the text div. The image's height should not be taller or shorter than the text.
The div containing the text should be as big as the amount of text in it.
Here's an example on JSFiddle
HTML:
<div class="body">
<div class="imgContainer"><img src="http://www.appleinspires.me/wp-content/uploads/2013/05/mzl.bneaekit.512x512-75.jpg" alt=""></div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime ducimus, excepturi ad! Porro officia, est omnis eum modi reiciendis, velit aliquid dolores tempore odit ipsa temporibus ullam. Adipisci, optio, neque?<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure deserunt amet aspernatur nisi, voluptatem consequuntur vel saepe dolorem odio atque, porro architecto alias aliquid. Atque ea soluta, obcaecati sapiente mollitia!</div>
</div>
CSS:
.body {
display: table;
background-color: grey;
margin-bottom: 10.38vw;
margin-left: 5.19vw;
margin-right: 5.19vw;
}
.imgContainer {
float: left;
position: relative;
height: 100%;
width: 40%;
background-color: green;
}
img {
height: 100%;
width: 100%;
display: block;
border-radius:100%;
margin-left: auto;
margin-right: auto;
}
.text {
background-color:blue;
float: right;
width: 60%;
font-size: 2.26vw;
text-align: left;
display: table-cell;
}
On the JSFiddle, the image's height appears to be taller than the height of the div with text, but this is not what I want. I'd like the image to be shrunken to the height of the right div. Since my container is displayed as a table and the children divs should be displayed as table-cells, the container's height is supposed to be the same height as the text, which is what I'm aiming for. The image should also be horizontally centered in its div once it's shrunk, but my current code should allow that to work.
Another thing: The point of this is because I'm trying to go for a responsive design. The image should stay the same height as the text as the browser window is resized.
And just to clarify, I do not want the height of the text div to enlarge to the height of the image; I want the image's height to be shrunken to the height of the text.
If anyone has any suggestion or solutions, please let me know. Thanks!
You can just change the parent element .body to a flexbox using display: flex like this:
.body {
display: flex;
}
The result of this is that your items will all line up in a row, using the size of the content as their size in the main axis. If some items are taller than others, all items will stretch along the cross axis to fill its full size.
Check this JSFiddle or run the Code Snippet below for a practical example of the above code:
.body {
display: flex;
background-color: grey;
margin: 0 auto;
}
.imgContainer {
background-color: green;
}
img {
height: 100%;
width: 100%;
border-radius: 100%;
}
.text {
background-color: blue;
font-size: 14px;
text-align: left;
}
.text2 {
background-color: red;
font-size: 14px;
text-align: right;
}
<div class="body">
<div class="imgContainer">
<img src="https://picsum.photos/512/512" alt="">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime ducimus, excepturi ad! Porro officia, est omnis eum modi reiciendis, velit aliquid dolores tempore odit ipsa temporibus ullam.!
</div>
<div class="text2">
Just some short sentence.
</div>
</div>
You can
Remove floats in order to use a tabular layout, which will ensure both elements have the same height.
Remove the image from the normal flow of the document using absolute positioning. This way .imgContainer will be as short as possible.
Make the image grow to fill .imgContainer.
.body {
display: table;
}
.imgContainer,
.text {
display: table-cell;
vertical-align: top;
}
.imgContainer {
position: relative;
width: 40%;
background-color: green;
}
img {
position: absolute;
height: 100%;
width: 100%;
border-radius: 100%;
}
.text {
background-color: blue;
width: 60%;
display: table-cell;
}
<div class="body">
<div class="imgContainer">
<img src="http://www.appleinspires.me/wp-content/uploads/2013/05/mzl.bneaekit.512x512-75.jpg" alt="">
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime ducimus, excepturi ad! Porro officia, est omnis eum modi reiciendis, velit aliquid dolores tempore odit ipsa temporibus ullam. Adipisci, optio, neque?<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure deserunt amet aspernatur nisi, voluptatem consequuntur vel saepe dolorem odio atque, porro architecto alias aliquid. Atque ea soluta, obcaecati sapiente mollitia!</div>
</div>