[HTML/CSS 공부] <table>를 이용한 레이아웃 만들기

헤븐리뷰·2023년 6월 4일
0

HTML/CSS 공부 기록

목록 보기
11/11

<table> 태그를 이용하여 간단한 장바구니 레이아웃을 만들어보았다.

주요 키워드: <table>, pseudo class, nth-child



공부 memo

  1. 표 만들 땐 <table>

  2. 가로행 만들 땐 <tr>

  3. 세로 열 만들 땐 <td>

  4. 제목용 세로 열 만들 땐 <th>

  5. <thead>, <tbody>
  • 제목 행은 <thead> 안에
  • 일반 행은 <tbody> 안에 넣음 좋다

  1. <table>은 기본적으로 틈이 존재
  • 없애려면 border-collapse: collapse

  1. 셀 안의 요소 상하정렬
  • vertical-align: top / middle / bottom

  1. vertical-align
  • inline/inline-block 요소 간의 세로정렬할 때 쓴다.
  • display: inline -> 항상 옆으로 채워지는 폭과 너비가 없는 요소 (예: <span>)

  1. nth-child 셀렉터 문법
  • n번째 등장하는 요소만 스타일링 할 때
th: nth-child(2) {
    width: 400px;
}

-> 2번째 나오는 th 요소를 선택해서 설정해라

  1. td 하나로 합치기
  • colspan 속성 쓰기
    • 예) <td colspan="5">

  1. pseudo-class로 인터랙티브 버튼 만들기
    pseudo-class로 클릭할 때 스타일을 다르게 줄 수 있다.
    - 마우스 올릴 때 스타일은 :hover
    - 커서 찍혀있을 때 스타일은 : focus
    - 클릭 중 스타일은 : active

(hover, focus, active를 이용한 인터랙티브 버튼 스타일)

.button {
    color:blue;
    display: block;
    margin-left: auto;

    width: auto;
    height: 40px;

    position: relative;
    top: -30px;

    cursor: pointer;

}
.button:hover {
    background-color: aliceblue;
}
.button:focus {
    background-color: aquamarine;
}
.button:active {
    background-color: blueviolet;
}



전체 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./layout5.css">

    <title>Document</title>

</head>
<body>
    <div class="background">
        <div class="ysc">
            <h3>Your Shopping Cart</h3>
        </div>
        <table class="table">
            <thead>
                    <td style="width:30%"></td>
                    <td>ITEM</td>
                    <td>AMOUNT</td>
                    <td>PRICE</td>
                    <td>TOTAL</td>
            </thead>
            <tbody>
                <tr>
                    <td> <img class="img" src="https://cdn.pixabay.com/photo/2014/08/29/14/53/camera-431119_1280.jpg" alt=""></td>
                    <td> <h4>Sony</h4> <span>FE 70-200 mm F28 GM ASS</span></td>
                    <td>1</td>
                    <td>$ 200.00</td>
                    <td>$ 200.00</td>
                </tr>
                <tr>
                    <td> <img class="img" src="https://cdn.pixabay.com/photo/2014/08/29/14/53/camera-431119_1280.jpg" alt=""></td>
                    <td><h4>Canon</h4><span>FE 70-200 mm F28 GM ASS</span></td>
                    <td>1</td>
                    <td>$ 350.00</td>
                    <td>$ 350.00</td>
                </tr>

                <tr>
                    <td colspan="5" style="text-align: right">$ 550.00</td>
                </tr>
            </tbody>
        </table>

        <div class="footer">
            <a href="">Edit your shopping cart</a>
            <button class="button">Choose payment method</button> 
        </div>
    </div>
</body>
</html>
// css 파일
.background {
    width: 100%;
    padding: 50px;
    margin: auto;
    box-sizing: border-box;

    background: #66a6ff;
    position: relative;
    
}

.table {

    background: white;
    text-align: center;
    margin:auto;
    border-collapse: collapse;
}

.button {
    color:blue;
    display: block;
    margin-left: auto;

    width: auto;
    height: 40px;

    position: relative;
    top: -30px;

    cursor: pointer;

}
.button:hover {
    background-color: aliceblue;
}
.button:focus {
    background-color: aquamarine;
}
.button:active {
    background-color: blueviolet;
}

.footer {
    text-align: left;
    margin-top: 30px;
}

.img {
    width: 100%;
    height: 100%;
}

.table td{
    padding: 15px;
    border-bottom: 1px solid #c2d3de;
}

thead {
    font-size: 16px;
    font-weight: 800;
}
profile
데이터로 세상을 쓰고 읽고 싶은 헤븐리뷰입니다.

0개의 댓글