F.P
F.P

Reputation: 17831

Prevent angled borders in highlighted table

Consider the following setup:

$(function() {
  var $cols = $("td:nth-child(2), th:nth-child(2)");
  $cols.hover(function() {
    $cols.addClass("highlight");
  }, function() {
    $cols.removeClass("highlight");
  });
});
div {
  background: #edf0f1;
  padding: 20px;
}
table {
  table-layout: fixed;
  width: 500px;
  border-collapse: separate;
  border-spacing: 0;
}
td {
  padding: 10px;
  background-color: #fff;
  border: 1px solid #edf0f1;
  border-right-width: 10px;
  border-left-width: 10px;
}
td.highlight,
th.highlight {
  border-right-color: black;
  border-left-color: black;
}
tr:last-child td {
  border-bottom-width: 10px;
}
tr:last-child td.highlight {
  border-bottom-color: black;
}
th {
  border: 1px solid #edf0f1;
  border-top-width: 10px;
  border-right-width: 10px;
  border-left-width: 10px;
}
th.highlight {
  border-top: 10px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <table>
    <thead>
      <tr>
        <th>Important</th>
        <th>Information</th>
        <th>Interchange</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Hello world, how are you today</td>
        <td>again</td>
        <td>and we're done</td>
      </tr>
      <tr>
        <td>More things</td>
        <td>Cow level is real!!1111</td>
        <td>over 9000%</td>
      </tr>
    </tbody>
  </table>
</div>

As you can see, the highlighted table shows ugly "arrows" from the borders upon hover:

Border arrows

How can I get rid of those?

Upvotes: 2

Views: 83

Answers (3)

F.P
F.P

Reputation: 17831

Thanks for all the good answers. Being me, I was not satisfied with "it doesn't work" and found a way with only very small adjustments, see below.

$(function() {
  var $cols = $("table [data-col]");
  $cols.hover(function() {
    var colNum = $(this).data("col");
    $("[data-col=" + colNum + "]").addClass("highlight");
  }, function() {
    var colNum = $(this).data("col");
    $("[data-col=" + colNum + "]").removeClass("highlight");
  });
});
div {
  background: #edf0f1;
  padding: 20px;
}
table {
  table-layout: fixed;
  width: 500px;
  border-collapse: separate;
  border-spacing: 0;
}
td {
  vertical-align: top;
  padding: 0;
  background-color: #fff;
  border: 0px solid #edf0f1;
  border-right-width: 10px;
  border-left-width: 10px;
}
td > div, th > div {
  padding: 10px;
  border-top: 1px solid #edf0f1;
  background: none;
}
td.highlight,
th.highlight {
  border-right-color: black;
  border-left-color: black;
}
tr:last-child td {
  border-bottom-width: 10px;
}
tr:last-child td.highlight {
  border-bottom-color: black;
}
th {
  border: 0px solid #edf0f1;
  border-top-width: 10px;
  border-right-width: 10px;
  border-left-width: 10px;
}
th.highlight {
  border-top: 10px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <table>
    <thead>
      <tr>
        <th data-col="1"><div>Important</div></th>
        <th data-col="2"><div>Information</div></th>
        <th data-col="3"><div>Interchange</div></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td data-col="1"><div>Hello world, how are you today</div></td>
        <td data-col="2"><div>again</div></td>
        <td data-col="3"><div>and we're done</div></td>
      </tr>
      <tr>
        <td data-col="1"><div>More things</div></td>
        <td data-col="2"><div>Cow level is real!!1111</div></td>
        <td data-col="3"><div>over 9000%</div></td>
      </tr>
    </tbody>
  </table>
</div>

All that was needed is to wrap the contents into a div and tweak the CSS a little.

This way, I keep the properties of the table (including dynamic row height) but can still highlight per column. Hope it helps someone.

Upvotes: 0

robjez
robjez

Reputation: 3788

You'll need to manually remove borders on hovered items in a columns, using this CSS:

$(function() {
  var $cols = $("td:nth-child(2), th:nth-child(2)");
  $cols.hover(function() {
    $cols.addClass("highlight");
  }, function() {
    $cols.removeClass("highlight");
  });
});
div {
  background: #edf0f1;
  padding: 20px;
}
table {
  table-layout: fixed;
  width: auto;
  border-collapse: separate;
  border-spacing: 0;
}
td {
  padding: 10px;
  background-color: #fff;
  border: 1px solid #edf0f1;
  border-right-width: 10px;
  border-left-width: 10px;
}
td.highlight,
th.highlight {
  border-right-color: black;
  border-left-color: black;
}
tr:last-child td {
  border-bottom-width: 10px;
}
th {
  border: 1px solid #edf0f1;
  border-top-width: 10px;
  border-right-width: 10px;
  border-left-width: 10px;
}

   

/* New CSS */
tbody tr:first-child > td.highlight {
   border-bottom: 1px solid black;
   border-top: 1px solid black;
}
tr:last-child td.highlight {
    border-bottom-color: black;
    border-top: 1px solid;
}
th.highlight {
    border-top: 10px solid black;
    border-bottom: 1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <table>
    <thead>
      <tr>
        <th>Important</th>
        <th>Information</th>
        <th>Interchange</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Hello world</td>
        <td>again</td>
        <td>and we're done</td>
      </tr>
      <tr>
        <td>More things to come</td>
        <td>over 9000%</td>
        <td>Cow level is real!</td>
      </tr>
    </tbody>
  </table>
</div>

Upvotes: 0

Jesse
Jesse

Reputation: 1262

Here, try this.. the larger the border is, the more pronounced the angle is. I changed the border size to 0px

fiddle: https://jsfiddle.net/uqdebsxp/

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <table>
    <thead>
      <tr>
        <th>Important</th>
        <th>Information</th>
        <th>Interchange</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Hello world</td>
        <td>again</td>
        <td>and we're done</td>
      </tr>
         <tr>
        <td>Hello world</td>
        <td>again</td>
        <td>and we're done</td>
      </tr>
      <tr>
        <td>More things to come</td>
        <td>over 9000%</td>
        <td>Cow level is real!</td>
      </tr>
    </tbody>
  </table>
</div>

css

    div {
  background: #edf0f1;
  padding: 20px;
}
table {
  table-layout: fixed;
  width: auto;
  border-collapse: separate;
  border-spacing: 0;
}
td {
  padding: 10px;
  background-color: #fff;
  border: 0px solid #edf0f1;
  border-right-width: 10px;
  border-left-width: 10px;
}
td.highlight,
th.highlight {
  border-right-color: black;
  border-left-color: black;
}
tr:last-child td {
  border-bottom-width: 10px;
}
tr:last-child td.highlight {
  border-bottom-color: black;
}
th {
  border: 0px solid #edf0f1;
  border-top-width: 10px;
  border-right-width: 10px;
  border-left-width: 10px;
}
th.highlight {
  border-top: 10px solid black;
}

Upvotes: 1

Related Questions