overlay with pseudo :before also darkens text that should remain white - html

I have an element with a background image and some text in it.
For various good reasons the element is positioned absolute.
I added an overlay to the image with a pseudo :before.
The problem now is that the overlay also darkens the text in the element.
How would I fix that? I want my text to remain nicely white!
JSFiddle: https://jsfiddle.net/xvdk95st/
.text {
position: absolute;
background-image: url('https://i.gyazo.com/1e88fee290bda821ba823a76a1e01c04.png');
background-size: cover;
background-repeat: no-repeat;
width: 400px;
line-height: 25px;
color: #fff;
min-height: 400px;
}
.text::before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
content: " ";
background-color: rgba(0, 0, 0, 0.4);
z-index: 2;
}
<div class="text">
<p>
Hi, I'm some text!
</p>
</div>

Use z-index with position on <p> (because z-index always works with position), like:
.text p {
z-index: 10;
position: relative;
}
Have a look at the snippet below:
.text {
position: absolute;
background-image: url('https://i.gyazo.com/1e88fee290bda821ba823a76a1e01c04.png');
background-size: cover;
background-repeat: no-repeat;
width: 400px;
line-height: 25px;
color: #fff;
min-height: 400px;
}
.text::before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
content: " ";
background-color: rgba(0, 0, 0, 0.4);
z-index: 2;
}
.text p {
z-index: 10;
position: relative;
}
<div class="text">
<p>
Hi, I'm some text!
</p>
</div>
Hope this helps!

If you want to darken background only, you can use multiple backgrounds:
.text {
position: absolute;
background-image:
linear-gradient(to right, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)),
url('https://i.gyazo.com/1e88fee290bda821ba823a76a1e01c04.png');
background-size: cover;
background-repeat: no-repeat;
width: 400px;
line-height: 25px;
color: #fff;
min-height: 400px;
}
<div class="text">
<p>
Hi, I'm some text!
</p>
</div>

Related

Child elements inheriting background color

I am trying to make a simple parallax effect, however I also want to add a custom semi-transparent color to the background image.
I tried many solutions, this one seemed to work however the color is applied on top of the children elements, even if I am using the ::before selector.
.spannerBg {
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
background-color: rgba(255, 150, 0, 0.5);
min-height: 300px;
}
.spannerBg::before {
content: "";
display: block !important;
background-color: inherit;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
p {
font-size: x-large;
}
<div class="spannerBg" style="
color: #fff;
background-color: rgba(0, 0, 0, 0.5);
background-image:url(https://images.pexels.com/photos/1051075/pexels-photo-1051075.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
<p>Somecontent</p>
</div>
<div>
beiwrfa<br>ewnifiebfia<br>fbjwqbfwebfj<br>
</div>
<div class="spannerBg" style="
background-image:url(https://images.pexels.com/photos/865711/pexels-photo-865711.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
<p>Somecontent</p>
</div>
<div>
<p>somecontent</p>
</div>
I saw something about using semi-transparent pngs or fake div but I would like it to be 100% css.
I also saw this question, but all answers seem to either be not css or have the same problem
You can give your .spannerBg::before a z-index: -1 and .spannerBg a z-index: 1
The before element is now moving behind its siblings.
.spannerBg {
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
background-color: rgba(255, 150, 0, 0.5);
min-height: 300px;
z-index: 1;
}
.spannerBg::before {
content: "";
display: block !important;
background-color: inherit;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
p {
font-size: x-large;
}
<div class="spannerBg" style="
color: #fff;
background-color: rgba(0, 0, 0, 0.5);
background-image:url(https://images.pexels.com/photos/1051075/pexels-photo-1051075.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
<p>Somecontent</p>
</div>
<div>
beiwrfa<br>ewnifiebfia<br>fbjwqbfwebfj<br>
</div>
<div class="spannerBg" style="
background-image:url(https://images.pexels.com/photos/865711/pexels-photo-865711.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
<p>Somecontent</p>
</div>
<div>
<p>somecontent</p>
</div>
I hope this helps:
.spannerBg {
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
min-height: 300px;
}
.spannerBg::before {
content: "";
display: block !important;
background-color: inherit;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="spannerBg" style="
background-image: linear-gradient( rgba(255, 150, 0, 0.5), rgba(255, 150, 0, 0.5) ), url(https://images.pexels.com/photos/865711/pexels-photo-865711.jpeg);
">
<p>Somecontent</p>
</div>

Background Blur div

Need help to create this background blur without blurring the text.
I have tried the css filter property, but couldn't make it look like this.
I'm not a CSS shark, so i hope someone can give me an easy solution :D
Adobe XD Background Blur:
Since you haven't provided any code so this will help you.
You can make a div with blur background using filter: blur(13px); in css and the content can't be inside the blurred div, so we will use a sibling element instead like this
body {
background: url('https://www.w3schools.com/html/img_girl.jpg') no-repeat center center fixed;
background-size: cover;
}
.blur {
background: url('https://www.w3schools.com/html/img_girl.jpg') no-repeat center center fixed;
background-size: cover;
overflow: hidden;
filter: blur(13px);
position: absolute;
height: 300px;
top: -50px;
left: -50px;
right: -50px;
bottom: -50px;
}
.widget {
border-top: 2px solid rgba(255, 255, 255, .5);
border-bottom: 2px solid rgba(255, 255, 255, .5);
height: 200px;
width: 100%;
overflow: hidden;
}
.center {
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.text h1 {
text-align: center;
color: #ffffff;
margin-top: 57px;
font-family: 'Lora', serif;
font-weight: 700;
font-size: 38px;
}
<div class="widget center">
<div class="blur"></div>
<div class="text center">
<h1 class="">I am blurred div</h1>
</div>
</div>

How to add two background images in css

i want help to add two image one over the other and position two images in center
<div class="im1"> </div>
.im1{
position: relative; top: 0; left: 0;
background-image:url("../images/img-shadow.png"),url("../images/img-1.png");
background-size:contain;
height:358px;
background-repeat: no-repeat,no-repeat;
}
background image is with shadow
i want it to be
This actually can be solved only with CSS.
I created an example here that generates the rotated border of the image.
Now you can insert any image inside :)
p.s of course you need to change the width and the height of the image inside to be the same as the frame diagonal. you can use CSS calculate for this.
body {
background-color: #F3F5F6;
}
.shadow:before,
.shadow:after {
content: "";
position: absolute;
z-index: -1;
height: 10%;
max-width: 90%;
width: 90%;
}
.shadow:before {
-webkit-transform: rotate(86deg);
left: -72px;
right: auto;
top: 118px;
box-shadow: 0 15px 5px rgba(0, 0, 0, 0.2);
}
.shadow:after {
-webkit-transform: rotate(84deg);
left: auto;
right: -92px;
bottom: 75px;
box-shadow: 0 -15px 5px rgba(0, 0, 0, 0.2);
}
.rotate {
-webkit-transform: rotate(45deg);
}
.box {
width: 200px;
height: 200px;
position: absolute;
top: 100px;
left: 100px;
}
.pic-wrapper {
width: 100%;
height: 100%;
border: 10px solid #fff;
overflow: hidden;
}
.pic {
background-image: url('http://modernschoolec.com/wp-content/uploads/2015/04/11-980x408.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
/* (side)(sqrt(2)) */
width: 282px;
height: 282px;
-webkit-transform: rotate(-45deg);
position: relative;
top: -40px;
left: -40px;
}
<div class="box shadow rotate">
<div class="pic-wrapper">
<div class="pic"></div>
</div>
</div>
You could either give the shadow image a z-index of 100 and give the school image a z-index of 101 or set one to be position relative and one to be position absolute, but you will need media queries to make it responsive.
.im1{
background-image:url("../images/img-1.png"),url("../images/img-shadow.png");
height:358px;
background-repeat: no-repeat,no-repeat;
background-position: center , center;
}

Issue with div with image as a gradient on high dpi devices

I have the following HTML + CSS:
.item {
position: relative;
width: 400px;
height: 400px;
background: url('http://i.imgur.com/FOmRt87.jpg') no-repeat;
}
.item .gradient {
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
left: 0;
background: url('http://i.imgur.com/oSpOTeK.png') repeat-x center bottom;
}
<div class="item">
<div class="gradient">
</div>
</div>
It's rendered in the browser properly. But on mobile (see the attached screenshot) there's a one thick line across the gradient, I have no idea why is that.
Here's also I js fiddle: https://jsfiddle.net/tcxka242/1/
First I thought that is repeated vertically as well, but the inspector says that the rule I've set: background: url(...) repeat-x center bottom; is expanded to :
background-image: url("http://i.imgur.com/oSpOTeK.png");
background-position-x: 50%;
background-position-y: 100%;
background-size: initial;
background-repeat-x: repeat;
background-repeat-y: no-repeat;
background-attachment: initial;
background-origin: initial;
background-clip: initial;
background-color: initial;
That's on Android Phone with Google Chrome.
Sorry but i cannot properly verify this , but i have an idea for you .
.item .gradient {
width:100%;
height: 100%;
position: absolute;
bottom: 0;
left: 0;
outline: 0;
border: none;
background: url('http://i.imgur.com/oSpOTeK.png') repeat-x center bottom;
}
As you can see i have set the outline to 0 and the border to none . There's a possibility that there is an outline from the div or a hidden border .
Specifying border-top: 0px; and box-shadow: none; will work for you
.item .gradient {
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
box-shadow: none;
left: 0;
background: url('http://i.imgur.com/oSpOTeK.png') repeat-x center bottom;
border-top: 0px;
}
I think this is caused on screens with high DPI. Therefore I am providing a CSS-only alternative.
https://jsfiddle.net/tcxka242/6/
.item {
position: relative;
width: 400px;
height: 400px;
background: url('http://i.imgur.com/FOmRt87.jpg') no-repeat;
}
.item:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
left: 0;
z-index: 1;
pointer-events: none;
background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 70%, rgba(0, 0, 0, 0.6) 100%);
}

I want this <p> tag to come bottom of the image?

I am totally new to this html/css I am trying to do float a text with background as image .Yes I did that but the problem is the text is floating on top of the image.If use margin-top to adjust the image,it just simple pull down the whole div.Here down i gave a pictorial representation of what I want.
html
<div class="maincon">
<div class="picon" style="background-image: linear-gradient(to bottom, rgba(0, 0, 0, .02), rgba(0, 0, 0, .8)), url('img/apple.jpg') "><p>Hi</p>
</div>
</div>
Css
.maincon {
margin-top: 95px;
width: 80%;
margin-left: auto;
margin-right: auto;
height: 100%;
}
.picon {
width: 100%;
height: 80%;
background: none center/cover #f2f2f2;
}
.picon p {
}
You can use position: absolute
.element {
height: 100vh;
background:linear-gradient(to bottom, rgba(0, 0, 0, .02), rgba(0, 0, 0, .8)), url('http://cdn2.macworld.co.uk/cmsdata/features/3598128/iphone_6s_review_20.jpg');
background-size: cover;
position: relative;
}
p {
position: absolute;
color: white;
font-size: 25px;
margin: 10px;
bottom: 0;
}
<div class="element">
<p>Apple iphone</p>
</div>
wihtout position absolute and relative .you can use flexbox module.it supports all latest browsers.
.element {
height: 80vh;
background: linear-gradient(to bottom, rgba(0, 0, 0, .02), rgba(0, 0, 0, .8)), url('http://cdn2.macworld.co.uk/cmsdata/features/3598128/iphone_6s_review_20.jpg');
background-size: cover;
align-items: flex-end;
display: flex;
}
p {
color: white;
font-size: 25px;
margin: 10px;
}
<div class="element">
<p>Apple iphone</p>
</div>
.maincon {
margin-top: 95px;
width: 80%;
margin-left: auto;
margin-right: auto;
height: 100%;
}
.picon {
width: 100%;
height: 80%;
background: none center/cover #f2f2f2;
position: relative;
}
.picon p {
position: absolute;
bottom: 0;
}