11

For those who can't wait Fiddle

I have this problem in CSS. The structure of the html and css code looks like this

HTML:

<div class="one">
    <div class="two">
        <div class="three">

        </div>
    </div>
</div>

CSS:

.one{
    width: 500px;
    height: 500px;
    background: url('http://www.moonlightcompanies.com/wp-content/uploads/2013/01/6973_MOONF-PomegranatesOnTree-8536-1.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    padding: 20px;
}

.two{
    width: 300px;
    height: 300px;
    background: blue;
    padding: 20px;
}

.three{
    width: 200px;
    height: 200px;
    background: transparent;
    padding: 20px;
    border: 5px solid yellow;
}

My problem is how do I make the background of <div class="three"></div> that it would be transparent and would blend to the background of the <div class="one"></div>. I want my desired output to be like the attached image. Is this possible ?

enter image description here

web-tiki
  • 99,765
  • 32
  • 217
  • 249
John
  • 425
  • 5
  • 19
  • 2
    The background is transparent but it transparent to its parent, and the background color of its parent is - blue. Consider "switching" the divs, the `second` div would be the transparent one and the `third` div would be the blue background one. – Ofir Baruch May 27 '15 at 06:48
  • background of three is transparent but two is not transparent. – ketan May 27 '15 at 06:51
  • This is related : http://stackoverflow.com/questions/27485098/css-transparent-border-showing-the-behind-background-image – web-tiki May 27 '15 at 07:46
  • May I ask why you didn't you use a [stack snippet](http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) to post your code? You even divided the html from css, it's really a matter of seconds to integrate this. Consider this instead of using an external service like jsfiddle. – Bakuriu May 27 '15 at 14:51

6 Answers6

14

You can try like this: Demo

.three{
    width: 200px;
    height: 200px;
    background: transparent;
    padding: 20px;
    border: 5px solid yellow;
    outline:solid 100px rgba(0,0,255,.5);
}

update the border value as per your requirement.

Updated Demo

.three {
    background: transparent;
    border: solid blue;
    margin: 10px 0px;
    border-width:20px 40px 40px 20px;
}
.inner {
    outline: 5px solid yellow;
    width: 200px;
    height: 60px;
    margin:0;
    padding: 20px;
}
G.L.P
  • 7,119
  • 5
  • 25
  • 41
  • This works great Sir :) I have another question. I've updated my fiddle http://jsfiddle.net/johnsemblante/ouy9thkk/5/ what if I have that kind of structure ? how will I do such Sir ? :) Anyway thanks a lot for the answer got some idea but have still hanging questions on my mind :) Thank you in advance Sir. – John May 27 '15 at 06:57
  • Maybe it also help you: [basics-css-blend-modes](https://css-tricks.com/basics-css-blend-modes/) and to check browser compatibility [can I use blend mode](http://caniuse.com/#feat=css-backgroundblendmode) – Usman Arshad May 27 '15 at 07:07
  • Good use of outline +1! You could probably mention that box-shadow can have the same output and offer more control on the width of the blue part on all four sides. – web-tiki May 27 '15 at 07:48
4

I have added :before and :after also for blue background in right and bottom, so that more content can be added in the second div.

Here is the fiddle - http://jsfiddle.net/afelixj/ouy9thkk/15/

Felix A J
  • 6,300
  • 2
  • 27
  • 46
3

As per your updated fiddle: https://jsfiddle.net/ouy9thkk/14/

I have used box shadow to achieve the expected result.

HTML

<div class="one">
    <div class="two">
        <div class="three"></div>
        <div class="three"></div>
        <div class="three"></div>
    </div>
</div>

CSS

.one{
    width: 800px;
    height:800px;
    background: url('http://www.moonlightcompanies.com/wp-content/uploads/2013/01/6973_MOONF-PomegranatesOnTree-8536-1.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    padding: 20px;
}

.two{
    width: 300px;
    height: 300px;
    padding: 20px;
}

.three{
    width: 200px;
    height: 80px;
    background: transparent;
    padding: 20px;
    margin-bottom:30px;
    border: 5px solid yellow;
    box-shadow: 10px 0px 0 30px blue
}
Nilesh Mahajan
  • 3,506
  • 21
  • 34
3

Try this Fiddle

.one{
    width: 500px;
    height: 500px;
    background: url('http://www.moonlightcompanies.com/wp-content/uploads/2013/01/6973_MOONF-PomegranatesOnTree-8536-1.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    padding: 20px;    
}
.blue{
    position: relative;
    width: 200px;
    height: 200px;
    border: 1px solid blue; 
    border-width: 20px 115px 65px 20px;
}
.yellow{
    width: 95%;
    height: 95%;
    border: 5px solid yellow;
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    margin: auto;    
}
stanze
  • 2,456
  • 10
  • 13
2

Closest I could get with this particular case:

.two{
width: 250px;
height: 250px;
background: trasparent;
padding: 0px;
border-top: 20px solid blue;
border-right: 60px solid blue;
border-bottom: 60px solid blue;
border-left: 20px solid blue;
}

JSFiddle

freewheeler
  • 1,306
  • 2
  • 12
  • 24
  • This works too Sir :) I have another question. I've updated my fiddle jsfiddle.net/johnsemblante/ouy9thkk/5 what if I have that kind of structure ? how will I do such Sir ? :) Anyway thanks for the answer :) Thank you in advance Sir. – John May 27 '15 at 06:59
-1

try this example:(

jsfiddle.net/MxspA/6/

) please use rectangle image which should transparent from center like following example

jsfiddle.net/MxspA/6/

sms247
  • 4,404
  • 5
  • 35
  • 45