简体   繁体   English

如何在其位置保留绝对定位的元素?

[英]How do I keep an absolutely positioned element in its place?

How can I keep an absolutely positioned element (that has been vertically centered with top: 0; bottom: 0; and margin: auto;) in its place when the height of its parent is changed? 当父级的高度发生变化时,如何保持一个绝对定位的元素(在顶部垂直居中:0;底部:0;以及margin:auto;)的位置?

The element is an img that is perfectly centered on a vertical tab. 元素是一个完全以垂直制表符为中心的img。 When the user clicks on the vertical tab it expands to show more information, this moves the perfectly centered image to now be vertically centered in the expanded tab. 当用户单击垂直选项卡时,它会展开以显示更多信息,这会将完美居中的图像移动到现在在展开选项卡中的垂直居中。

Yes,it can be possible by css3. 是的,它可以通过css3实现。 Please try this and you will get resolve your trick. 请尝试这个,你将解决你的伎俩。

<div class="responsive-container">
    <div class="dummy"></div>

    <div class="img-container">
        <img src="http://placehold.it/150x150" alt="" />
    </div>
</div>

This is css for the top 这是顶部的css

.responsive-container {
    position: relative;
    width: 100%;
    border: 1px solid black;
}

.dummy {
    padding-top: 100%; /* forces 1:1 aspect ratio */
}

.img-container {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;

    text-align:center; /* Align center inline elements */
    font: 0/0 a;
}

.img-container:before {
    content: ' ';
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}

.img-container img {
    vertical-align: middle;
    display: inline-block;
}

Please try and let me know. 请尝试让我知道。

If it is possible in this case, remove the top, left, bottom, right parameters from css - and the element will stay in its place. 如果在这种情况下可以从css中删除top,left,bottom,right参数 - 元素将保留在原位。

Those parameters make it positioned absolutely relative to the window, but removing them places the element where it had been. 这些参数使其绝对相对于窗口定位,但是移除它们将元素放置在原来的位置。 Then setting width, height and margin will do the rest of the job. 然后设置宽度,高度和边距将完成剩下的工作。

Another option is to make parent element position: relative, absolute or fixed. 另一种选择是使父元素位置:相对,绝对或固定。 Then child's position will be calculated relative to that. 然后将相对于孩子的位置进行计算。

If you don't mind a little JavaScript, this will do what you want. 如果你不介意一点JavaScript,这将做你想要的。 The img is in the vertical middle of the section at first, but it will stay in place when the size of the section changes. img最初位于该部分的垂直中间,但是当该部分的大小发生变化时,它将保持在原位。

 document.querySelector('.abs').dataset.top = document.querySelector('.abs').offsetTop; document.getElementById('more').onclick = function() { var img = document.querySelector('.abs'); if (document.getElementById('more').checked) { img.style.top = img.dataset.top+'px'; img.style.margin = '0'; } else { img.style.top = ''; img.style.margin = ''; } }; 
 section {position: relative;} #more ~ p {display: none;} #more:checked ~ p {display: block;} .abs { position: absolute; left: 50%; top: 0; bottom: 0; height: 40px; margin: auto 0; } 
 <section> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <input id="more" type="checkbox"><label for="more"> Show more</label> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor</p> <img src="https://placehold.it/150x40" alt="150x40" class="abs"> </section> 

You try this. 你试试这个。

 body,html { height:100%; margin:0; padding:0; } .second-div { width:30%; height:50%; background:red; position:absolute; top:0; bottom:0; margin:auto; } .first { width:60%; height:200px; position:relative; background:pink; } 
 <div class="first"> <div class="second-div"><div> </div> 

Use this to specify the exact position of any object. 使用此选项指定任何对象的确切位置。

<style>
Position: absolute;
Top: 0px;
Left: 0px;
</style>

This will lock the object to the top left corner. 这会将对象锁定在左上角。

And then use this to move it around: 然后用它来移动它:

<style> Transform: Translate(1px, 1px); </style>

This is just examples, modify it to fit with your format (px, %, vh/vw or whatever you use) :-) 这只是示例,修改它以适合您的格式(px,%,vh / vw或您使用的任何东西):-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将绝对定位的图像放置在其父元素已隐藏溢出的外部? - how do I place an absolutely positioned image outside its parent element which has overflow hidden? 浏览器窗口调整大小时如何保持绝对定位的元素 - How to keep absolutely positioned element in place when browser window resized 如何将绝对定位的元素居中? - How do I center an absolutely positioned element? CSS:如何将对象放置在绝对定位的对象下方? - CSS: How do I place objects under absolutely positioned objects? 如何居中绝对定位的HTML块元素? (相对于其父代?) - How do I center an absolutely positioned HTML block element? (Relative to its parent?) 如何绝对定位相对定位的元素? - How do I absolutely position a relatively positioned element? 当浏览器放大或缩小时,如何保持绝对定位的元素响应? - How can I keep an absolutely positioned element responsive when the browser is zoomed in or out? 如何使元素对绝对定位的元素做出反应,就好像它不是绝对定位的一样 - How to make an element react to an absolutely positioned element as if it were not absolutely positioned 绝对定位的元素不会被其容器夹住 - Absolutely positioned element is not clipped by its container 当绝对定位的元素(部分)离开屏幕时,如何阻止鼠标拖动时的屏幕滚动? - How do I prevent screen scrolling on mouse drag when an absolutely positioned element is (partially) off screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM