html - 如何使绝对定位的元素在需要为窗口的 50% 时对齐中心
问题描述
我需要制作一个宽度为“内容包装器”宽度的 50% 的横幅,并且该内容包装器的大小动态调整为最大宽度(因此理论上可以是 0px-1330px)。内容包装器是这里的深灰色框。它包含网站的内容。在该内容中是从窗口一侧出现的横幅。此处的窗口恰好具有黑色轮廓的边界。
如何使用 HTML/CSS 使紫色容器的内容适合黑暗的“内容包装”,即使它是窗口的 50%,并且当我不知道具体内容时,内容与包装的左侧对齐宽度。
我已经尝试了各种数学,但我无法完全理解。我正在使用 CSSvar
作为站点最大宽度,我可以使用var
或calc
完成这项工作,但没有组合工作。
解决方案
这符合你的问题吗?
* {
margin: 0;
padding: 0;
list-style: none;
}
.wrap {
width: 100%;
max-width: 500px;
height: 100vh;
margin: auto;
background-color: #aaa;
}
.banner {
width: 50%;
background-color: #a0f;
padding: 20px 20px 20px 0;
position: relative;
box-sizing: border-box;
}
.banner::before {
content: '';
position: absolute;
background-color: #a0f;
top: 0;
left: calc(100% - 50vw);
height: 100%;
width: calc(50vw - 100%);
}
<div class="wrap">
<h1>Content Wrapper</h1>
<div class="banner-wrap">
<div class="banner">
<h2>The title here</h2>
<p>Some other text here</p>
</div>
</div>
</div>
推荐阅读
- python - Python - 执行时间长
- html - Div 调整大小以适合子图像尺寸
- gatsby - 安装 gatsby 图像插件时出错
- symfony - JWT 令牌的 UniqueConstraint - 如何?
- python - Python中的Unicode错误和列表索引超出范围错误
- javascript - 如何提高我的应用程序的性能?Nodejs 中的繁重计算
- apache-kafka - Kafka reactor - 如何禁用自动启动的 KAFKA 消费者?
- powerbi - 令牌过期时重新验证 GraphQl API 的自定义 Power Bi 连接器?
- flutter - 只有当设备连接到 USB 充电器时,flutte 应用程序才会在后台运行
- visual-studio-code - 输出窗口上的不可读字符