최신 CSS를 사용하여 반응형, 동일 높이 카드 구축(Flexbox의 마법 및 미디어 쿼리 없음)

WBOY
풀어 주다: 2024-07-23 13:43:51
원래의
818명이 탐색했습니다.

Building Responsive, Equal-Height Cards with Modern CSS (Magic of Flexbox & No Media Queries)

목차

소개

우리의 목표는 무엇인가요?

시맨틱 HTML5를 사용하여 구조 구축

최신 CSS를 사용하여 스타일 추가
- CSS 재설정
- Flexbox를 이용한 카드 레이아웃 디자인
- 카드 이미지 스타일링
- 카드 콘텐츠 스타일링
- 카드 버튼 스타일링
- 호버 전환 추가
- CSS 변수 사용

결론


소개

웹 개발자로서 우리는 카드 구성 요소를 만들어야 하는 경우가 종종 있습니다. 제품/프로젝트 쇼케이스, 사용자 프로필, 블로그 게시물 등 어디에나 카드가 있습니다.

과거에는 반응형 레이아웃을 만드는 것이 어려웠습니다. 현대 CSS 기술, 특히 CSS Flexbox의 출현으로 인해 이러한 디자인을 만드는 것이 훨씬 더 간단하고 직관적이 되었습니다.

Flexbox는 반응형 레이아웃을 만드는 과정을 단순화합니다. 복잡한 미디어 쿼리를 사용하지 않고도 컨테이너의 항목을 쉽게 배열, 정렬 및 간격을 둘 수 있습니다. 즉, 정확한 중단점을 지정하지 않고도 다양한 화면 크기와 방향에 맞게 아름답게 조정되는 레이아웃을 구축할 수 있습니다.

우리의 목표는 무엇입니까?

CSS Flexbox를 사용하여 중단점에 의존하지 않고 동일한 높이의 반응형 카드를 만드는 것이 목표입니다. 콘텐츠 길이에 관계없이 각 카드가 동일한 높이를 유지하여 다양한 화면 크기에 원활하게 적응할 수 있도록 하겠습니다.

레이아웃의 주요 CSS 속성:

  • 디스플레이: 플렉스
  • 항목 정렬
  • 내용 정당화
  • 플렉스 성장

이제 카드를 만들어 CSS Flexbox의 마법을 탐험해 보겠습니다!

시맨틱 HTML5를 사용하여 구조 구축

<main class="card-container">
<!--   First card -->
  <article class="card">
    <img src="https://placehold.co/320x240" width="320" height="240" alt="Replace placeholder image here" class="card-image" loading="lazy">
    <h2 class="card-title">Title one</h2>
    <p class="card-description">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quod, aliquid ex vel labore fugit dignissimos libero eos hic, fuga, vitae consequuntur quidem.</p>
    <button class="card-button" aria-label="Read more about Title one">Read More</button>
  </article>
  <!--   Second card -->
  <article class="card">
    <img src="https://placehold.co/320x240" width="320" height="240"  alt="Replace placeholder image here" class="card-image" loading="lazy">
    <h2 class="card-title">Title two</h2>
    <p class="card-description">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Magnam aperiam consequuntur, saepe at repellat nobis.</p>
    <button class="card-button" aria-label="Read more about Title two">Read More</button>
  </article>
  <!--   Third card -->
  <article class="card">
    <img src="https://placehold.co/320x240" width="320" height="240"  alt="Replace placeholder image here" class="card-image" loading="lazy">
    <h2 class="card-title">Title three</h2>
    <p class="card-description">Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolorum reprehenderit at cumque? Architecto numquam nam placeat suscipit!</p>
    <button class="card-button" aria-label="Read more about Title three">Read More</button>
  </article>
</main>

로그인 후 복사

최신 CSS를 사용하여 스타일 추가

CSS 재설정

/* Basic CSS Reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
로그인 후 복사

/* Ensure that our layout is centred horizontally and vertically on the page */
body {
    display: flex; /* using CSS flexbox to vertically and horizontally centre all cards */
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    overflow-x: hidden; /* Prevent horizontal scrolling */
}
로그인 후 복사

Flexbox를 사용하여 카드 레이아웃 디자인

/* Cards */
.card-container {
    display: flex; /* using CSS flexbox to display each card at the centre */
    justify-content: center;
    align-items: stretch; /* use stretch for equal height of all cards */
    gap: 1.5625rem; /* add space between each card */
    flex-wrap: wrap;
    padding: 1rem; 
    max-width: 100%; /* Prevent container from exceeding viewport width */
}

.card {
    display: flex;
    flex-direction: column;
    width: 20rem;
    background-color: #fff;
    box-shadow: 0 0.25rem 0.5rem rgba(0, 0, 0, 0.4);
    text-align: center;
    text-wrap: balance; /* ensures content is evenly distributed across multiple lines for better readability. */
    overflow: hidden;
}

로그인 후 복사

카드 내부 콘텐츠 스타일 지정

카드 이미지 스타일링

.card-image {
    width: 100%;
    height: auto;
    object-fit: cover;
    margin-bottom: 0.85rem;
}
로그인 후 복사

카드 콘텐츠 스타일 지정

.card-title {
    font-size: 1.25rem;
    padding: 1rem;
    color: #3ca69f;
}

.card-description {
    flex-grow: 1; /* allow the content to take available space, thus maintaining equal height no matter the length of the content */
    padding: 0 1rem 1rem;
    font-size: 0.975rem;
    line-height: 1.5;
}
로그인 후 복사

카드 버튼 스타일링

/* Cards button */
.card-button {
    align-self: center; /* placing the button at the center */
    margin: 1rem 0 3rem;
    padding: 0.625rem 1rem;
    font-size: 1rem;
    color: #ffffff;
    background-color: #3ca69f;
    border: none;
    border-radius: 0.3125rem;
    cursor: pointer;
}
로그인 후 복사

호버 전환 추가

.card {
    transition: 0.5s ease all;
}

.card-button {
    transition: 0.5s ease all;
}

/* cards hover effect */
.card:hover {
    background-color: #276662;
    color: #ffffff;
}

.card:hover > .card-button {
    background-color: #ffffff;
    color: #276662;
    font-weight: 700;
}

.card:hover > .card-title {
    color: #ffffff;
}

로그인 후 복사

CSS 변수 사용

/* Declare variables */
:root {
    --primary-color: #3ca69f;
    --secondary-color: #276662;
    --text-color: #ffffff;
    --shadow-color: rgba(0, 0, 0, 0.4);
    --border-radius: 0.3125rem;
    --spacing: 1rem;
    --transition-duration: 0.5s;
}
로그인 후 복사

결론

모든 것을 하나로 모으기

맨 위로 가기

위 내용은 최신 CSS를 사용하여 반응형, 동일 높이 카드 구축(Flexbox의 마법 및 미디어 쿼리 없음)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!