Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

HTML-CSS: Masonry Layout

HTML-CSS : Exercise-40 with Solution

Using HTML, CSS creates a masonry-style layout that is especially useful when working with images.

  • Create a masonry-style layout that consists of "bricks" that fall into each other with either a fixed width (vertical layout) or a fixed height (horizontal layout), forming a perfect fit. Especially useful when working with images.
  • Define .masonry-container This is the container for the masonry layout and .masonry-columns, an inner container in which .masonry-brick elements will be placed.
  • Apply display: block to .masonry-brick elements to allow the layout to flow properly.
  • Use the :first-child pseudo-element selector to apply a different margin for the first element to account for its positioning.
  • Use CSS variables and media queries for greater flexibility and responsiveness.

HTML Code:

<!--License: https://bit.ly/3GjrtVF-->
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Using HTML, CSS creates a masonry-style layout that is especially useful when working with images</title>
</head>
<body>
<div class="masonry-container">
  <div class="masonry-columns">
    <img
      class="masonry-brick"
      src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-10.jpeg"
      alt="An image"
    />
    <img
      class="masonry-brick"
      src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-11.jpeg"
      alt="Another image"
    />
    <img
      class="masonry-brick"
      src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-12.jpeg"
      alt="Another image"
    />
    <img
      class="masonry-brick"
      src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-13.jpeg"
      alt="One more image"
    />
    <img
      class="masonry-brick"
      src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-14.jpeg"
      alt="And another one"
    />
    <img
      class="masonry-brick"
      src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-15.jpeg"
      alt="Last one"
    />
  </div>
</div>
</body>
</html>

CSS Code:

/* Container */
.masonry-container {
  --column-count-small: 1;
  --column-count-medium: 2;
  --column-count-large: 3;
  --column-gap: 0.125rem;
  padding: var(--column-gap);
}

/* Columns */
.masonry-columns {
  column-gap: var(--column-gap);
  column-count: var(--column-count-small);
  column-width: calc(1 / var(--column-count-small) * 100%);
}

@media only screen and (min-width: 640px) {
  .masonry-columns {
    column-count: var(--column-count-medium);
    column-width: calc(1 / var(--column-count-medium) * 100%);
  }
}

@media only screen and (min-width: 800px) {
  .masonry-columns {
    column-count: var(--column-count-large);
    column-width: calc(1 / var(--column-count-large) * 100%);
  }
}

/* Bricks */
.masonry-brick {
  width: 100%;
  height: auto;
  margin: var(--column-gap) 0;
  display: block;
}

.masonry-brick:first-child {
  margin: 0 0 var(--column-gap);
}

HTML-CSS Editor:

See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



HTML-CSS: Tips of the Day

What is the difference between id and class in CSS, and when should I use them?

Example:

<div id="header_id" class="header_class">
Text
</div>
#header_id {font-color:#fff}
.header_class {font-color:#000}

(Note that CSS uses the prefix # for IDs and . for Classes.)

However color was an HTML 4.01 <font> tag attribute deprecated in HTML 5. In CSS there is no "font-color", the style is color so the above should read:

Example:

<div id="header_id" class="header_class">
Text
</div>
#header_id {color:#fff}
.header_class {color:#000} 

The text would be white.

Ref: https://bit.ly/39Stv4P