I’m working on a table and inside the elements I’m rendering a with and inside, based on if that column is sortable or not. The problem I’m running into is that the that contain the are using much more width than the ones that do not when the is like 15px wide.
Here’s a draft of my code.
.table{
position: relative;
margin-top: 2rem;
width: 100%;
}
.table thead{
position: relative;
}
.table th{
font-size: .9rem;
text-align: left;
padding: 1rem 0;
}
.table th span{
margin-left: .4rem;
}
.table th span svg{
width: 1rem;
vertical-align: bottom;
}
<table class="table">
<thead>
<tr>
<th> // This th has too much width
thead1
<span>
<svg>...</svg>
</span>
</th>
<th>thead2</th> // This th is ok.
</tr>
</thead>
</table>
I’m expecting table
to span the full width of it’s parent element and the <th>
elements to have an unset width each using a somewhat consistent space.
This is what my table looks like
This is what I want to achieve
I have tried to set table-layout: fixed
and that solved the issue but I don’t really want my columns to have a fixed width.
I have tried to do some regular manipulation with css like adding display: flex
to the parent and flex-grow: 1
for the elements, but that makes everything worse.
I’m pretty lost here…
2
Answers
you can use the css width to solve it:
and you should use tr also
First you need to add
table-layout: auto;
in your table style.Then, since the
svg
tag create an space even you didn’t set up the style, you need to set it’swidth:0;
and addmin-width:100%;
So yoursvg
tag now have no margin or padding.Set up the width and height of your svg with
rect
.Here, I made these samples and hoping it can help you in some ways.