Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max-width not working for table-cell

Tags:

html

css

For me, the expected behavior would be that the middle cell fills the whole space horizontally as long as its container is not wider than 500px. However, it doesn't matter how large I scale the page, the middle cell always fills up 100%, ignoring the max-width property, and the chocolate never shows :(

div.container {
    width: 100%;
    display: table;
}
div.container > * {
    display: table-cell;
}
div.middle {
    width: 100%; max-width: 500px;
    background-color: black;
}
div.side {
    width: auto;
    background-color: chocolate;
}
<div class="container">
    <div class="side"></div>
    <div class="middle">.</div>
    <div class="side"></div>
</div>

What causes the problem, and what could be a workaround?

like image 325
tom Avatar asked Oct 24 '15 12:10

tom


People also ask

How do you set the maximum width of a table?

If you want to make your max-width to work, you need to set the CSS property table-layout: fixed; on the table and use width , not max-width . Show activity on this post. Add the following rule your css section. Show activity on this post.

How fix TD table width?

By using CSS, the styling of HTML elements is easy to modify. To fix the width of td tag the nth-child CSS is used to set the property of specific columns(determined by the value of n) in each row of the table.

Can every cell of table have different width?

HTML tables can have different sizes for each column, row or the entire table. Use the style attribute with the width or height properties to specify the size of a table, row or column.

How do you change the width of a table cell in HTML?

To set the cell width and height, use the CSS style. The height and width attribute of the <td> cell isn't supported in HTML5. Use the CSS property width and height to set the width and height of the cell respectively.


Video Answer


2 Answers

I guess the reason is quite clear to you now, so i am just going to suggest you a workaround to this. By using display:block/inline-block; instead of display:table/table-cell;.

So the solution is:

div.container {
  width: 100%;
  display: block; /* Changed from display:table; */
}
div.container > * {
  display: inline-block; /* Changed from display:table-cell; */
  float:left; /* Added floating to avoid space between elements */
}
div.middle {
  width: 100%; 
  max-width: 500px;
  background-color: black;
}
div.side {
  width: auto;
  background-color: chocolate;
}
<div class="container">
  <div class="side">Left</div>
  <div class="middle">.</div>
  <div class="side">Right</div>
</div>

Working : Fiddle

Updated Solution:

Found a solution

Going back to tables would work by giving table-layout:fixed; to container.

Working Demo

HTML

<div class="container">
  <div class="side">Left</div>
  <div class="middle">.</div>
  <div class="side">Right</div>
</div>

CSS

html, body {
    width:100%;
    height:100%;
    margin:0;
    padding:0;
}
.container {
    display:table;
    width:100%;
    height:10px;
    table-layout:fixed;
}
.side, .middle {
    display:table-cell;
    text-align:center;
    vertical-align:middle;
}
.middle {
    width:500px;
    background:black;
}
.side {
    width : calc(50% - 500px);
    overflow:hidden;
    background:chocolate;
}

@media (max-width: 500px) {
    .side
    {
        display:none;
    }
    .middle
    {
        width:100%;
    }
}
like image 167
divy3993 Avatar answered Sep 21 '22 08:09

divy3993


Thanks Aziz, now I know that max-width shouldn't be used for table cells as

In CSS 2.1, the effect of 'min-width' and 'max-width' on tables, inline tables, table cells, table columns, and column groups is undefined.

Waiting for workarounds...

like image 25
tom Avatar answered Sep 21 '22 08:09

tom