Transparent color of solid image? [duplicate] - html

This question already has answers here:
Semi-transparent color layer over background-image?
(19 answers)
Closed 2 years ago.
I am trying to have a large header where there is a background photo and then a solid color overtop it with a opacity of like 90%. (so you can barely see the photo).
This is basically what I have:
.bgoverlay {
background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
}
.bgimg-1 {
background-position: center top;
background-repeat: no-repeat;
top: 0px;
background-size: 100%;
background-color: #053426;
min-height: 60%;
opacity: 0.9;
}
<header class="bgimg-1 bgoverlay"></header>
edit
Thank you everyone - adding the :before is so far working out nicely. Although, when it comes to responsive, is there a way to change the background size? I tried background-size but it isn't changing.
For example, if I have the min-height at 70% so the whole header takes up 3/4th of the page but then when I shrink it to mobile size the solid background color is revealed below and the photo is small and not large enough to cover the 71% min height.
Thanks
edit 2
nvm I ended up using an #media screen to just shrink the overall header on mobile and now it looks great. Thank you!

You can use a pseudo-element like :before.
First, add position: relative to the header element. Then, add the pseudo-element absolutely positioned with the color and opacity, occupying the whole width and height of its parent (header).
header {
width: 100%;
height: 300px;
}
.bgoverlay {
position: relative;
background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
}
.bgimg-1 {
background-position: center top;
background-repeat: no-repeat;
top: 0px;
background-size: 100%;
min-height: 60%;
}
.bgoverlay:before {
content: '';
position: absolute;
height: 100%;
width: 100%;
left: 0;
top: 0;
opacity: 0.9;
background-color: #053426;
}
<header class="bgimg-1 bgoverlay"></header>

hope this help you.
.bgoverlay {
background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
}
.bgimg-1 {
background-position: center top;
background-repeat: no-repeat;
top: 0px;
background-size: 100%;
min-height: 300px;
position: relative;
}
header.bgimg-1.bgoverlay:after {
content: '';
height: 100%;
width: 100%;
background: #000;
opacity: 0.9;
position: absolute;
}
<header class="bgimg-1 bgoverlay">
</header>

Use a really large inner box-shadow if the solution by Azametzin doesn't pan out for you (relative positioning might get tricky with your content). But please use their solution as a real one, and maybe mine as a fallback. It is a bit hacky after all.
header {
width: 100%;
height: 300px;
}
.bgoverlay {
position: relative;
background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
box-shadow: inset 0 0 0 100vmax rgba(2, 20, 15, 0.9)
}
.bgimg-1 {
background-position: center top;
background-repeat: no-repeat;
top: 0px;
background-size: 100%;
min-height: 60%;
}
<header class="bgimg-1 bgoverlay"></header>

Related

Stretch background-image of parent to pseudo elements?

I'm trying to make the background-image of a parent stretched to a pseudo element.
I'm currently using the code below and it works in a sense that it's using the same image but the placement is not correct (see screenshot). I'd like this to be seamless.
.parent {
position: relative;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.parent::after {
content: '';
position: absolute;
bottom: -15px;
left: 0;
width: 100%;
height: 15px;
background: inherit;
z-index: 1;
}
Setting the parent's background-attachement to fixed seems to make it work but then I get an unwanted parallax effect on the parent.
Is there a way to make this work in a way that allows me to stretch the background image but avoid parallax? All help much appreciated!
Make the pseudo element cover the whole element and only its background will be visible:
.parent {
position: relative;
background-image:url(https://picsum.photos/id/1018/800/800);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
position:relative;
z-index:0;
height:100px;
}
.parent::after {
content: '';
position: absolute;
z-index:-1;
bottom: -15px;
left: 0;
width: 100%;
top:0;
background: inherit;
z-index: -1;
}
<div class="parent">
</div>

Multiple fixed backgrounds without background-attachment: fixed

I'm creating a simple website that is supposed to have a parallax-type effect. Originally it was using background-attachment: fixed;, however that's causing a repaint on each scroll, leading to some noticeable FPS drops.
Here is the desired effect, which in the real use-case, is causing slight stuttering:
body {
padding: 0;
margin: 0;
}
#container,
#container2 {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
height: 100vh;
}
#container {
background-image: url('http://fillmurray.com/1200/1200');
}
#container2 {
background-image: url('http://placekitten.com/1200/1200')
}
#more-content {
font-size: 48px;
padding: 35vh 0;
text-align: center;
background-color: white;
}
<div id="container"></div>
<div id="more-content">
Here's some more content
</div>
<div id="container2"></div>
As a work-around, I've created a div and applied a ::before pseudo-element that's position: fixed;, and I've set it into the background by doing z-index: -1;. I've added will-change: transform; to avoid repaint issues.
body {
padding: 0;
margin: 0;
}
#container {
height: 100vh;
overflow: hidden;
position: relative;
}
#container::before {
content: ' ';
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
background-image: url("http://www.fillmurray.com/1000/1000");
background-size: cover;
background-repeat: no-repeat;
z-index: -1;
will-change: transform;
}
#more-content {
text-align: center;
font-size: 48px;
padding: 50vh 0px;
background-color: white;
}
<div id="container"></div>
<div id="more-content">
Here's some more content
</div>
The problem is introduced once I try and create a second fixed image, beneath the section that says "Here's some more content". Because position: fixed; takes the element out of the flow (despite its parent being position: relative), I can't figure out a method to incorporate the second image, preserving the "wipe" effect in my first example. Being that they occupy the same space, only one image shows.
I've seen a few people ask similar questions, though most were not recent, nor did they have any answers.
I'm open to suggestions, otherwise I'll be tasking myself with a JavaScript workaround.

Html/css: Put elements behind a repeating background

I am trying to find a way to put a nav bar behind some background images that repeat. Here it is:
Basically, I want to have a navigation bar behind the repeating plants image, but in front of the sun image. I am going to make the nav elements popup when they are hovered over. Here is my css for the header:
header {
height: 250px;
width: 100%;
background-image: url("top.png"), url("banner.png");
background-repeat: repeat-x, no-repeat;
background-size: auto 40px, cover;
background-position: bottom;
}
I would recommend z-index. From W3Schools:
"The z-index property specifies the stack order of an element.
An element with greater stack order is always in front of an element with a lower stack order."
The larger the z-index of an element, the closer to the front the element is.
Part of the solution was to use z-index as Howzieky mentioned but did not provide an example for. Here is how I did it:
css:
header {
height: 250px;
width: 100%;
position: relative;
}
#background-far {
height: 250px;
width: 100%;
background-image: url("banner.png");
background-repeat: no-repeat;
background-size: cover;
position: absolute;
top: 0;
left: 0;
z-index: 0;
}
#header-body {
height: 250px;
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
#background-close {
height: 250px;
width: 100%;
background-image: url("top.png");
background-repeat: repeat-x;
background-size: auto 40px;
background-position: bottom;
position: absolute;
top: 0;
left: 0;
z-index: 2;
}
html:
<header>
<div id="background-far"></div>
<div id="header-body">
<img src="logo.png"/>
</div>
<div id="background-close"></div>
</header>
I also needed split the header into 3 sections (background-far, header-body and background-close). Header body will store everything I will have in the header such as my nav bar. The important part was to make the header use position: relative and each section to use position: absolute; top: 0; left: 0;
Thanks for all your help everyone!

Cannot position background image of :before element correctly

I am trying to achieve frosted glass effect as described here: https://css-tricks.com/frosting-glass-css-filters/. In order to do this, I need to add :before element with the same background and apply blur effect.
Here's my HTML:
<div class="root">
<div class="content">
<p>Ok hello</p>
</div>
</div>
and CSS:
.root {
background: url('http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_527bf56961712_1.JPG') no-repeat center center fixed;
background-size: cover;
height: 400px;
}
.content {
position: relative;
top: 50%;
transform: translateY(-50%);
background-color: red;
padding-top: 16px;
padding-bottom: 16px;
}
.content:before {
content: "";
position: absolute;
width: 100%;
top: 0;
bottom: 0;
z-index: -1;
background: url('http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_527bf56961712_1.JPG') no-repeat center center fixed;
background-size: cover;
opacity: 0.5;
/* -webkit-filter: blur(5px); */
}
Here's the pen: http://codepen.io/anon/pen/MKgbKR
I can't seem to position the background of :before element correctly. It is always shifted vertically. I tried playing with translation of this element, adjust the background, but to no avail. The problem can be easily noticed in the right part of the image, especially if you try resizing the resulting document vertically.
I am an absolute beginner in CSS, so I'm out of ideas. Your help will be appreciated.
EDIT: I think I should clarify my requirements. I need to keep the content block centered vertically regardless of window size changes. The image should remain centered and cover the background. The content never changes.
This might not show correctly on the snippet box, test it on your site and lmk
.root {
background: url('http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_527bf56961712_1.JPG') no-repeat center center;
background-size: cover;
height: 600px;
}
.content {
position: relative;
top: 50%;
height: 200px;
transform: translateY(-50%);
background-color: red;
}
.content:before {
content: "";
position: absolute;
width: 100%;
top: 0;
bottom: 0;
z-index: -1;
background: url('http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_527bf56961712_1.JPG') no-repeat center center;
background-size: cover;
opacity: 0.5;
-webkit-filter: blur(5px);
}
<div class="root">
<div class="content">
<p>Ok hello</p>
</div>
</div>

div height depend on content div with pseudo code

Hi how do i force wpdemos_wrapper div to be basing his height to the content? If i remove the height 900px the 2nd background wont appear.
div.wpdemos_wrapper
{
position: relative;
min-height:100%;
padding:0 0 20px;
background-image: url("./images/body/bg_honeycomb_top.png");
background-repeat: no-repeat;
background-position: top center;
background-size: contain;
width: 100%;
z-index: 0;
height: 900px;
}
div.wpdemos_wrapper:before
{
position: absolute;
content: "";
background-image: url("./images/body/bg_honeycomb_bottom.png");
background-repeat: no-repeat;
background-position: bottom center;
background-size: contain;
width: 100%;
bottom: 0;
left: 0;
height: 100%;
z-index: -1;
}
I think it would be much easier to apply the bg_honeycomb_top.png image to the :before pseudo-element and then apply the bg_honeycomb_bottom.png image to the main div.wpdemos_wrapper. The :before pseudo-element appears before the content in the div, so it is much easier that way. :) You can then remove the fixed height on the main div and clean it up a bit.
Sample code
This will allow you to repeat the background images across the entire width of the top and bottom of the div. This is using repeat-x for the images but you can change it to contain if you're wanting. :)
div.wpdemos_wrapper {
padding:0 0 20px;
background: rgba(0,0,0,0) url("./images/body/bg_honeycomb_bottom.png") repeat-x bottom center;
}
div.wpdemos_wrapper:before {
display: block;
content: " ";
background: rgba(0,0,0,0) url("./images/body/bg_honeycomb_top.png") repeat-x top center;
height: 20px; /* height of the background image */
width: 100%;
}
you may be able to use display: table on div.wpdemos_wrapper and corresponding display: table-cell on its children? Then the wrapper will always be the same height as their children even without an explicit height.
this is my new code nerwood. It is now working fine with height issues. However the top background wont appear..
div.wpdemos_wrapper {
position: relative;
padding:0 0 20px;
background: rgba(0,0,0,0) url("./images/body/bg_honeycomb_bottom.png") no-repeat bottom center/contain;
width: 100%;
}
div.wpdemos_wrapper:before {
position: absolute;
content: "";
background: rgba(0,0,0,0) url("./images/body/bg_honeycomb_top.png") no-repeat top center/contain;
width: 100%;
}