简体   繁体   English

更改绝对元素的位置而不会发生容器溢出

[英]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: 0right: 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.

相关问题 如何定位绝对元素并处理元素溢出的情况(没有滚动条) - How to position absolute elements and take care of case of overflow of the element (without scroller) 跨度与位置:绝对不会在容器溢出时创建滚动条 - span with position:absolute won't create scrollbar on container overflow 具有绝对定位的工具提示元素被溢出的容器剪切:auto - Tooltip element with absolute positioning being clipped by container with overflow: auto 如果指定了绝对位置和溢出-x隐藏位置,则无法滚动到元素 - Cannot scroll to element if position absolute and overflow-x hidden specified 具有绝对 position 的嵌套元素上的文本溢出省略号 - Text-overflow ellipsis on a nested element with absolute position 如果元素在容器之外,是否可以相对于容器进行绝对定位? - Is it possible to position an element with absolute positioning relative to a container if it is OUTSIDE the container? jQuery-将元素在容器内的绝对位置和旋转随机化 - JQuery - Randomise element absolute position and rotation within container 聊天-当元素到达底部时将绝对位置更改为相对位置 - Chat - Changing absolute to relative position when element reaches bottom Position absolute 不能使用溢出自动? - Position absolute is not working with overflow auto? 溢出与绝对位置和浮动位置相结合 - Overflow combined with position absolute and floats
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM