简体   繁体   English

CSS 宽度和最大宽度组合

[英]CSS width and max-width combined

I have the following code:我有以下代码:

<table style="width: 100%; max-width: 800px; table-layout: fixed;">
    ... table stuff here ...
</table>

I think it's obvious I intend for the table to take the full width available, with a capped maximum size of 800px.我认为很明显我打算让表格采用可用的全宽,最大尺寸上限为 800 像素。

This works in Inte.net Explorer and Firefox, however in Chrome it appears that the max-width is being ignored when width is present.这适用于 Inte.net Explorer 和 Firefox,但是在 Chrome 中,当width存在时, max-width似乎被忽略了。

I have tried using max-width: 100%; width: 800px;我试过使用max-width: 100%; width: 800px; max-width: 100%; width: 800px; , which again works in IE and FF but the max-width is ignored in Chrome. ,这在 IE 和 FF 中再次有效,但max-width在 Chrome 中被忽略。 I have tried using just max-width: 800px but in Chrome the table comes out 1159 pixels wide instead... !我试过只使用max-width: 800px但在 Chrome 中,表格显示为 1159 像素宽......!

If anyone can help with this, it would be much appreciated.如果有人可以提供帮助,我们将不胜感激。

Add

display: block;

to the table element's style attribute (preferably in a CSS file or the <style> section of the document rather than as in inline style). 表元素的style属性(最好是在CSS文件或文档的<style>部分,而不是在内联样式中)。

<div> elements have display: block by default, while <table> elements have display: table; <div>元素有display: block默认情况下,而<table>元素有display: table; by default. 默认。 Your problem is that the max-width property only applies to block elements, so you have to apply display: block; 您的问题是max-width属性仅适用于块元素,因此您必须应用display: block; to the table. 到桌子。

Wrapping in a div with max-width and using display: block don't work on Chrome 66. However, table-layout: fixed does: https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout 使用max-width包含div并使用display: block在Chrome 66上不起作用。但是, table-layout: fixed确实可以: https//developer.mozilla.org/en-US/docs/Web/CSS/表格的布局

Table and column widths are set by the widths of table and col elements or by the width of the first row of cells. 表和列宽度由table和col元素的宽度或第一行单元格的宽度设置。 Cells in subsequent rows do not affect column widths. 后续行中的单元格不会影响列宽。 Under the "fixed" layout method, the entire table can be rendered once the first table row has been downloaded and analyzed. 在“固定”布局方法下,可以在下载和分析第一个表行后呈现整个表。 This can speed up rendering time over the "automatic" layout method, but subsequent cell content might not fit in the column widths provided. 这可以加快“自动”布局方法的渲染时间,但后续单元格内容可能不适合所提供的列宽。 Cells use the overflow property to determine whether to clip any overflowing content, but only if the table has a known width; 单元格使用overflow属性来确定是否剪切任何溢出内容,但仅当表格具有已知宽度时; otherwise, they won't overflow the cells. 否则,它们不会溢出细胞。

Wrap the table in a div that has max width: 将表包装在具有最大宽度的div中:

<div style="width: 100%; max-width: 600px;">
    <table style="width:100%;">
        <tr><td></td></tr>
     </table>
</div>

You can use: 您可以使用:

min-width:100%; max-width:800px

It works for me in both IE9 and Chrome. 它适用于IE9和Chrome。

You just have it backwards.你只是把它倒过来。 It should be width:800px; max-width:100%;它应该是width:800px; max-width:100%; width:800px; max-width:100%; – this will make the layout shrink when the window is smaller than 800px, as opposed to trying to limit the 100% page width to 800px (which doesn't work). – 当 window 小于 800px 时,这将使布局缩小,而不是试图将 100% 页面宽度限制为 800px(这不起作用)。

Use min-width:800 or you want and set the width:100%. 使用最小宽度:800或您想要并设置宽度:100%。 If size of your browser it will set but the size of your page will not be less than the 800px 如果您将设置浏览器的大小,但页面大小不会小于800px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM