Hướng dẫn media table css - bảng phương tiện css

thoát khỏi vùng hiển thị (ẩn các

  • Trang chủ
  • Hướng dẫn học
  • Hướng dẫn học Responsive
  • Responsive table

Responsive table

Table là dạng layout dành cho việc hiển thị dữ liệu nhiều, chi tiết, nên việc hiển thị trên các màn hình nhỏ sẽ không đơn giản đối với những bạn mới biết về layout web, bài học này sẽ giúp các bạn biết cách thực hiện responsive một số dạng table thường gặp.

Table có nhiều dạng khác nhau, tuy nhiên 2 dạng thường gặp là table dọc và table ngang, ta lần lượt xem hướng dẫn cách xử lý table trên các chiều rộng màn hình khác nhau nhé.

Responsive với table dọc

Table dọc là cách gọi của Học Web Chuẩn cho dễ hình dung thôi, cách gọi dựa theo cách sắp xếp các thẻ

theo chiều dọc, table ngang là table có cách sắp xếp các thẻ theo chiều ngang.

Tạo một table dọc với các thuộc tính đơn giản sau:

HTML viết:

Lorem ipsum Integer aliquam magna vitae mattis aliquam.
Dolor sit Nullam lacinia massa eget libero porttitor
Consectetur Eu ultrices sem posuere.
Elit Maecenas Proin vehicula ipsum vel luctus accumsan. Aenean sit amet dictum ligula.
Sodales Ut sagittis semper lorem a tincidunt. Vivamus id risus tellus.

CSS viết:

table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}

Hiển thị trình duyệt:

Lorem ipsumInteger aliquam magna vitae mattis aliquam.
Dolor sitNullam lacinia massa eget libero porttitor
ConsecteturEu ultrices sem posuere.
Elit MaecenasProin vehicula ipsum vel luctus accumsan. Aenean sit amet dictum ligula.
SodalesUt sagittis semper lorem a tincidunt. Vivamus id risus tellus.

Với table dọc như trên, ta vẫn có thể xem tốt ở một số thiết bị có chiều rộng màn hình trung bình, tuy nhiên với những màn hình nhỏ thì ngay cả giảm chiều rộng theo phần trăm vẫn khó có thể xem tốt được, để giải quyết trường hợp này, ta sẽ làm như sau:

CSS viết:

table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}

@media only screen and (max-width: 700px) {
    /* Table full chiều rộng màn hình, cách đều trái phải 10px */
    table {
        margin: 0 10px;
        width: auto;
    }
}

@media only screen and (max-width: 480px) {
    table th,
    table td {
        display: block;
        border-bottom: none;
    }

    table tr:last-child td {
        border-bottom: 1px solid #ccc;
    }
}

  • Hiển thị trình duyệt:
  • Lorem ipsum
    • Integer aliquam magna vitae mattis aliquam.

Hướng dẫn media table css - bảng phương tiện css

Dolor sit

Nullam lacinia massa eget libero porttitor

Consectetur

HTML viết:

Lorem ipsum Dolor sit Consectetur adipiscing Elit Maecenas Sodales
Integer aliquam magna vitae mattis aliquam. Nullam lacinia massa eget libero porttitor Eu ultrices sem posuere. Proin vehicula ipsum vel luctus accumsan. Aenean sit amet dictum ligula. Ut sagittis semper lorem a tincidunt. Vivamus id risus tellus.
Aenean pellentesque condimentum augue vitae finibus. Aenean molestie tincidunt lorem. Et efficitur turpis pretium vel. Ut congue gravida lacinia. Nunc id congue urna. Proin interdum eleifend ex sit amet facilisis. Maecenas venenatis vitae justo nec viverra. Cras et est augue.
Ut porttitor nulla velit, eget consequat sapien ultrices sed. Vivamus condimentum commodo tincidunt. Donec diam magna, tincidunt ut mollis id, mattis vel sem. Quisque eleifend nulla ut massa lobortis auctor. Ut aliquam accumsan quam, a posuere augue molestie vitae.

CSS viết:

table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}

Hiển thị trình duyệt:

Lorem ipsumDolor sitConsecteturElit MaecenasSodales
Integer aliquam magna vitae mattis aliquam. Nullam lacinia massa eget libero porttitor Eu ultrices sem posuere. Proin vehicula ipsum vel luctus accumsan. Aenean sit amet dictum ligula. Ut sagittis semper lorem a tincidunt. Vivamus id risus tellus.
Với table dọc như trên, ta vẫn có thể xem tốt ở một số thiết bị có chiều rộng màn hình trung bình, tuy nhiên với những màn hình nhỏ thì ngay cả giảm chiều rộng theo phần trăm vẫn khó có thể xem tốt được, để giải quyết trường hợp này, ta sẽ làm như sau:
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
1: do chiều rộng của table là 700px, nên ta cần chỉnh breakpoint tại 700px để thay đổi chiều rộng table cho phù hợp.
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
2: đối với table theo chiều dọc này, tùy vào giao diện có độ rộng ra sao mà thay đổi điểm breakpoint cho phù hợp, tuy nhiên thông thường điểm breakpoint sẽ nằm ở màn hình từ 480px trở xuống.
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
3: các phần tử
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
5 cách thể hiện giống như các inline (sắp xêp theo chiều ngang), do đó ta cần thay đổi cách sắp xếp theo chiều dọc (khối block) đối với các màn hình nhỏ.
Click ví dụ responsive table dọc để xem kết quả.
Responsive với table ngangTạo một table ngang với các thuộc tính đơn giản sau:Aenean pellentesque condimentum augue vitae finibus. Aenean molestie tincidunt lorem. Et efficitur turpis pretium vel. Ut congue gravida lacinia.

Nunc id congue urna. Proin interdum eleifend ex sit amet facilisis.

  • Maecenas venenatis vitae justo nec viverra. Cras et est augue.
  • Ut porttitor nulla velit, eget consequat sapien ultrices sed.

CSS viết:

table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}

@media only screen and (max-width: 700px) {
    /* Table full chiều rộng màn hình, cách đều trái phải 10px */
    table {
        margin: 0 10px;
        width: auto;
    }

    /* Cho 
) */ table thead tr { position: absolute; top: -9999px; left: -9999px; } table tr:nth-child(odd) { background: #ccc; } table td { border: none; border-bottom: 1px solid #eee; position: relative; padding-left: 50%; } /* Chèn dữ liệu vào trước từng mục của */ table td:before { position: absolute; top: 0; left: 6px; width: 45%; padding-right: 10px; white-space: nowrap; } table td:nth-of-type(1):before { content: "Lorem ipsum"; } table td:nth-of-type(2):before { content: "Dolor sit"; } table td:nth-of-type(3):before { content: "Consectetur"; } table td:nth-of-type(4):before { content: "Elit Maecenas"; } table td:nth-of-type(5):before { content: "Sodales"; } }

Hiển thị trình duyệt:

Hướng dẫn media table css - bảng phương tiện css

Dolor sit

Nullam lacinia massa eget libero porttitor

Consectetur

HTML viết:

Proin colspan="2">Vehicula colspan="2">Ipsum colspan="2">Vel
Luctus Accumsan Aenean Sit Amet Dictum Ligula
Nunc idy Congue urna Proin Interdum Eleifend Ex
Posuere colspan="2">Dictum colspan="2">Donec colspan="2">Diam
Quisque Eleifend Nulla Ut Massa Lobortis Auctor
Vivamus Condimentum Commodo Tincidunt Justo Nec Viverra

CSS viết:

table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}

Hiển thị trình duyệt:

Lorem ipsumInteger aliquam magna vitae mattis aliquam. Dolor sitNullam lacinia massa eget libero porttitor
ConsecteturEu ultrices sem posuere. Elit MaecenasProin vehicula ipsum vel luctus accumsan. Aenean sit amet dictum ligula. SodalesUt sagittis semper lorem a tincidunt. Vivamus id risus tellus. Với table dọc như trên, ta vẫn có thể xem tốt ở một số thiết bị có chiều rộng màn hình trung bình, tuy nhiên với những màn hình nhỏ thì ngay cả giảm chiều rộng theo phần trăm vẫn khó có thể xem tốt được, để giải quyết trường hợp này, ta sẽ làm như sau:
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
1: do chiều rộng của table là 700px, nên ta cần chỉnh breakpoint tại 700px để thay đổi chiều rộng table cho phù hợp.
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
2: đối với table theo chiều dọc này, tùy vào giao diện có độ rộng ra sao mà thay đổi điểm breakpoint cho phù hợp, tuy nhiên thông thường điểm breakpoint sẽ nằm ở màn hình từ 480px trở xuống.
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
3: các phần tử
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
5 cách thể hiện giống như các inline (sắp xêp theo chiều ngang), do đó ta cần thay đổi cách sắp xếp theo chiều dọc (khối block) đối với các màn hình nhỏ.
Click ví dụ responsive table dọc để xem kết quả.Responsive với table ngangTạo một table ngang với các thuộc tính đơn giản sau:Aenean pellentesque condimentum augue vitae finibus.
Aenean molestie tincidunt lorem. Et efficitur turpis pretium vel. Ut congue gravida lacinia. Nunc id congue urna. Proin interdum eleifend ex sit amet facilisis. Maecenas venenatis vitae justo nec viverra. Cras et est augue.
Ut porttitor nulla velit, eget consequat sapien ultrices sed. Tạo một table ngang với các thuộc tính đơn giản sau:Aenean pellentesque condimentum augue vitae finibus. Aenean molestie tincidunt lorem. Et efficitur turpis pretium vel. Ut congue gravida lacinia. Nunc id congue urna. Proin interdum eleifend ex sit amet facilisis. Maecenas venenatis vitae justo nec viverra. Cras et est augue.
Ut porttitor nulla velit, eget consequat sapien ultrices sed. Vivamus condimentum commodo tincidunt. Donec diam magna, tincidunt ut mollis id, mattis vel sem. Quisque eleifend nulla ut massa lobortis auctor. Ut aliquam accumsan quam, a posuere augue molestie vitae. Với những màn hình nhỏ, thì table như trên sẽ không thể hiển thị được dữ liệu tốt được, do đó việc thay đổi giao diện table trên là cần thiết, để thực hiện ta làm như sau:Ẩn tất cả hiện tại.

Sắp xếp dữ liệu của

vào từng mục
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
5 tương ứng, bằng cách sử dụng thuộc tính
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
9 add nội dung
vào trước từng mục
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}
5 tương ứng.

CSS viết:

table {
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    width: 700px;
}

table th,
table td {
    border: 1px solid #ccc;
    padding: 10px;
}

table th {
    background-color: #f3f3f3;
}

@media only screen and (max-width: 700px) {
    div {
        width: 100%;
        overflow-x: auto;
    }
}

Hiển thị trình duyệt:

Hướng dẫn media table css - bảng phương tiện css

Click ví dụ responsive với table phức tạp để xem kết quả.