I have a ul
element and 5 child <li>
.
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
The <ul>
has a display: flex
property.
I tried using the calc property on the <li>
to evenly size my list items:
calc:(100% / 5);
This gives the desired result and evenly sizes the 5 <li>
blocks
Now I added borders to the right side of all, but the last child <li>
element. So I reduced the total width of all the borders combined from the total width of the <ul>
.
calc:((100% - 8px) / 5);
This also worked properly and evenly sized the <li>
blocks with the borders.
ul {
width: 600px;
height: 300px;
margin: 0;
padding: 0;
display: flex;
background: red;
}
li {
list-style: none;
display: block;
width: calc((100% - 0.8px) / 5);
height: 100%;
border-right: 0.2px solid black;
background: blue;
}
li:last-child {
border-right: none;
}
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
Now I tried to set the border width in viewport unit vw
instead of px but it gives a different result.
ul {
width: 600px;
height: 300px;
margin: 0;
padding: 0;
display: flex;
background: red;
}
li {
list-style: none;
display: block;
width: calc((100% - 0.8vw) / 5);
height: 100%;
border-right: 0.2vw solid black;
background: blue;
}
li:last-child {
border-right: none;
}
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
As you can see from the snippet above, there is a little bit of space to the right. This becomes bigger with a wider viewport (try viewing the snippet full page). So I think the problem lies with the vw
units and flexbox here.
So what is the cause of this error?
EDIT:
From the provided answers and comments, I have seen there are other and more proper approches to achieve what I was trying to do. I appreciate those answers but those are not the answers to my question. Since calc
is showing an error in this case, most likely it will cause more problems when I try to use calc
and viewport units in other cases (not just borders). So I need to know the reason and "calc
" fix.