What's the canonical way to ensure an image and text are vertically separated from one another in a fully responsive way?
Look at this:
Via putting the image and text in separate divs, specifying width and ensuring display:block-inline, I'm able to create the following:
But this isn't perfectly responsive. How? For instance, for really small sizes (sizes that I must support), the image and the text run into one another like below:
There has to be a way to keep them vertically separated along any screen size. Perhaps I should use tables?
Please advise with an illustrative example, preferably with well-support CSS 2.1 attributes since a substantial number of clients I have to cater are Opera Mini browsers with no CSS3 or JS support.
My code is:
<div style="background-color:#E1F5FE;border-radius:10px;padding:10px;overflow:hidden;">
<div style="width:20%;display:inline-block;">
<img src="X.png">
</div>
<div style="width:80%;display:inline-block;float:right;text-align:center;">
Lorem ipsum dolor sit amet, ex eam nulla veritus abhorreant, magna vocent molestiae ea pri, ut eos tritani incorrupte
</div>
</div>
This is a variant of the answers given to this similar SO question. It doesn't really solve my problem.
Simply add width:100% for the image
But check the whole css, on small screens it is better to show the div below each others
.main{
background-color:#E1F5FE;
border-radius:10px;
padding:10px;
overflow:hidden;
}
.main > div{
display:inline-block;
float:left;
text-align:center;
}
.imgDiv{
width:20%;
}
.textDiv{
width:80%;
}
.imgDiv > img{
width:100%;
}
#media screen and (max-width: 450px) {
.imgDiv{
margin: 0 0 20px 25%;
width:50%;
}
.textDiv{
width:100%;
}
}
<div class="main">
<div class="imgDiv">
<img src="https://pbs.twimg.com/profile_images/839721704163155970/LI_TRk1z_400x400.jpg" style="width:100%">
</div>
<div class="textDiv">
Lorem ipsum dolor sit amet, ex eam nulla veritus abhorreant, magna vocent molestiae ea pri, ut eos tritani incorrupte
</div>
</div>
Here's a solution using flex with flex-wrap: wrap to take care of the responsive sizing. At small sizes the icon and text will stack vertically, and at larger sizes the icon and text will be horizontally aligned.
Flex has great support these days, even for the browser you mention.
.main {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.imgDiv {
flex: 1 1;
min-width: 80px;
text-align: center;
}
.imgDiv > img {
width: 80%;
height: auto;
max-width: 150px;
line-height: 0px;
}
.textDiv {
flex: 1 1 250px;
display: flex;
}
.textDiv > p {
flex: 1 0 0px;
padding: 0;
margin: 0;
align-self: center;
}
<div class="main">
<div class="imgDiv">
<img src="https://pbs.twimg.com/profile_images/839721704163155970/LI_TRk1z_400x400.jpg">
</div>
<div class="textDiv">
<p>Lorem ipsum dolor sit amet, ex eam nulla veritus abhorreant, magna vocent molestiae ea pri, ut eos tritani incorrupte</p>
</div>
</div>
Related
I want to make the part of my site centered,there is one img and text putted in square,but also it should be responsive, here is my css and html codes:
#part1{
padding-bottom:100px;
margin:auto;
overflow:hidden;
width:80%;
}
#part1 h1{
text-align:center;
font-size:250%;
padding-top:35px;
}
#part1 p{
text-align:center;
font-size:110%;
padding:30px;
background-color:#F0F8FF;
margin-top:60px;
width: 40%;
height: 60%;
float:left;
}
#part2 h1{
text-align:center;
font-size:250%;
}
and html:
<section id="part1">
<div class="container">
<h1>About</h1>
<div class="about-center">
<p>Accommodation in comfortable guest houses made of river stone on the shore of the lake, the possibility of constant communication with animals,</br> horseback riding through endless meadows, the possibility of developing a life, ecological food from the products of one’s own farm and unity with the spirit of the village, this is agro-tourism in Zagatala.</br> Those who wish can walk to domestic cows, cook cottage cheese, mow hay and much more.</br>
Eco-farm "Lake of Hope" is, first of all, an ecologically clean, healthy and congenial holiday, which highly appreciates the level of noise and bustle of the general population.</p>
<img id="about-image" src="images/about.jpg" width="570" height="450" />
</div>
</div>
</section>
using CSS grid, we don't need a lot of code, and it will also be responsive always!
useful docs:
CSS grid: https://developer.mozilla.org/en-US/docs/Web/CSS/grid
grid-template-columns: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns
object-fit: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
place-items: https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
.about-center {
display: grid;
/* 50% 50% */
grid-template-columns: 1fr 1fr;
/* centering */
place-items: center;
/* 1gap between items */
gap: 1rem;
/* padding */
padding: 0.5rem;
/* for debugging purposes, delete this */
border: 1px solid red;
}
/* responsive image */
.about-center img {
width: 100%;
height: 100%;
/* make image not stretch */
object-fit: cover;
}
<body>
<section id="part1">
<div class="container">
<div class="about-center">
<!-- 1 -->
<div>
<h1>About</h1>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Est eaque aspernatur soluta non, pariatur adipisci labore? Voluptatibus quis repellat placeat ex debitis. Quia, perspiciatis commodi tempora odio saepe praesentium beatae!</p>
</div>
<!-- 2 -->
<img id="about-image" src="https://picsum.photos/500" />
</div>
</div>
</section>
</body>
I believe the site you've posted uses css flexbox to deal with responsive design and div alignment. You can learn about this here https://css-tricks.com/snippets/css/a-guide-to-flexbox/.
.about-center {
display: flex;
align-items: center;
justify-content: space-between;
}
This should do the trick.
To make it responsive you can use flex-wrap property to make the content inside the .about-center class wrap so that the text will be on top of the image when the width of the screen shrinks.
Does anyone happen to know how to create a layout similar to this in HTML? I am trying to make a "profile" based layout where it displays a picture where the person's name is directly in the right corner and a description is under the header, yet still to the right of the image. Here is a picture of what I am trying to create along with what I have tried to do.
.container {
position: relative;
}
.topright {
position: absolute;
top: 8px;
right: 16px;
font-size: 18px;
}
img {
width: 100%;
height: auto;
}
<body>
<h2>Image Text</h2>
<p>Add some text to an image in the top right corner:</p>
<div class="container">
<img src="img_5terre_wide.jpg" alt="Cinque Terre" width="1000" height="300">
<div class="topright">NAME HERE </div>
Sample text goes here
</div>
</body>
The problem with my code above is that the 'NAME HERE' message appears inside of the image and the sample text is below the image. I would prefer for them to both be on the right of the image. I have taken a simple approach to it, but if anyone knows how to incorporate it using CSS or another method, it would be appreciate it.
<!DOCTYPE>
<html>
<head>
<style>
.img{
width:250px;
height:250px;
}
h1{
display: inline-block;
vertical-align: top;
}
.text{
display: inline-block;
vertical-align: top;
display: inline-block;
width: 330px;
padding-left: 15px;
}
p{
font-size: 15px;
}
</style>
</head>
<body>
<image class="img" src="https://image.freepik.com/free-photo/image-human-brain_99433-298.jpg"></image>
<div class='text'>
<h1>NAME HERE</h1>
<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>
</div>
</body>
</html>
You can begin with something like this, I have made slightly modification to html.
You can use flex for the container class that will make the content to flow in row.
you need the right content to be in column so add flex and direction property to .topright{...} this will give you content one below the other.
.container {
position: relative;
display: flex;
border: 2px solid black;
width: 700px;
margin: 10px;
}
.topright {
width: 100%;
height: 50px;
font-size: 28px;
text-align: left;
display: flex;
flex-direction: column;
margin: 15px;
}
.text {
width: 100%;
font-size: 15px;
}
<body>
<h2>Image Text</h2>
<p>Add some text to an image in the top right corner:</p>
<div class="container">
<img src="http://placekitten.com/301/301" alt="Cinque Terre" width="300" height="300">
<div class="topright">NAME HERE
<div class="text">
What is Lorem Ipsum Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen
book it has? Easily generate Lorem Ipsum placeholder text in any number of characters, words sentences or paragraphs. Learn about the origins of the passage </div>
</div>
</div>
</body>
you can add img to div tag and provide witdth to both divs.
.container {
position: relative;
}
.topright {
top:0;
margin-left:20px;
position:absolute;
font-size: 18px;
}
.d-block{
dispaly:block;
}
img{width:100%}
.w-50{
width:48%;
display:inline-block
}
<div class="container">
<div class="w-50">
<img src="img_girl.jpg" alt="Cinque Terre" >
</div>
<div class="topright w-50">
<p class="d-block">NAME HERE</p>
<p class="d-block">Sample text goes here</p>
</div>
I'm trying to nest two child elements in a wrapper which specifies side margins so there's space between its contents and the sides of the screen when the display is narrow and a max-width for when the display is wide.
The second child has some overflow which should be visible while the first child should stay strictly within the wrapper's content box. With the first child removed, the second child behaves as desired. When I add in the first child though, it seems to completely ignore the wrapper's margins, stretching the wrapper's content box and breaking the second child along with it.
Applying overflow: hidden to the wrapper fixes the margin problem but clips the second child. Applying the margins to the first child didn't make it collapse with the parent since it's in a new block formatting context.
The only workaround I've found so far would be to do:
.wrapper {
> * {
margin-left: 1.5rem;
margin-right: 1.5rem;
}
}
and increase the max-width of the wrapper by 3rem but I was hoping there was some solution that didn't require me to shift the margin from the wrapper to its children.
https://codepen.io/HybridCore/pen/jjoWmd
body {
background-color: #000;
color: #FFF;
display: flex;
justify-content: center;
}
.wrapper {
margin: 0 1.5rem;
max-width: 40rem;
width: 100%;
}
.fit_content_box {
display: flex;
align-items: center;
}
.L {
min-width: 0;
flex: 1 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.R {
margin-left: 1rem;
height: 1rem;
width: 1rem;
background-color: #FFF;
}
.overflow {
display: flex;
justify-content: space-between;
}
.overflow>div {
width: 0;
display: flex;
justify-content: center;
}
<body>
<div class="wrapper">
<div class="fit_content_box">
<p class="L">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>
<div class="R"></div>
</div>
<div class="overflow">
<div>
<p>0</p>
</div>
<div>
<p>12</p>
</div>
<div>
<p>24</p>
</div>
</div>
</div>
</body>
You mainly have two issues:
You are setting width:100% to the wrapper and this doesn't account for margin so you will logically have overflow and since the body is a flex container with justify-content:center the margin will overflow equally from both sides that's why you think it's not applied.
You are facing the min-width constraint of flexbox which is forcing you to set width:100% thinking it's the good solution. This same constraint is also preventing the element from shrinking lower than the 100% you specified (related: Why is a flex item limited to parent size?)
To fix this you need to remove width:100% from wrapper and consider min-width:0 instead. You can also remove the min-width applied to .L and you need to consider flex-shrink:0 on .R (or replace its width by min-width)
body {
background-color: #000;
color: #FFF;
display: flex;
justify-content: center;
}
.wrapper {
margin: 0 1.5rem;
max-width: 40rem;
min-width:0;
}
.fit_content_box {
display: flex;
align-items: center;
}
.L {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.R {
margin-left: 1rem;
flex-shrink:0;
height: 1rem;
width: 1rem;
background-color: #FFF;
}
.overflow {
display: flex;
justify-content: space-between;
}
.overflow>div {
width: 0;
display: flex;
justify-content: center;
}
<body>
<div class="wrapper">
<div class="fit_content_box">
<p class="L">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>
<div class="R"></div>
</div>
<div class="overflow">
<div>
<p>0</p>
</div>
<div>
<p>12</p>
</div>
<div>
<p>24</p>
</div>
</div>
</div>
</body>
If you want the element to remain at least equal to max-width when there is a small amount of text add flex-grow:1:
body {
background-color: #000;
color: #FFF;
display: flex;
justify-content: center;
}
.wrapper {
margin: 0 1.5rem;
max-width: 40rem;
min-width:0;
flex-grow:1;
}
.fit_content_box {
display: flex;
align-items: center;
}
.L {
flex-grow:1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.R {
margin-left: 1rem;
flex-shrink:0;
height: 1rem;
width: 1rem;
background-color: #FFF;
}
.overflow {
display: flex;
justify-content: space-between;
}
.overflow>div {
width: 0;
display: flex;
justify-content: center;
}
<body>
<div class="wrapper">
<div class="fit_content_box">
<p class="L">Lorem ipsum dolor sit e dolor sit e</p>
<div class="R"></div>
</div>
<div class="overflow">
<div>
<p>0</p>
</div>
<div>
<p>12</p>
</div>
<div>
<p>24</p>
</div>
</div>
</div>
</body>
To better illustrate the (1) here is another example with overflowing margin that you can hardly notice:
.container {
width:200px;
margin:auto;
display:flex;
justify-content:center;
}
.box {
height:50px;
width:100%;
background:red;
}
<div class="container">
<div class="box" style="margin:0 5966px">a_long_text_to_avoid_the_shrink</div>
</div>
<div class="container">
<div class="box">a_long_text_to_avoid_the_shrink</div>
</div>
You can see that we have a long text forcing our element to not shrink (the min-width constraint), the element is taking full width and we are centring the content. This will make the margin overflowing like if there is no margin.
If you break one rule then you will see the effect of the margin.
Remove the long text:
.container {
width:200px;
margin:auto;
display:flex;
justify-content:center;
}
.box {
width:100%;
height:50px;
background:red;
}
<div class="container">
<div class="box" style="margin:0 5966px">a long text to avoid the shrink</div>
</div>
<div class="container">
<div class="box">a long text to avoid the shrink</div>
</div>
Remove the centring:
.container {
width:200px;
margin:auto;
display:flex;
}
.box {
width:100%;
height:50px;
background:red;
}
<div class="container">
<div class="box" style="margin:0 5966px">a_long_text_to_avoid_the_shrink</div>
</div>
<div class="container">
<div class="box">a_long_text_to_avoid_the_shrink</div>
</div>
Make a different margin on each side
.container {
width:200px;
margin:auto;
display:flex;
justify-content:center;
}
.box {
width:100%;
height:50px;
background:red;
}
<div class="container">
<div class="box" style="margin:0 500px 0 400px">a_long_text_to_avoid_the_shrink</div>
</div>
<div class="container">
<div class="box">a_long_text_to_avoid_the_shrink</div>
</div>
(2) The white-space is creating the min-width contraint preventing the element from shrinking.
Here is an exmaple to illustrate:
.body {
display: flex;
justify-content: center;
margin: 10px 100px;
border: 2px solid red;
}
.wrapper {
border: 1px solid;
margin: 0 20px;
}
.box {
display:flex;
}
The below is a logical behavior where the text will wrap and the margin are respected
<div class="body">
<div class="wrapper">
<div class="box">
<div>some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Let's add white-space:nowrap. We add a min-width contraint since we said to the text to never wrap thus our flex element will not shrink and overflow.
<div class="body">
<div class="wrapper">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
If we add width:100% we force its width to be the same as the container BUT the margin aren't included and are kept outside (the text will logically overflow)
<div class="body">
<div class="wrapper" style="width:100%">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Now if we add min-width:0 we remove the constaint of minimum sizing and we can see the margin again even if we keep width:100% because the element will shrink by default
<div class="body">
<div class="wrapper" style="width:100%;min-width:0">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
The trick is that we are centring the element and applying the same margin on both side which will create the illusion of a collapsing margin but it's a simple overflow of the margin from both sides equally.
Let's change the margin slightly on one side to see a little offset to the other side:
.body {
display: flex;
justify-content: center;
margin: 10px 100px;
border: 2px solid red;
}
.wrapper {
border: 1px solid;
margin: 0 20px 0 40px;
}
.box {
display:flex;
}
The below is a logical behavior where the text will wrap and the margin are respected
<div class="body">
<div class="wrapper">
<div class="box">
<div>some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Let's add white-space:nowrap. We add a min-width contraint since we said to the text to never wrap thus our flex element will not shrink and overflow.
<div class="body">
<div class="wrapper">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
If we add width:100% we force its width to be the same as the container BUT the margin aren't included and are kept outside (the text will logically overflow)
<div class="body">
<div class="wrapper" style="width:100%">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Now if we add min-width:0 we remove the constaint of minimum sizing and we can see the margin again even if we keep width:100% because the element will shrink by default
<div class="body">
<div class="wrapper" style="width:100%;min-width:0">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
It appears that the source of the problem is white-space: nowrap, which is applied to the content element (.L) inside the first child item (.fit_content_box).
.L {
border: solid 1px #FF0000;
min-width: 0;
flex: 1 0;
overflow: hidden;
white-space: nowrap; <--- trouble maker
text-overflow: ellipsis;
}
If you remove that line of code, your side margins on .wrapper work as expected.
So the key questions are:
Why does the white-space property on a grand-child (.L) collapse the side margins of the grand-parent (.wrapper)?
Why does the white-space property not collapse the side margins when they are applied to the parent (.fit_content_box)?
Why does the overflow property, when applied to the grand-parent (.wrapper), with a value other than visible, allow the margins to hold firm on the grand-child (.L)?
You wrote:
Applying the margins to the first child didn't make it collapse with the parent since it's in a new block formatting context.
Actually, this isn't an issue of conventional margin collapsing, because:
We're talking about horizontal margins, and horizontal margins never collapse, and
We're working inside a flex container, and margins inside a flex container never collapse.
So although a full understanding of the problem may lie in the block (or flex) formatting context, I'm not sure that's why margins on the parent don't collapse.
This is as far as I've gotten on this issue. I'll do more research when I have time. Or maybe somebody else can pick it up from here.
Background
I have a div with two columns. In column A, there will be a div with an "About Me" section. This is height X. In column B will be another image of a map. This is height Y. X is not equal to Y.
I am making both columns (combined) = to 100vw. Each column is defined as 50%.
(I can calculate the height of the map in column B with the following math (which can be seen more clearly in the linked Jfiddle): Image is 350x600px. If 350px = 50%, then 600px = ~171%. 171 / 2 = 85.5% so the following code snippet should give the correct transform value:
margin-top: 85.5%;
transform: translateY(-50%);)
Objective
I would like the column height to adjust to the height of the tallest element. The tallest element will most likely always be in column B. I would like the div in column A to sit halfway down the page. However, if I should ever change the order and want to put something taller in column A than in column B, it would be really great to have a fallback so that the element in column B repositions itself to become vertically centred. (But I can live without that and do it manually should I need to.)
Problem
Column heights aren't behaving themselves. The height of column A (shown in dark green) is higher than that of column B, even though the element in A is shorter.
Jfiddle
http://jsfiddle.net/ubjo1s3y/28/
(With nice bright div colours)
Code
css:
.column {
margin: -5px 0px 30px 0px;
float: left;
width: 50%;
height: auto;
background-color: green;
}
.row:after {
background-color: pink;
content: "";
display: table;
clear: both;
height: 0;
}
#aboutmecontainer {
background-color: aqua;
width: 90%;
float: left;
padding: 0px 5% 0px 5%;
margin-top: 85.5%;
transform: translateY(-50%);
}
#facephoto {
background-color: red;
float: left;
width: 130px;
margin: 0px 0px 10px 50%;
transform: translateX(-50%);
}
#aboutmetext {
background-color: yellow;
float: left;
width: 100%;
height: auto;
}
#map {
background-color: yellowgreen;
float: left;
width: 90%;
padding: 0px 5% 0px 5%;
}
html:
<div class="row">
<div class="column">
<!-- container for round face photo -->
<div id="aboutmecontainer">
<div id="facephoto">
<!-- photo -->
<img src="http://via.placeholder.com/200x200" alt="Face" style="height: 100%; width: 100%; object-fit: contain;" />
</div>
<!-- container for text underneath face photo -->
<div id="aboutmetext"><h3 style="color: #000000;">About Me</h3><p></p><h5 style="color: #000000;">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</h5>
</div>
</div>
</div>
<div class="column">
<!-- div for map to right of "about me" section -->
<div id="map">
<!-- map picture can be edited in photoshop to add new countries -->
<img src="http://via.placeholder.com/350x600" alt="Map" style="width: 100%; height: 100%; object-fit: contain;">
</div>
</div>
</div>
First of all, I would definitely recommend learning how to use flexbox.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox.
Additionally, using floats in this situation is going to ruin the flow of the dom. There are situations where using floats can work, but it removes the targeted element from the regular flow of the dom and will always cause you grief if you don't know how to use floats.
Here is a revised jsfiddle that does not use flex. You want to set .column's display to inline-block and put font-size: 0 on .row (removing white-space). And remove all of your floats.
JSFiddle
Have you looked into Flexbox? It drastically simplifies this old height problem.
See this fiddle :
http://jsfiddle.net/uy2pqbkL/
Everything else is the same except the new properties added to your .row class.
.row {
display:flex;
align-items:center;
}
We have a problem where we need to have a list of divs with dynamic content.
There will always be 2 divs per row. Both of those elements should have the same height.
Currently we have a solution which sets the height of the boxes with JavaScript, but it's not very performant, since it recalculates the sizes on every resize (Responsive design).
Is there a solution without fixed height values?
Important: The boxes still need to be padded, and the padding needs to be in percent (currently 4% margin on div)
Jsfiddle: http://jsfiddle.net/6dmwU/
<div class="boxes">
<div class="box-wrapper">
<div class="box" style="height: 203px;">
<p class="box-title">Lorem Vulputate</p>
<p>On corerias sunturero in cullabore dolestionet apid utatur On corerias sunturero in cullabore dolestionet apid utatur</p>
</div>
<div class="box" style="height: 203px;">
<p class="box-title">Egestas Pharetra</p>
<p>On corerias sunturero in cullabore dolestionet apid utatur</p>
</div>
</div>
<div class="box-wrapper">
<div class="box" style="height: 151px;">
<p class="box-title">Vulputate Egestas</p>
<p>On corerias sunturero in cullabore dolestionet apid utatur</p>
</div>
<div class="box" style="height: 151px;">
<p class="box-title">Egestas Pharetra</p>
<p>On corerias sunturero in cullabore dolestionet apid utatur</p>
</div>
</div>
</div>
Any help will be very appreciated
You can use a flex model for this:
.boxes .box
{
margin-left: 2%;
margin-bottom: 2%;
width: 50%;
padding: 4%;
border: 1px solid #b6b6b6;
border: 0.0625rem solid #b6b6b6;
box-sizing: border-box;
}
.box-wrapper
{
width: 100%;
display: -webkit-box;
display: -moz-box;
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
jsFIddle
This way every row will be the height of the highest child element.
However the support for this is limited.
So if you rather not use this method you can transform you structure in a table structure. This way every row will be the height of the highest child element.
.boxes .box
{
display: table-cell;
margin-left: 2%;
margin-bottom: 2%;
width: 50%;
padding: 4%;
border: 1px solid #b6b6b6;
border: 0.0625rem solid #b6b6b6;
box-sizing: border-box;
}
.box-wrapper
{
display: table-row;
}
.boxes
{
display: table;
border-collapse: separate;
border-spacing: 5px;
}
Because margin doesn't work between table-cells i used border-spacing to define the seperation between the cells.
jsFiddle
Try this
.boxes .box {
float: left;
margin-left: 2%;
margin-bottom: 2%;
width: 38%;
padding: 4%;
border: 1px solid #b6b6b6;
border: 0.0625rem solid #b6b6b6;
box-sizing: border-box;
display:inline-block
}
.box-wrapper,.boxes{
display:inline-block;
width:100%;
}
Fiddle
Have a look at this fiddle You can use display:table-cell;
CSS:
.row {
display: table;
width: 100%;
}
.left {
width:50%;
background: blue;
display:table-cell;
}
.right {
width:50%;
background: red;
display:table-cell;
}
HTML
<div class='row'>
<div class='left'>
"Lorem ipsum dolor sit amet, consectetur adipisicing 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."
</div>
<div class='right'>
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos
</div>
</div>
I use flexbox, it's magic ^^ :
HTML
<div class="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
CSS
.boxes {
display: flex;
flex-wrap: wrap;
}
.box {
margin: 0 1% 1% 0;
width: 48%;
}
CODEPEN DEMO
Complete guide to flexbox on css-trick