Can you mix % and px to align a table?

前端 未结 4 1483
野的像风
野的像风 2021-01-14 09:15

I am trying to align different tables with different amount of columns so the first two columns are align and the rest are independently align but using % it is very difficu

相关标签:
4条回答
  • 2021-01-14 09:40

    you can add width using style attribute:

    td style="width:120px"
    
    0 讨论(0)
  • 2021-01-14 09:51

    You can mix % and px, but you have to think of % as relative to the parent. If the table is 500px, then 50% will be 250px, etc.

    If the table has no specified width, then it will automatically adjust to the content width. This makes no sense if you're going to mix, so at a minimum you should specify a table width.

    0 讨论(0)
  • 2021-01-14 09:56

    You can... But what would you expect this to do? Most of the time, your table will have extra space or not enough space, and won't be able to honor one or more of your requested widths. Leave a column with the width unspecified...

    0 讨论(0)
  • 2021-01-14 09:58

    You will have to use the colspan attribute to do this. Tables are designed such that there are discrete rows and columns, where the number of columns in a table is equal to the largest number of TD elements in any row + the number of columns those cells span (default of 1). So, you need to figure out some base unit for a column in your table, and then have the cells be multiples of that base.

    One thing to remember is that any row that has fewer columns specified in it (both single TD columns and TDs that span more than 1 column) will fill in as many columns as it can, left to right (unless rtl text is specified in the CSS), so you can have rows of differing numbers of columns, because a TD cell cannot spill out of it's column unless a colspan attribute is set (in other words, a TD has to occupy an integer number of columns, not partial columns)


    EDIT: You can only use the method I have described when you are dealing with only 1 table. To align 2 tables, wrap them both in a parent element (DIV), and then give both tables a width of 100%. You can then control the width of the div, which will make the tables inside use all available space. The problem then becomes one where you just have to make sure the tables fill up the space in the same way, which is easier because it is more controlled.

    0 讨论(0)
提交回复
热议问题