html - 具有固定边距的中心响应绝对定位元素
问题描述
我有一个绝对定位的 CSS 横幅,它具有基于视口大小的响应宽度和高度。该元素以桌面和平板设备为中心,但不在移动设备上(当横幅的宽度开始缩小时)。
我不能使用经典margin: 0 auto;
技巧,因为必须将边距设置为固定值才能使横幅起作用。
代码如下:
HTML:
<h1 class="banner">A Simple CSS Banner</h1>
CSS:
@import url(https://fonts.googleapis.com/css?family=Rye);
body
{ background: #eee; }
.banner {
position: absolute;
left: 50%;
display: block;
margin: 100px -200px;
width: 400px;
max-width: calc(100% - 150px);
height: 60px;
border: 1px solid #8a1;
font: normal 30px/60px 'Rye';
text-align: center;
color: #451;
background: #9b2;
border-radius: 4px;
box-shadow: 0 0 30px rgba(0,0,0,.15) inset,
0 6px 10px rgba(0,0,0,.15);
}
.banner::before,
.banner::after {
content: '';
position: absolute;
z-index: -1;
left: -70px;
top: 24px;
display: block;
width: 40px;
height: 0px;
border: 30px solid #9b2;
border-right: 20px solid #791;
border-bottom-color: #94b81e;
border-left-color: transparent;
transform: rotate(-5deg);
}
.banner::after {
left: auto;
right: -70px;
border-left: 20px solid #791;
border-right: 30px solid transparent;
transform: rotate(5deg);
}
@media (max-width: 475px) {
.banner {
height: 90px;
line-height: 45px;
}
.banner::before,
.banner::after {
border-width: 45px;
border-right-width: 30px;
}
.banner::after {
border-left-width: 30px;
border-right-width: 45px;
}
}
链接到工作代码笔: https ://codepen.io/Adam0410/pen/QZOKdV
谢谢您的帮助!
解决方案
检查这是否对您有帮助。
@import url(https://fonts.googleapis.com/css?family=Rye);
body {
background: #eee;
}
.banner {
position: absolute;
left: 50%;
transform: translate(-50%);
display: block;
margin: 100px 0;
width: 400px;
max-width: calc(100% - 150px);
min-height: 60px;
}
.banner span {
display: block;
position: relative;
z-index: 1;
border: 1px solid #8a1;
font: normal 30px/60px "Rye";
text-align: center;
color: #451;
background: #9b2;
border-radius: 4px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.15) inset, 0 6px 10px rgba(0, 0, 0, 0.15);
}
.banner::before,
.banner::after {
content: "";
position: absolute;
z-index: -1;
left: -70px;
bottom: -24px;
display: block;
width: 40px;
height: 0px;
border: 30px solid #9b2;
border-right: 20px solid #791;
border-bottom-color: #94b81e;
border-left-color: transparent;
transform: rotate(-5deg);
}
.banner::after {
left: auto;
right: -70px;
border-left: 20px solid #791;
border-right: 30px solid transparent;
transform: rotate(5deg);
}
@media (max-width: 475px) {
.banner {
min-height: 90px;
line-height: 45px;
}
.banner::before,
.banner::after {
border-width: 45px;
border-right-width: 30px;
}
.banner::after {
border-left-width: 30px;
border-right-width: 45px;
}
}
<h1 class="banner"><span>A Simple CSS Banner</span></h1>
推荐阅读
- .net - EF fluent 映射 DbGeography 问题:类型“System.Data.Spatial.DbGeography”必须是不可为空的值类型才能将其用作参数“T”
- datetime - DateTime 的国际化不起作用
- python - 无法识别使用 **kwarg 格式的任意关键字位置参数
- java - Java Android如何将多个复选框保存到字符串如果选中并存储到SharedPref并将其作为字符串检索
- wordpress - 无法压缩我的 wordpress 网站
- ios - 请求音乐库权限 swift 或 obj-c
- c# - 按 XAttribute 的值合并 XDocument
- python - Azure Notebook 中的无头浏览器
- c++ - 如何使用 .ui 文件在 Qt Creator 中使用 cmake 制作 GUI Qt 项目
- oracle - REGEXP_REPLACE 以下如何工作?