CSS flex 약식 속성이란?
flex 속성은 flex-grow, flex-shrink, flex-basis 세 가지 개별 속성을 한 줄로 간결하게 설정할 수 있는 CSS 약식(shorthand) 속성입니다. 플렉스 컨테이너 안에서 아이템의 크기가 늘어나고 줄어드는 방식을 한 번에 지정할 수 있어 코드를 훨씬 깔끔하게 관리할 수 있습니다.
flex 속성의 세 가지 값
- flex-grow: 컨테이너에 남는 공간이 있을 때 해당 아이템이 차지할 확대 비율
- flex-shrink: 공간이 부족할 때 아이템이 줄어들 축소 비율
- flex-basis: 아이템 크기 계산의 기준이 되는 기본 크기
예를 들어 flex: 20 0 350px;처럼 작성하면 확대 비율은 20, 축소 비율은 0(공간이 부족해도 축소되지 않음), 기본 크기는 350px로 설정됩니다.
예제
다음 코드를 실행하면 flex 속성이 실제 화면에서 어떻게 적용되는지 확인할 수 있습니다.
<!DOCTYPE html>
<html>
<head>
<style>
.mycontainer {
display: flex;
background-color: orange;
}
.mycontainer > div {
background-color: white;
text-align: center;
line-height: 40px;
font-size: 25px;
width: 100px;
margin: 5px;
}
</style>
</head>
<body>
<h1>Quiz</h1>
<div class = "mycontainer">
<div>Q1</div>
<div>Q2</div>
<div>Q3</div>
<div style = "flex: 20 0 350px">Q4</div>
<div>Q5</div>
<div>Q6</div>
<div>Q7</div>
<div>Q8</div>
<div>Q9</div>
</div>
</body>
</html>