简体   繁体   English

背景颜色未填满 CSS 网格中的行

[英]Background color not filling up row in CSS Grid

I tried to create a card in pure CSS using the grid.我尝试使用网格在纯 CSS 中创建一张卡片。 I divided the card into 4 rows.我把卡片分成 4 行。 In the last row, I included a button, and I wanted the button background color to fill up the entire 4th row.在最后一行,我包含了一个按钮,我希望按钮背景颜色填满整个第 4 行。 But it doesn't fill up when I put background-color:#F25F5C .但是当我输入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.甚至overflow: hidden不起作用。 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:我放宽度时的屏幕截图:

截屏

Make two adjustments to your grid container:对您的网格容器进行两项调整:

  1. Remove justify-content: center .删除justify-content: center

  2. Remove align-items: center .移除align-items: center

Then manage the centering of content at the grid item level.然后在网格项级别管理内容的居中。 Here are the details:以下是详细信息:

use利用

display: block; margin-left: auto; margin-right: auto; for you the img tag.给你img标签。

 .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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM