How to make a child div transparent? - html

Suppose there is a div, say "parent-div".
The parent div has a background color. What if the child div, "child-div", needs to be set with a transparent background,such that it is set with the background image of the grandparent div, with class name "wrapper"?
I know that a child div can inherit css properties from parent div, but how do I set the background to transparent, making the whole picture appear like the parent-div has a hole in it?
.wrapper{
background-image: url('http://media.istockphoto.com/photos/medium-golden-brown-wood-texture-background-picture-id513694258?k=6&m=513694258&s=170667a&w=0&h=xETakP7VjpAtRj9e6rJRYNqw_AJLZ9ovLlC4ebR5BOQ=');
}
.parent-div{
width: 100px;
height: 100px;
background: #ff0000;
padding: 10px;
margin: auto;
}
.child-div{
width: 60%;
height: 60%;
margin: auto;
background: transparent;
border: 1px solid;
}
<div class="wrapper">
<div class="parent-div">
<div class="child-div">
</div>
</div>
</div>

Don't apply background on .parent-div.
Instead use a large value of box-shadow on .child-div and add overflow: hidden on .parent-div to hide unwanted shadow effect.
Following css will do the work:
.parent-div {
overflow: hidden;
}
.child-div {
box-shadow: 0 0 0 500px #f00;
}
.wrapper {
background-image: url('http://media.istockphoto.com/photos/medium-golden-brown-wood-texture-background-picture-id513694258?k=6&m=513694258&s=170667a&w=0&h=xETakP7VjpAtRj9e6rJRYNqw_AJLZ9ovLlC4ebR5BOQ=');
}
.parent-div {
overflow: hidden;
width: 100px;
height: 100px;
padding: 10px;
margin: auto;
}
.child-div {
box-shadow: 0 0 0 500px #f00;
border: 1px solid;
width: 60%;
height: 60%;
margin: auto;
}
<div class="wrapper">
<div class="parent-div">
<div class="child-div">
</div>
</div>
</div>

Check this Fiddle
based on:
.parent{
width:300px;
height:300px;
position:relative;
border:1px solid red;
}
.parent:after{
content:'';
background:url('http://www.dummyimage.com/300x300/000/fff&text=parent+image');
width:300px;
height:300px;
position:absolute;
top:0;
left:0;
opacity:0.5;
}
.child{
background:yellow;
position:relative;
z-index:1;
}
ref

Related

Overflow child div background-color over parent div

Is there a way to overflow the background color of a child-div over its parent-div container? I'm trying to add full-screen width background-color but the parent-div has a fixed width. here is my HTML structure:
<div id="parent">
<div class="child">PAGE TITLE</div>
</div>
The CSS:
#parent {
max-width: 760px;
}
.child {
width: 100%;
background-color: red;
}
Use box-shadow to simulate the background overflow since it's a solid color:
#parent {
max-width: 760px;
height: 100px;
padding: 10px 0;
margin:auto;
background:blue;
}
.child {
width: 100%;
height: 100%;
background-color: red;
box-shadow:
760px 0 0 red,
1520px 0 0 red,
-760px 0 0 red,
-1520px 0 0 red;
}
<div id="parent">
<div class="child">PAGE TITLE</div>
</div>
What you are trying to do is impossible with the current code setup as the child's z-index is set to the same stacking index as its parent as are its dimensions if you are using percentages.
You will need to do something like this to achieve your desired result.
HTML
<div id="parent">
<div class="child">PAGE TITLE</div>
</div>
<div id="page"></div>
CSS
#parent {
max-width: 760px;
background: green;
}
.child {
width: 100%;
background-color: red;
}
#page {
width: 760px;
height: 760px;
background: green;
position: relative;
z-index: 1;
}
don't know if this is what you want.
#parent {
max-width: 760px;
margin: 10px auto;
}
.child:before {
width: 100%;
height: 24px;
content: ' ';
position: absolute;
left: 0;
z-index: -1;
background-color: green;
}
Note: #parent's background-color must be transparent to make these css work.
Check my fiddle

CSS Clip - Allow background to show through

I am trying to apply css clip to an element to allow the layer behind to show through. I have the following layout..
body, html {
margin:0;
padding:0;
}
.container {
background:lightgray;
}
.clip {
position:fixed;
height:100%;
width:100px;
clip: rect(10px, 100px, 100px, 100px);
}
.section1, .section2 {
height:100vh;
width:100%;
}
.section_left_red {
height:100%;
width:100px;
background:red;
}
.section_left_blue {
height:100%;
width:100px;
background:blue;
}
<div class="container">
<div class="clip">
</div>
<div class="section1">
<div class="section_left_red">
</div>
<div>
<div class="section2">
<div class="section_left_blue">
</div>
<div>
</div>
I am trying to achieve something like this..
So as I scroll down, the blue background then shows through. Can anyone show me what I am doing wrong?
You can probably use multiple background to create this. The idea is to color only a part of the background making the remaining transparent:
body,
html {
margin: 0;
padding: 0;
}
.clip {
position: fixed;
height: 100%;
width: 200px;
box-sizing:border-box;
border:5px solid lightgray;
background:
linear-gradient(lightgray,lightgray) right/50% 100%,
linear-gradient(lightgray,lightgray) bottom/100% 80%;
background-repeat:no-repeat;
}
.section1,
.section2 {
height: 100vh;
width: 100%;
background: red;
}
.section2 {
background: blue;
}
<div class="container">
<div class="clip">
</div>
<div class="section1">
</div>
<div class="section2">
</div>
</div>
I don't believe that clip is the technique that you'll need to achieve this.
Clip css property is meant cut off part of an image that is absolutely positioned. I don't think it's meant for other elements.
instead of clip, have you tried using gradients to create a hole inside? Or maybe even a border:
.clip {
position: fixed;
height: 100vh;
width: 100vw;
top: 0;
left: 0;
border-top: 10px solid #fff;
border-left: 10px solid #fff;
border-right: calc(100vw - 60px) solid #fff;
border-bottom: calc(100vh - 60px) solid #fff;
box-sizing: border-box;
}

HTML/CSS position absolute for parent and children

Convert the boxes into cocentric circles (circles within each other that share the same center). The outer circle should be black with a size of 300px and the inner circle should be white with a size of 200px.
html:
<div id="p10">
<div id="outer">
<div class="rectangle" id="inner"></div>
</div>
css:
#p10 #outer {
border-radius: 100%;
background-color: #000;
width: 300px;
height: 300px;
border-color: #000;
position: absolute;
}
#p10 #inner {
background-color: #fff;
border-color: #fff;
border-radius: 100%;
width: 200px;
height: 200px;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
The css works only if #p10 #outer position's absolute. I'm kind of confused on why this is so. Does this mean that any time I want a subelement position's to be absolute, all of the parent's positions must be absolute?
The position of a position:absolute element is relative to the closest container with which the position is set to either absolute, relative, or fixed, otherwise it is relative to the viewport.
It can also be relative to the initial containing block if none of the top, right, bottom, or left offset value was specified.
There could be more possibilities, you can learn more on W3C, and MDN.
just change the position relative of parent div
#p10 #outer {
border-radius: 100%;
background-color: #000;
width: 300px;
height: 300px;
border-color: #000;
position: relative;
}
I would suggest to use position:absolute for the outer and position:relative for the inner. Then, set the border-radius property at half the width in pixels. Percentage in border-radius could cause some problems. Naturally you need to center the inner, so give it these properties.
#inner {
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
position:relative;
width:200px;
border-radius:100px;
}
Absolute/relative might not be neede here , at least relative for inner content.
you can also relay on padding and mind box-sizing:
#p10 #outer {
border-radius: 100%;
background-color: #000;
width: 200px;
height: 200px;
padding:50px;
border-color: #000;
/*position: absolute;*//* did you need it ? it will work the same for the child; */
box-sizing:content-box; /*make sure padding is not included in size calculation*/
}
#p10 #inner {
background-color: #fff;
border-color: #fff;
border-radius: 100%;
width: 200px;
height: 200px;
}
<div id="p10">
<div id="outer">
<div class="rectangle" id="inner"></div>
</div>
you can also relay on marging and mind collapsing margins:
#p10 #outer {
border-radius: 100%;
background-color: #000;
width: 300px;
height: 300px;
border-color: #000;
/*position: absolute;*//* did you need it ? it will work the same for the child; */
padding:1px; /* mind [collapsing margins][1] */
}
#p10 #inner {
background-color: #fff;
border-color: #fff;
border-radius: 100%;
width: 200px;
height: 200px;
margin:50px;
}
<div id="p10">
<div id="outer">
<div class="rectangle" id="inner"></div>
</div>
You may also use flex :
#p10 #outer {
border-radius: 100%;
background-color: #000;
width: 300px;
height: 300px;
display:flex;
align-items:center;
justify-content:center;
border-color: #000;
/*position: absolute;*//* did you need it ? it will work the same for the child; */
}
#p10 #inner {
background-color: #fff;
border-color: #fff;
border-radius: 100%;
width: 200px;
height: 200px;
}
<div id="p10">
<div id="outer">
<div class="rectangle" id="inner"></div>
</div>
You can also use a single box
.circle {
/* diplay, float, position, .. whatever is needed to be inserted mong the rest of your document styles*/
margin:55px;
height:200px;
width:200px;
border:solid;
box-shadow:0 0 0 50px gray, 0 0 0 53px;
border-radius:50%;
<div class="circle"></div>

div height fill rest of the white space

I'm trying to make my orange div to get all of the white space in height. Im using 1920x1080 monitor. When i open bottom code in my page i have white space under red, blue and green div's. I wanna orange div to move my red, blue, green div's down and fill that white space under them.
The idea is site automatically to fill browser window without scrollbars.
I try to write 100% instead of 700px, but when my attribute is 100%, orange div disappear.
Can someone tell me why that is happening, where is my mistake, how can i prevent it.
Also is it there another way to give equal space to my red, blue and green div's? I calculate that 100% of page divided by 3 is 33.3333 in period. That's why i set my width to be 33.33% but it didn't fill page completely.
.wrapper{
position: relative;
}
.pink{
background-color: pink;
height: 100px; width: 100%;
position: relative;
}
.orange{
background-color: orange;
height: 700px; width: 100%;
position: relative;
margin: 0px 0px 0px 0px;
}
.red{
background-color: red;
height: 300px; width: 33.33%;
position: relative;
float: left;
}
.blue{
background-color: blue;
height: 300px; width: 33.33%;
position: relative;
float: left;
}
.green{
background-color: green;
height: 300px; width: 33.33%;
position: relative;
float: left;
}
<div class="wrapper">
<div class="pink"></div>
<div class="orange"></div>
<div class="red"></div><div class="blue"></div><div class="green"></div>
</div>
Give height:100% to parent div, body and html
body, html{
height:100%;
}
.wrapper{
position: relative;
height:100%;
}
.orange{
background-color: orange;
height: 100%; width: 100%;
position: relative;
margin: 0px 0px 0px 0px;
}
Please check this fiddle.
Is this what you mean?
I've made use of
display: table
display: table-row
display: table-cell
The orange div will now fill the remaining height of the window.
Fiddle
EDIT: I updated the fiddle. tidied the code a bit.
Include this in your style:
body{
margin:0;
padding:0;
}
Wrap orange and pink inside a separate div from last three and use display:flex; on that div.
You can make three div eualwidth by using display:flex to the parent div and flex:1 to the children divs. You don't necessarily have to use width:33.33%;
html,body{
width:100%;
height:100%;
margin:0;
padding:0;
}
.wrapper{
position: relative;
margin:0;
padding:0;
display:flex;
min-height:100vh;
flex-direction:column;
}
.pink{
background-color: pink;
height: 50px;
width: 100%;
position: relative;
flex-shrink:0;
}
.orange{
background-color: orange;
height:100%;
width: 100%;
position: relative;
margin: 0px 0px 0px 0px;
flex-shrink:0;
flex-grow:1;
}
.wrapper2{
position: relative;
margin:0;
padding:0;
flex-shrink:0;
width:100%;
height:100px;
display:flex;
flex-direction:row;
justify-content: space-between;
}
.red{
background-color: red;
height:100%;
position: relative;
float: left;
flex: 1;
}
.blue{
background-color: blue;
height:100%;
flex: 1; position: relative;
float: left;
}
.green{
background-color: green;
height:100%;
flex: 1; position: relative;
float: left;
}
<div class="wrapper">
<div class="pink"></div>
<div class="orange"></div>
<div class="wrapper2">
<div class="red"></div>
<div class="blue"></div>
<div class="green"></div>
</div>
</div>

How to center a div above another div all the time

This scenario is somewhat different than the traditional margin:0 auto.
I try to center a div above another div, while both of them are in the same div (no need to look like they are in the same div, because blue box will be bigger than black box on top)!
I have create a fiddle of what I got so far, and I did align the top div into sort center, but its center in a sense of top left corner of each div. I want to center in a sense that, the blue box is RIGHT above the blackbox, but the bluebox's middle line is aligned with the middleline of the blackbox. So something like this
|______|
|__|
top box blue, bottom box black
But I just can't think of a way to do that.
http://jsfiddle.net/adamchenwei/nay8fe5q/
HTML
<div class="blockcontainer">
<div class="blockcenterbox">
<div class="blocktop">abc</div>
</div>
<div class="blockbottom"></div>
</div>
CSS
.blockcontainer {
margin:0 auto;
width:25px;
background-color:#00CC66;
}
.blocktop {
width:100px;
background-color:#6699FF;
height:50px;
}
.blockcenterbox {
width: .1px;
height: 5px;
background-color: yellow;
margin: 0 auto;
position: relative;
float: none;
}
.blockbottom {
width:25px;
height:25px;
background-color:black;
}
Is this what you're trying to achieve?
.blue {
width: 200px;
height: 100px;
background-color: blue;
}
.black {
width: 150px;
height: 80px;
background-color: black;
margin: 0 auto;
}
.blockcontainer {
margin: 0 auto;
width: 200px;
}
<div class="blockcontainer">
<div class="blue"></div>
<div class="black"> </div>
</div>
Try this working link at plnkr:
http://plnkr.co/edit/OaQBWxlIfa2fVvanKKEl?p=preview
Hope it helps!!!
HTML
<div class="blockcontainer">
<div class="blockcenterbox">
<div class="blocktop">abc</div>
</div>
<div class="blockbottom"></div>
</div>
CSS
.blockcontainer {
width:200px;
height:200px;
background-color:#00CC66;
overflow:hidden;
}
.blocktop {
width:100px;
background-color:#6699FF;
height:50px;
}
.blockcenterbox {
width: 100px;
height: 50px;
background-color: yellow;
margin: 0 auto;
}
.blockbottom {
width:25px;
height:25px;
background-color:black;
margin: 0 auto;
}