2

I tried to create a card in pure CSS using the grid. I divided the card into 4 rows. In the last row, I included a button, and I wanted the button background color to fill up the entire 4th row. But it doesn't fill up when I put background-color:#F25F5C. If I tried to increase the width (by adding grid or inline-block display to the button class), the entire grid acts weird (I've attached the screenshot of that). And even overflow: hidden doesn't work. What should I do?

.cards {
  display: grid;
  grid-template-rows: 3fr 1fr 1fr 1fr;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 200px;
  height: auto;
  border: 1px solid #fff;
  background: #afafaf;
  border-radius: 15px;
}

.cards img {
  width: 100px;
  height: 100px;
  border-radius: 100px;
}

.btn-book {
  background: #F25F5C;
  color: #fff;
}
<div class="cards">
  <img src="Resources/Images/dsc0060.jpg" alt="paris-image" class="image">
  <h4>PARIS</h4>
  <p>$500/4 days</p>
  <a class="btn-book" href="#">Book Now</a>
</div>

Screenshot of when I put width:

Screenshot

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Depak
  • 147
  • 6
  • You need to add the bottom radius to the button. Width should be 2 pixels less than grid for border. set btn-book: width: 198px and border-radius:0 0 15px 15px – Greg Apr 17 '20 at 17:32

2 Answers2

0

Make two adjustments to your grid container:

  1. Remove justify-content: center.

  2. Remove align-items: center.

Then manage the centering of content at the grid item level. Here are the details:

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

use

display: block; margin-left: auto; margin-right: auto; for you the img tag.

.cards {
  display: grid;  
  grid-template-rows: 3fr 1fr 1fr 1fr;
  align-items: center;
  text-align: center;
  width: 200px;
  height: auto;
  border: 1px solid #fff;
  background: #afafaf;
  border-radius: 15px; 
}

.cards .image {
  width: 100px;
  height: 100px;
  border-radius: 100px;
  display: block;
  margin-left: auto;
  margin-right: auto; 
}

.btn-book {
  background: #F25F5C;
  color: #fff;
}
<div class="cards">

  <img src="http://placekitten.com/301/301" alt="paris-image" class="image">
  <h4>PARIS</h4>
  <p>$500/4 days</p>
  <a class="btn-book" href="#">Book Now</a>
</div>
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • Hey, just one small doubt, my card height increases only if I include padding in .btn-book class, and also how did you manage to get the button width taken fully, mine have a little gap left until I remove the border property. – Depak Apr 19 '20 at 14:52
  • 1
    Based the snippet, I was able to fix those. I am not sure how it is behaving there! padding to `.btn-book` has an impact on the height and width of the card and the `btn` size., about the `height` of the card can be increased or decreased using `.cards{..}` `height` property, currently it has been set to `auto`; – Manjuboyz Apr 19 '20 at 15:21