Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using box-sizing to create an inside border

Tags:

html

css

I want to create an HTML table where each cell is clickable, and clicking on a cell adds a border to the single div within the cell. I want that div's border to exist entirely within the existing confines of the td that contains it, without resizing the table or its cells at all. I can't seem to make this happen correctly.

This previous question seems to address the same issue and points to some articles about the box-sizing CSS options. I have a fiddle where I tried to implement this without success: http://jsfiddle.net/YsAGh/3/.

* {
    box-sizing:border-box;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
}


<table>
  <tr>
    <td><div>1</div></td>
    <td><div>2</div></td>
    <td><div>3</div></td>
  </tr>
  ....
</table>

Here's what currently happens. The border causes the containing td to grow to accommodate the div's border.

I want the red border to appear without resizing the td

How can I add the border to the div without it affecting the containing table?

like image 362
Chris Farmer Avatar asked Aug 26 '13 22:08

Chris Farmer


People also ask

Does box-sizing border-box include border?

With the CSS box-sizing Property The box-sizing property allows us to include the padding and border in an element's total width and height. If you set box-sizing: border-box; on an element, padding and border are included in the width and height: Both divs are the same size now!

What is the use of box-sizing border-box?

border-box tells the browser to account for any border and padding in the values you specify for an element's width and height. If you set an element's width to 100 pixels, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width.

What's the difference between box-sizing content box and border-box?

box-sizing: border-box; Note :- when using box-sizing : content-box; the content size remain same while border-box size grows as padding and border grow. but when using box-sizing: border-box; , the size of border-box remains same while size of content decreases as padding and border grow.

Should I always use box-sizing border-box?

It guarantees that the content box shrinks to make space for the padding and borders. Therefore, if you set your element width to 200 pixels, border-box makes sure that the content, padding, and borders fit in this number. Tip: border-box is the best choice for designing layouts and managing the sizes of HTML elements.


1 Answers

Look at my JSFiddle.

You need to provide a width/height to your cells:

td {
    // ...
    width:33.3%;
    height:33.3%;
}
like image 131
glautrou Avatar answered Nov 15 '22 05:11

glautrou