테이블 태그 없이 CSS를 사용하여 Div 태그에서 테이블 스크롤을 만드는 방법

'디스플레이' 속성을 사용하여 테이블을 나타내는 방법은 무엇입니까?

아래 표는 동일한 요소를 나타내기 위해 ' table ' 태그와 해당 지원 CSS 속성 간의 관계를 제공합니다. 따라서 테이블을 생성할 때 HTML ' table ' 태그 대신 ' div ' 태그를 사용 하고 해당 CSS를 추가하여 테이블을 표시하기만 하면 됩니다.

<테이블> {디스플레이:테이블}
<TR> {디스플레이: 테이블 행}
<머리> {디스플레이: 테이블 헤더 그룹}
<티바디> {디스플레이: 테이블-행-그룹}
<티풋> {디스플레이: 테이블 바닥글 그룹}
<콜> {디스플레이: 테이블 열}
<컬그룹> {디스플레이: 테이블-열-그룹}
<티디>, <일> {디스플레이: 테이블 셀}
<자막> {디스플레이: 테이블 캡션}

1단계: 테이블에 대한 마스터 div 생성

HTML

<div class="d-tbl"></div>

CSS

.d-tbl {  
    width: 100%;  
    display: table;  
    border-collapse: collapse;  
}

3단계: 표 캡션, 머리글, 본문, 바닥글 만들기

HTML

<div class="d-tbl">  
    <div class="d-tbl-head"></div>  
    <div class="d-tbl-body">/div>  
    <div class="d-tbl-foot"></div>  
</div>

CSS

.d-tbl-caption{  
    display: table-caption;  
    text-align: center;  
    font-weight: bold;  
}  
.d-tbl-head,  
.d-tbl-foot {  
    display: table-header-group;  
    background-color: white;  
}  
.d-tbl-body {  
    display: table-row-group;  
}

3단계: 테이블 행, 셀, 헤드 셀, 풋 셀 생성

HTML

<div class="d-tbl">  
    <div class="d-tbl-head">  
        <div class="d-tbl-row">  
            <div class="d-tbl-cell d-tbl-head-cell">Header 1</div>  
            <div class="d-tbl-cell d-tbl-head-cell">Header 2</div>  
            <div class="d-tbl-cell d-tbl-head-cell">Header 3</div>  
            <div class="d-tbl-cell d-tbl-head-cell">Header 4</div>  
        </div>  
    </div>  
    <div class="d-tbl-body">  
        <div class="d-tbl-row">  
            <div class="d-tbl-cell">Column 1</div>  
            <div class="d-tbl-cell">Column 2</div>  
            <div class="d-tbl-cell">Column 3</div>  
            <div class="d-tbl-cell">Column 4</div>  
        </div>  
    </div>  
    <div class="d-tbl-foot">  
        <div class="d-tbl-row">  
            <div class="d-tbl-cell d-tbl-head-foot">Footer 1</div>  
            <div class="d-tbl-cell d-tbl-head-foot">Footer 2</div>  
            <div class="d-tbl-cell d-tbl-head-foot">Footer 3</div>  
            <div class="d-tbl-cell d-tbl-head-foot">Footer 4</div>  
        </div>  
    </div>  
</div>

CSS

.d-tbl-row {  
    display: table-row;  
}  
.d-tbl-cell {  
    display: table-cell;  
    padding: 5px;  
    border: 1px solid #dee2e6;  
}  
.d-tbl-head-cell,  
.d-tbl-foot-cell {  
    font-weight: bold;  
}

결과

css div를 테이블로

4단계: 테이블에 스크롤 막대 추가

HTML

<div class="p_fix_table">  
    <div class="d-tbl">  
        <div class="d-tbl-head" id="d-tbl-fix-head"></div>  
        <div class="d-tbl-body" id="d-tbl-fix-body" onscroll="tblFixScroll('d-tbl-fix-head', 'd-tbl-fix-body')"></div>  
    </div>  
</div>

CSS

.p_fix_table {  
    width: 100%;  
    max-height: 250px;  
    overflow: hidden;  
}  
.p_fix_table .d-tbl {  
    display: flex;  
    flex-direction: column;  
    flex: 1 1 auto;  
    width: 100%;  
    max-height: 250px;  
    border: 1px solid #dee2e6;  
    border-collapse: collapse;  
    overflow: hidden;  
}  
.p_fix_table .d-tbl-head {  
    flex: 1 0 auto;  
    display: block;  
    overflow-x: hidden;  
    overflow-y: scroll;  
    scrollbar-base-color: #dee2e6;  
    scrollbar-face-color: #dee2e6;  
    scrollbar-highlight-color: #dee2e6;  
    scrollbar-track-color: #dee2e6;  
    scrollbar-arrow-color: #dee2e6;  
    scrollbar-shadow-color: #dee2e6;  
}  
.p_fix_table .d-tbl-head::-webkit-scrollbar {  
    display: block;  
    background-color: transparent;  
}  
.p_fix_table .d-tbl-head::-webkit-scrollbar-track {  
    background-color: transparent;  
}  
.p_fix_table .d-tbl-body {  
    display: block;  
    overflow: scroll;  
    max-height: 220px;  
}  
.p_fix_table .d-tbl-body:nth-child(3) {  
    display: none;  
}  
.p_fix_table .d-tbl-cell,  
.p_fix_table .d-tbl-head-cell {  
    width: 170px;  
    min-width: 170px;  
    padding: 5px;  
    border: 1px solid #dee2e6;  
}  
.p_fix_table .d-tbl-row .d-tbl-cell:first-child,  
.p_fix_table .d-tbl-row .d-tbl-head-cell:first-child {  
    position: sticky;  
    left:0;  
    background: white;  
    z-index: 1;  
}

JS

function tblFixScroll(thead_id, tbody_id) {  
    let thead = document.getElementById(thead_id);  
    let tbodyScroll = document.getElementById(tbody_id).scrollLeft;  
    thead.scrollLeft = tbodyScroll;  
    //document.getElementById("frozen").scrollLeft = 0;  
}