Filippo
Filippo

Reputation: 126

HTML adaptable table (width, height)

I know there's a CSS solution to set up the width of a table. For example:

table {width: 100%; height:300px}

will adapt the width of the table taking the 100% of the screen but, if I insert height: 100% it doesn't work. I want to create a table (or more tables) that fits to the screen dimensions (for smartphone, pc, ecc..). Is there any solution?

Upvotes: 1

Views: 3441

Answers (4)

miladfm
miladfm

Reputation: 1526

just use width: 100%; and height: 100vh;

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

CSS:

html, body {
  margin: 0px;
  padding: 0px;
}
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
    height: 100vh;

}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}

see Example: https://codepen.io/miladfm/pen/MGPdOL

Upvotes: 1

לבני מלכה
לבני מלכה

Reputation: 16251

see code below: (you can also add margin: 0 to body to prevent margin)

body{
height:100%;
}
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
    height:100vh;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
<body>

<h2>HTML Table</h2>

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

</body>

Upvotes: 0

Omar Abu Omar
Omar Abu Omar

Reputation: 60

it's not necessary to use a table, you can write HTML behave as table:

<section style="display: table;">
 <header style="display: table-row;">
<div style="display: table-cell;"></div>
<div style="display: table-cell;"></div>
<div style="display: table-cell;"></div>
</header>
   <div style="display: table-row;">
   <div style="display: table-cell;"></div>
   <div style="display: table-cell;"></div>
   <div style="display: table-cell;"></div>
   </div>
</section>

and use the CSS to style it:

display: table                /* <table>     */
display: table-cell           /* <td>        */
display: table-row            /* <tr>        */
display: table-column         /* <col>       */
display: table-column-group   /* <colgroup>  */
display: table-footer-group   /* <tfoot>     */
display: table-header-group   /* <thead>     */

You might occasionally hear: tables are unsemantic. That's not true - they semantically indicate tabular data. Tables are the right choice when that is the case. refer to link

Upvotes: 0

user8850199
user8850199

Reputation:

Use height: 100vh may it work for you

Upvotes: 0

Related Questions