표의 배경색 정하는 방법

배경 색상은 background-color 속성으로 만듭니다. table, tr, th, td, thead, tbody, tfoot에 배경 색을 적용할 수 있습니다. 기본 모양 다음 표를 기본으로 하고, 배경색을 여러 곳에 추가해보겠습니다.
3 min read

배경색을 만드는 속성

배경 색상은 background-color 속성으로 만듭니다. table, tr, th, td, thead, tbody, tfoot에 배경색을 적용할 수 있습니다.

 

예제

기본 모양

  • 다음 표를 기본으로 하고, 배경색을 여러 곳에 추가해보겠습니다.
<table>
  <thead>
    <tr>
      <th>Lorem</th><th>Ipsum</th><th>Dolor</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem</td><td>Ipsum</td><td>Dolor</td>
    </tr>
    <tr>
      <td>Lorem</td><td>Ipsum</td><td>Dolor</td>
    </tr>
    <tr>
      <td>Lorem</td><td>Ipsum</td><td>Dolor</td>
    </tr>
    <tr>
      <td>Lorem</td><td>Ipsum</td><td>Dolor</td>
    </tr>
    <tr>
      <td>Lorem</td><td>Ipsum</td><td>Dolor</td>
    </tr>
    <tr>
      <td>Lorem</td><td>Ipsum</td><td>Dolor</td>
    </tr>
  <tbody>
</table>
table {
  width: 100%;
  border-top: 1px solid #444444;
  border-collapse: collapse;
}
th, td {
  border-bottom: 1px solid #444444;
  padding: 10px;
  text-align: center;
}

 

표 전체에 배경색 추가하기

  • 표 전체에 하나의 배경색을 넣을 때는 <table>에 배경색을 추가하는 게 편합니다.
table {
  width: 100%;
  border-top: 1px solid #444444;
  border-collapse: collapse;
  background-color: #bbdefb;
}
th, td {
  border-bottom: 1px solid #444444;
  padding: 10px;
  text-align: center;
}

 

제목이 있는 행과 내용이 있는 행에 다른 색 추가하기

  • <th> 요소와 <td> 요소에 서로 다른 배경색을 추가합니다.
table {
  width: 100%;
  border-top: 1px solid #444444;
  border-collapse: collapse;
}
th, td {
  border-bottom: 1px solid #444444;
  padding: 10px;
  text-align: center;
}
th {
  background-color: #bbdefb;
}
td {
  background-color: #e3f2fd;
}

 

행의 배경색을 번갈아 넣기

  • 홀수번째 행과 짝수번째 행의 배경색을 다르게 하고 싶다면 <tr> 요소에 nth-child 선택자를 이용하여 배경색을 추가합니다.
table {
  width: 100%;
  border-top: 1px solid #444444;
  border-collapse: collapse;
}
th, td {
  border-bottom: 1px solid #444444;
  padding: 10px;
  text-align: center;
}
thead tr {
  background-color: #0d47a1;
  color: white;
}
tbody tr:nth-child(2n) {
  background-color: #bbdefb;
}
tbody tr:nth-child(2n+1) {
  background-color: #e3f2fd;
}

 

열의 배경색을 번갈아 넣기

  • 홀수번째 열과 짝수번째 열의 배경색을 다르게 하고 싶다면 <th> 또는 <td> 요소에 nth-child 선택자를 이용하여 배경색을 추가합니다.
table {
  width: 100%;
  border-top: 1px solid #444444;
  border-collapse: collapse;
}
th, td {
  border-bottom: 1px solid #444444;
  padding: 10px;
  text-align: center;
}
thead tr {
  background-color: #0d47a1; 
  color: white;
}
td:nth-child(2n) {
  background-color: #bbdefb;
}
td:nth-child(2n+1) {
  background-color: #e3f2fd;
}

 

기타 CSS 참조

You may like these posts

  • align-content flex-wrap 속성의 값이 wrap인 경우, 아이템들의 가로폭의 합이 콘테이너의 가로폭을 넘어가면 아이템이 다음 줄로 내려갑니다. 이때 여러 줄이 되어버린 아이템들의 정렬을 어떻게 할지 정하는 속성이 align-content입니다.   기본형 justif…
  • align-items align-items로 교차축(cross axis) 아이템 정렬을 정합니다. 기본값은 stretch로, 컨테이너의 높이에 맞게 늘립니다.   기본형 align-items : stretch | flex-start | flex-end | center | baselin…
  • transform / skew transform의 skew로 요소를 비틀기 또는 기울이기를 할 수 있습니다. 평면(2D)에서의 이동입니다.   기본형 transform : skew ( value ); value에 값을 입력합니다. 단위는 deg, rad, grad 등을 사용…
  • transform / scale transform의 scale로 요소를 확대 또는 축소할 수 있습니다. 평면(2D)에서의 확대 또는 축소입니다.   기본형 transform : scale ( value ); value에 값을 입력합니다. 1은 원래의 크기이고 0.5, 1.5…
  • transform / rotate transform의 rotate로 요소를 회전시킬 수 있습니다. 평면(2D)에서의 회전입니다.   기본형 transform : rotate ( angle ); angle에는 각의 크기를 입력합니다. 단위는 deg, rad, grad, tur…
  • align-self align-self 속성을 이용하면 특정 플렉스 아이템을 개별적으로 배치할 수 있습니다. 플렉스 컨테이너에서 아이템 전체의 배치 방법을 결정하지만 align-self 속성으로 특정 아이템의 배치 방법을 변경할 수 있습니다. 예를 들어 display:flex 속성으로 정의된 플…

Post a Comment