Stretch background-image of parent to pseudo elements? - html

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>

Related

Css pseudo element curve background positioning

I am trying to do a challenge from frontend mentor where you have some div elements with curve images on top and bottom. I am trying to do it with before and after pseudo elements like this
.feature__item {
width: 100%;
position: relative;
margin: 200px 0;
padding: 50px;
}
.feature__item-1::before {
content: '';
width: 100%;
height: 139px;
background-image: url('../images/bg-section-top-desktop-1.svg');
background-repeat: no-repeat;
background-size: 100% auto;
position: absolute;
left: 0;
top: -139px;
}
.feature__item-1::after {
content: '';
width: 100%;
height: 139px;
background-image: url('../images/bg-section-bottom-desktop-1.svg');
background-repeat: no-repeat;
background-size: 100% auto;
position: absolute;
left: 0;
bottom: -139px;
}
The element located at the bottom does not create a problem. But the top element crashes when I play with the width of the browser. Is there a way to make bottom of the :before element sit on top of the parent div? Or is there another way to fix this?

Pseudo element not showing background-image

Why is my background-image inside the pseudo element ::before not showing up? I also tested of replacing the background-image with a background-color and it still doesn't work. This is in SASS format in case some would be wondering of the nested ::before.
.logoframe{
float: left;
height: 817px;
width: 20%;
position: relative;
left: -6%;
transform: skewX(-11deg);
border: 1px solid #e26f6f;
&::before{
content: "";
background-image: url('/images/theseven/seven_img_old.png');
background-repeat: no-repeat;
position: relative;
height: 817px;
width: 150px;
}
}
<div class="logoframe"></div>
the "display" property. display is CSS's most important property for controlling layout. Every element has a default display value depending on what type of element it is. The default for most elements is usually block or inline . A block element is often called a block-level element.
&::before{
content: "";
display: block;/*missing prop*/
background-image: url('/images/theseven/seven_img_old.png');
background-repeat: no-repeat;
position: relative;
height: 817px;
width: 150px;
}
Sometimes you need to add background-size property too with display: block.
&::before{
content: "";
background-image: url('/images/theseven/seven_img_old.png');
background-repeat: no-repeat;
background-size:100%;
position: relative;
height: 817px;
width: 150px;
display:block;
}
You should update below css part. if you need background image in center please update background-position.
.logoframe{
float: left;
height: 817px;
width: 20%;
position: relative;
left: 0;
transform: skewX(-11deg);
border: 1px solid #e26f6f;
}
.logoframe:before {
content: "";
background: url('https://n2.sdlcdn.com/imgs/a/a/1/Chromozome_Yamaha_102025_m_1_2x-4ab77.jpg') 0 0 no-repeat;/* replace 0 0 to center center */
background-repeat: no-repeat;
position: absolute;
background-size:contain;
top:0;
left:0;
height: 817px;
width: 100%;
}
<div class="logoframe"></div>
I don't know if this helps but sometimes if you are using the shortcuts for the background property it might not work, but if you use the properties differently I think it might work. I am saying this from experience.
.showcase::before {
content: '';
background-image: url(../images/Desert.jpg) no-repeat center center/cover;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: -1;
}
But this one did.
.showcase::before {
content: '';
background-image: url(../images/Desert.jpg);
position: absolute;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: -1;
}

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>

Attach div to an exact position relative to an background (with size: cover)

I have a webpage with an background-image with background-size:cover.
Now I want to overlay this background-image with certain div's, which contain additional informations. These div's have to be at an exact position relative to the background image, even though I resize the broswer window.
That's just one attempt that didn't work.
HTML
<body>
<div class="icon">
<div class="background picture_rendering"></div>
</body>
CSS
.background {
width:100%;
height:100%;
background-image: url(images/bg.jpg);
background-size: cover;
z-index: 0;
position: absolute;
}
.icon {
width: 100%;
height: 100%;
left: 0px;
top: 0px;
position: relative;
background-image: url('/images/icon.jpg');
background-size: 5% auto;
background-position: 227px center;
background-attachment: fixed;
}
It should be something like the map-tag: http://www.w3schools.com/tags/tag_map.asp But instead of links there should be icons.
I hope you understand :-)
Best regards,
The One
Basically you can create a parent or wrapper element which would have the background image and then place all the elements like icons etc inside this and do all your positioning etc. So I've created this for you:
CSS
.container {
background: url(http://www.w3schools.com/tags/planets.gif) no-repeat;
width: 145px;
height: 126px;
position: relative;
}
.icon {
position: absolute;
width: 30px;
height: 30px;
border-radius: 100%;
z-index: 2;
}
.icon1 {
background: green;
top: 20%;
right: 10%;
}
.icon2 {
background: red;
bottom: 10%;
left: 10%;
}
HTML
<div class="container">
<div class="icon icon1"></div>
<div class="icon icon2"></div>
</div>
Here is an example on jsFiddle http://jsfiddle.net/j5cgt22z/
So each icon is positioned inside the container, the planets need to use position:absolute to float them around in the container space but the container needs to have position:relative so they are positioned in relation to their parent http://css-tricks.com/absolute-positioning-inside-relative-positioning/
You can then use z-index on each position:absolute icon to stack each icon so the higher the z-index higher up the stack.
Hope this helps
After realising that there is no general solution for the problem yet. (object-fit isn't widely support).
I used the jquery-Plugin imagefill.js.
CSS
.background {
width:100%;
height:100%;
top: 0;
left: 0;
background-image: url(http://connect.homes.com/wp-content/uploads/2012/05/200392710-0012.jpg);
background-size: cover;
z-index: 0;
position: absolute;
background-position: center center;
}
.container_icons
{
position: absolute;
height: 100%;
width: 100%;
}
.test
{
position: absolute;
background-image: url('http://www.clipartbest.com/cliparts/ncX/qyL/ncXqyLdcB.png');
background-size: 70px auto;
background-repeat: no-repeat;
background-position: 17% 49%;
}
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/2.1.0/jquery.imagesloaded.min.js"></script>
<script src="http://johnpolacek.github.io/imagefill.js/js/jquery-imagefill.js"></script>
<div class="background"></div>
<div class="container_icons"><img class="test" src="http://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png" width="3869px" height="2574px" /></div>
<script>
$('.container_icons').imagefill();
</script>
Here is a jsfiddle --> It doesn't work as good as on my webpage ;-)

How to make the background image of the pseudo element stretch to fit the width?

I am trying to overlay some text on an image, and also make the background image 0.2 transparent. now my question is: How to make the background image of the pseudo element stretch to fit the width or crop the image to fit the screen(without distort the image)? Edit the code on codepen here.
css:
#main:after {
content : "";
display: block;
position: absolute;
top: 0;
left: 0;
background-image: url(http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Selwyn_College_Old_Court%2C_Cambridge%2C_UK_-_Diliff.jpg/944px-Selwyn_College_Old_Court%2C_Cambridge%2C_UK_-_Diliff.jpg);
width: 100%;
height: 100%;
opacity : 0.2;
z-index: -1;
}
html:
<div id="main">
Text on top, no big deal, no big deal. Just a little text and stuff. That's all.
</div>
It's not entirely clear what you are tying to do but you could try adding background-size:cover as follows:
JSfiddle
CSS
#main:after {
content : "";
display: block;
position: absolute;
top: 0;
left: 0;
background-image: url(http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Selwyn_College_Old_Court%2C_Cambridge%2C_UK_-_Diliff.jpg/944px-Selwyn_College_Old_Court%2C_Cambridge%2C_UK_-_Diliff.jpg);
background-repeat: no-repeat;
background-size: cover;
width: 100%;
height: 100%;
opacity : 0.2;
z-index: -1;
}
You can use this:
#main:after {
content : "";
display: block;
position: absolute;
top: 0;
left: 0;
background-image: url(http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Selwyn_College_Old_Court%2C_Cambridge%2C_UK_-_Diliff.jpg/944px-Selwyn_College_Old_Court%2C_Cambridge%2C_UK_-_Diliff.jpg);
width: 100%;
height: 100%;
opacity : 0.2;
z-index: -1;
background-size: 100% 100%; /* added*/
background-repeat: no-repeat; /* optinal*/
}
Or in one line syntax like this:
background-size: auto|length|cover|contain|initial|inherit;
For more detail click.