How to set max-width to tbody or tr
Hi guys. I want to set max width to a row of a row but it does not work. Can you help me?
11 Replies
Is there a reason you aren't setting
.wrapper
with a max-width instead?
I don't use a lot of tables, but afaik, you can't declare width on a tr
or tbody
. with the way tables work, having a width on tr doesn't really make sense. I'd have to check the spec, but I think table elements are sort of similar to inline elements in that wayIn the fixed table layout algorithm, the width of each column is determined as follows: A column element with explicit width sets the width for that column. Otherwise, a cell in the first row with explicit width determines the width for that column. Otherwise, the column gets the width from the shared remaining horizontal space. ā https://developer.mozilla.org/en-US/docs/Web/CSS/table-layoutThe
table-layout:fixed
is designed to fix the width of columns. You can set the width of the table by... setting the width on the table. Using table-layout:fixed
doesn't do anything unless you also set a size for the column or the first cell in a row.
A max-width
on a tr
or tbody
won't do anything, because they default to table-specific display styles display:table-row
and display:table-body
which ignore widths, because this is not how tables are meant to work.
Options:
- set a max-width on the whole table in addition to width:100%
and drop the table-layout:fixed
entirely (or put it in a wrapper)
- add <col>
tags with a set width to set the width of the individual columnsMDN Web Docs
table-layout - CSS: Cascading Style Sheets | MDN
The table-layout CSS property sets the algorithm used to lay out cells, rows, and columns.
Because I'm creating an email template and my co-worker said that I should only use table element
yeah, but you could put the max-width on the .wrapper, and not on the tr/tbody
you also won't want to use table-layout: fixed; for email... or at least, I don't think you will š
I mean the wrapper will take up 100% width of screen for background-color and I want limit the width of its content
So:
should work
you can use divs too š
oh. I got it. Thanks
tables are used for layouts because the rendering engines of email clients are old (some of them), so that means we can't use grid or flex to create the layout... but that also means things like custom properties won't work
engines of email clients are also support div right?
i'm not familiar with email template š
If you're using it as a learning opportunity, I'd say keep plugging away at it, you'll learn a ton.
If you're doing this for something that's going to be used for actual emails being sent out, i'd look into some tools for creating the table for you.
https://mjml.io/ is the only one I can think of off the top of my head
MJML - The Responsive Email Framework
The only framework that makes responsive email easy. MJML is a markup language designed to reduce the pain of coding a responsive email.
Ok thank you :thumbup: