HTML 및 CSS에서 Div를 중앙에 배치하는 다양한 방법
LinkedIn에서 나를 팔로우하세요
Github.com에서 저를 팔로우하세요
클릭하여 읽기
Boaring Setion 없이도 코딩으로 방향을 바꿀 수 있습니다!
1. Flexbox 사용
Flexbox는 수평 및 수직으로 요소를 쉽게 중앙에 배치할 수 있는 강력한 레이아웃 도구입니다.
예:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Flexbox</title> <style> .container { display: flex; justify-content: center; /* Horizontal center */ align-items: center; /* Vertical center */ height: 100vh; /* Full viewport height */ } .centered-div { width: 200px; height: 200px; background-color: lightblue; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Flexbox</div> </div> </body> </html>
2. 그리드 활용하기
CSS 그리드는 요소를 쉽게 중앙에 배치할 수 있는 또 다른 강력한 레이아웃 시스템입니다.
예:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Grid</title> <style> .container { display: grid; place-items: center; /* Center both horizontally and vertically */ height: 100vh; /* Full viewport height */ } .centered-div { width: 200px; height: 200px; background-color: lightcoral; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Grid</div> </div> </body> </html>
3. 절대 위치 지정 및 변환 사용
이 방법에는 div를 절대적으로 배치하고 변환을 사용하여 가운데에 배치하는 방법이 포함됩니다.
예:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Absolute Positioning</title> <style> .container { position: relative; height: 100vh; /* Full viewport height */ } .centered-div { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 200px; height: 200px; background-color: lightgreen; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Absolute Positioning</div> </div> </body> </html>
4. 마진자동 이용하기
여백 설정: 지정된 너비를 가진 요소의 자동은 수평으로 중앙에 배치될 수 있습니다.
예:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Margin Auto</title> <style> .container { width: 100%; height: 100vh; /* Full viewport height */ display: flex; align-items: center; /* Vertical center */ } .centered-div { margin: 0 auto; /* Horizontal center */ width: 200px; height: 200px; background-color: lightcoral; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Margin Auto</div> </div> </body> </html>
5. 테이블 디스플레이 활용하기
이 방법은 요소를 중앙에 배치하기 위해 display: table 및 display: table-cell을 사용합니다.
예:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Table Display</title> <style> .container { display: table; width: 100%; height: 100vh; /* Full viewport height */ } .centered-div { display: table-cell; vertical-align: middle; /* Vertical center */ text-align: center; /* Horizontal center */ } .inner-div { display: inline-block; width: 200px; height: 200px; background-color: lightpink; } </style> </head> <body> <div class="container"> <div class="centered-div"> <div class="inner-div">Centered with Table Display</div> </div> </div> </body> </html>
바이
즐거운 코딩하세요!
위 내용은 HTML 및 CSS에서 Div를 중앙에 배치하는 다양한 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

새로운 프로젝트가 시작될 때, Sass 컴파일은 눈을 깜박이게합니다. 특히 BrowserSync와 짝을 이루는 경우 기분이 좋습니다.

이번 주에 플랫폼 뉴스 라운드 업 RONDUP, Chrome은로드에 대한 새로운 속성, 웹 개발자를위한 접근성 사양 및 BBC Move를 소개합니다.

이것은 처음으로 HTML 요소를보고 있습니다. 나는 그것을 잠시 동안 알고 있었지만 아직 스핀을 위해 그것을 가져 갔다. 그것은 꽤 시원하고 있습니다

구매 또는 빌드는 기술 분야의 고전적인 논쟁입니다. 신용 카드 청구서에 라인 항목이 없기 때문에 물건을 구축하는 것이 저렴할 수 있지만

한동안 iTunes는 팟 캐스팅에서 큰 개 였으므로 "Podcast 구독"을 링크 한 경우 다음과 같습니다.

이번 주에 타이포그래피를 검사하기위한 편리한 북마크 인 Roundup, JavaScript 모듈과 Facebook의 Facebook 등을 어떻게 가져 오는지 땜질하기 위해 대기하는 편리한 북마크 인 Roundup과 Facebook의

사이트에서 방문자 및 사용 데이터를 추적하는 데 도움이되는 분석 플랫폼이 많이 있습니다. 아마도 널리 사용되는 Google 웹 로그 분석
