HTML CSS

flex-direction 속성

ryeonng 2024. 7. 1. 17:17

FlexBox의 두 개의 축

flexbox 레이아웃의 정렬을 이해하는 데 가장 중요한 역할을 한다. 주축(main axis)의 방향과 교차축(cross axis)의 방향을 결정하는 flex-direction 이라는 속성이 있다. flex-direction의 기본 값은 row이다.

 

  1.  주축 방향(main axis) : Flex container에 선언된 flex-direction의 값에 따라 자식 요소인 flex item이 나열되는 방향이다. flex-direction : row 인 경우는 주축이 수평이 되고, flex-direction : column 인 경우는 주축이 수직 방향이 된다.
  2.  교차축 방향(cross axis) : Flex container의 주축에 직각을 이루는 축으로, 주축이 수평이며 교차축은 수직 방향이 된다. 

 

flex-direction 속성

flex-direction 속성은 주 축의 방향을 설정한다.
  • row : 아이템들을 수평으로 배치한다. (기본값)
  • row-reverse : 아이템들을 수평으로 배치하되, 반대 방향으로 배치한다.
  • column : 아이템들을 수직으로 배치한다.
  • column-reverse : 아이템들을 수직으로 배치하되, 반대 방향으로 배치한다.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>인라인블록 요소 예시</title>
<style type="text/css">
	
	.container {
		display: flex;
		border: 1px solid black;
		flex-direction: row;
	}
	.item {
		background-color: lightblue;
		padding: 10px;
		margin: 5px;
		border: 1px solid blue;
	}
	
</style>
</head>
<body>
	<!-- http://localhost:8080/demo1/demo_03.html  -->
	<h1>여기는 demo_03.html 파일 입니다.</h1>
	<br>
	
	<h3>flex-direction : row(기본값)</h3>
	<div class="container">
		<div class="item">아이템1</div>
		<div class="item">아이템2</div>
		<div class="item">아이템3</div>
	</div>
	
	<h3>flex-direction : row-reverse</h3>
	<div class="container" style="flex-direction: row-reverse;">
		<div class="item">아이템1</div>
		<div class="item">아이템2</div>
		<div class="item">아이템3</div>
	</div>
	
	<h3>flex-direction : column</h3>
	<div class="container" style="flex-direction: column;">
		<div class="item">아이템1</div>
		<div class="item">아이템2</div>
		<div class="item">아이템3</div>
	</div>
	
	<h3>flex-direction : column-reverse</h3>
	<div class="container" style="flex-direction: column-reverse;">
		<div class="item">아이템1</div>
		<div class="item">아이템2</div>
		<div class="item">아이템3</div>
	</div>
	
</body>
</html>

 

앵커 태그를 사용하여 페이지 내 특정 위치로 이동 가능하다.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
	
	.navbar {
		display: flex;
		border: 1px solid black;
		background-color: #f8f9fa;
		padding: 10px;
	}

	.nav-item {
		margin: 5px; 
		padding: 10px;
		background-color: lightblue;
		color: white;
		text-align: center;
		border-radius: 4px;
		text-decoration: none;
	}
	
	.section {
		height: 300px;
		padding: 20px;
		margin-top: 20px;
		border: 1px solid #000;
	}
	
</style>
</head>
<body>
	<!-- http://localhost:8080/demo1/demo_04.html  -->
	<h1>여기는 demo_04.html 파일 입니다.</h1>
	<br>
	
	<h2>flex-direction: row</h2>
	<div class="navbar">
		<a href="#home"  class="nav-item" >홈</a>
		<a href="#about"  class="nav-item" >소개</a>
		<a href="#services" class="nav-item">서비스</a>
		<a href="#contact"  class="nav-item">연락처</a>
	</div>	
	
    <h2>flex-direction: column</h2>
    <div class="navbar" style="flex-direction: column;">
        <a href="#home" class="nav-item">홈</a>
        <a href="#about" class="nav-item">소개</a>
        <a href="#services" class="nav-item">서비스</a>
        <a href="#contact" class="nav-item">연락처</a>
    </div>
	
	<div class="section" id="home"> 
		<h2>홈 섹션</h2>
		<p>여기는 홈 섹션 영역입니다.</p> 
	</div>
	
	<div class="section" id="about">
		<h2>소개 섹션</h2>
		<p>여기는 소개 섹션 영역입니다.</p> 
	</div>
	
    <div id="services" class="section">
        <h2>서비스 섹션</h2>
        <p>여기는 서비스 섹션입니다.</p>
    </div>

    <div id="contact" class="section">
        <h2>연락처 섹션</h2>
        <p>여기는 연락처 섹션입니다.</p>
    </div>
	
</body>
</html>

'HTML CSS' 카테고리의 다른 글

FlexItem의 세밀한 제어 flex 속성  (0) 2024.07.04
교차축 정렬 align-items와 align-content  (0) 2024.07.02
주축 방향 정렬 justify-content  (0) 2024.07.02
flex-wrap  (0) 2024.07.02
Flexbox  (2) 2024.07.01