This is my html code
<div class="longtext">
<div class="image">
<img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
</div>
<div class="text">
<p>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.</p>
</div>
<div class="buy">
$ 3053
</div>
</div>
I would like to achieve to display all content inline, something like this.
Image have defined dimensions, .text and .buy width is unknown. .buy must be always displayed whole and at single line. .text must be displayed on one line as well but its not necessary to display whole text.
Here is my css yet
.longtext {
border: 1px solid red;
overflow: hidden;
}
.longtext > * {
float: left;
}
.longtext img {
height: 60px;
width: 60px;
}
.buy {
float: right;
border: 1px solid blue;
}
.text {
border: 1px solid black;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Codepen
I would use flex on the parent and align-items to align them vertically. Add flex-grow: 1 to .text (or flex: 1 0 0; for short) so it fills the available space, and add the overflow/ellipsis to the p. Then you can remove the floats from everything else. Also added white-space: nowrap to the .buy element so the space between the $ and numbers won't break.
.longtext {
border: 1px solid red;
display: flex;
align-items: center;
}
.longtext img {
height: 60px;
width: 60px;
vertical-align: top;
}
.buy {
border: 1px solid blue;
}
.text {
border: 1px solid black;
overflow: hidden;
}
.text p {
text-overflow: ellipsis;
overflow: hidden;
}
.buy, .text {
white-space: nowrap;
}
<div class="longtext">
<div class="image">
<img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
</div>
<div class="text">
<p>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.</p>
</div>
<div class="buy">
$ 3053
</div>
</div>
.longtext {
border: 1px solid red;
display: flex;
}
.longtext img {
height: 60px;
width: 60px;
}
.longtext div{
vertical-align: top;
}
.buy {
border: 1px solid blue;
}
.text {
border: 1px solid black;
overflow: hidden;
}
.text p {
text-overflow: ellipsis;
overflow: hidden;
}
.buy, .text {
white-space: nowrap;
}
<div class="longtext">
<div class="image">
<img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
</div>
<div class="text">
<p>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.</p>
</div>
<div class="buy">
$ 30530000
</div>
</div>
<div class="longtext">
<div class="image">
<img src="https://c1.staticflickr.com/8/7610/17149522281_3b6ae4c948_b.jpg" alt="">
</div>
<div class="text">
<p>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.</p>
</div>
<div class="buy">
$ 30500
</div>
</div>
Related
I have 2 contents, the first one is blue section and the second one is purple section.
I add position absolute in the second section since I want to make the content share background, half with blue section and purple section.
My question, if I add more text on the first section, the text for the first section is hidden behind the second section.
Is there a way to make the first section flexible?
So whenever I add more text, the second section will not hide the text.
.commercial-wrapper {
display: flex;
position: relative;
bottom: 320px;
}
.commercial-white {
background-color: #f1f1f4;
width: 340px;
height: 460px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-bottom: 15px solid #005da0;
margin: 10px;
}
.commercial-blue {
position: absolute;
background-color: #005da0;
color: #ffffff;
width: 340px;
height: 473px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 10px;
border: 2px solid #ffffff;
visibility: hidden;
}
.open.commercial-blue {
visibility: visible;
}
.commercial-flip {
padding: 20px;
pointer-events: none;
}
.main-wrapper {
display: flex;
flex-direction: column;
background-color: purple;
justify-content: center;
}
.commercial-wrapper-text {
text-align: center;
background-color: #215cad;
color: #ffffff;
padding: 20px 0;
height: 500px;
}
.commercial-main-wrapper-flip {
background-color: purple;
position: relative;
height: 200px;
}
.commercial-wrapper-flip {
display: flex;
justify-content: center;
position: relative;
flex-wrap: wrap;
}
#industry-block {
background-color: #dce5ee;
}
.commit-industry {
border: 1px solid purple;
text-align: center;
position: relative;
}
<div class="commercial-solution-wrapper">
<div class="commercial-wrapper-text">
<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.<br />
<br />
<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.<br />
<br />
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.<br />
<br />
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>
</div>
<div class="commercial-main-wrapper-flip">
<div class="commercial-wrapper-flip">
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
</div>
</div>
EDIT: add an image for expected result
Because you are adding position: absolute you are taking that element out of document flow - so the text won't ever interact with the content below (hence the hiding).
One way to do it is to add a linear gradient to the second wrapper. So you are essentially "faking" the overlap.
The important part:
.commercial-main-wrapper-flip {
background: linear-gradient(0deg, rgba(191, 0, 254, 1) 55%, rgba(33, 92, 173, 1) 55%);
position: relative;
}
.commercial-wrapper {
display: flex;
position: relative;
}
.commercial-white {
background-color: #f1f1f4;
width: 340px;
height: 460px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-bottom: 15px solid #005da0;
margin: 10px;
}
.commercial-blue {
position: relative;
background-color: #005da0;
color: #ffffff;
width: 340px;
height: 473px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 10px;
border: 2px solid #ffffff;
visibility: hidden;
}
.open.commercial-blue {
visibility: visible;
}
.commercial-flip {
padding: 20px;
pointer-events: none;
}
.main-wrapper {
display: flex;
flex-direction: column;
background-color: purple;
justify-content: center;
}
.commercial-wrapper-text {
text-align: center;
background-color: #215cad;
color: #ffffff;
padding: 20px 0;
}
.commercial-main-wrapper-flip {
background: linear-gradient(0deg, rgba(191, 0, 254, 1) 55%, rgba(33, 92, 173, 1) 55%);
position: relative;
}
.commercial-wrapper-flip {
display: flex;
justify-content: center;
position: relative;
flex-wrap: wrap;
}
<div class="commercial-solution-wrapper">
<div class="commercial-wrapper-text">
<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.<br />
<br />
<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.<br />
<br /> 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.<br />
<br /> 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>
</div>
<div class="commercial-main-wrapper-flip">
<div class="commercial-wrapper-flip">
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
<div class="commercial-wrapper">
<div class="commercial-white">
<div class="commercial-flip">
<h3>Lorem ipsum</h3>
</div>
</div>
</div>
</div>
</div>
I'm creating a "Why Choose Us" splash page and I want to create tables with an image on one side and a reason on the other. I need the image to vertically align in the center.
So far I've tried making another table inside the table, and vertically aligning the image with 'vertical-align: middle' via a table cell wrapped around it.
<section class="reason">
<div class="content">
<div class="float-left table">
<div class="table-cell">
<img class="thumbnail" src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
</div>
</div>
<div class="float-right">
<h3>
Lorem Ipsum Dolor Sit Amet
</h3>
<p class="appeal">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.</p>
</div>
</div>
</section>
<style>
.reason {
padding: 100px;
background-color: #fff;
}
.content {
display: table;
clear: both;
max-width: 935px;
margin: 0 auto;
}
.float-left {
float: left;
width: 50%;
}
.float-right {
float: right;
width: 50%;
}
.table {
display: table;
}
.table-cell {
display: table-cell;
vertical-align: middle;
}
.thumbnail {
display: block;
margin: 0 auto;
}
</style>
Here's a codepen outlining the problem: https://codepen.io/AlecAcres/pen/NVMRoY
May be a style to <td>?
<style>
td img {
vertical-align: top;
}
</style>
I have changed the code (basically used flex instead of table) -:
Working demo here
HTML
<section class="reason">
<div class="content">
<div class="float-left">
<div class="table-cell">
<img class="thumbnail" src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
</div>
</div>
<div class="float-right">
<h3>
Lorem Ipsum Dolor Sit Amet
</h3>
<p class="appeal">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.</p>
</div>
</div>
</section>
CSS
.reason {
padding: 100px;
background-color: #fff;
}
.content {
display: flex;
justify-content: center;
max-width: 935px;
margin: 0 auto;
}
.float-left {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
}
.float-right {
flex: 1
}
.table-cell {
display: table-cell;
vertical-align: middle;
}
.thumbnail {
display: block;
margin: 0 auto;
}
.flex-container {
display: flex;
}
.flex-image {
display: flex;
flex-flow: column wrap;
justify-content: space-evenly;
}
<div class="flex-container">
<div class="flex-image">
<img
src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
</div>
<div class="flex-content">
<h3>
Lorem Ipsum Dolor Sit Amet
</h3>
<p class="appeal">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.</p>
</div>
</div>
Here is a more modern approach using FlexBox!
Demo:
https://codepen.io/anon/pen/WBJoxY
HTML:
<section class="reason">
<div class="content">
<div class="thumbnail">
<img src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
</div>
<div class="text">
<h3>
Lorem Ipsum Dolor Sit Amet
</h3>
<p class="appeal">
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.
</p>
</div>
</div>
</section>
CSS:
.reason {
padding: 100px;
background-color: #fff;
}
.content {
display: flex;
flex-direction: row;
max-width: 935px;
margin: 0 auto;
}
.text {
max-width: 50%;
}
.thumbnail {
display: flex;
margin: 0 auto;
justify-content: center;
align-items: center;
}
Suppose I have some fixed-size container and I want all elements to fit inside, but I don't know all heights of inside elements. Some element has lots of text, so I set overflow: hidden. But this element ignores the height of container and just stretches to fit contents. How do I do it right?
Edit: if I set overflow on my container, overflowing text will be hidden, but bottom padding will be ignored (see 2nd snippet). How do I cut text 5px from the bottom border, so all sides look equal?
.outer {
width: 200px;
height: 200px;
}
.inner {
padding: 5px;
background-color: #ccc;
height: 150px;
border: 1px solid black;
}
.text {
overflow: hidden;
}
<div class="outer">
<div class="inner">
<span style="color: red">Some element so we can't make text 100% height</span>
<div class="text">
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
</div>
</div>
<div>Some other text</div>
</div>
.outer {
width: 200px;
height: 200px;
}
.inner {
padding: 5px;
overflow: hidden;
background-color: #ccc;
height: 150px;
border: 1px solid black;
}
.text {
overflow: hidden;
}
<div class="outer">
<div class="inner">
<span style="color: red">Some element so we can't make text 100% height</span>
<div class="text">
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
</div>
</div>
<div>Some other text</div>
</div>
You can nest an additional div inside of .inner (I used .inner2 in this example, you can come up with a more meaningful name! :) ).
Basically, you need to separate the background/border from the container that will control the overflow (as you're right, overflow goes to the edge of the element, it doesn't care about padding).
Just an example, but I added a second div (.inner2) inside of .inner and moved the the height and overflow rules to that one instead. The background/padding/border stay put.
Edit: Added a lime border to inner2 to better illustrate what's happening.
.outer {
width: 200px;
height: 200px;
}
.inner {
padding: 5px;
background-color: #ccc;
border: 1px solid black;
}
.inner2 {
height: 150px;
overflow: hidden;
border: 1px solid lime;
}
.text {
}
<div class="outer">
<div class="inner">
<div class="inner2">
<span style="color: red">Some element so we can't make text 100% height</span>
<div class="text">
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
</div>
</div>
</div>
<div>Some other text</div>
</div>
I am creating a layout but I am getting some issue with the position.
I tried position: absolute but I am getting some issue.
I user display: flex and flex:1 for display the equal height of the div.
Would you help me out in this?
I tried below code to set.
.set_white_box{
position: absolute;
width: 50%;
top: 50%;
transform: translateY(-50%);
}
I added a simple structure in the snippet.
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.equal_padding {
padding: 100px 0;
}
.service_content1 {
display: flex;
}
.service_img_col {
width: 50%;
flex: 1;
}
.service_img_col img {
width: 100%;
}
.service_box {
flex: 1;
background-color: #fff;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.18);
box-sizing: border-box;
padding: 40px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section>
<div class="container">
<div class="row">
<div class="equal_padding clearfix service_content1">
<div class="service_box set_white_box">
<p>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.</p>
</div>
<div class="service_img_col"><img src="https://images.pexels.com/photos/1097768/pexels-photo-1097768.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></div>
</div>
<div class="equal_padding clearfix service_content1">
<div class="service_img_col"><img src="https://images.pexels.com/photos/1097768/pexels-photo-1097768.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></div>
<div class="service_box set_white_box">
<p>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.</p>
</div>
</div>
</div>
</div>
</section>
you can use transform to move the image around and z-index to change order
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.equal_padding {
padding: 100px 0;
}
.service_content1 {
display: flex;
}
.service_img_col {
width: 50%;
flex: 1;
/* translate to move it around */
transform: translate(-50px, 50px);
}
.service_img_col img {
width: 100%;
}
.service_box {
flex: 1;
background-color: #fff;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.18);
box-sizing: border-box;
padding: 40px;
/* z-index to bring text to front */
position:relative;
z-index: 2;
}
/* jsut using nth-child to target the 2nd version */
.equal_padding:nth-child(even) .service_img_col {
transform: translate(50px, -50px);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section>
<div class="container">
<div class="row">
<div class="equal_padding clearfix service_content1">
<div class="service_box set_white_box">
<p>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.</p>
</div>
<div class="service_img_col"><img src="https://images.pexels.com/photos/1097768/pexels-photo-1097768.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></div>
</div>
<div class="equal_padding clearfix service_content1">
<div class="service_img_col"><img src="https://images.pexels.com/photos/1097768/pexels-photo-1097768.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></div>
<div class="service_box set_white_box">
<p>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.</p>
</div>
</div>
</div>
</div>
</section>
Please try following code. also you can add multiple box no change design and looking good with left & right side align
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.equal_padding {
padding: 100px 0;
}
.equal_padding:nth-child(2n + 1) .service_img_col {
position: relative;
left: -50px;
top: 30px;
}
.equal_padding:nth-child(2n + 2) .service_img_col {
position: relative;
right: -50px;
top: -30px;
}
.equal_padding:nth-child(2n + 1) {
padding-left: 50px;
}
.equal_padding:nth-child(2n + 2) {
padding-right: 50px;
}
.service_content1 {
display: flex;
}
.service_img_col {
width: 50%;
flex: 1;
border: 4px solid #000;
}
.service_img_col img {
width: 100%;
}
.service_box {
flex: 1;
background-color: #fff;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.18);
box-sizing: border-box;
padding: 40px;
position: relative;
z-index: 9;
border: 4px solid #000;
}
<section>
<div class="container">
<div class="row">
<div class="equal_padding clearfix service_content1">
<div class="service_box set_white_box">
<p>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.</p>
</div>
<div class="service_img_col"><img src="https://images.pexels.com/photos/1097768/pexels-photo-1097768.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></div>
</div>
<div class="equal_padding clearfix service_content1">
<div class="service_img_col"><img src="https://images.pexels.com/photos/1097768/pexels-photo-1097768.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></div>
<div class="service_box set_white_box">
<p>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.</p>
</div>
</div>
</div>
</div>
</section>
I've got this container with 2 elements inside: http://jsfiddle.net/scQa2/1/ (JSFiddle doesn't seem to center properly so it's best to copy and paste the code)
test.html
<div id="main">
<img src="http://images.fanpop.com/images/image_uploads/Flower-Wallpaper-flowers-249402_1024_768.jpg" id="image"/>
<div id="text">
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>
test.css
#main {
width: 410px;
margin: auto;
}
#image {
max-width: 200px;
width: 100%;
float: left;
border: 1px solid blue;
}
#text {
max-width: 200px;
width: 100%;
float: left;
border: 1px solid red;
}
What I am to do is have the contents align in the centre of the container, rather than have the container centred as since the two elements are both using max-width.
If I set the margin of the container to auto and set it to a specific width (say 410px, just enough for the 2 max-widths of 200px) , I get this:
But if the child elements shrink below the max-width they do not align as the container has not changed width:
Is there a way I can ensure that the two child elements are centred horizontally at all times, preferably without JavaScript and with just pure CSS/HTML?
Try this, hope its what you're after...
#main {
border: 1px solid red;
display: block;
width: 90%;
margin: 0 auto;
text-align: center;
}
.image{
vertical-align: top;
border: 1px solid blue;
display: inline-block;
}
.image img {
max-width: 200px;
}
#text {
vertical-align: top;
max-width: 200px;
border: 1px solid red;
display: inline-block;
}
html
<div id="main">
<p class="image">
<img src="http://images.fanpop.com/images/image_uploads/Flower-Wallpaper-flowers-249402_1024_768.jpg"/>
</p>
<p id="text">
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.
</p>
</div>