CSS Position - top 100% Is Not Equal To bottom 0

你。 提交于 2021-02-04 16:35:08

问题


I noticed this when I assign fixed position to an element for css3 animation, that top: 100% isn't gave the same effect as bottom: 0;. It's locate the element outside of document, while bottom:0; is still showing the whole of the element.

JSFIDDLE DEMO

Is there an opposite of css position top:0; that is automatically give the same effect as bottom:0;?


回答1:


That is because top value takes the top edge as a reference point, you need to use transform: translateY(-100%) to make the bottom edge a reference point.

.top {
  position: fixed;
  top: 100%;
}
.bottom {
  position: fixed;
  top: 100%;
  transform: translateY(-100%);
}
<div class="top" style="padding:20px;border:1px solid #000;">TOP: 100%;</div>

<div class="bottom" style="padding:20px;border:1px solid #000;">BOTTOM: 0;</div>



回答2:


.top{
    position:fixed;
    top: calc(100% - 60px);    
}

is equal to

.bottom {
    position:fixed;
    bottom:0;    
}

.top{
    position:fixed;
    top: calc(100% - 60px); /*60px the height of the element*/
    right: 0
}

.bottom {
    position:fixed;
    bottom:0;  
    left: 0
}
<div class="top" style="padding:20px;border:1px solid #000;">TOP: 100%;</div>

<div class="bottom" style="padding:20px;border:1px solid #000;">BOTTOM: 0;</div>

or

.top{
    position:fixed;
    top: 100%; 
    margin-top: -60px; /*60px the height of the element*/
    right: 0
}

.bottom {
    position:fixed;
    bottom:0;    
}
<div class="top" style="padding:20px;border:1px solid #000;">TOP: 100%;</div>

<div class="bottom" style="padding:20px;border:1px solid #000;">BOTTOM: 0;</div>


来源:https://stackoverflow.com/questions/27997865/css-position-top-100-is-not-equal-to-bottom-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!