How to make CSS3 rounded corners hide overflow in Chrome/Opera

后端 未结 13 1766
无人及你
无人及你 2020-11-22 09:10

I need round corners on a parent div to mask content from its childen. overflow: hidden works in simple situations, but breaks in webkit based browsers and Oper

相关标签:
13条回答
  • 2020-11-22 09:53

    As for me none of the solutions worked well, only using:

    -webkit-mask-image: -webkit-radial-gradient(circle, white, black);
    

    on the wrapper element did the job.

    Here the example: http://jsfiddle.net/gpawlik/qWdf6/74/

    0 讨论(0)
  • 2020-11-22 09:54

    I tried every answer but without success. After few hours of investigating I found solution for this problem. Using this properties in your class will not allow div elements to overflow the container.

    .container {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
    }
    
    0 讨论(0)
  • 2020-11-22 09:57

    If you are looking to create a mask for an image and position the image inside the container don't set the 'position: absolute' attribute. All you have to do is change the margin-left and margin-right. Chrome/Opera will adhere to the overflow: hidden and border-radius rules.

    // Breaks in Chrome/Opera.
        .container {
            overflow: hidden;
            border-radius: 50%;
            img {
                position: absolute;
                left: 20px;
                right: 20px;
            }
        }
    
    // Works in Chrome/Opera.
        .container {
            overflow: hidden;
            border-radius: 50%;
            img {
                margin-left: 20px;
                margin-right: 20px;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 09:59

    I found another solution for this problem. This looks like another bug in WebKit (or probably Chrome), but it works. All you need to do - is to add a WebKit CSS Mask to the #wrapper element. You can use a single pixel png image and even include it to the CSS to save a HTTP request.

    #wrapper {
    width: 300px; height: 300px;
    border-radius: 100px;
    overflow: hidden;
    position: absolute; /* this breaks the overflow:hidden in Chrome/Opera */
    
    /* this fixes the overflow:hidden in Chrome */
    -webkit-mask-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC);
    }
    
    #box {
    width: 300px; height: 300px;
    background-color: #cde;
    }​
    

    JSFiddle Example

    0 讨论(0)
  • 2020-11-22 09:59

    Nevermind everyone, I managed to solve the problem by adding an additional div between the wrapper and box.

    CSS

    #wrapper {
        position: absolute;
    }
    
    #middle {
        border-radius: 100px;
        overflow: hidden; 
    }
    
    #box {
        width: 300px; height: 300px;
        background-color: #cde;
    }
    

    HTML

    <div id="wrapper">
        <div id="middle">
            <div id="box"></div>
        </div>
    </div>
    

    Thanks everyone who helped!

    → http://jsfiddle.net/5fwjp/

    0 讨论(0)
  • 2020-11-22 10:00

    change the opacity of the parent element with the border and this will re organize the stacked elements. This worked miraculously for me after hours of research and failed attempts. It was as simple as adding an opacity of 0.99 to re organize this paint process of browsers. Check out http://philipwalton.com/articles/what-no-one-told-you-about-z-index/

    0 讨论(0)
提交回复
热议问题