kya
kya

Reputation: 1828

Images in a responsive grid

I ran into a problem trying to create a grid menu, but instead of words in a grid square, I would like to add an image icon.

I've created the following grid: enter image description here

Below is the HTML and CSS I used:

.wrapper {
  display: grid;
  grid-template-columns: 150px 150px 150px;
  grid-template-rows: 150px 150px 150px;
  grid-gap: 50px;
  color: #444;
}

img {
  float: inherit;
  padding: 10px;
  height: inherit;
  width: inherit;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 10px;
  padding: 20px;
  font-size: 150%;
}
<div class="wrapper">
  <div class="box a"><img src="https://lorempixel.com/400/400" /></div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
  <div class="box e">E</div>
  <div class="box f">F</div>
</div>

As shown in the picture above, adding an image with the picture does not get the icon to sit in the grid box.

I don't know much about CSS. Please assist.

Upvotes: 3

Views: 95

Answers (2)

User that hates AI
User that hates AI

Reputation: 468

You need to do this in CSS ...

.box img {
    max-width: 100%;
    width: auto;
    height: auto;
    /* @VxP stated this: */
    display: block;
}

... for this HTML:

<div class="box">
    <img src="" alt="">
</div>

I created a fiddle to demonstrate this:

.wrapper {
display: grid;
grid-template-columns: 150px 150px 150px;
grid-template-rows: 150px 150px 150px;
grid-gap: 50px;

color: #444;
}

img {
    float:inherit;
    padding:10px;
    height: inherit;
    width: inherit;
}

.box {
    background-color: #444;
    color: #fff;
    border-radius: 10px;
    padding: 20px;
    font-size: 150%;

}

/* My code */


.box img {
    max-width: 100%;
    width: auto;
    height: auto;
    /* @VxP stated this: */
    display: block;
}

* {
  box-sizing: border-box;
}
<div class="wrapper">
<div class="box a"><img src="http://lorempixel.com/400/400/" /></div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
</div>

PS: Maybe you need box-sizing: border-box for this to work 100% correct. I added this to the above snippet.

Upvotes: 2

barbarossa
barbarossa

Reputation: 129

The following css code for your image should be enough.

img {
    width: 100%;
    height: 100%;
}

Upvotes: 1

Related Questions