Syling Tables Flashcards

1
Q

How do you make a table scroll horizontally on small screens instead of shrinking its content?

A

CSS

.table-container {
  overflow-x: auto;
  max-width: 100%;
}

table {
  width: 100%;
  border-collapse: collapse;
}

HTML

<div class="table-container">
  <table>
    <tr>
      <th>Name</th>
      <th>Age</th>
    </tr>
    <tr>
      <td>John</td>
      <td>25</td>
    </tr>
  </table>
</div>

This wraps the table in a scrollable div so it doesn’t break on small screens.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between border-collapse: separate; and border-collapse: collapse; when styling table borders?

A

border-collapse: collapse; merges adjacent table borders into one.

border-collapse: separate; keeps individual cell borders separate.

table {
  border-collapse: collapse; /* Borders merge */
}
th, td {
  border: 1px solid black;
}

This removes double borders between cells.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you add alternating row colors (striped effect) to a table?

A
tr:nth-child(even) {
  background-color: #f2f2f2; /* Light gray for even rows */
}

This improves readability by highlighting every other row.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you make the <thead> stay fixed at the top while scrolling down a long table?

A
thead {
  position: sticky;
  top: 0;
  background: white;
  z-index: 10;
}

This keeps the table header visible while scrolling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you center-align text in table cells but right-align numbers?

A

CSS

td {
  text-align: center; /* Center text */
}

td.numeric {
  text-align: right; /* Right-align numbers */
}

HTML

<tr>
  <td>Product</td>
  <td class="numeric">100</td>
</tr>

This ensures better data readability, especially for financial tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you change a row’s background color when the user hovers over it?

A
tr:hover {
  background-color: lightblue;
}

This improves user experience by giving visual feedback when hovering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why should you use padding inside <td> elements instead of margin?

A

Tables do not allow margins inside cells, so you should use padding:

td {
  padding: 10px;
}

This creates space inside the cell without breaking table structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you make a table take full width of the page while ensuring one column does not exceed 200px?

A
table {
  width: 100%;
}

td:first-child {
  max-width: 200px;
  word-wrap: break-word;
}

This ensures the first column doesn’t stretch too wide while keeping the table responsive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why would you use border-spacing: 0; in a table?

A
table {
  border-spacing: 0;
}

By default, tables add spacing between cells.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you create a table with no visible borders but still keep cell spacing?

A
table {
  border: none;
  border-collapse: separate;
  border-spacing: 10px;
}

This removes borders while keeping space between cells for a clean look.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly