Languages

HTML / CSS - how to set cells borders inside grid to have the same width?

3 points
Asked by:
Kenya-Spears
860

I want to add borders of the same width to all cells inside my grid. How can I get rid of multiplying borders?

What I have:

What I want:

I've tried to modify the solution below using box-sizing to have borders of the same width in a whole grid, but it didn't work.

My code:

// ONLINE-RUNNER:browser;

<!DOCTYPE html>
<html>
<head>
  <style>

    .grid {
        display: flex;
    }

    .column {
        flex: 1;
    }

    .cell{
        border: 1px solid;
        text-align: center;
    }

  </style>
</head>
<body>
  <div class="grid">
    <div class="column">
      <div class="cell">1.1</div>
      <div class="cell">1.2</div>
      <div class="cell">1.3</div>
    </div>
    <div class="column">
      <div class="cell">2.1</div>
      <div class="cell">2.2</div>
      <div class="cell">3.3</div>
    </div>
    <div class="column">
      <div class="cell">3.1</div>
      <div class="cell">3.2</div>
      <div class="cell">3.3</div>
    </div>
  </div>
</body>
</html>

Note:

The code is based on the following article: HTML - create grid using flexbox 

1 answer
3 points
Answered by:
Kenya-Spears
860

There's a simple trick to achieve this:

1. Set border-top and border-left properties of the whole grid.

Set border-top and border-left

2. Set border-right and border-bottom of your cells.

Set border-right and border-bottom

3. The two above together will give you a pretty border.

HTML / CSS - set pretty border for grid

 

Practical example

// ONLINE-RUNNER:browser;

<!DOCTYPE html>
<html>
<head>
  <style>

    .grid {
        display: flex;
        border-top: 1px solid;  /*   <--- required  */
        border-left: 1px solid; /*   <--- required  */
    }

    .column {
        flex: 1;
    }

    .cell {
        text-align: center;
        border-right: 1px solid;  /*   <--- required  */
        border-bottom: 1px solid; /*   <--- required  */
    }

  </style>
</head>
<body>
  <div class="grid">
    <div class="column">
      <div class="cell">1.1</div>
      <div class="cell">1.2</div>
      <div class="cell">1.3</div>
    </div>
    <div class="column">
      <div class="cell">2.1</div>
      <div class="cell">2.2</div>
      <div class="cell">3.3</div>
    </div>
    <div class="column">
      <div class="cell">3.1</div>
      <div class="cell">3.2</div>
      <div class="cell">3.3</div>
    </div>
  </div>
</body>
</html>
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join