Caz*_*tor 7 css css-position css-calc
我正在尝试创建一个宽度和高度为视图宽度 37% 的 div。
我希望 div 位于中间居中,因此我尝试采用视图宽度的 50% 并减去 37% 的值。
然后我希望 div 位于父 div 之外 50%。它基本上是一张带有个人资料图片的封面照片。底部位置需要为负数,以便将业务封面徽标强制放在业务视图包装器之外,而我能想到的唯一方法就是乘以负数。
<div class="business-view-cover">
<div class="business-cover-logo"></div>
</div>
.business-view-cover {
position: relative;
height: calc(100vw * 0.5625);
background-size: cover;
background-position: center center;
background-image: url('../images/business-cover-1.png');
.business-cover-logo {
position: absolute;
--width-profile: calc(100vw * 0.37);
--half-width: calc(var(--width-profile) / 2);
--profile-bottom: calc(-1 * var(--half-width));
bottom: calc(var(--profile-bottom) * -1);
left: calc(50vw - var(--width-profile));
width: var(--width-profile);
height: var(--width-profile);
border: 4px solid $e1-background-grey;
border-radius: 1.6rem;
background-size: cover;
background-position: center center;
background-image: url('../images/logo-cover-napa.png');
}
}
Run Code Online (Sandbox Code Playgroud)
使用固定值的示例。
.business-view-cover {
position: relative;
height: calc(100vw * 0.5625);
background-size: cover;
background-position: center center;
background-image: url('../images/business-cover-1.png');
.business-cover-logo {
position: absolute;
bottom: -7.65rem;
left: calc(50vw - 7.65rem);
width: 15.3rem;
height: 15.3rem;
border: 4px solid $e1-background-grey;
border-radius: 1.6rem;
background-size: cover;
background-position: center center;
background-image: url('../images/logo-cover-napa.png');
}
}
Run Code Online (Sandbox Code Playgroud)
在了解您的需求后,我编辑了我的答案:要定位您的徽标,请使用position: relative并将其设置bottom为负值,为了使徽标居中,请使用left: calc(50% - 100px)徽标100px大小的一半。
超文本标记语言
<div class="cover-photo">
<div class="logo">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
.cover-photo{
width: 100%;
margin: auto;
border: 1px solid black;
text-align: center;
}
.logo{
background-color: navy;
width: 200px;
height: 200px;
position:relative;
bottom: -100px;
left: calc(50% - 100px);
}
Run Code Online (Sandbox Code Playgroud)
另外,CSS 中没有嵌套类,您应该移到.business-cover-logo括号之外
| 归档时间: |
|
| 查看次数: |
28824 次 |
| 最近记录: |