[英]Changing position of absolute element without container overflow
Maybe an obvious question but how do I make an element with a absolute position not overflow its container when moving it's position right? 也许是一个明显的问题,但是当正确移动位置时,如何使绝对位置的元素不会溢出其容器? I know I could change it to relative position or move it 99% but for my project that won't due.
我知道我可以将其更改为相对位置或将其移动99%,但是对于我的项目而言,这是不可行的。 I tried using margins, padding, object-fit, all with no success.
我尝试使用边距,填充,对象适合,但都没有成功。 Thanks for any help
谢谢你的帮助
var green = document.getElementById('green'); function myFunct() { green.style.right = '100%'; }
h1 { position: relative; width: 80%; height: 100px; margin: auto; background-color: red; } #green { position: absolute; right: 0px; height: 100px; background-color: green; width: 20px; }
<h1> <div id = 'green'></div> </h1> <button onclick="myFunct()">FindHighScore</button>
Use CSS calc()
使用CSS
calc()
var green = document.getElementById("green");
function myFunct() {
green.style.right = "calc(100% - 20px)";
}
Or, apply left: 0
and right: auto
(reset) 或者,
left: 0
应用left: 0
, right: auto
(重置)
var green = document.getElementById("green");
function myFunct() {
green.style.left = "0";
green.style.right = "auto";
}
A <div>
should not be in a <h1>
tag by the way. 顺便说一下,
<div>
不应位于<h1>
标记中。
You can set overflow
to hidden
at parent container. 您可以将
overflow
设置为hidden
在父容器中。
<h1>
permitted content is Phrasing content <h1>
允许的内容是短语内容
var green = document.getElementById('green'); function myFunct() { green.style.right = '100%'; }
div:not(#green) { position: relative; width: 80%; height: 100px; margin: auto; background-color: red; overflow: hidden; } #green { position: absolute; right: 0px; height: 100px; background-color: green; width: 20px; }
<div> <div id='green'></div> </div> <button onclick="myFunct()">FindHighScore</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.