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: Responsive image mosaic

HTML-CSS : Exercise-34 with Solution

Using HTML, CSS creates a responsive image mosaic.

  • Use display: grid to create an appropriate responsive grid layout.
  • Use grid-row: span 2 / auto and grid-column: span 2 / auto to create items that span two rows or two columns respectively.
  • Wrap the previous styles into a media query to avoid applying on small screen sizes.

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 responsive image mosaic.</title>
</head>
<body>
<div class="image-mosaic">
  <div
    class="card card-tall card-wide"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-1.jpeg')"
  ></div>
  <div
    class="card card-tall"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-2.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-3.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-4.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-5.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-6.jpeg')"
  ></div>
  <div
    class="card card-wide"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-1.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-2.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-3.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-4.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-5.jpeg')"
  ></div>
  <div
    class="card"
    style="background-image: url('https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-6.jpeg')"
  ></div>
</div>
</body>
</html>

CSS Code:

.image-mosaic {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  grid-auto-rows: 240px;
}

.card {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #353535;
  font-size: 3rem;
  color: #fff;
  box-shadow: rgba(3, 8, 20, 0.1) 0px 0.15rem 0.5rem, rgba(2, 8, 20, 0.1) 0px 0.075rem 0.175rem;
  height: 100%;
  width: 100%;
  border-radius: 4px;
  transition: all 500ms;
  overflow: hidden;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 0;
  margin: 0;
}

@media screen and (min-width: 600px) {
  .card-tall {
    grid-row: span 2 / auto;
  }

  .card-wide {
    grid-column: span 2 / auto;
  }
}

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