html - 元素达到最大宽度时不居中

标签 html css

我的 HTML 中有一个元素具有这种样式:

{
   max-width: 1500;
   padding-rigth: 40px;
   padding-left: 40px;
   width: auto;
}

当屏幕小于 1580px 时一切正常,元素在屏幕中居中,但是当屏幕大于 1580px 时,元素不居中并粘在屏幕右侧(有 40px 填充)。

当屏幕大于 1580 像素时,如何使元素居中?

最佳答案

when the screen is less than 1580px everything is ok and the element is centered in the screen

我假设该元素是一个 block 级元素,对吧?如果是这样,您只需提供 automargin-left/margin-right 即可实现对齐。

{
   max-width: 1500;
   padding-rigth: 40px;
   padding-left: 40px;
   width: auto;
   margin-left: auto;
   margin-right: auto;
}

关于html - 元素达到最大宽度时不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306933/

相关文章:

css - Firefox float 定位和 Firefox div :hover not working how it should?

css - 我怎样才能使这个 css 边框属性成为一个衬里?

javascript - 在页面上 overflow hidden 的滚动上切换粘性菜单类

javascript - jQuery 如何使用延迟加载在滚动条上加载更多内容

html - CSS 内容不会包裹多个 div

javascript - 如何在不使用 ID 的情况下删除 div

jquery - 使用 .length 获取元素数量不准确?

html - 无法在移动设备和桌面设备上将照片居中

javascript - 在图像的特定位置显示向下滚动的文本和箭头?

iphone - iphone 上的双 header