I'm trying to make the width property equal to (100/3)%
because I am trying to divide the width evenly amongst 3 columns. But obviously this is not valid css. What is the proper way to accomplish this?
Asked
Active
Viewed 88 times
2

askingstupidquestions
- 189
- 1
- 9
-
[Please do not upload images of code when asking a question.](//meta.stackoverflow.com/q/285551) Copy and paste the code in as text instead. – Tim Dec 11 '20 at 10:01
-
1Ah, i didn't know. Will do so moving forward :) – askingstupidquestions Dec 13 '20 at 03:17
2 Answers
3
calc
is the way to go.
Just make sure to leave a space between the operators and operands.
// This is correct
.class {
width: calc(100% - 5px);
}
// This produces an error
.class {
width: calc(100%-5px);
}
2
pure CSS method:
th, td {
width: calc(100% / 7);
}
Javascript method: You can get your table elements and assigns styles, try this:
let th = document.getElementsByTagName('th');
let td = document.getElementsByTagName('td');
console.log(th, td)
for(let el of th){
el.style.width = (100 / 3) + 'px';
}
for(let el of td){
el.style.width = (100 / 3) + 'px';
}
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>

sonEtLumiere
- 4,461
- 3
- 8
- 35
-
I know you can do it like this, but is there a way to do it directly on the stylesheet without using javascript? – askingstupidquestions Dec 11 '20 at 03:52
-
1
-
1thank you! This is more of the kind of thing I was trying to do. Thank you both tho! – askingstupidquestions Dec 11 '20 at 04:08
-