CSS Background-image layer add by CSS - html

I have a DOM element:
<div style="background-image: url(layer1.png)">...</div>
I want to add next layer using CSS:
div {
background-image: url('layer2.png'),radial-gradient( rgba(255, 255, 255, 0.85) 0%, rgba(236, 245, 245, 0.19) 75%, rgba(236, 245, 245, 0) 100%);
}
As a result i need to have DIV with all 3 layers as a background image.
Is it possible by pure HTML5 CSS3?

Inline styling totally replaces the properties defined by CSS for that element.
What you can do is create a parent element and child elements as layers, then layer it with the help of z-index and then use opacity to give it some transparency.
HTML
<div class="layers">
<div class="layer1" style="opacity: 0.2; background-image: url(foo.png);"> </div>
<div class="layer2" style="opacity: 0.4;"></div>
<div class="layer3" style="opacity: 0.2;"></div>
</div>
CSS
.layers{
position:relative;
width: 100px;
height: 100px;
}
.layer1, .layer2, .layer3{
position:absolute;
top:0px;
left:0px;
width: 100%;
height: 100%;
}
.layer1{ background-color: red; z-index: 0;}
.layer2{ background-color: orange; z-index: -1;}
.layer3{ background-color: pink; z-index: -2; }
JsFiddle

No, it's not possible. What will happen:
Your div {} rule has lower priority than inline css, so your inline css background-image will overwrite previous rule, not be appended to it.
Solution:
Use child element or :before/:after to apply layer1.png
.wrapper {
height: 200px;
width: 300px;
background-image: radial-gradient( rgba(255, 255, 255, 0.85) 0%, rgba(236, 245, 245, 0.19) 75%, rgba(236, 245, 245, 0) 100%);
position: relative;
}
.wrapper div {
position: absolute;
left: 10%;
top: 10%;
width: 80%;
height: 80%;
}
body {
background-color: black;
}
<div class="wrapper"><div style="background-image: radial-gradient(rgba(0, 255, 0, 0.85) 10%, rgba(0, 245, 0, 0.19) 75%, rgba(0, 245, 0, 0) 90%);"></div></div>

Related

A faded bluish circle appears on a black background

My goal is to create a faded blue circle on black background.
However, there is a white square surrounding the circle, and it doesn't look good.
What can I do to get rid of this white background?
body {
background-color: black;
}
.circle {
border-radius: 50%;
width: 200px;
height: 200px;
background-color: blue;
}
.circle:after {
content: "";
display: block;
width: 100%;
height: 100%;
background-image: radial-gradient(ellipse at center center, rgba(0, 0, 0, 0) 0%, rgba(255, 255, 255, 1) 70%, rgba(255, 255, 255, 1) 100%);
}
<div class="circle"> </div>
You seems to overcomplicate a simple task:
body {
background-color: black;
}
.circle {
border-radius: 50%;
width: 200px;
height: 200px;
background: radial-gradient(farthest-side,blue,#0000);
}
<div class="circle"> </div>
One way is to fade away with black instead of white.
body{
background-color:black;
}
.circle {
border-radius: 50%;
width:200px;
height:200px;
background-color:blue;
}
.circle:after {
content: "";
display: block;
width: 100%;
height: 100%;
background-image: radial-gradient(ellipse at center center, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) 70%, rgba(0, 0, 0, 1) 100%);
}
<div class="circle"> </div>

Transparent background from the grand-parent element

I am trying to apply to an element transparent background, but directly to the grand parent element. for example:
body {
background: linear-gradient( 45deg, rgba(5, 175, 240, 0.14), rgba(239, 77, 54, 0.14), rgba(243, 200, 57, 0.14)) fixed
}
.parent {
height: 100px;
width: 100px;
background: black;
}
.child {
height: 50px;
width: 50px;
background: // here, i want the background to reflect the body background
}
<div class="parent">
<div class="child"></div>
</div>
No need of span you can achieve this using clip-path CSS property
body {
background: linear-gradient( 45deg, rgba(5, 175, 240, 0.14), rgba(239, 77, 54, 0.14), rgba(243, 200, 57, 0.14)) fixed
}
div {
height: 100px;
width: 100px;
background: black;
clip-path: polygon(0% 50px, 50px 50px, 50px 0%, 85% 0%, 100% 0, 100% 15%, 100% 85%, 100% 100%, 85% 100%, 15% 100%, 0 100%, 0% 85%);
}
<div>
</div>
Use the same gradient for both elements
body {
--g: linear-gradient( 45deg, rgba(5, 175, 240, 0.14), rgba(239, 77, 54, 0.14), rgba(243, 200, 57, 0.14)) fixed;
background: var(--g)
}
.parent {
height: 100px;
width: 100px;
background: black;
}
.child {
height: 50px;
width: 50px;
background: var(--g);
background-color: #fff;
}
<div class="parent">
<div class="child"></div>
</div>

linear-gradient on different angle

I want to archive a background linear-gradient (to right, from light pink to red) and fade out vertically (to bottom, to white color or opacity to 0), but it seems it's not possible to do it by using css linear-gradient.
background: linear-gradient(to left, rgba(224, 130, 131, 0.8), rgba(207, 0, 15, 0.8));
any ideas how to archive this kind of effect without using static image?
You need mask:
.box {
height:100px;
position:relative;
border:1px solid;
}
.box:before {
content:"";
position:absolute;
inset:0;
-webkit-mask:linear-gradient(#000,#0000);
background: linear-gradient(to left, rgba(224, 130, 131, 0.8), rgba(207, 0, 15, 0.8));
}
<div class="box"></div>
If you want to achieve a background gradient from light pink to red and then fade out on bottom.
This might help you.
.divX {
position: relative;
z-index: 0;
background: linear-gradient(to right, rgba(224, 130, 131, 0.8), rgba(207, 0, 15, 0.8));
/* Ignore these below */
width: 100%;
height: auto;
min-height: 150px;
}
.divX::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1;
background: linear-gradient(180deg, transparent, rgb(255 255 255));
}
.divXcontent {
position: relative;
z-index: 3;
}
<div class="divX">
<div class="divXcontent">
<h1>test test</h1>
</div>
</div>
Then add another div inside .divX and set position:relative and z-index to 3, for the content to be on top of everything.

How to center 4 absolute-divs (balls) in the middle of my screen?

I am trying to create a loading-screen effect for my current assignment.
It requires us to create a <div class="overlay"> with position: fixed. This funds as the background. Withing this div, there are 4 <div class="circle"> with position: absolute.
We have to center these using absolute-position and transform: translate. and each ball has a margin of 80px inbetween them
Here is an image of an example I try to re-create
I have managed so far to perfectly-center the balls, but cause of the position: absolute they all overlap. How can I make sure I can get all 4 balls like in the picture?
html {
scroll-behavior: smooth;
}
body {
z-index: 0;
background-color: #fff;
}
.overlay {
background-color: #0d1133;
position: fixed;
top: 0;
left: 0;
z-index: 1;
height: 100vh;
width: 100vw;
}
.circle {
width: 80px;
height: 80px;
background: #fff;
display: inline-block;
border-radius: 40px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#yellow {
background-color: linear-gradient(180deg, rgba(248, 255, 0, 1) 0%, rgba(255, 145, 0, 1) 50%, rgba(255, 145, 0, 1) 100%);
}
#red {
background: linear-gradient(180deg, rgba(255, 0, 215, 1) 0%, rgba(255, 0, 61, 1) 50%, rgba(255, 0, 0, 1) 100%);
}
#blue {
background: linear-gradient(180deg, rgba(0, 255, 243, 1) 0%, rgba(0, 224, 255, 1) 50%, rgba(0, 185, 255, 1) 100%);
}
#purple {
background: linear-gradient(180deg, rgba(255, 0, 241, 1) 0%, rgba(222, 0, 255, 1) 50%, rgba(157, 0, 255, 1) 100%);
}
<main>
<div class="overlay">
<div class="circle" id="yellow"></div>
<div class="circle" id="red"></div>
<div class="circle" id="blue"> </div>
<div class="circle" id="purple"></div>
</div>
</main>
You can wrap them inside a wrapper class and position that div absolute (like you did for each ball).
html {
scroll-behavior: smooth;
}
body {
z-index: 0;
background-color: #fff;
}
.overlay {
background-color: #0d1133;
position: fixed;
top: 0;
left: 0;
z-index: 1;
height: 100vh;
width: 100vw;
}
.wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
.circle {
width: 80px;
height: 80px;
background: #fff;
display: inline-block;
border-radius: 40px;
}
#yellow {
background-color: linear-gradient(180deg, rgba(248, 255, 0, 1) 0%, rgba(255, 145, 0, 1) 50%, rgba(255, 145, 0, 1) 100%);
}
#red {
background: linear-gradient(180deg, rgba(255, 0, 215, 1) 0%, rgba(255, 0, 61, 1) 50%, rgba(255, 0, 0, 1) 100%);
}
#blue {
background: linear-gradient(180deg, rgba(0, 255, 243, 1) 0%, rgba(0, 224, 255, 1) 50%, rgba(0, 185, 255, 1) 100%);
}
#purple {
background: linear-gradient(180deg, rgba(255, 0, 241, 1) 0%, rgba(222, 0, 255, 1) 50%, rgba(157, 0, 255, 1) 100%);
}
<main>
<div class="overlay">
<div class="wrapper">
<div class="circle" id="yellow"></div>
<div class="circle" id="red"></div>
<div class="circle" id="blue"> </div>
<div class="circle" id="purple"></div>
</div>
</div>
</main>
I would go with flexboxes, but if you need to animate them using transform: translate you can position them using:
transform: translate(calc(-50% + <OFFSET>px), -50%)
html {
scroll-behavior: smooth;
}
body {
z-index: 0;
background-color: #fff;
}
.overlay {
background-color: #0d1133;
position: fixed;
top: 0;
left: 0;
z-index: 1;
height: 100vh;
width: 100vw;
}
.circle {
width: 80px;
height: 80px;
background: #fff;
display: inline-block;
border-radius: 40px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#yellow {
background-color: linear-gradient(180deg, rgba(248, 255, 0, 1) 0%, rgba(255, 145, 0, 1) 50%, rgba(255, 145, 0, 1) 100%);
transform: translate(calc(-50% - 240px), -50%);
}
#red {
background: linear-gradient(180deg, rgba(255, 0, 215, 1) 0%, rgba(255, 0, 61, 1) 50%, rgba(255, 0, 0, 1) 100%);
transform: translate(calc(-50% - 80px), -50%);
}
#blue {
background: linear-gradient(180deg, rgba(0, 255, 243, 1) 0%, rgba(0, 224, 255, 1) 50%, rgba(0, 185, 255, 1) 100%);
transform: translate(calc(-50% + 80px), -50%);
}
#purple {
background: linear-gradient(180deg, rgba(255, 0, 241, 1) 0%, rgba(222, 0, 255, 1) 50%, rgba(157, 0, 255, 1) 100%);
transform: translate(calc(-50% + 240px), -50%);
}
<main>
<div class="overlay">
<div class="circle" id="yellow"></div>
<div class="circle" id="red"></div>
<div class="circle" id="blue"> </div>
<div class="circle" id="purple"></div>
</div>
</main>
You could use display: flex, aswell as align-items and justify content on the parent like so :
html {
scroll-behavior: smooth;
}
body {
z-index: 0;
background-color: #fff;
}
.overlay {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
background-color: #0d1133;
position: fixed;
top: 0;
left: 0;
z-index: 1;
height: 100vh;
width: 100vw;
}
.circle {
width: 80px;
height: 80px;
background: #fff;
display: inline-block;
border-radius: 40px;
}
#yellow {
background-color: linear-gradient(180deg, rgba(248, 255, 0, 1) 0%, rgba(255, 145, 0, 1) 50%, rgba(255, 145, 0, 1) 100%);
}
#red {
background: linear-gradient(180deg, rgba(255, 0, 215, 1) 0%, rgba(255, 0, 61, 1) 50%, rgba(255, 0, 0, 1) 100%);
}
#blue {
background: linear-gradient(180deg, rgba(0, 255, 243, 1) 0%, rgba(0, 224, 255, 1) 50%, rgba(0, 185, 255, 1) 100%);
}
#purple {
background: linear-gradient(180deg, rgba(255, 0, 241, 1) 0%, rgba(222, 0, 255, 1) 50%, rgba(157, 0, 255, 1) 100%);
}
<main>
<div class="overlay">
<div class="circle" id="yellow"></div>
<div class="circle" id="red"></div>
<div class="circle" id="blue"> </div>
<div class="circle" id="purple"></div>
</div>
</main>
Add an absolutely-positioned div container around the circles, and centre this on the page.
Then put the circles inside it (no longer absolutely-positioned) and give them a margin.
html {
scroll-behavior: smooth;
}
body {
z-index: 0;
background-color: #fff;
}
.overlay {
background-color: #0d1133;
position: fixed;
top: 0;
left: 0;
z-index: 1;
height: 100vh;
width: 100vw;
}
.circles-container {
width: 100%;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.circle {
width: 30px;
height: 30px;
background: #fff;
margin: 30px;
display: inline-block;
border-radius: 40px;
transform: translate(-50%, -50%);
}
#yellow {
background-color: linear-gradient(180deg, rgba(248, 255, 0, 1) 0%, rgba(255, 145, 0, 1) 50%, rgba(255, 145, 0, 1) 100%);
}
#red {
background: linear-gradient(180deg, rgba(255, 0, 215, 1) 0%, rgba(255, 0, 61, 1) 50%, rgba(255, 0, 0, 1) 100%);
}
#blue {
background: linear-gradient(180deg, rgba(0, 255, 243, 1) 0%, rgba(0, 224, 255, 1) 50%, rgba(0, 185, 255, 1) 100%);
}
#purple {
background: linear-gradient(180deg, rgba(255, 0, 241, 1) 0%, rgba(222, 0, 255, 1) 50%, rgba(157, 0, 255, 1) 100%);
}
<main>
<div class="overlay">
<div class="circles-container">
<div class="circle" id="yellow"></div>
<div class="circle" id="red"></div>
<div class="circle" id="blue"> </div>
<div class="circle" id="purple"></div>
</div>
</div>
</main>
This is perfect and clean code using css flex
Html Code:
<div class="circles-container">
<div class="circle" id="red"></div>
<div class="circle" id="orange"></div>
<div class="circle" id="green"> </div>
<div class="circle" id="purple"></div>
</div>
</div>
Css Code:
.main-div{
width:100%;
height:-webkit-fill-available;
display:flex;
background: navy;
justify-content: center;
flex-direction: column;
align-items: center;
}
.circle{
width:10px;
height:10px;
border-radius:50px;
display:inline-block;
}
#red{
background:red
}
#orange{
background:orange;
}
#green{
background:green;
}
#purple{
background:purple;
}

How to make a background gradient

I need help with a webpage I'm webmastering. Here's some code:
<body>
<div class="left">
</div>
And here's the css for it:
.left {
position: fixed;
width:50%;
height: 100vh;
top:0;
background-image: url('../img/plakatm.jpg');
background-size: 1164px,1000px;
background-position: center;
background-repeat: no-repeat;
}
The problem is, that i need to make a gradient at the right edge of it. I can't add the gradient to the image, beacause the .left element changes size on smaller monitors and the gradient would not show up.
Here you can see the full site (It's in polish but you don't need to understand it) Click here to see it.
Thanks.
Adam
Use CSS linear-gradient, something like below will work for you, better separate it to a separate into a different class, not call it .left, I call it .gradient in this example:
.left {
position: fixed;
width: 50%;
height: 100vh;
top: 0;
background-image: url('http://weknownyourdreamz.com/images/jungle/jungle-04.jpg');
background-size: 1164px, 1000px;
background-position: center;
background-repeat: no-repeat;
}
.left:after {
position: absolute;
content: '';
top: 0;
height: 100%;
width: 100%;
display: block;
background: white;
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
background: -o-linear-gradient(right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
background: -moz-linear-gradient(right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
}
<body>
<div class="left">
</div>
</body>
There is a css-property for gradients. That should help.
Here is how you can have background gradient.
.left {
background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%);
height: 100px;
width: 100px;
}
<div class="left">
</div>
And this is the link where you can find good gradients: https://webgradients.com/