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;
}
Related
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>
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?
I'm trying to get these two objects fixed on the users screen. Please note that I can only modify this with using CSS so the HTML can't be edited!, this is a CSS zen garden example (based on the '90s) I'm trying (which means in short you make a design based on a fixed html file so you can 'show off' what CSS is capable off.)
You can find a live example here.
http://lucasdebelder.be/zengarden/index.html
I got the top fixed and working with the following syntax.
body::before {
content: '';
position: fixed;
width: 100%;
height: 12.5%;
background: url(header_browser.svg);
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
I then tried the ::after statement on the body. But that doesn't work how can I get the bottom image (footer) sticked to the bottom?
body::after {
content: '';
position: fixed;
width: 100%;
height: 12.5%;
background: url(footer_browser.svg);
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
Tell your ::before pseudo element to go up the top at 0.
Tell your ::after pseudo element to go down the bottom at 0.
body::before {
content: '';
position: fixed;
top: 0;
width: 100%;
height: 12.5%;
background: #0f0;
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
body::after {
content: '';
position: fixed;
bottom: 0;
width: 100%;
height: 12.5%;
background: #f00;
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
this is the html
<div id="tab-1">
</div>
her is the css
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
demo
How to display background-image? If I use background-color then it works but why not background-image? And even if sometimes works in jsfiddle but not in my localhost.
You must take out the no-repeat from the background-image as you are using a short hand syntax inside background-image property which is not valid, inorder to use short hand syntax, you need to use background property instead
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: " ";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Demo (Separating background-repeat if you want to keep background-image)
Demo 2 (CSS Short hand syntax using background property)
Check out : http://jsfiddle.net/6nDKP/4/
Instead of :
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
Use :
background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
Separate the no-repeat
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Fiddle: http://jsfiddle.net/6nDKP/1/
The reason its not working is that your are including no-repeat in background-image style.
Exclude it to background-repeat: no-repeat;. Then it will work fine. Here is the code:-
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Change background-image with background.
#tab-1:before{
background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Demo: http://jsfiddle.net/zyuWW/
I am wanting to construct navigation items from three (background) images, with the first and last a fixed width, and the central a variable width, depending on the width of the text in the nav item. I have been led to believe that using the pseudo elements before and after would be the best method. When I try this though, the main (central) background image for the nav item is overlapping with the before and after background images.
You can see what I mean on this page.
Here is the CSS:
.box {
background-image: url(nav/images/nav_02.png);
background-repeat: repeat;
height:20px;
position: absolute;
padding: 10px 13px;
}
.box:before {
left: 0;
background-image: url(nav/images/nav_01.png);
}
.box:after {
right: 0;
background-image: url(nav/images/nav_03.png);
}
.box:before,
.box:after {
content: ' ';
width: 13px;
height:40px;
position: absolute;
top: 0;
background-repeat: no-repeat
}
And the HTML:
<div class="box">here is some text</div>
Can I use background-images in this way using pseudo elements?
Thanks,
Nick
Yes, but you will have to use left and right attributes for moving the pseudo elements in the right position. padding is not correct for the main box to position. Better use margin.
.box {
background-repeat-x: repeat;
background-image: url(nav/images/nav_02.png);
background-repeat: repeat;
height: 40px;
position: absolute;
margin: 0 13px;
line-height: 40px;
}
.box:before, .box:after {
content: ' ';
display:block;
width: 13px;
height: 40px;
position: absolute;
top: 0;
background-repeat: no-repeat;
}
.box:before {
left: -13px;
background-image: url(nav/images/nav_01.png);
}
.box:after {
right: -13px;
background-image: url(nav/images/nav_03.png);
}