카드 그리드 레이아웃
Coddy HTML 여정의 실전 프론트엔드 섹션에 포함된 레슨. 35개 중 27번째.
Card Grid는 전자상거래 사이트, 포트폴리오 또는 블로그에서 사용되는 일반적인 반응형 패턴입니다. 이 패턴은 항목(예: product, article 또는 profile)을 간격이 고르게 배치된 카드로 정렬합니다. 각 카드는 일반적으로 이미지, 제목, 일부 text, 때로는 button 또는 링크를 포함합니다.
이 레이아웃은 화면 크기에 따라 행마다 표시되는 cards의 수가 달라지도록 CSS Grid 또는 Flexbox를 사용하여 구축하는 경우가 많습니다(예: mobile에서는 1 column, tablet에서는 2~3개, desktop에서는 4개 이상).
먼저, product listing을 위한 HTML 구조를 만드세요:
<div class="product-grid">
<div class="product-card">
<img src="product1.jpg" alt="Product 1">
<h3>Product Name</h3>
<p>$19.99</p>
<button>Add to Cart</button>
</div>
<!-- 여기에 더 많은 제품 카드 -->
</div>이제 반응형으로 만들기 위해 CSS를 추가하세요:
.product-grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
/* For tablets and up */
@media (min-width: 600px) {
.product-grid {
grid-template-columns: repeat(2, 1fr);
}
}
/* For desktops */
@media (min-width: 900px) {
.product-grid {
grid-template-columns: repeat(3, 1fr);
}
}
.product-card {
border: 1px solid #ddd;
border-radius: 8px;
padding: 1rem;
}이렇게 하면 mobile에서는 단일 column, Tablet에서는 두 개의 column, Desktop 화면에서는 세 개의 column이 생성됩니다.
챌린지
쉬움여행 사진 gallery를 위한 HTML 페이지가 주어집니다. 여러분의 과제는 다양한 화면 크기에 맞게 조정되는 반응형 card Grid를 만드는 것입니다.
요구 사항:
- 레이아웃에 CSS Grid를 사용하세요.
- mobile(작은 화면)에서는 행마다 card 1개를 표시하세요.
- tablets에서는 행마다 card 2개를 표시하세요.
- desktops에서는 행마다 card 3개 이상을 표시하세요.
각 card를 다음과 같이 style 지정하세요:
- 멋진 background color
- 둥근 모서리
목표: 어떤 화면 크기에서도 멋지게 보이는 깔끔하고 현대적인 gallery Layout을 만드세요.
직접 해보기
<!DOCTYPE html>
<html>
<head>
<title>Card Grid Layout</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f9f9f9;
margin: 0;
padding: 2rem;
}
h1 {
text-align: center;
margin-bottom: 2rem;
color: #333;
}
.gallery-grid {
gap: 1.5rem;
}
.card {
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
overflow: hidden;
transition: transform 0.3s ease;
}
.card:hover {
transform: scale(1.03);
}
.card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.card h3 {
margin: 0.8rem;
color: #444;
}
.card p {
margin: 0 0.8rem 1rem;
font-size: 0.9rem;
color: #666;
}
/* Tablet: 2 columns */
@media (min-width: 600px) {
.gallery-grid {
}
}
/* Desktop: 3 columns */
@media (min-width: 900px) {
.gallery-grid {
}
}
</style>
</head>
<body>
<h1>Travel Gallery</h1>
<div class="gallery-grid">
<div class="card">
<img src="https://images.unsplash.com/photo-1507525428034-b723cf961d3e" alt="Beach">
<h3>Sunny Beach</h3>
<p>Relaxing views of golden sand and turquoise waters.</p>
</div>
<div class="card">
<img src="https://images.unsplash.com/photo-1469474968028-56623f02e42e" alt="Mountains">
<h3>Mountain Escape</h3>
<p>Fresh air and breathtaking peaks for hiking lovers.</p>
</div>
<div class="card">
<img src="https://upload.wikimedia.org/wikipedia/commons/0/05/View_of_Empire_State_Building_from_Rockefeller_Center_New_York_City_dllu.jpg" alt="City Lights">
<h3>City Lights</h3>
<p>A bustling city skyline shining bright at night.</p>
</div>
<div class="card">
<img src="https://upload.wikimedia.org/wikipedia/commons/b/be/Hutan_Tangkahan.jpg" alt="Forest">
<h3>Green Forest</h3>
<p>Peaceful trails and endless shades of green.</p>
</div>
<div class="card">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c6/Caravan_in_the_desert.jpg" alt="Desert">
<h3>Desert Adventure</h3>
<p>Golden dunes stretching far into the horizon.</p>
</div>
<div class="card">
<img src="https://images.unsplash.com/photo-1501785888041-af3ef285b470" alt="Lake">
<h3>Crystal Lake</h3>
<p>Mirror-like reflections surrounded by mountains.</p>
</div>
</div>
</body>
</html>
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
실전 프론트엔드의 모든 레슨
직접 연습해 보세요: Web 플레이그라운드