HTML rowspan conflict with css elements

Viewed 131

I guess my code would speak for itself.

<!DOCTYPE HTML>
<html>

<head>
  <title>&nbsp;</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
     :root {
      --color1: #c83349;
      --color2: #fff;
    }
    
    table {
      border-spacing: 0;
      border: 1px solid purple;
      margin: 10px auto;
      text-align: center;
      width: 300px;
      border-bottom: none;
    }
    
    tr,
    th,
    td {
      width: 100px;
      height: 100px;
    }
    
    th,
    td:not(:nth-child(2)) {
      background: repeating-linear-gradient(45deg, var(--color2) 25%, var(--color2) 50%, var(--color1) 50%, var(--color1) 75%);
    }
    
    tr:last-child td {
      border-bottom: 1px solid purple;
    }
    
    td:nth-child(2) {
      border: 1px solid purple;
      border-bottom: none;
    }
  </style>
</head>

<body>

  <table>

    <tr>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
    <tr>
      <td>4</td>
      <td rowspan=2>5</td>
      <td>6</td>
    </tr>
    <tr>
      <td>7</td>
      <td>8</td>
    </tr>

  </table>

</body>

</html>

Box number 8 is not exceptional from all the others [ - except td:not(:nth-child(2)) -]. I guess it's conflicting with my table rowspan somehow but I can't figure out why is that.

My goal so the table would look like this:

***
* *
* *

I guess tiny tweak needed here :-)

2 Answers

Use the rowspan attribute as selector instead of nth-child

* {
  margin: 0;
  padding: 0;
}

:root {
  --color1: #c83349;
  --color2: #fff;
}

table {
  border-spacing: 0;
  border: 1px solid purple;
  margin: 10px auto;
  text-align: center;
  width: 300px;
  border-bottom: none;
}

tr, th, td {
  width: 100px;
  height: 100px;
}

th,
td:not([rowspan]) {
  background: repeating-linear-gradient(45deg, var(--color2) 25% 50%, var(--color1) 50% 75%);
}

tr:last-child td {
  border-bottom: 1px solid purple;
}

td[rowspan] {
  border: 1px solid purple;
  border-bottom: none;
}
<table>
  <tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
  </tr>
  <tr>
    <td>4</td>
    <td rowspan=2>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td>7</td>
    <td>8</td>
  </tr>
</table>

when you were applying background with th, td:not(:nth-child(2)), you were excluding the second child element (td), And the last row has only two elements so it obviously doesn't apply to the last element. Here is a similar version that you were using but I prefer to use id/class to exclude a particular element.

<!DOCTYPE HTML>
<html>
<head>
<title>&nbsp;</title>
<style>

*
{
    margin:0;
    padding:0;
}

:root
{
    --color1:#c83349;
    --color2:#fff;
}

table
{
    border-spacing:0;
    border:1px solid purple;
    margin:10px auto;
    text-align:center;
    width:300px;
    border-bottom:none;
}

tr,th,td
{
    width:100px;
    height:100px;
}

th,td
{
    background:repeating-linear-gradient(45deg,var(--color2) 25%,var(--color2) 50%,var(--color1) 50%,var(--color1) 75%);
}

tr:last-child td
{
    border-bottom:1px solid purple;
}

tr:nth-child(2) td:nth-child(2)
{
    border:1px solid purple;
    border-bottom:none;
    background:var(--color2);
}
</style>
</head>
<body>

<table>

<tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
</tr> 
<tr>
    <td>4</td>
    <td rowspan=2>5</td>
    <td>6</td>
</tr>
<tr>
    <td>7</td>
    <td>8</td>
</tr> 

</table>

</body>
</html>

Related