CSS - Background Opacity Not Working - html

I want a parent div to fill the screen with a transparent background but I can't seem to get this to work with the following code.
The parent div only appears as a solid colour.
Any ideas what I'm doing wrong:
.outer {
position: relative;
z-index: 1;
background-color: rgba(255, 255, 255, 0.5);
height: 100%;
}
.inner {
position: absolute;
width: 100%;
top: 50px;
background-color: white;
padding: 20px;
}
<div class="outer">
<div class="inner">
<h5>Search</h5>
</div>
</div>

Your are using 50% opaque white on 100% opaque white. How exactly do you expect that to become visible?
Also, your body has a computed height of 0. That makes .outer have 100% of 0 - guess what that is...
html, body { height: 100%; }
body { background-color: #f0f0f0; }
.outer {
position: relative;
z-index: 1;
background-color: rgba(200, 0, 0, 0.5);
height: 100%;
}
.inner {
position: absolute;
width: 100%;
top: 50px;
padding: 20px;
}
<div class="outer">
<div class="inner">
<h5>Search</h5>
</div>
</div>

Use the vh (viewport height) instead of the 100% height. When you use the 100% height, it takes the 100% of parent and the parent is body which height is 0px.
.outer {
position: relative;
z-index: 1;
background-color: rgb(255, 255, 255, 0.5);
height: 100vh;
}
.inner {
position: absolute;
width: 100%;
top: 50px;
background-color: white;
padding: 20px;
}
<div class="outer">
<div class="inner">
<h5>Search</h5>
</div>
</div>

parent div's opacity wont achieved when there is a solid background on the child and the .outer as no height so its not possible to show it. (because of position:absolute)
May be reverse these backgrounds and give position relative will make u understandable about how it works like below.
Note:Even position:absolute; in both places makes some sense
.outer {
position: relative;
background-color: red;
z-index: 1;
height: 100%;
}
.inner {
position: relative;
width: 100%;
top: 50px;
background-color: rgba(255, 255, 255, 0.5);
padding: 20px;
}
<div class="outer">
<div class="inner">
<h5>Search</h5>
</div>
</div>

As others have pointed out, a div with a white background won't work when overlapping another white element.
However, even if you were to change the colour of .outer to grey (as I've done in my example), it still won't fill the full height of the screen.
Instead of using height: 100%, you can view height: 100vh to fill the screen. You can learn more about vh (Viewport-percentage) from this article.
Here's a work example:
.outer {
position: relative;
z-index: 1;
background: rgba(0, 0, 0, .5);
height: 100vh;
}
.inner {
position: absolute;
width: 100%;
top: 50px;
background-color: white;
padding: 20px;
}
<div class="outer">
<div class="inner">
<h5>Search</h5>
</div>
</div>

Related

Fill only half of the container with a background color

I have something like this:
#container {
width: 300px;
background-color: red;
display: flex;
justify-content: center;
}
#child {
background-color: green;
width: 250px;
height: 150px;
position: relative;
top: 75px;
}
<div id="container">
<div id="child"></div>
</div>
The red box is the container and it's height is equal to its content - in this case 150px. What I'd like to achieve is to make the container height equal half the size of its content size.
The effect that I want to achieve is to have the background-color fill half of the containers content.
If this can be made in some other way than changing the container height - feel free to propose a solution.
Use a linear gradient:
#child {
background: linear-gradient(to bottom, green 50%, white 0%);
}
change white with body color or any color you want.
Source: CSS-Tricks
You can use gradients here.
Not sure if you want to keep the offset above the green div, but if so, you can also use calc() inside gradients :
add a margin-top: 75px to the child
set background: linear-gradient(red calc(50% + 37px), transparent calc(50% + 37px)) on the parent
This way you don't have to fix any height. Will ease your responsive work afterwards!
#container {
width: 300px;
background: linear-gradient(red 50%, transparent 50%);
display: flex;
justify-content: center;
}
#child {
background-color: green;
}
<div id="container">
<div id="child">
content!<br>
content!<br>
content!<br>
</div>
</div>
Just assign a height to the container and set height: 200%; for the child, like this to make sure the container has half the height of the child:
#container {
width: 300px;
height: 75px;
background-color: red;
display: flex;
justify-content: center;
background-size: 100% 50%;
}
#child {
background-color: green;
width: 250px;
height: 200%;
position: relative;
top: 75px;
}
This is not exactly the way you want to do this, but the result is the same. Maybe it'll help you.
You can make a pseudo element, which has half the height of the container and give it a height 50%;. This way you get a different background-color for half of the container s height.
#container {
width: 300px;
background-color: red;
display: flex;
justify-content: center;
position: relative;
}
#child {
background-color: green;
width: 250px;
height: 150px;
position: relative;
opacity: .5;
top: 75px;
z-index: 1;
}
div#container:after {
content: " Different Color! ";
color: #ffffff;
position: absolute;
width: 100%;
height: 50%;
left: 0;
bottom: 0;
background: blue;
}
<div id="container">
<div id="child"></div>
</div>

Using a css-filter on a child element to affect its parent

Is there any way of adding a filter (CSS or other type) to an element so this filter then effectively applies to the element below it.
I have an element with reduced opacity, that sits on top of a background image. I would like the element with reduced opacity to basically apply a black and white filter to the part of the element directly below it (effectively working as a mask). In the example below this means that the part of the image below the white box has the filter applied to it.
https://codepen.io/emilychews/pen/zWjWxo
Two things to note:
1) Because I'm using vh and vw units and the layout changes dependent on device / window size, I can't slice the image in photoshop or similar and then add or align it separately.
2) The image placeholder service I'm using serves up random images, and sometimes the image in the example is black and white - please don't let this confuse the issue.
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
}
#row-1 {
position: relative;
width: 80vw;
height: 70vh;
background: red;
border: 1px solid black;
background-image: url("https://loremflickr.com/320/240");
background-size: cover;
}
#inner {
background: rgba(255, 255, 255, .8);
position: absolute;
width: 50%;
height: 40%;
right: 0;
top: 0;
padding: 1rem;
}
<div id="row-1">
<div id="inner">
<p id="text">Some Text</p>
</div>
</div>
Short answer - no. But there is a non-standard -webkit-back-drop filter that works in iOS, but it's only available in other browsers behind an experimental flag.
https://webdesign.tutsplus.com/tutorials/css-backdrop-filters--cms-27314
(SVG 1.1 had a mechanism to do this, but the spec was poorly written and only IE10+ ever implemented it (and they may have backed it out))
You can try mix-blend-mode
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
}
#row-1 {
position: relative;
width: 80vw;
height: 70vh;
background: red;
border: 1px solid black;
background-image: url("https://loremflickr.com/320/240");
background-size: cover;
}
#inner {
background: rgba(255, 255, 255, .8);
position: absolute;
width: 50%;
height: 40%;
right: 0;
top: 0;
padding: 1rem;
mix-blend-mode: exclusion;
}
<div id="row-1">
<div id="inner">
<p id="text"></p>
</div>
</div>
Please, try this solution I have made on Codepen. You can play around with the Z-index & opacity in background-color.
It uses the :after pseudo element to make some sort of filter. Being stretched all over the div with an absolute position, everything underneath it will be affected.
I hope it can help you!
HTML:
<div class="box">
<p>Hello world!</p>
</div>
CSS:
.box {
position: relative;
width: 400px;
height: 300px;
background: url(http://farm2.staticflickr.com/1486/23990047223_5b7a0c82e8_b.jpg);
background-size: cover;
margin: 0 auto 100px;
z-index: 1;
}
.box:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: block;
// change color opacity here!
background-color: rgba(0, 0, 0, 0.5);
}
// styling
.box p {
margin: 0;
font-size: 26px;
text-align: center;
color: white;
padding-top: 50px;
position: relative;
z-index: 0;
}

CSS - how do I make a 1/4 circle that is 100vh?

I want something like this (the pink circle): CSS quarter circle 100vh example.
So far, I have a half-circle (see CSS below), but when I try to make it 100vh, it stretches and I can't figure out how to keep it proportional.
.circle {
height: 180px;
width: 90px;
border-radius: 0 90px 90px 0;
-moz-border-radius: 0 90px 90px 0;
-webkit-border-radius: 0 90px 90px 0;
background: red;
margin: 100px;
position: absolute;}
Any insights greatly appreciated. Thanks
I modified the code to only use 200vh to calculate both width and height of circle. This will give you a perfect circle at any screen size.
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100vw;height:100vh}
.box {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.box > .circle {
height: 200vh;
width: 200vh;
position: absolute;
bottom: -100vh;
right: -100vh;
border-radius: 50%;
background: hotpink;
}
<div class="box">
<div class="circle"></div>
</div>
You can do it like this:
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100vw;height:100vh}
.box {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.box > .circle {
height: 200vh;
width: 125.5vw; /* if exactly 16:9 vw/vh aspect ratio */
position: absolute;
bottom: -100vh;
right: -56.250vw; /* if exactly 16:9 vw/vh aspect ratio */
border-radius: 50%;
background: hotpink;
}
<div class="box">
<div class="circle"></div>
</div>
Conclusion: This works perfectly if the screen viewport is exactly at 16:9 aspect ratio (see it inside the editor (not "Run code snippet") without Menu Bar & Bookmarks Toolbar but better to see it in full screen), anything else than that fails so I wouldn't recommend using viewport units for this task. If anyone can prove me wrong or do it better, go ahead.
And with px:
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}
.box {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.box > .circle {
height: 180px;
width: 180px;
position: absolute;
bottom: -90px;
right: -90px;
border-radius: 50%;
background: hotpink;
}
<div class="box">
<div class="circle"></div>
</div>
you can start to keep the box into the ratio you want and center it when it doesn't fill the whole screen (=> downscaling to fit within the screen)
to size things, you can relay on %, vw,vh,vmax and or vmin units.
basicly you can start with size and max-size using the viewport as reference for the main box:
height:100%;
width:100%;
max-width:179vh;/* height viewport*/
max-height:56vw;/* width viewport*/
to align content you can use the flex display and margins and position.
To draw that 1/4 circle, you need a square that is at least the height of your box if it is to be drawn from border-radius. ( else a radial-gradient would do just fine).
for the vertical text, you may take a look at writing-mode.
A mix of CSS3 rules and positionning method can allow to do something that is about fluid.
Run the snippet below in fullpage and resize your window (heigh/width/both) your browser to see behavior.(or play with the codepen)
html {
height: 100%;
display: flex;
background: #ccc;
}
body {
margin: auto;
background: linear-gradient( to right, rgb(231, 231, 231) 25%, rgb(225, 207, 207) 25%);
height: 100%;
width: 100%;
max-width: 179vh;
max-height: 56vw;
display: flex;
align-items: center;
overflow: hidden;
position: relative;
box-shadow: 0 0 0 2px;
/* debug , see me */
}
/* make div be squares */
div:before {
content: "";
display: inline-block;
padding-top: 100%;
vertical-align: top;
}
.small {
border-radius: 50%;
background: rgb(101, 112, 168);
width: 25%;
margin-left: 13%;
box-shadow: 20vmin 20vmin 40vmin;
}
.big {
background: linear-gradient(to top, rgb(195, 90, 131), rgb(195, 90, 131)) no-repeat 0 0;
width: 56%;/* according to the ratio choosen */
border-top-left-radius: 100%;
flex-shrink: 0;/* avoid flex to shrink it */
margin: auto 0 0 auto;
}
/* position piece of text via absolute */
p {
margin: 0;
position: absolute;
bottom: 1%;
right: 5%;
font-size: 15vmin;
white-space: nowrap;
color: white;
}
p span {/* this rules might need to be tune to specific font-family*/
width: 0.25em;
display: inline-block;
vertical-align: -0.1em;
font-size: 0.155em;
writing-mode: vertical-lr;
transform: scale(-1);/*=> writing-mode:sideways-lr; not avalaible everywhere*/
}
h1,
body:before {
top: 0;
color: rgb(101, 112, 168);
position: absolute;
width: 5em;
font-size: 2vmin;
margin: 12vmin 0 5vmin 5vmin;
}
body:before {
content: "2017";
top: auto;
bottom: 0;
font-weight: bold;
}
h1:before {
content: "HB";
color: rgb(195, 90, 131);
border-radius: 50%;
position: absolute;
bottom: 150%;
font-size: 2.5em;
width: 0.75em;
height: 0.75em;
line-height: 0.75em;
letter-spacing: -0.35em;
border: solid 1px;
text-indent: -0.35em;
overflow: hidden;
}
<h1>VISUAL EXPLORATION</h1>
<p><span>BACK TO</span>BASIS</p>
<div class="small"></div>
<div class="big"></div>
By using the same view port unit for both height and width, you can achieve a perfect circle. Then putting it within a container which is fixed to the height of the view port and hiding the rest of the overflow will allow you to remove any unnecessary scroll bars and still allow for content below the circle.
The below demo will show the circle proportionally correct, always 100% of the view port height while making sure it is always a perfect circle.
It will obviously look better in full screen.
* {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 100vh;
position: relative;
overflow: hidden;
background: #dedede;
}
.dark {
background: #777777;
}
.circle {
width: 200vh;
height: 200vh;
border-radius: 100%;
position: absolute;
right: -100vh;
background: pink;
}
<div class="container">
<div class="circle"></div>
</div>
<div class="container dark">
</div>

Fixed div inherit width of relative div

I've got a relative div with a width of 40%. After that there is a fixed div that needs to inherit this width.
So I set my html document to this:
<div class="sidebar"></div>
<div class="maincontent">
<div class="fixed--wrapper">
<div class="fixed--header">
</div>
</div>
</div>
And my css:
.sidebar {
width: 20%;
height: 100vh;
overflow: hidden;
position: fixed;
background-color: #000;
color: #fff;
float: left;
}
.maincontent {
float: right;
position: relative;
width: 80%;
background-color: #D00;
height: 300px;
}
.fixed--wrapper {
width: 40%;
position: relative;
float: left;
border-right: 1px solid rgba(0, 0, 0, 0.2);
height: 100%;
overflow-y: scroll;
background-color: #E00;
}
.fixed--header {
height: 60px;
width: inherit;
position: fixed;
z-index: 2;
background-color: #fff;
}
Now the problem arises that my fixed--header is like 8% bigger than the fixed--wrapper. To illustrate this view the following fiddle: https://jsfiddle.net/zj0Lpu0q/1/
I want my fixed--header to be 40% as well.
Sidenote: I know there are quiet the number of questions about this, but I couldn't find one that has a relative div with widths in percentages defined. Therefore I created this new question. If you could link me to another answer I'm happy as well.
Try replacing your css to this
.fixed--wrapper {
width: 40%;
position: relative;
float: left;
border-right: 1px solid rgba(0, 0, 0, 0.2);
height: 100%;
overflow-y: scroll;
background-color: #E00;}
.fixed--header {
height: 60px;
position:absolute;
width:100%;
z-index: 2;
background-color: #fff;}

Fullwidth website with vertically split background

I am implementing a website which have a layout like below
I am a bit confuse about the HTML structure for this layout blue section is almost half divide my background and content will be centered and blue section will always be exactly divided in this ratio can anyone assist me how to make it possible. One more thing I need to use blue section as background color may be in RGBA.
A pseudo-element could work here
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
body {
background-image: url(http://lorempixel.com/output/abstract-q-c-100-100-9.jpg);
}
body::before {
content: '';
position: absolute;
top: 0;
left: 50%;
height: 100vh;
width: 50%;
background: rgba(0, 0, 255, 0.5);
z-index: -1;
}
.navbar {
margin-top: 25px;
height: 50px;
background: green;
border: 1px solid black;
}
<div class="navbar"></div>
Place this DIV after body tag:
<body>
<div id="blue"></div>
content
</body>
body {
background:url('img');
height:100%;
width:100%;
}
#blue {
position: fixed;
top: 0;
left: 50%;
background: rgba(0, 0, 255, 0.35);
height: 100%;
width: 1000px;
}
JSFiddle