html - 如何使用 position: absolute 定位元素,使其下半部分隐藏在视口下方?底部:-50%;不工作
问题描述
正如您在下面的片段中看到的,我在页面底部有一个绝对定位的红色圆圈。我想要实现的是让圆的上半部分在屏幕的底部可见,而将下半部分隐藏在视口的底线下方。
我想做底部:-50% 会解决我的问题,但是,它隐藏了超过 70-80% 的圆圈,这让我感到困惑。
*, *:after, *:before {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #000;
color: white;
}
.container {
position: relative;
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
overflow: hidden;
}
.test-layer8 {
position: absolute;
background-color: red;
border-radius: 50%;
bottom: -50%;
width: 500px;
height: 500px;
}
<div class="container" @mousemove='getCoordinates($event)'>
<div class="test-layer8"></div>
</div>
解决方案
使用绝对位置,一旦元素被发送到坐标,边距或平移可以帮助重置屏幕上的位置:
bottom:0;
+translateY(50%)
可能会在这里帮助你
*, *:after, *:before {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #000;
color: white;
}
.container {
position: relative;
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
overflow: hidden;
}
.test-layer8 {
position: absolute;
background-color: red;
border-radius: 50%;
bottom: 0;
transform:translateY(50%);
width: 500px;
height: 500px;
}
<div class="container" @mousemove='getCoordinates($event)'>
<div class="test-layer8"></div>
</div>
- 从已知高度 :
bottom : 0 ;
+margin-bottom -x
; 也可以。
*, *:after, *:before {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #000;
color: white;
}
.container {
position: relative;
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
overflow: hidden;
}
.test-layer8 {
position: absolute;
background-color: red;
border-radius: 50%;
bottom: 0;
margin-bottom:-250px;
width: 500px;
height: 500px;
}
<div class="container" @mousemove='getCoordinates($event)'>
<div class="test-layer8"></div>
</div>
推荐阅读
- c# - 如何用操纵杆移动鼠标光标?
- kubernetes - 为什么 AKS 创建的 Azure 负载均衡器设置为将流量定向到节点上的端口 80 和 443,而不是服务打开的节点端口?
- angular - 从输入字段更新日期时获取 ngx-daterangepicker-material 的最新更新
- r - 未提供编码:默认为 UTF-8。性格洞察分析
- awk - 如何用awk替换找到的字符串?
- arrays - Pyspark:检查元组是否包含在元组列表中
- r - 使用 RenderPlot 在 R 中显示图形
- r - 用相同的列R替换基于另一个df的列中的一些值
- regex - 如何使用 -Filter(使用正则表达式)?
- android - Android Q foregroundServiceType 属性