css - 为什么在使用 margin auto 居中元素时 max-width 的行为与 width 不同?

标签 css

对图像使用 max-width 而不是 width 可以避免对图像进行上采样,但为什么这在使用时会有所不同:

max-width: 800px;
margin: 0 auto;

最佳答案

如果你想在你的容器的中心显示你需要display:block,所以代码应该是:

max-width: 800px;
display: block;
margin: 0 auto;

max-width 用于填充所需的宽度。

关于css - 为什么在使用 margin auto 居中元素时 max-width 的行为与 width 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519537/

相关文章:

html - 如何在CSS中使用 "float:right"元素的最小宽度?

javascript - 这些天什么更容易使用或更容易实现 a :hover or javascript coding for changes on links?

html - 修复字体响应

html - 为什么我的按钮总是出现在所有内容之上?

html - Web 应用程序更新 : Caching old app

javascript - 单击时的 CSS 动画

html - Div 应包含屏幕的 100% 高度

css - 无限从上到下的纯 CSS 照片横幅,堆叠在第一张图片上

jquery - Flexslider 容器不调整大小

css - 如何使 CSS 网格在 IE 上工作?