Aligning text always in the center of div next to image? - html

https://jsfiddle.net/joel081112/6yud4bvo/3/
I have this fiddle which shows my current situation. I cant get my text to always be aligned in the middle of the div next to the image. At the minute the text stays level with the top of the div.
I have tried vertical-aligned and top: 50% but I must be missing something easy
.homeInfo2 {
padding: 20px;
height: auto;
text-align: center;
float: left;
}
.homeIm1 {
height: auto;
float: right;
}
.item {
display: flex;
width: 100%;
}
.item:nth-child(odd) {
flex-direction: row-reverse;
}
.item > div {
width: 50%;
}
<article class="item">
<div class="homeInfo2">
some text 1
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
That code is the crux of what I'm using

Since your .item element is a flexbox set align-items: center on the item:
.item {
display: flex;
width: 100%;
align-items: center;
}
.item:nth-child(odd) {
flex-direction: row-reverse;
}
.item>div {
width: 50%;
}
.homeIm1 {
height: auto;
}
.homeInfo2 {
padding: 20px;
text-align: center;
}
img {
max-width: 100%;
height: auto;
}
#media (max-width: 500px) {
.item,
.item:nth-child(odd) {
flex-direction: column;
}
.item>div {
width: 100%;
}
}
<div class="container">
<article class="item">
<div class="homeInfo2">
some text 1
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text 2
</div>
<div class="image">
some image 2
</div>
</article>
<hr>
<article class="item">
<div class="homeInfo2">
some 1 text blah
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
</div>

Try using flex.
You have to create a div next to the image and then justify-content-center and align-items-center. For more information use google :) Especially css-tricks would be helpful! CSS-Tricks

Just add align-items: center; in your item class.
.item {
display: flex;
width: 100%;
align-items: center;
}
.item:nth-child(odd) {
flex-direction: row-reverse;
}
.item>div {
width: 50%;
}
.homeIm1 {
height: auto;
float: right;
}
.homeInfo2 {
padding: 20px;
height: auto;
text-align: center;
float: left;
}
img {
max-width: 100%;
height: auto;
}
#media (max-width: 500px) {
.item,
.item:nth-child(odd) {
flex-direction: column;
}
.item>div {
width: 100%;
}
}
<div class="container">
<article class="item">
<div class="homeInfo2">
some text 1
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text 2
</div>
<div class="image">
some image 2
</div>
</article>
<hr>
<article class="item">
<div class="homeInfo2">
some 1 text blah
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
</div>

Related

Getting flex items to horizontal scroll in flex container

I'm trying to implement horizontal scrolling of fixed width images which are wrapped in divs. The entire layout is wrapped in flex with a left right layout.
However, I'm not able to keep the parent width of the boxes from overflowing. I need the children boxes to scroll horizontally and its parent contained in a flex.
Link to fiddle: https://jsfiddle.net/wn8zd2t6/43/
.dash {
display:flex;
width:100%;
}
.left {
width: 380px;
height: 100vh;
}
.right {
flex: 1 1 0%;
display:flex;
flex-direction: column;
}
.b {
border:1px solid black;
}
.ig {
display:inline-block;
height:100px;
width:180px;
object-fit: cover;
}
.scrollable {
white-space: nowrap;
overflow-x:scroll;
}
.box {
display: inline-block;
}
<div class="dash">
<div class="left b">
left
</div>
<div class="right b">
<div>
top section
</div>
<div class="container">
<!-- necessary -->
<div>
scrollable section title
</div>
<!-- need this to be scroll -->
<div class="scrollable">
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
</div>
</div>
</div>
</div>
The flex-basis: 0% isn't enough to define a fixed width on the images container, therefore the overflow function doesn't have a break point.
Instead of flex: 1 1 0% use width: calc(100% - 380px) (the 380px being the fixed width of the other column).
This is all you need:
.right {
/* flex: 1 1 0%; */
width: calc(100% - 380px); /* new */
}
revised jsfiddle
.dash {
display: flex;
width: 100%;
}
.left {
width: 380px;
height: 100vh;
}
.right {
/* flex: 1 1 0%; */
width: calc(100% - 380px); /* new */
display: flex;
flex-direction: column;
}
.b {
border: 1px solid black;
}
.ig {
display: inline-block;
height: 100px;
width: 180px;
object-fit: cover;
}
.scrollable {
white-space: nowrap;
overflow-x: scroll;
}
.box {
display: inline-block;
}
<div class="dash">
<div class="left b">
left
</div>
<div class="right b">
<div>
top section
</div>
<div class="container">
<!-- necessary -->
<div>
scrollable section title
</div>
<!-- need this to be scroll -->
<div class="scrollable">
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
</div>
</div>
</div>
</div>
I simplified your flex code into the following. I added border styling to the image divs for visual clarity of what's going on.
It's important to remember that to constrain a child element's width to a parent with flex view, the parent flex container must have a constraint on it's width, even if it's width: 100%;
You can see this within the .right class and that controls how the rest of that column's children behave with their flex-grow.
/* Utility */
.border-settings {
border: 1px solid black;
}
.scrollable {
display: flex;
flex-direction: row;
overflow-x: scroll;
padding: 10px;
}
/* Container Settings */
.dashboard {
display: flex;
flex-direction: row;
width: 100%;
height: 100vh;
}
.left {
display: flex;
flex-direction: column;
flex-grow: 1;
}
.right {
display: flex;
flex-direction: column;
flex-grow: 1;
height: 100%;
max-width: 50%;
}
.container {
display: flex;
flex-direction: column;
flex-grow: 1;
}
.card {
margin: 10px;
padding: 10px;
border-style: solid;
border-color: black;
border-width: 1px;
border-radius: 10px;
}
/* Element Settings */
img {
height: 100px;
width: 180px;
}
<div class="dashboard">
<div class="left border-settings">
left Section
</div>
<div class="right border-settings">
<div class="top">
top section
</div>
<div class="container">
<!-- necessary -->
<div>
scrollable section title
</div>
<!-- need this to be scroll -->
<div class="scrollable">
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
</div>
</div>
</div>
</div>

Container with several boxes and centered items

I am struggling with my code. I need 8 boxes (2 columns with 4 rows) and the content shall be centered (horizontally and vertically). The content will be either an image or a text. The boxes shall have 50% of the width and there shall be space between the two boxes on one line. I tried different ways, but I wasn't successful.
My current code looks like this:
.desc {
display: grid;
grid-template-columns: 50% 50%;
padding-bottom: 30px;
text-align: center;
}
.image {
height: 200px;
max-height: 300px;
max-width: 500px;
object-fit: cover;
object-position: center;
}
.text {
text-align: center;
vertical-align: middle;
}
<div class="desc">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
If you check the below example, there are 8 boxes which are aligned in 2 columns and 4 rows with contents are vertically and horizontally centred. Check it and let me know if this is what you are looking for
* {
box-sizing: border-box;
}
section {
display: flex;
flex-wrap: wrap;
border: 1px solid red;
}
div {
flex: 1;
display: inline-flex;
max-width: 50%;
min-width: 50%;
border: 1px solid #ddd;
justify-content: center;
align-items: center;
text-align: center;
}
.bg-image {
background-image: url('https://i.ytimg.com/vi/k-POG1-Cp1k/maxresdefault.jpg');
background-size: cover;
min-height: 200px;
}
<section>
<div>1</div>
<div>2</div>
<div>3sdass asd assadas asd asdasadas asd asdasdassdaasasd sd as as</div>
<div>4</div>
<div class='bg-image'>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</section>
EDIT
Have updated the snippet with an item with a background-image, the image will automatically resize.
Flex Implementation. An another way.
If you want the texts in the tesxt section to be in two seperate lines, use flex-direction: column; for .text class
.desc {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.image, .text {
display: flex;
flex: 1 1 50%;
align-items: center;
justify-content: center;
padding: 5px;
box-sizing: border-box;
border: 1px dashed black;
flex-direction: column;
}
.image img {
max-width: 100%;
}
<div class="desc">
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
.desc,
.desc_inner {
width: 99%;
overflow: hidden;
margin: 0px auto;
}
.desc_inner {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 2px dashed #f69c55;
}
.text {
width: 100%;
text-align: center;
}
<!-- today -->
<div class="desc">
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
</div>

How to put Rows And Columns in center of a Section

I'm learning HTML and I'm working on this project where basically I'm trying to show 8 pictures inside a section with 4 each in one row. To solve this problem, I looked up how to make rows and columns without using Table (I'm not allowed to for this project).
I came across this:
* {
box-sizing: border-box;
}
/* Create four equal columns that floats next to each other */
.column {
float: left;
width: 25%;
padding: 10px;
height: 300px; /* Should be removed. Only for demonstration */
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
<h2>Four Equal Columns</h2>
<div class="row">
<div class="column" style="background-color:#aaa;">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#bbb;">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#ccc;">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#ddd;">
<h2>Column 4</h2>
<p>Some text..</p>
</div>
</div>
I used this trick and made two Rows to solve my problem. I put these two Rows inside a <section> I created myself called gallery1:
.gallery1 {
align-items: center;
justify-content: center;
margin: auto;
}
Using above code, final code looks like this:
.whitetitle {
color: white;
}
.green {
background-color: #4AB19A;
}
.gallery1 {
align-items: center;
justify-content: center;
margin: auto;
}
.gallery1 img {
border: 1px solid whitesmoke;
border-radius: 90px;
margin: 15px;
height: 150px;
width: 150px;
}
.column {
align-items: center;
justify-content: center;
float: left;
display: flex;
width: 20%;
padding: 10px;
margin: auto;
}
.row:after {
content: "";
display: table;
clear: both;
align-items: center;
justify-content: center;
margin: auto;
}
<section class=" green gallery1">
<h2 class="whitetitle"> Graphic Designing </h2>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
</section>
So it turned out good and all but I have a problem! I want the pictures to be in the center, horizontally and vertically. To be exact, I want the title and two rows to be in center, both ways.
I tried the following
align-items: center;
justify-content: center;
margin: auto;
Inside the Gallery in CSS but it didn't work. can anyone help me on this please? what should I have done to fix/avoid this.
Also, I tried flex-box thing and put display: flex; inside Gallery but it shows everything (title and two rows) in one row which is not what I want.
Since you have two sections you can simply wrap them into one section and give it class called main then apply the flex-box centralization on it.
Here is an Example
.whitetitle {
color: white;
}
.main {
background-color: #4AB19A;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.green {
text-align: center;
background-color: #4AB19A;
}
.gallery1 {
align-items: center;
justify-content: center;
margin: auto;
}
.gallery1 img {
border: 1px solid whitesmoke;
border-radius: 90px;
margin: 15px;
height: 150px;
width: 150px;
}
.column {
align-items: center;
justify-content: center;
float: left;
display: flex;
width: 20%;
padding: 10px;
margin: auto;
}
.row:after {
content: "";
display: table;
clear: both;
align-items: center;
justify-content: center;
margin: auto;
}
<section class="main">
<section class=" green gallery1">
<h2 class="whitetitle"> Graphic Designing </h2>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
</section>
</section>
Or you can simply achieve it with the grid-box the following is an example
.whitetitle {
color: white;
}
.green {
background-color: #4AB19A;
display: grid;
text-align: center;
}
.gallery1 {
align-items: center;
justify-content: center;
margin: auto;
}
.gallery1 img {
border: 1px solid whitesmoke;
border-radius: 90px;
margin: 15px;
height: 150px;
width: 150px;
}
.column {
align-items: center;
justify-content: center;
float: left;
display: flex;
width: 20%;
padding: 10px;
margin: auto;
}
.row:after {
content: "";
display: table;
clear: both;
align-items: center;
justify-content: center;
margin: auto;
}
<section class=" green gallery1">
<h2 class="whitetitle"> Graphic Designing </h2>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
</section>
All lines marked with /* <-- */ are new.
.whitetitle {
color: white;
margin-left: auto; /* <-- */
margin-right: auto; /* <-- */
text-align: center; /* <-- */
}
.green {
background-color: #4AB19A;
}
.gallery1 {
align-items: center;
justify-content: center;
margin: auto;
display: flex; /* <-- */
flex-wrap: wrap; /* <-- */
align-items: center; /* <-- */
justify-content: center; /* <-- */
}
.gallery1 img {
border: 1px solid whitesmoke;
border-radius: 90px;
margin: 15px;
height: 150px;
width: 150px;
}
.column {
align-items: center;
justify-content: center;
float: left;
display: flex;
width: 20%;
padding: 10px;
margin: auto;
}
.row::after {
content: "";
display: table;
clear: both;
align-items: center;
justify-content: center;
margin: auto;
}
<section class=" green gallery1">
<h2 class="whitetitle"> Graphic Designing </h2>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
<section class="row">
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
<div class="column">
<img src="https://i.imgur.com/yX7UIXh.jpg">
</div>
</section>
</section>

"show more" button on gallery for mobile only - pure CSS

How can I create a “Show All” button after the first image in the gallery on mobile - 600px<, and have a scrollable fixed-width block (div) everywhere else?
It seems, that the only way to do it without js is to have either one or the other - either "Show more" button, or a fixed-width scrollable section.
.container {
display: flex;
flex-direction: column;
align-items: center;
margin: 0 auto;
max-width: 1200px;
padding: 0 1rem;
}
.book {
max-width: 100%;
}
.cell img {
display: block;
}
#media screen and (min-width: 600px) {
.container{
overflow: auto;
height: calc(70vh);
}
.grid {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.cell {
width: calc(50% - 2rem);
}
}
#media screen and (min-width: 1000px) {
.container{
overflow: auto;
max-height: 80vh;
}
.cell {
width: calc(33.3333% - 2rem);
}
}
.cell {
margin: 1rem;
}
<div class="container" id="books">
<div class="container">
<div class="grid">
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
<div class="cell">
<img src="http://lorempixel.com/200/300" class="book">
</div>
</div>
</div>
</div>

CSS - How to change divs order [duplicate]

This question already has an answer here:
Change div order with CSS depending on device-width
(1 answer)
Closed 3 years ago.
I have a div that shows an image and text and i load the content with a foreach and i need to invert the order div in second row of foreach.
First div shows:
Image - Text
Second div shows:
Text - Image
For example:
.content
{
width:100%;
height: 200px;
}
.content .image, .content .text
{
width:50%;
float:left;
}
.content .image img
{
width:100%;
}
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text1</span>
</div>
</div>
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text2</span>
</div>
</div>
Thank you
You can do this with at least 2 options
1. using flexbox see below
.content {
width: 100%;
height: 200px;
display: flex;
}
.content .image,
.content .text {
width: 50%;
float: left;
}
.content .image img {
width: 100%;
}
.content:nth-child(even) {
flex-direction: row-reverse
}
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text1</span>
</div>
</div>
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text2</span>
</div>
</div>
2. using float:right see below
.content {
width: 100%;
height: 200px;
}
.content .image,
.content .text {
width: 50%;
float: left;
}
.content .image img {
width: 100%;
}
.content:nth-child(even) .image{
float:right;
}
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text1</span>
</div>
</div>
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text2</span>
</div>
</div>