简体   繁体   English

使用CSS为SVG和DIV提供相同的背景色

[英]Give SVG and DIV the same background color with css

I really did not understand much about masking/clipping. 我真的对蒙版/剪切了解不多。 I´m trying something new with svg as a arrow for a chat bubble. 我正在尝试使用svg作为聊天气泡的箭头的新功能。 Here is my pen: codepen 这是我的笔: codepen

 *, *::after, *::before { box-sizing: border-box; } html, body, div, b { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } html { font-size: 62.5%; } body { font: { size: 1.6rem; family: 'Lato', sans-serif; } background-color: #132333; overflow: hidden; } .chat-container { position: relative; display: block; width: 100%; height: 100%; padding: 60px 40px; } .msg { position: relative; z-index: 100; display: inline-block; width: 100%; &:not(:first-child) { margin-top: 40px; } .box { position: relative; max-width: 900px; width: 70%; padding-bottom: 20px; display: inline-block; .user { position: relative; display: block; float: inherit; vertical-align: top; img { border-radius: 50%; width: 50px; height: 50px; } } .bubble { position: relative; display: block; float: inherit; max-width: 600px; min-height: 100%; padding: 5px 25px; margin: 0 auto; border-radius: 40px; p { text-align: left; font-size: 16px; line-height: 24px; word-wrap: break-word; direction: ltr; } &::before { content: ''; position: absolute; display: block; float: inherit; top: 18px; width: 20px; height: 25px; -webkit-clip-path: url(http://imgh.us/bubble.svg); -webkit-mask: url(http://imgh.us/bubble.svg); clip-path: url(http://imgh.us/bubble.svg#svgClip); z-index: 0; } } .info { position: absolute; bottom: 0; font-size: 12px; color: rgba(255, 255, 255, 0.3); } &.left { float:left; .user img { margin-right: 18px; } .bubble { background: linear-gradient(to right, #4589D0 0%, #1B6FAB 100%); p { color: rgba(245, 245, 245, 0.9); } &::before { left: -12px; background: #4589D0; } } .info { left: 65px; } } &.right { float:right; .user img { margin-left: 18px; } .bubble { background: rgba(255, 255, 255, 0.1); p { color: rgba(245, 245, 245, 0.5); } &::before { right: -12px; background: rgba(255, 255, 255, 0.1); -moz-transform: scaleX(-1); -webkit-transform: scaleX(-1); -o-transform: scaleX(-1); transform: scaleX(-1); -ms-filter: fliph; /*IE*/ filter: fliph; /*IE*/ } } .info { right: 65px; } } } } 
 <div class="chat-container"> <div class="msg"> <div class="box left"> <div class="user"> <img src="https://pbs.twimg.com/profile_images/515651751291006977/MrdYuJ3C_400x400.png" alt="username"> </div> <div class="bubble"> <p>Hey digger, wie gehts dir. Hast du schon gehört das cih jetzt ein neues chatt-app erstelle für den Brwoser? Nein okey dann schau mal hier</p> </div> <div class="info"> <b class="pe-clock"></b> <b>Sent</b> 8:50 AM, <b>From</b> Cologne </div> </div> </div> <div class="msg"> <div class="box right"> <div class="user"> <img src="https://pbs.twimg.com/profile_images/515651751291006977/MrdYuJ3C_400x400.png" alt="username"> </div> <div class="bubble"> <p>Hey digger, wie gehts dir.</p> </div> <div class="info"> <b class="pe-clock"></b> <b>Sent</b> 8:50 AM, <b>From</b> Cologne </div> </div> </div> </div><!-- .chat-container --> 

So my question is: how can I give the svg before element and the div itself the same background color. 所以我的问题是:如何给元素前的svg和div本身赋予相同的背景色。 Right now both are overlapping... 现在两者都是重叠的...

The easiest fix would be, if you used a solid color instead of a transparent one. 最简单的解决方法是,如果您使用纯色而不是透明色。

replace 更换

background: rgba(255, 255, 255, 0.1);

with

background: #2A3847;

With that the overlap wont be visible. 这样,重叠将不可见。

EDIT: 编辑:

If you want to keep transparency, you can make the bubble shape separate from the text, stretch it over your original bubble and make it solid color with opacity: 如果要保持透明度,可以使气泡形状与文本分开,将其拉伸到原始气泡上,并使其具有不透明性的纯色:

<div class="bubble">
    <p>Hey digger, wie gehts dir.</p>
    <div class="bubble-shape"></div>
</div>
.bubble {
    position: relative;
    display: block;
    float: inherit;
    max-width: 600px;
    min-height: 100%;
    padding: 5px 25px;
    margin: 0 auto;
    /* p css */
    .bubble-shape {
        position: absolute;
        left: 0; top: 0; right: 0; bottom: 0;
        border-radius: 40px;
        z-index: -1;
        &::before {
            content: '';
            position: absolute;
            display: block;
            float: inherit;
            top: 18px;
            width: 20px;
            height: 25px;
            -webkit-clip-path: url(http://imgh.us/bubble.svg);
            -webkit-mask: url(http://imgh.us/bubble.svg);
            clip-path: url(http://imgh.us/bubble.svg#svgClip);
            z-index: -1;
        }
    }
    p { /* css */ }
}
&.left {
    /* css */
    .bubble {
        .bubble-shape {
            background: linear-gradient(to right, #4589D0 0%, #1B6FAB 100%);
            /* p css */
            &::before {
                left: -12px;
                background: #4589D0;
            }
        }
    }
    /* css */
}
&.right {
    /* css */
    .bubble {
        .bubble-shape {
            background: rgb(255, 255, 255); /* solid color */
            opacity: 0.1; /* add opacity */
            /* p css */
            &::before {             
                right: -12px;
                background: rgb(255, 255, 255); /* solid color */
                -moz-transform: scaleX(-1);
                -webkit-transform: scaleX(-1);
                -o-transform: scaleX(-1);
                transform: scaleX(-1);
                -ms-filter: fliph; /*IE*/
                filter: fliph; /*IE*/
            }
        }
    }
    /* css */
}

full codepen 全码本

I didn't dig very far into the issues you might have with svgs, but it looks like your problem is just using a background color with opacity. 我没有深入探讨svgs可能遇到的问题,但看来您的问题只是使用具有不透明性的背景色。

I just switched to using full opacity color on the bubble and the arrow and it fixed it. 我只是切换为在气泡和箭头上使用完全不透明的颜色,并对其进行了修复。

background: rgba(41, 57, 71, 1);

工作解决方案的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM