CSS hack:A,B,C 화면 정중앙에 위치시키기(justify-content: space-between 안 먹힐 때)

Jaden·2023년 3월 12일

css

목록 보기
6/7

Flex-box안에 ABC가 있을때 A를 화면 좌측끝 B를 화면 중앙 C를 화면 우측끝에 위치시키고 싶다.
이때 Flex-box의 justify-content: space-between를 보통 사용한다.

그러나 A,B,C의 width가 달라서 B가 화면의 정중앙에 들어가지 않을 경우가 있음. 이럴 때 CSS Hack 사용

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="css/styles.css" />
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CSS Hack</title>
  </head>
  <body>
    <div class="container">
      <div class="container__column">
        <span>first_column_content</span><i class="fas fa-wifi"></i>
      </div>
      <div class="container__column">
        <span>second_column_content</span>
      </div>
      <div class="container__column">
        <span>last_column_content</span>
        <i class="fas fa-battery-full fa-lg"></i>
        <i class="fas fa-bolt"></i>
      </div>
    </div>


    <script
      src="https://kit.fontawesome.com/6478f529f2.js"
      crossorigin="anonymous"
    ></script>
  </body>
</html>
/* CSS hack */
.container{
    display:flex;
    justify-content: center ;	//items 가운데로 모아주기 
}

.container__column{
    width:33%; 					//width 개수만큼 분할
}

.container__column:nth-child(2){
    display:flex;
    justify-content:center;		//세로 축 가운데 배치 
}

.container__column:last-child{
    display:flex;				
    justify-content:flex-end;  //가로 축 맨 끝에 배치
    align-items: center;	   //세로 축 가운데 배치
}
profile
Quadrilingual Programmer

0개의 댓글