- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直面临一个我发现难以解决的问题。
基本上,我希望 div 从 margin 0 auto 的左侧位置开始,并在视口(viewport)边缘结束。请参阅下图以帮助说明这一点。
HTML 的基本部分是这样的......
<div style="max-width:500px; margin: 0 auto;">
<div> <!-- needs to start at left margin and stretch to 100% of viewport -->
<ul>
<li></li>
</ul>
</div>
</div>
如有任何帮助,我们将不胜感激!
谢谢
最佳答案
是的,这是完全可以实现的。但是,在使用 calc
之前,请记住它是 CSS 3 并随 some usability limitations 一起提供。 (没有 IE8,没有 Opera Mini)。
.container {
max-width: 500px;
margin: 0 auto;
}
.container > div {
margin-top: 20px;
min-height: 200px;
background-color: #ccc;
}
.container > div > ul {
width: 100%;
margin: 0;
padding: 0;
}
.container > div > ul > li {
padding: 10px;
list-style-type: none;
}
.container > div > ul > li:nth-child(2) {
width: calc(((100vw - 100%) / 2) + 100%);
background-color: red;
min-height: 30px;
color: white;
box-sizing: border-box;
text-align: right;
}
<div class="container">
<div>
<!-- needs to start at left margin and stretch to 100% of viewport -->
<ul>
<li>first li</li>
<li>second li</li>
</ul>
</div>
</div>
大部分 CSS 是为了让它看起来像您的图片。但是模型是:
.grand-parent {
width: {some-fixed-width}; // 500px in our case
margin: 0 auto;
position: relative;
padding-left: 0;
padding-right: 0;
}
.parent {
width: 100%;
padding-left: 0;
padding-right: 0;
margin-left: 0;
margin-right: 0;
}
.child {
width: calc(((100vw - 100%) / 2) + 100%);
box-sizing: border-box;
margin-left: 0;
margin-right: 0;
}
其中一些甚至没有出现在我上面的代码中,因为它们是默认值,但它们对于它的工作很重要。如果您决定在父级上使用一些填充,则必须在子级中补偿它,无论是在 calc 中(100%
变为 100%
+ left-padding of parent
+ right-padding-of-parent
) 和左边距(negative left margin of parent
)。但是,为了简单起见,不要在父级上使用左右边距或填充。 :))
jsFiddle here .
关于html - 居中左边距但右边距 100% 的视口(viewport)(到最后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34017564/
我正在开发一个需要在屏幕上查找对象的程序,到目前为止工作正常,但我遇到了多显示器配置问题。 GraphicsEnvironment.getLocalGraphicsEnvironment().getS
使用 mySql,我想列出客户对特定产品类别进行的所有购买。 所以,我有 3 个表:客户 (idCustomer, Name)、类别 (idCategory, CategoryName) 和订单 (i
我的网站上有一个关于 background-size:cover 的小问题我一直在 Firefox 中测试它,但是当我在谷歌浏览器中加载页面时,我在左边得到 1px 的白色。当我使用 backgrou
我已经搜索了几个小时来找到解决我的问题的方法,但没有成功。我遇到的问题是两个按钮的垂直堆叠。 这就是我想要做的:它说按钮在这里两次是我试图放置按钮的地方,但我所能做的就是让它们水平排列而不是垂直排列。
我有一个包含多个元素的导航栏。 Left1 Left2 Left3 Right1 Right2 Right3 我不知道如何将“fixedLef
您好,我正在尝试让 2 个 div 在左侧与右侧对齐。 #div1 #div2 #div1 #div2 #div3 #div2 #div3 #div3 诀窍是当浏览器窗口变小时,我希望#div2 位于
body { font-family: Arial, Helvetica, sans-serif; font-size: 13px;
这个问题在这里已经有了答案: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties? (6
这是我的代码的 jsfiddle 链接: https://jsfiddle.net/Krisalay/zvxxeagh/ 我的 HTML 代码是: MESSAGE 1
所以我有 10 个复选框,每个标签都取自数组中相应的索引。我正在使用 ng-repeat 来展示它们: {{entity}}
我是一名优秀的程序员,十分优秀!