• 회원가입
  • 로그인
  • 구글아이디로 로그인

[grid] CSS - Grid Item - 그리드 아이템

목차

  1. Grid Item 예제
  2. Grid Item 의미
  3. grid-column 속성 - 가로 크기
  4. grid-row 속성 - 세로 크기
  5. grid-area 속성1 - 영역 지정
  6. grid-area 속성2 - 이름 짓기
  7. grid-area 속성3 - 순서 지정

 

Grid Item 예제

 

<style>

.grid-container {

  display: grid;

  gap: 10px;

  background-color: pink;

  padding: 10px;

}


.grid-item {

  background-color: rgba(255, 255, 255, 0.8);

  text-align: center;

  padding: 10px 0;

  font-size: 30px;

}


.a {

  grid-column: 1 / span 2;

  grid-row: 1;

}


.b {

  grid-column: 3;

  grid-row: 1 / span 2;

}


.e {

  grid-column: 1 / span 3;

  grid-row: 3;

}

</style>


<div class="grid-container">

  <div class="grid-item a">a</div>

  <div class="grid-item b">b</div>

  <div class="grid-item d">c</div>  

  <div class="grid-item d">d</div>

  <div class="grid-item e">e</div>

</div>

 

결과보기

 

Grid Item 의미

 

그리드 컨테이너 (Grid Container)의 자식요소들로서, 그리드 레이아웃 따라 배치될 요소들.

 

※ 각 아이템은 기본적으로 1행1열 차지하나, 여러행 여러열 지정 가능. 

 

grid-column 속성 - 가로 크기

 

아이템 시작열 위치(grid-column-start 속성)과 끝열 위치(grid-column-end 속성)를 한번에 지정해 아이템 가로 크기 지정 가능. 자세히보기

 


[예제1] 1열부터 5열 바로 앞까지 (즉, 5열은 포함 X)

 

.item1 {

  grid-column: 1 / 5;

}

 

결과보기


[예제2] - 1열부터 시작해 3개열 확장

 

.item1 {

  grid-column: 1 / span 3;

}

 

결과보기 

 

grid-row 속성 - 세로 크기

 

아이템 시작행 위치(grid-row-start 속성)과 끝행 위치(grid-row-end 속성)를 한번에 지정해 아이템 세로 크기 지정 가능. 자세히보기

 


[예제1] - 1행부터 시작해 4행 바로 앞까지 (즉, 4행은 포함 X)

 

.item1 {

  grid-row: 1 / 4;

}

 

결과보기


[예제2] - 1행부터 시작해 2개 행 확장.

 

.item1 {

  grid-row: 1 / span 2;

}

 

결과보기 

 

grid-area 속성1 - 영역 지정

 

2가지 방식으로 아이템의 영역을 직접 지정 가능.

  • 시작행열과 끝행열 지정
  • 시작행열과 확장할 행열수 지정

 


[시작행열과 끝행열 지정]

(예) 1행부터 시작해 5행 바로 앞까지, 2열부터 시작해 6열 바로 앞까지.

 

.item1 {

  grid-area: 1 / 2 / 5 / 6;

}

 
결과보기


[시작행열과 확장할 행열수 지정]

(예) 2행부터 시작해 2개행 확장 사용, 1열부터 시작해 3개열 확장 사용.

 

.item8 {

  grid-area: 2 / 1 / span 2 / span 3;

}


결과보기

 

grid-area 속성2 - 이름 짓기

 

아이템의 각 영역 이름 짓고, grid-template-areas 속성으로 영역 지정.

 


[예제1] - 1행 1/2/3/45열을 모두 사용.

 

.item1 {

  grid-area: hz;

}

.grid-container {

  grid-template-areas: 'hz hz hz hz hz';

}


결과보기 


[예제2] - 1행의 1/2열만 사용.

 

.item1 {

  grid-area: hz;

}

.grid-container {

  grid-template-areas: 'hz hz . . .';

}


결과보기


[예제3] - 1행의 1/2열과 2행의 1/2열 사용. 

 

.item1 {

  grid-area: hz;

}

.grid-container {

  grid-template-areas: 'hz hz . . .' 'hz hz . . .';

}


결과보기 


[예제4] - 3단 레이아웃 구성

 

.item1 { grid-area: header; }

.item2 { grid-area: left; }

.item3 { grid-area: main; }

.item4 { grid-area: right; }

.item5 { grid-area: footer; }


.grid-container {

  grid-template-areas:

    'header header header header header header'

    'left main main main right right'

    'left footer footer footer footer footer';

}


결과보기

 

grid-area 속성3 - 순서 지정

 

그리드 컨테이너에서 grid-template-columns 속성으로 사용할 열 개수와 너비 지정 후, 그리드 아이템에서 grid-area 속성으로 각각의 위치 지정.  

 


[예제1] - 각각의 아이템 위치 지정

 

.grid-container {

  grid-template-columns: auto auto auto;

}


.item1 { grid-area: 1 / 3 / 2 / 4; }

.item2 { grid-area: 2 / 3 / 3 / 4; }

.item3 { grid-area: 1 / 1 / 2 / 2; }

.item4 { grid-area: 1 / 2 / 2 / 3; }

.item5 { grid-area: 2 / 1 / 3 / 2; }

.item6 { grid-area: 2 / 2 / 3 / 3; }

 

결과보기


[예제2] - 반응형 (= 모바일기기 경우 순서 별도 지정)

 

<style>

.grid-container {

  grid-template-columns: auto auto auto;

}


@media only screen and (max-width: 500px) {

  .item1 { grid-area: 1 / span 3 / 2 / 4; }

  .item2 { grid-area: 3 / 3 / 4 / 4; }

  .item3 { grid-area: 2 / 1 / 3 / 2; }

  .item4 { grid-area: 2 / 2 / span 2 / 3; }

  .item5 { grid-area: 3 / 1 / 4 / 2; }

  .item6 { grid-area: 2 / 3 / 3 / 4; }

}

</style>


결과보기 



분류 제목
border CSS - border-bottom-left-radius 속성 - 테두리하단왼쪽모서리둥글게 (= border…
func CSS - attr() 함수 - 선택요소의 속성값 반환. (= attr함수 = 어트르함수) ※ 속성값얻기
selector CSS - :last-of-type 가상선택자 - 지정타입 마지막자식요소 (순서선택자,타입선택자, IE9)
flex CSS - flex-shrink 속성(I) ★ - 동일컨테이너 안 나머지 플렉스아이템에 비해 얼마나 줄어들지…
selector CSS - .class1.class2 클래스선택자 ★★★ - 두 클래스가 (모두/함게/둘다/동시) 정의된 요…
selector CSS - :indeterminate 가상선택자 - 불확정요소 선택 (= :indeterminate선택자 =…
flex CSS - flex-direction 속성(C) ★★ - 기본축 방향 결정. (= flex-direction…
border CSS - border-bottom-style 속성 - 테두리하단스타일 (= 보더바텀스타일) (상속 X)
selector CSS - :link 가상선택자 - 미방문링크 (= 링크선택자)
selector CSS - :nth-last-child(n) 가상선택자 ★★★ - (그 부모의) 마지막n번째 자식요소 (= …
selector CSS - :required 가상선택자 - 필수입력요소 (= required속성있는요소, IE10)
transform CSS - transform-style 속성 - 중첩요소를 3D공간에서 표시 방법 (= transform-s…
intro CSS - 브라우저 접두어 (= vendor prefix = 벤더 프리픽스)
animation CSS - animation-play-state 속성 - 애니재생상태 (= 애니작동상태설정 = 움직임 재생/…
css CSS - currentcolor 키워드 - 현재 글자색 속성값을 그대로 사용 (= 커런트컬러 속성값)
animation CSS - animation-name 속성 - 키프레임명 (= 키프레임이름 = 애니이름 = 애니명 = 애니메…
animation CSS - animation-fill-mode 속성 - 애니미작동스타일 (= 움직임 작동안할때 스타일 = 애…
column CSS - column-width 속성 - 컬럼 너비 지정 (= column-width속성 = 컬럼위드스속성…
css CSS - 반응형 마름모 갤러리 (Responsible Rhomb gallery on grids + clip…
background CSS - background-origin 속성 - 배경이미지 좌표시작점 (= 백그라운드오리진 속성)
16/27
목록
찾아주셔서 감사합니다. Since 2012