我目前正在學習CSS網格,並且被要求通過將其分解為網格來製作這張卡片。附上了我想要製作的網格的圖片。
實際上有一個更大的網格(在CSS中稱為profile-grid),所有這些卡片都排列在其中,這個更大的網格的高度為255px。我想讓卡片和卡片內部的網格都遵循這個255px的高度,我成功地使卡片本身的高度正確,但是內部網格的高度卻沒有。內部網格本身由2行組成,第一行是圖片,高度為150px,第二行設定為1fr,但我無法讓內部網格的高度為255px。有哪個地方出錯了嗎?
https://i.stack.imgur.com/g9Eh6.png
以下是jsFiddle連結:https://jsfiddle.net/40tnwd1o/
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap'); body { font-family: roboto; } p { display: block; margin: auto; } .profile-card { margin-top: 0px; width: 150px; height: 255px; border: none; box-shadow: 0px 0px 5px 3px rgba(73, 73, 73, 0.301); } .profile-grid { display: grid; grid-template-columns: 100%; grid-template-rows: 150px 1fr; } .social-ava { width: 100%; height: 100%; background-color: gray; transition: opacity 0.15s; } .social-ava:hover { opacity: 0.8; } .social-text { height: 100%; padding: 8px; } .social-name { margin-top: 0px; cursor: pointer; transition: color 0.15s; } .social-name:hover { color: rgb(52, 98, 167); } .mutual-grid { display: grid; grid-template-columns: 20px 1fr; margin-top: 6px; align-items: center; } .mutual-pic { width: 20px; height: 20px; background-color: gray; border-radius: 10px; cursor: pointer; } .mutual-friend { font-size: 12px; color: rgb(80, 80, 80); cursor: pointer; } .mutual-friend:hover { text-decoration: underline; } .social-add { margin-top: 6px; padding: 8px 16px; border: none; border-radius: 4px; color: white; background-color: rgb(25, 118, 240); font-size: 12px; cursor: pointer; transition: opacity 0.1s; } .social-add:hover { opacity: 0.8; }
<!-- profile card start --> <div class="profile-card"> <!-- profile grid start --> <div class="profile-grid"> <!-- row 1: picture start --> <div class="image-container"> <div class="social-ava"></div> <!-- placeholder for profile picture --> </div> <!-- row 1: picture end --> <!-- row 2: info start --> <div class="social-text"> <p class="social-name"><strong>Name</strong></p> <div class="mutual-grid"> <!-- grid for mutual friends info start --> <div class="mutual-pic"></div> <!-- placeholder for mutual's profile picture --> <p class="mutual-friend">2 mutual friends</p> </div> <!-- grid for mutual friends info end --> <button class="social-add">Add Friend</button> </div> <!-- row 2: info end --> </div> <!-- profile grid end --> </div> <!-- profile card end -->
i.stack.imgur.com/g9Eh6.png 這是
profile-grid
的高度,而不是profile-card
。profile-card
上設定了255px
的高度。你需要在
profile-grid
上新增height:100%
。