html - 转义固定宽度的 CSS 列
问题描述
如果您被困在页面换行中 - 假设页面的所有内容都在一个宽度为 900 像素的 div 中,那么您需要一个 div 内,即整个页面宽度。最简单的方法是什么?
我知道你可以结束 900px div,做全宽 div,然后开始另一个 900px div,但是有没有办法设置内部 div 的样式,这样你就不必逃避它了?100vw 可以使其尺寸合适,但不能将其放置在正确的位置。
如此简单的例子:
<div style="width:900px;margin-right:auto;margin-left:auto;display:block;">
<p>text text</p>
<div style="width:100vw;">
<p>I want this section to be the full page width and centered</p>
</div>
<p>text text</p>
</div>
谢谢!
解决方案
您可以使用负左边距(-50vw + half parent width)
。
body {margin: 0;}
#a {background: red;}
#b {background: green; margin-left: calc(-50vw + 200px)
<div id="a" style="width:400px;margin-right:auto;margin-left:auto;display:block;">
<p>text text</p>
<div style="width:100vw;" id="b">
<p>I want this section to be the full page width and centered</p>
</div>
<p>text text</p>
</div>
对于这个代码示例,我添加了ID
s(用于更简洁的 CSS 样式)并将父 div 更改为400px
(因为窗口较小)。
推荐阅读
- influxdb - InfluxDB - 查询每个现有标签的最后一个值的 MAX
- python - optomax 数字液体传感器 - 树莓派的 python
- java - Remove every space EXCEPT leading spaces
- canvas - 当我有响应的画布宽度和高度时,html5画布的问题无法呈现矩形
- java - 在 SSL SOAP 请求期间: SSLHandshake.consume 忽略 com.sun.jndi.ldap.object.disableendpointidentification 的值
- java - 如果默认浏览器是边缘或使用 java 代码的 Chrome,如何限制浏览器的启动
- javascript - 添加组件以查看 React Native
- azure - Azure 混合连接管理器错误安装
- android - 错误:元素类型无效:预期为字符串(用于内置组件).......升级本机版本后
- google-cloud-platform - 是否有一种简单的方法可以在 Google 云中的项目之间克隆 SQL 实例?