How to overlay one div over another div

前端 未结 9 1883
旧时难觅i
旧时难觅i 2020-11-21 17:39

I need assistance with overlaying one individual div over another individual div.

My code looks like this:

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 17:59

    Here follows a simple solution 100% based on CSS. The "secret" is to use the display: inline-block in the wrapper element. The vertical-align: bottom in the image is a hack to overcome the 4px padding that some browsers add after the element.

    Advice: if the element before the wrapper is inline they can end up nested. In this case you can "wrap the wrapper" inside a container with display: block - usually a good and old div.

    .wrapper {
        display: inline-block;
        position: relative;
    }
    
    .hover {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 188, 212, 0);
        transition: background-color 0.5s;
    }
    
    .hover:hover {
        background-color: rgba(0, 188, 212, 0.8);
        // You can tweak with other background properties too (ie: background-image)...
    }
    
    img {
        vertical-align: bottom;
    }

提交回复
热议问题