Although I am much in favour of using 'Flexbox Layout', my code will show you how to use the good ol' CSS column mechanism to get the result you want without hassle and FBL overhead.
I also have made your initial layout 'responsive' by introducing .row {...column-width...}
and using a bit of math (linear equation y=mx+b, MathIsFun: Equation of a Straight Line) in a CSS calc()
. If you down't want the 'responsive' behaviour, simply disable or remove .row {...column-width...}
from the final code.
For comparison (CSS custom properties vs. classes) I added a second row which additionally uses a little spacing between the columns.
The code is divided into two sections
- [MANDATORY], minimally required CSS to make it work
- [OPTIONAL], eye-candy which can be safely removed
All further explanation is inside the code so you can read as you go....
tiny update: check w3schools: CSS Selector Reference for the use of CSS selectors in the code.
Snippet
/*****************************************/
/* [MANDATORY] generic 'newspaper' style */
/*****************************************/
/*
I am using this CSS for some 'newspaper' style with varying number of columns.
With a CSS 'custom property', instead of classes, using different number of columns
become easy (a personal choice, use classes if you prefer those).
Added classes to show difference, use either/or.
*/
.row, [newspaper] {
display: block !important; /* css 'columns' won't work with flexbox */
column-fill: balance; /* default but important. Check difference with 'auto'! */
column-gap: 0; /* default is 16px, see [newspaper*=".gaps"] below */
}
.row *, [newspaper] * {
break-inside: avoid; /* keeps related content in the same column, disable to see effect */
}
.row.cols-3,
[newspaper*="3"] {
/*
Using 'column-count' and 'column-width'
1) WITHOUT 'column-width'
- the number of columns will always be 'column-count', regardless of column content.
2) WITH 'column-width'
- no-of-columns = Min('column-count', parent-width / 'column-width')
- the actual column width will become parent-width / no-of-columns
(meaning that the columns will grow to equal width to fill parent-width)
NOTE: Using a percentage (% or VW) for 'column-width' is useless as 'column-count' will
override any percentage, but with a fixed unit (e.g. px or some calc()) 'column-width'
will be used as a 'min-width' (or breakpoint).
*/
column-count: 3;
column-width: calc(10vw + 288px);
/*
Using linear equation y=mx+b with point1(320,320) point2(1920,480) => y=0.1x + 288
MathIsFun: Equation of a Straight Line (https://www.mathsisfun.com/equation_of_line.html)
Given two sizes (a min and a max) this equation calculates all values inbetween.
Net result:
- at viewport width=320px we want a column-width=320px
(yielding 1 column at vp <= 960px)
- at viewport width=1920px we want a column-width=480px
(yielding 2 columns at vp >= 960px and 3 columns at vp >= 1440px)
Check various devices with your browsers' Developer Tools
*/
/* column-width: 100%; /* TRY THIS: at any viewport size you will still get 3 columns */
}
/* add your own for different situations, optionally with alternative 'column-width' */
.row.cols-1, [newspaper*="1"] { column-count: 1 }
.row.cols-2, [newspaper*="2"] { column-count: 2 }
.row.cols-4, [newspaper*="4"] { column-count: 4 }
/* etc.... */
.column {
/* DEMO: same as 'column-width' multiplied by some-ratio (think: 'cards') */
height: calc((10vw + 288px) * 1.2);
}
.column>img {
/*
Because your images are different in size we need to make the heights equal
and let CSS default 'width: auto' determine the final width.
*/
height: 10rem; /* some required height suited for the column, any 'height <= column-width' will do */
margin: 0 auto; /* center horizontally */
}
/*******************************************************/
/* [OPTIONAL] specific 'newspaper' style and eye-candy */
/*******************************************************/
header { width: 100%; text-align: center }
[newspaper*=".gaps"] {
column-gap: .5rem; /* HTML default gap width / 2 = 8px / 16 = 0.5rem */
padding: .5rem .5rem 0; /* 0 to counteract '[newspaper]>* margin-bottom' */
}
[newspaper*=".gaps"]>* {
margin-bottom: .5rem; /* some distance when content wraps; make equal to 'column-gap' */
}
/**********************************************/
/* [OPTIONAL] personal preferred global rules */
/**********************************************/
html,body { box-sizing: border-box; width: 100%; max-width: 100% }
*::before,*::after, * { box-sizing: inherit }
body {
margin: 0; /* disable default HTML (8px) */
min-height: 100%; /* at least fill viewport */
}
p {
margin : 0; /* disable default HTML (='1em 0') */
padding: 1em 0; /* using padding instead */
}
img {
display: block; /* default 'inline', 'block' removes unwanted space below image */
object-fit: cover /* stretch/clip to fit when smaller/larger than given/claimed space */
}
/* debugging the layout: show all elements with outlines (put in <body>) */
[outlines="1"] * { outline: 1px dashed }
<body outlines="0">
<header><h1>Three Equal Columns</h1></header>
<header><h3>no gaps</h3></header>
<div class="row cols-3">
<div class="column" style="background-color: #aaa;">
<img src="https://img.icons8.com/pastel-glyph/2x/person-male.png">
<h2>Column 1</h2>
<p>Some text..Tutorial to learn more about website layouts</p>
</div>
<div class="column" style="background-color: #bbb;">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS9ZSW2Y6SmhAL8R0yD_9rZjTGvlT79DWkBBULhSg5vVoREb9r5_g&s">
<h2>Column 2</h2>
<p>Some text..Tutorial to learn more about website layouts Tutorial to learn more about website layouts</p>
</div>
<div class="column" style="background-color: #ccc;">
<img src="https://cdn0.iconfinder.com/data/icons/set-ui-app-android/32/8-512.png">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
<header><h3>with gaps</h3></header>
<div newspaper="3.gaps">
<div class="column" style="background-color: #aaa;">
<img src="https://img.icons8.com/pastel-glyph/2x/person-male.png">
<h2>Column 1</h2>
<p>Some text..Tutorial to learn more about website layouts</p>
</div>
<div class="column" style="background-color: #bbb;">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS9ZSW2Y6SmhAL8R0yD_9rZjTGvlT79DWkBBULhSg5vVoREb9r5_g&s">
<h2>Column 2</h2>
<p>Some text..Tutorial to learn more about website layouts Tutorial to learn more about website layouts</p>
</div>
<div class="column" style="background-color: #ccc;">
<img src="https://cdn0.iconfinder.com/data/icons/set-ui-app-android/32/8-512.png">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
</body>