반응형 웹 개발을 위한 최고의 가이드

Susan Sarandon
풀어 주다: 2024-10-05 06:18:02
원래의
541명이 탐색했습니다.

The Ultimate Guide to Responsive Web Development

소개

디자인을 유연하게 유지하고 모든 기기에서 멋지게 보이도록 하는 방법은 다음과 같습니다. 반응형 웹 애플리케이션을 만들 때 고려해야 할 주요 사항을 살펴보겠습니다.

CSS 단위

CSS는 다양한 단위를 제공하므로 때로는 올바른 단위를 선택하는 것이 혼란스러울 수 있습니다.

  • px: 픽셀 단위는 화면 크기에 관계없이 동일하게 유지됩니다.
  • %: 백분율 기반 단위는 상위 요소의 크기를 기준으로 합니다.
  • vw 및 vh: 뷰포트 너비/높이 기반 단위는 뷰포트 너비/높이를 기준으로 합니다.
  • dvw 및 dvh: 동적 뷰포트 너비 및 높이 단위는 vw 및 vh와 유사하지만 화면 키보드가 나타날 때 뷰포트의 변경 사항에 따라 동적으로 조정됩니다.
  • rem: 루트 요소의 글꼴 크기를 기준으로 크기 조정을 위한 일관된 기반을 제공합니다.
  • em: 현재 요소의 글꼴 크기를 기준으로 하며 구성 요소 내에서 크기를 조정하는 데 유용합니다.
  • calc(): 다양한 단위로 계산을 수행할 수 있는 함수입니다. 예를 들어 calc(100% - 20px)는 상대 단위와 절대 단위를 혼합하는 데 도움이 될 수 있습니다.

CSS 단위의 전체 목록(다수는 거의 사용되지 않음)을 보려면 이 MDN 웹 문서 페이지를 확인하세요.


반응형 이미지

웹에서 이미지 반응성을 향상시킬 수 있는 몇 가지 방법이 있습니다.

너비 및 높이 사용: 자동

최대 너비 제한과 높이를 자동으로 설정하면 이미지의 가로 세로 비율을 변경하지 않고도 반응형 이미지를 만들 수 있습니다.


img {
    width: 100%; /* or any fixed width */
    height: auto; 
  }


로그인 후 복사

이미지 크기를 줄이려면 width 대신 max-width를 사용하세요. 단, 원래 크기보다 크게 늘리지는 마세요.

img 요소 및 srcset 사용

다른 뷰포트 크기나 해상도에 대해 동일한 이미지의 다른 버전이 필요한 경우 어떻게 해야 합니까? srcset 속성이 있는 태그를 사용하면 브라우저가 장치에 가장 적합한 이미지를 자동으로 선택할 수 있습니다.
버전이 다르다는 것은 동일한 파일을 의미하는 것이 아니라 다양한 장치에 맞게 조정(예: 크기 조정, 압축)된 이미지를 의미한다는 점에 유의하세요.


<img src="small.jpg" 
     srcset="small.jpg 600w, medium.jpg 1200w, large.jpg 2000w"
     sizes="(max-width: 600px) 100vw, 
            (min-width: 601px) and (max-width: 1200px) 75vw, 
            (min-width: 1201px) 50vw" 
     alt="Example Image">


로그인 후 복사
  • 뷰포트 너비가 600px 이하인 경우 브라우저는 너비가 100vw인 small.jpg를 사용합니다. 브라우저는 또한 small.jpg를 대체 이미지로 사용합니다.
  • 뷰포트 너비가 601px에서 1200px 사이인 경우 브라우저는 너비가 75vw인 Medium.jpg를 사용합니다.
  • 뷰포트 너비가 1200px을 초과하는 경우 브라우저는 너비가 50vw인 Large.jpg를 사용합니다.

그림 요소 및 srcset 사용

뷰포트 크기나 해상도에 따라 다른 이미지가 필요한 경우 어떻게 해야 합니까? <사진> srcset 속성이 있는 요소를 사용하면 다양한 뷰포트 크기나 해상도에 대해 다양한 이미지를 정의할 수 있습니다.


<picture>
  <source srcset="small.jpg" media="(max-width: 600px)">
  <source srcset="medium.jpg" media="(max-width: 1200px)">
  <img src="large.jpg" alt="Example image">
</picture>


로그인 후 복사

이 예에서는:

  • 뷰포트 너비가 600px 이하인 경우 브라우저는 small.jpg를 사용합니다.
  • 뷰포트 너비가 601px에서 1200px 사이인 경우 브라우저는 Medium.jpg를 사용합니다.
  • 뷰포트 너비가 1200px을 초과하는 경우 브라우저는 Large.jpg를 사용합니다.

반응형 타이포그래피

상대 단위


html {
    font-size: 16px;
}
.parent {
    font-size: 2rem; /* 32px (2 * 16px) */
}
.child {
    font-size: 0.5em; /* 16px (0.5 * 32px) */
}


로그인 후 복사
  • em 단위는 상위 요소의 글꼴 크기를 기준으로 합니다. 위의 예에서 하위 클래스의 글꼴 크기는 16px입니다. 이는 상위 요소 글꼴 크기인 32px의 절반이기 때문입니다.

  • rem 단위는 루트 요소의 글꼴 크기(html)에 상대적입니다. 위의 예에서 상위 클래스의 글꼴 크기는 32px입니다. 이는 루트 글꼴 크기인 16px의 두 배이기 때문입니다.

뷰포트 기반 단위


h1 {
  font-size: 5vw;
}
h2 {
  font-size: 5vh;
}


로그인 후 복사
  • vw 단위는 뷰포트 너비에 상대적입니다.
  • vh 단위는 뷰포트 높이에 상대적입니다.

클램프 기능

상대 또는 뷰포트 기반 단위를 사용해야 하지만 최소 및 최대 제한 내에서 사용해야 하는 경우 어떻게 해야 합니까?
예를 들어, 글꼴 크기를 뷰포트 너비에 상대적으로 설정하고 싶지만 최소값은 12px, 최대값은 48px이어야 합니다. 클램프 기능은 이러한 시나리오에 이상적인 선택입니다.


h1 {
  font-size: clamp(12px, 3vw, 48px);
}


로그인 후 복사

반응형 레이아웃

플렉스박스

주로 1차원 레이아웃으로 항목을 정렬해야 한다면 어떻게 해야 할까요? (행 또는 열)


<div class="card-container">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
</div>


로그인 후 복사
로그인 후 복사

.card-container {
  display: flex; /* Enable flexbox layout */
  justify-content: space-around; /* Space evenly between and around cards */
}
.card {
  background-color: black;
  border: 1px solid white;
  color: white;
  text-align: center;
  padding: 20px;
}


로그인 후 복사

여기서 Flexbox에 대해 자세히 알아보세요

그리드

주로 2차원 레이아웃으로 항목을 정렬해야 한다면 어떻게 해야 할까요? (행 및 열)


<div class="card-container">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
  <div class="card">Card 4</div>
</div>


로그인 후 복사

.card-container {
  display: grid; /* Enable grid layout */
  grid-template-columns: 1fr 1fr; /* Two equal columns */
  grid-template-rows: 1fr 1fr; /* Two equal rows */
  gap: 10px; /* Space between grid items */
  width: 100%; /* Full width of the container */
}
.card {
  background-color: black;
  border: 1px solid white;
  color: white;
  text-align: center;
  padding: 20px;
}


로그인 후 복사

여기에서 그리드에 대해 자세히 알아보세요


미디어 쿼리

기기가 특정 조건을 만족할 때 특정 스타일을 적용하고 싶다면 어떻게 해야 할까요? 가장 일반적으로 이러한 조건은 장치의 너비와 관련이 있습니다.


<div class="card-container">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
</div>


로그인 후 복사
로그인 후 복사

.card-container {
  display: flex;
  flex-direction: column; /* Default: single-column layout */
}

/* Media query for tablet devices (min-width: 768px) */
@media (min-width: 768px) {
  .card-container {
    flex-direction: row; /* Change to two-column layout */
  }
  .card {
    flex: 1; /* Equal width for all cards */
  }
}

/* Media query for desktop devices (min-width: 1024px) */
@media (min-width: 1024px) {
  .card {
    flex: 25%; /* Each card takes 25% of the width */
  }
}


로그인 후 복사

Media queries can also be used with other characteristics, such as height, orientation, aspect-ratio, resolution, pointer, prefers-color-scheme, and display-mode.

Viewport Meta Tag


<meta name="viewport" content="width=device-width, initial-scale=1.0">


로그인 후 복사

This tag is responsible for giving instructions to the browser on how to control the page's dimensions and scaling. The width=device-width part sets the width of the page to follow the screen width and initial-scale=1.0, which controls the zoom level when the page is first loaded.

Responsive frameworks and component libraries

If you don't want to reinvent the wheel, there are many responsive frameworks available to save time and effort.

  • Bootstrap: A widely used framework with pre-designed components for quick responsive site development.

  • Tailwind CSS: A utility-first framework that enables fast custom design with utility classes.

  • MUI: A React library based on Material Design, offering responsive pre-styled components.

  • ShadCN: Focuses on modern, accessible, and customizable responsive components.

  • Ant Design: A comprehensive design system by Alibaba for enterprise applications, featuring responsive components.

Designing with a Mobile-First Approach

A mobile-first approach means starting with the design for smaller screens, like smartphones, and then gradually adding more features for larger screens, like tablets and desktops. This way, we ensure that the basic experience works great on mobile, and then you build on that for bigger devices.


.card-container { /* default code is for mobile view */
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
  padding: 20px;
  gap: 12px;
}

@media (min-width: 768px) { /* queries/cases are for larger views */
  .card-container {
    flex-direction: row;
    gap: 18px;
  }
}


로그인 후 복사

위 내용은 반응형 웹 개발을 위한 최고의 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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