I'm trying to draw a focus border on a grid cell.
First, I tried
outlinewithoffset. Almost perfect, but left and top borders aren't covered.Then I tried with box shadows (also with inset). Here I tried to add multiple shadows to the cell, but I couldn't manage to cover the right and bottom edges. I guess, because the shadow begins always after the border painting.
Then I tried with a pseudo
afterelement. This gives the righ result, but only if I setoverflownot tohidden. But I need this on each cell. I could set up an anotherdivinside each cell and maybe give this divsoverflow: hidden, but I don't want to do this, because it would duplicate the cell elements unnecessarily.
My goal is to have a focus border, which doesn't affect/shift layout (so simple border painting with a thicker width is no go) and which covers the outer borders on each side and also 1px in the cell on each side. Maybe it is also good, if the extra 1px is outside. I didn't try it yet, how it would look.
.row {
background-color: #ffff;
display: grid;
grid-template-columns: 100px 100px 100px 100px 100px;
grid-column: 1 / -1;
}
.cell {
border-right: 1px solid #ddd;
border-bottom: 1px solid #ccc;
overflow: hidden;
text-overflow: ellipsis;
box-sizing: border-box;
white-space: nowrap;
padding: 2px;
font-size: 14px;
}
.container {
border-top: 1px solid #ccc;
border-left: 1px solid #ccc;
width: 500px;
position: absolute;
top: 50px;
left: 100px;
}
.active {
outline: 2px solid blue;
outline-offset: -2px;
}
.active2 {
position: relative;
overflow: visible;
}
.active2::after
{
content: "";
position: absolute;
inset: 1px;
box-shadow: 0 0 0 2px blue;
pointer-events: none;
}
<div class="container">
<div class="row">
<div class="cell">row 1 cell 1</div>
<div class="cell">row 1 cell 2</div>
<div class="cell">row 1 cell 3</div>
<div class="cell">row 1 cell 4</div>
<div class="cell">row 1 cell 5</div>
</div>
<div class="row">
<div class="cell">row 2 cell 1</div>
<div class="cell active">row 2 cell 2</div>
<div class="cell">row 2 cell 3</div>
<div class="cell active2">row 2 cell 4</div>
<div class="cell">row 2 cell 5</div>
</div>
<div class="row">
<div class="cell">row 3 cell 1</div>
<div class="cell">row 3 cell 2</div>
<div class="cell">row 3 cell 3</div>
<div class="cell">row 3 cell 4</div>
<div class="cell">row 3 cell 5</div>
</div>
</div>
<table>? It would make things much better. With styles, it looks ok, you just need to add proper padding and vertical align for cells.active-cellclass to the active cell. And that's it. So, this is only a css issue. And I use grid for other reasons.outline. I don't see where is the problem.