If you're adamant about using a solely CSS solution, consider employing floats and relative width units or percentages.
Below is a straightforward structure that doesn't rely heavily on code or frameworks like Bootstrap.
- Enclose the entire configuration within a div with the class "wrapper"
- Subsequently...group every two elements with the class "square" in a horizontal row specified by the class "row"
- Then...utilize nth-of-type selectors to adjust offsets accordingly, based on whether the element is the first or second one inside the "row" div.
Each "row" div will consistently occupy an entire line
All elements within the "row" are limited to 50% maximum width...resize your screen to see the effect.
See the Functional Demo:
* {
box-sizing: border-box;
}
body {
background: grey
}
.wrapper {
width: 100%;
margin: 12% auto;
}
img {
display: block;
max-width: 100%;
}
.square {
background: black;
width: 49%;
text-align: center;
display: inline-block;
color:white;
}
.row {
display: block;
width: 80%;
margin:0 auto;
}
.row .square:nth-of-type(1) {
float: left;
}
.row:not(:nth-of-type(1)) .square:nth-of-type(1) {
margin-top: 12%;
}
.row .square:nth-of-type(2) {
margin-top: 12%;
float: right;
}
<div class="wrapper">
<div class="row">
<div class="square"><img src="http://via.placeholder.com/1000x1000?text=one">Line for element 1</div>
<div class="square"><img src="http://via.placeholder.com/1000x1000?text=two">Line for element 2</div>
</div>
<div class="row">
<div class="square"><img src="http://via.placeholder.com/1000x1000?text=three">Line for element 3</div>
<div class="square"><img src="http://via.placeholder.com/1000x1000?text=four">Line for element 4</div>
</div>
</div>
This presents my method of utilizing pure CSS for this task.
However, here's how I would prefer to approach it.
I'd opt to integrate Masonry into the project. Despite its possible limitations for this specific use case, it significantly enhances design versatility.
[...] a JavaScript grid layout library. It works by placing elements in optimal position based on available vertical space, sort of like a mason fitting stones in a wall. You’ve probably seen it in use all over the Internet.
I understand your preference to avoid external libraries, but...
You can implement Masonry without jQuery:
Masonry v3 supports vanilla JS, without requiring jQuery.
Additionally, the minified library size is under 8kb, accessible at:
https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="640905170b0a161d4908051d0b111024504a564a54">[email protected]</a>/dist/masonry.pkgd.min.js
Considering this, utilizing Masonry might be beneficial.
Furthermore, you can initiate Masonry through HTML alone. For example:
<div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 200 }'>
Following these steps should yield the following outcome...
Note: (images may load slowly)
* {
box-sizing: border-box;
}
body {
text-align: center;
background: #131418;
}
.grid {
max-width: 100%;
margin: 0 auto;
width: 95vw;
}
.grid-item {
width: 50%;
background: white;
border: 2px #131418 solid;
}
.grid-item img {
width: 100%;
height: auto;
}
.grid-item p {
font-size: 16px;
padding: 2px 15px;
text-align: justify
}
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
<div class="grid" data-masonry='{ "itemSelector": ".grid-item"}'>
<div class="grid-item"><img src="//unsplash.it/501/501" />
<p>Lorem ipsum dolor sit amet, et equidem suscipit delicata eos. Simul scribentur delicatissimi te eam. Ius ea ullum dolorum disputando, pri putent definitiones et. Eos odio deleniti euripidis te, mea ex iudico praesent, et vix accusata percipitur. Ei
nec quod lorem constituto. Eius intellegebat ad vis. Erat adipiscing conclusionemque mea at, omnesque cotidieque per ea. Oblique philosophia eos id, vel causae appellantur dissentiunt ad. Utroque corpora his an, suas ullamcorper nam ut. Nec iuvaret
facilisi definiebas ea, ad omnium utamur ius.</p>
</div>
<div class="grid-item"><img src="//unsplash.it/511/511" />
<p>Lorem ipsum dolor sit amet, sit prima oporteat ad, ad ridens tamquam his. Docendi platonem mea te, inani mentitum eum cu, in lorem eripuit debitis eum. Nam ei novum causae cotidieque, placerat similique at per, no his stet ferri animal. An option
omittam invidunt vim. Dico aperiam cum te, his ei dicit civibus praesent, sed no salutandi hendrerit. Eam te inani verterem honestatis, id sit choro utroque pertinax. Sit cibo duis laoreet et, ea adhuc aliquam eum, mea paulo nostrum atomorum eu.
Amet habeo semper ex vix, qui ea quas minimum. His at diam quaestio, ut hinc vocent principes eum. Ius sint movet labitur ea, per ad dignissim efficiendi. Idque assum vix at, diceret bonorum impedit eum ei, eos an duis decore deleniti. Duo dictas
conceptam te.</p>
</div>
// Additional grid items go here
</div>
For another illustration featuring generic placeholders, where each item spans 50% width and boasts dynamic height, refer below. This variant also features the vertical offset similar to your example image.
* {
box-sizing: border-box;
}
body {
text-align: center;
background: #131418;
}
.grid {
max-width: 100%;
margin: 0 auto;
width: 95vw;
}
.grid-item {
width: 50%;
background: white;
border: 2px #131418 solid;
}
.grid-item img {
width: 100%;
height: auto;
}
.grid-item p {
font-size: 16px;
padding: 2px 15px;
text-align: justify
}
.grid-item:nth-of-type(2) {
margin-top: 10em
}
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
<div class="grid" data-masonry='{ "itemSelector": ".grid-item"}'>
<div class="grid-item"><img src="//via.placeholder.com/400x400" />
<p>Lorem ipsum dolor sit amet, et equidem suscipit delicata eos. Simul scribentur delicatissimi te eam. Ius ea ullum dolorum disputando, pri putent definitiones et. Eos odio deleniti euripidis te, mea ex iudico praesent, et vix accusata percipitur. Ei
nec quod lorem constituto. Eius intellegebat ad vis. Erat adipiscing conclusionemque mea at, omnesque cotidieque per ea. Oblique philosophia eos id, vel causae appellantur dissentiunt ad. Utroque corpora his an, suas ullamcorper nam ut. Nec iuvaret
facilisi definiebas ea, ad omnium utamur ius.</p>
</div>
<div class="grid-item"><img src="//via.placeholder.com/500x500" />
<p>Lorem ipsum dolor sit amet, sit prima oporteat ad, ad ridens tamquam his. Docendi platonem mea te, inani mentitum eum cu, in lorem eripuit debitis eum. Nam ei novum causae cotidieque, placerat similique at per, no his stet ferri animal. An option
omittam invidunt vim. Dico aperiam cum te, his ei dicit civibus praesent, sed no salutandi hendrerit. Eam te inani verterem honestatis, id sit choro utroque pertinax. Sit cibo duis laoreet et, ea adhuc aliquam eum, mea paulo nostrum atomorum eu.
Amet habeo semper ex vix, qui ea quas minimum. His at diam quaestio, ut hinc vocent principes eum. Ius sint movet labitur ea, per ad dignissim efficiendi. Idque assum vix at, diceret bonorum impedit eum ei, eos an duis decore deleniti. Duo dictas
conceptam te.</p>
</div>
// More grid items go here
</div>
Just a heads-up: Angular offers a similar tool, although I lack personal experience with it.