且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

边距左边距:自动元素=填充左边100%宽度溢出项目

更新时间:2022-06-07 22:49:23

calc c>方法



这可以通过(实验) calc 属性来完成:

div article {
    margin-left: calc(50% - 300px/2);
}

演示

这里必须输入 < header> (在这种情况下 300px ),它会自动确定 50% 是。如果您更改< header> 的样式,并且太多浏览器不支持

Here you'd have to enter the width of the <header> (in this case 300px) and it will automatically determine what 50% - (width) / 2 is. This won't automatically change if you change the style for the <header> though, and it is not supported in too many browsers.

这是一个可以在每个现代浏览器中使用的方法:定义 定位,然后留下负值 margin

This is a method that would work in every modern browser: defining a left positioning, and then a negative left margin:

div article {
    position:relative;
    left: 50%;
    margin-left: -150px;
}

演示

首先将元素移动到网页上的50%宽度,然后通过其负边距将其向左移动150px。您必须输入< header> 的宽度,例如 calc()方法,你必须自己除以2(不应太难:P)

This first moves the element to 50% width on the page, and then moves it 150px to the left via its negative margin. You will have to enter the width of the <header>, like the calc() method, but here you'll have to divide it by 2 yourself (shouldn't be too hard :P)