⊗mkSpRsBR 125 of 128 menu

Rebuilding Blocks via Media Queries in CSS

Let's learn how to rebuild blocks on different screen widths. For example, let's implement the following behavior:

First, let's write the HTML code for our blocks:

<div class="parent"> <div class="child">1</div> <div class="child">2</div> </div>

Now let's write styles for the parent of our blocks:

.parent { display: flex; width: 90%; margin: 50px auto; border: 1px solid red; }

Now let's write styles for the blocks themselves, without specifying the properties that will be changed by media queries:

.child { box-sizing: border-box; padding: 20px; border: 1px solid green; }

On large screen widths, let the gap between blocks be calculated automatically:

@media (min-width: 500px) { .parent { justify-content: space-between; } }

Let's set the width of our blocks to slightly less than 50% to leave some space for indentation:

@media (min-width: 500px) { .child { width: 49.5%; } }

On a small screen width, we'll place our blocks in a column:

@media (max-width: 500px) { .parent { flex-direction: column; } }

Let's give them a bottom indent:

@media (max-width: 500px) { .child { margin-bottom: 10px; } }

Let's put it all together and get the following code:

.parent { display: flex; width: 90%; margin: 50px auto; } .child { box-sizing: border-box; padding: 20px; border: 1px solid green; } @media (max-width: 500px) { .parent { flex-direction: column; } .child { margin-bottom: 10px; } } @media (min-width: 500px) { .parent { justify-content: space-between; } .child { width: 49.5%; } }

Repeat the example below:

Repeat the example below:

Repeat the example below:

Repeat the example below:

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline