css - Responsive table with headers on each row -
what have: responsive table. when table becomes responsive remove table headers ( th
) , show table data (td
). can become problem if data not self explanatory. want display table headers before each row in next manner
_______________________ | name apples | |_______________________| | amount 10t | |_______________________| _______________________ | name oranges | |_______________________| | amount 2kg | |_______________________|
i figured part can use content:attr(headers) display headers before data. since headers dynamic , not hardcode them.
example
td:before { content: attr(headers); }
the problems i'm having it's janky , not pretty. know need find way format somehow fit needs.
ideal way if headers take 50% , data 50% of width.
i want css only solution.
demo:
.table-bordered { width: 100%; border-collapse: collapse; } .table-bordered th { text-align: left; } .table-bordered th:first-child { padding-left: 0; } .table-bordered th:last-child { padding-right: 0; } .table-bordered td:first-child { padding-left: 0; } .table-bordered th, .table-bordered td { border: 1px solid #000; } @media (max-width:768px) { .table-bordered thead, .table-bordered tbody, .table-bordered th, .table-bordered td, .table-bordered tr { display: block; } .table-bordered tr { margin-bottom: 0.5em; } .table-bordered th { display: none; } .table-bordered td { padding: 0.5em !important; border: 1px solid #000; } }
<table id="report_artikli" class="table-bordered"> <thead> <tr><th id="sif_art">sif art</th><th id="naz_art">naz art</th></tr> </thead> <tbody> <tr><td headers="sif_art">568846</td><td headers="naz_art">hlaČe 3sa pes pant /dark navy/bijela/</td> <tr><td headers="sif_art">562588</td><td headers="naz_art">obuĆa trČanje vanquish /crna/met.silver/</td></tr> <tr><td headers="sif_art">772442</td><td headers="naz_art">obuĆa originali anzo low /turf grey/</td></tr> <tr><td headers="sif_art">467304</td><td headers="naz_art">obuĆa originali rhyolite mid k /titan grey/sky/</td></tr> <tr><td headers="sif_art">397385</td><td headers="naz_art">Štitnik +predator lite /bijela/metallic silver/</td></tr> <tr><td headers="sif_art">059850</td><td headers="naz_art">kapa feel beanie /frost pink/medium grey/</td></tr> </tbody> </table>
i have partially solves problem.
td:before { content: attr(headers); float: left; text-align: left; width: 50%; display: inline-block; }
problem arises when data column overflows new line. in case overflow way left since height not taken heading.
i use floats , min-height fix problem. try code below.
@media (max-width : 768px) { table td, .table-bordered td { width : 47%; min-height : 30px; } .table td, .table-bordered td:first-of-type { float : left; } .table td, .table-bordered td:last-of-type { float : right; } .table tr, .table-bordered tr { overflow : hidden; } }
Comments
Post a Comment