Is there a CSS way to position an HTML element vertically following the golden ratio?

前端 未结 3 931
终归单人心
终归单人心 2021-02-10 00:58

I want to position an HTML element in the horizontal middle and vertical golden ratio with CSS only. The height of the element must be absolutly flexible, so I can not just set

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-10 01:30

    Okay, I've tested this and it appears to work. The trick though requires the two divs, inner and dummy to have exactly the same contents. dummy is used to give the outer div the appropriate height so that the inner div can be positioned by a percentage of that height. A bit hacky but no javascript.

    http://jsfiddle.net/fVQeC/4/

    Something
    too
    more
    more
    more
    more
    Something
    too
    more
    more
    more
    more

    .outer{
        position: absolute;
        top: 38.2%;
    }
    
    .inner{
        width: 200px;
        background-color: blue;
        position: absolute;
        top: -38.2%;
    }
    
    .dummy{
        width: 200px; 
       visibility: hidden; 
    }
    

提交回复
热议问题