Allow specific tag to override overflow:hidden

前端 未结 11 2340
抹茶落季
抹茶落季 2020-11-27 11:47

I\'ve got a

, that\'s a certain height and width, and overflow:hidden so that specfic inner images are clipped
11条回答
  •  萌比男神i
    2020-11-27 12:50

    The trick is to keep the overflow:hidden element with position:static and position the overflowing element relative to a higher parent (rather than the overflow:hidden parent). Like so:

    .relative-wrap {
        /*relative on second parent*/
        position: relative;
    }
    
    .overflow-wrap {
        height: 250px;
        width: 250px;
        overflow: hidden;
        background: lightblue;
        
        /*no relative on immediate parent*/
    }
    
    .respect-overflow {
        position: relative;
        top: 75px;
        left: 225px;
        height: 50px;
        width: 50px;
        background: green;    
    }
    .no-overflow {
        position: absolute;
        top: 150px;
        left: 225px;
        height: 50px;
        width: 50px;
        background: red;
    }

    I also want to note, a very common use case for the desire to have an element overflow its container in this way is when you want animate the height of a dropdown or container from X to 0, and therefore need to have overflow: hidden on the container. Usually you have something inside the container that you want to overflow regardless. Since these elements are only accessibly when the container is "open", you can take advantage and set the overflow back to visible after the container is fully open, and then set it back to hidden before trying to animate the container back to height: 0.

提交回复
热议问题