본문 바로가기
Code🦜/HTML,CSS,JS

CSS-Flexbox 정리

by lie_ji 2025. 3. 30.

예제2)

속성설명주요 값코드 예시
displayFlexbox를 적용하려면 반드시 선언해야 함flexdisplay: flex;
flex-direction주축의 방향 설정row, columnflex-direction: row;
justify-content주축 방향 정렬 (가로 정렬)flex-start, center, space-between 등justify-content: center;
align-items교차축 방향 정렬 (세로 정렬)stretch, center, flex-start 등align-items: center;
flex-wrap화면 공간 부족 시 줄바꿈 여부nowrap, wrapflex-wrap: wrap;
flex-basis기본 크기 설정예: 150pxflex-basis: 150px;
flex-grow여유 공간 비율 설정0, 1, 2, ...flex-grow: 1;
flex-shrink축소 비율 설정0, 1, 2, ...flex-shrink: 1;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="container row">
      <div class="one">one</div>
      <div class="two">two</div>
      <div class="three">three</div>
    </div>
    <div class="container column">
      <div class="one">one</div>
      <div class="two">two</div>
      <div class="three">three</div>
    </div>

    <style>
      .container {
        /* 초기 flex선언을 해줘야 아래 플렉스 동작함 */
        display: flex;
        border: 10px solid red;
        margin: 10px;
        height: 300px;
        /* justify-content:center;
        align-items:center; */
        flex-wrap: wrap; /*화면이 부족하면 줄바꿈*/
      }
      .row {
        flex-direction: row;
      }
      .column {
        flex-direction: column;
      }
      .one {
        border: 10px solid blue;
        /* flex-basis: 30px; */
        /* flex-grow: 0; */
        /* flex-shrink: 0; */
      }

      .two {
        border: 10px solid green;
        /* flex-basis: 60px; */
        /* flex-grow: 0; */
        /* flex-shrink: 0; */

      }
      .three {
        border: 10px solid pink;
        /* flex-basis: 150px; */
        /* flex-grow: 1; -나머지 여백을 3가 다가져가- */
        /* flex-shrink: 1; -부족한 여백이 있을때 얘를 아래나 옆으로- */
      }
    </style>
  </body>
</html>

 


예제1)
ol , article과의 차이
display:flex  플렉스의 시작
flex-wrap: wrap,no-wrap 자리 없을떄 줄바꿈 

flex-basis: -px 고정
flex-grow:1,0남은여백처리
flex-shrink:1,0부족한여백처리
justify-content:기본축 정렬 start,end,space-around
align-items:교차축 정렬 start,end,space-around
height(높이),margin(마진): 태그별 flex-basis가 다르게 적용되있을때 고정값으로 정해주는 것

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    .container{
        display: flex;
        border: 10px dashed red;
        justify-content:space-around;
        height: 600px;

    }
    ol{
        flex-basis: 100px;
    }
    article{
        flex-basis: 200px;
    }
    ol,article{
        border: 10px solid blue;
        margin: 50px;
    }

</style>
<body>
<div class="container">
    <ol>
        <li>html</li>
        <li>css</li>
        <li>js</li>
    </ol>
    <article>자 이제 시작이야 내 꿈은 내 꿈을 위한 여행 핏카츄! 걱정 따윈 없어 없어 내 친구와 함께니깐</article>
</div>
</body>
</html>

 
2.