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);
}
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>
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;
}
I try to put a line on only one word. With changing its size and its position.
Here the result I would like to have:
I tried to use a span with background-image but no success.
https://jsfiddle.net/XZKS/193u9dam/
And other problem, background-image don't work when using local image.
My website arborescence:
_include
css
style.css
js
img
line.png
background-image: url("../img/line.png");
I hope someone could help me, thanks
Try this:
.myWordWithLine {
position: relative;
}
.myWordWithLine::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
right: 0;
z-index: -1;
height: 24px; // your line height
background-color: red; // your line color
}
Method #01:
You can use css linear-gradient() to draw this background:
Steps:
Create background image with linear-gradient().
Adjust its size with css background-size property.
Place it at the bottom position of the element with background-position css property.
Necessary CSS:
.line {
background: linear-gradient(to right, green, green) no-repeat;
background-size: 100% 5px;
background-position: left bottom 5px;
}
h3 {
font-size: 24px;
}
.line {
background: linear-gradient(to right, green, green) no-repeat;
background-size: 100% 5px;
background-position: left bottom 5px;
position: relative;
padding-top: 0;
padding-bottom:0;
}
<h3>MY <span class="line">BLOG</span></h3>
Method #02:
You can use ::before OR ::after pseudo element:
h3 {
font-size: 24px;
}
.line {
position: relative;
}
.line::after {
background: green;
position: absolute;
bottom: 5px;
z-index: -1;
content: '';
left: 0;
right: 0;
height: 5px;
}
<h3>MY <span class="line">BLOG</span></h3>
You can use this
http://codepen.io/B101844/pen/bgLPPb
html
<div class="main">MY
<div class="blog">
BLOG
<div class="underline"></div>
</div>
</div>
Css
.main{
font-size: 30px;
color: #1c3d93;
font-weight: 900;
}
.blog{
display: inline-block;
position: relative;
}
.underline{
position: absolute;
background-color: #91dfcf;
left: 0px;
right: 0px;
bottom: 7px;
z-index: -1;
height: 8px;
}
Trying to use sass to position a sprite: CodePen Link. I want to have each card show, but I can only get the visa one to show using the &:before method. Can I not nest .visa/.mastercard/.amex in &:before?
<div class="saved_cc_block">
<div class="saved_cc">
<a class="cc_img visa"></a>
</div>
<div class="saved_cc">
<a class="cc_img mastercard"></a>
</div>
</div>
.cc_img {
position: relative;
height: 26px;
left: 9px;
padding-left: 50px;
&:before {
content: "";
position: absolute;
width: 41px;
height: 26px;
top: 50%;
left: 0;
background: url('http://i.imgur.com/3zRD5fn.png') no-repeat;
.visa {
background-position: 0;
}
.mastercard {
background-position: -51px 0;
}
.amex {
background-position: -102px 0;
}
}
}
:before is a pseudo element, you can't nest other elements within it. I think you probably want to structure your css like this:
.cc_img {
position: relative;
height: 26px;
left: 9px;
padding-left: 50px;
&:before {
content: "";
position: absolute;
width: 41px;
height: 26px;
top: 50%;
left: 0;
background: url('http://i.imgur.com/3zRD5fn.png') no-repeat;
}
}
.visa:before {
background-position: 0;
}
.mastercard:before {
background-position: -51px 0;
}
.amex:before {
background-position: -102px 0;
}
No, you can't.
The elements with those class names are children of the cc_img div, not of the generated pseudo-element that appears before it. Psuedo-elements can't have child nodes.
Look at generating the psuedo-element before the link instead of before the div.
I am using multiple backround images width one div but it dosn't work in IE8.
Here is my css code:
.description-page #main-navigation ul{
text-align:left;
width: 451px;background:url(../images/menu-desing.png) no-repeat center 26px ,
url(../images/top-bar1.png) no-repeat center 0px ;
height: 86px; z-index:100;padding-top: 9px;
}
Is there is any solution to this problem?
you can use CSS3pie to achieve this - http://css3pie.com/documentation/supported-css3-features/#pie-background
You can do something like the following to achieve two backgrounds also for IE8:
You need only one element in the DOM, we will create the second one with the help of pseudo-elements, which work already in IE8 (not IE7).
.description-page #main-navigation ul {
text-align:left;
width: 451px;
background:url(../images/menu-desing.png) no-repeat center 26px;
height: 86px;
padding-top: 9px;
position: relative;
z-index: 1;
}
/* Generate a new element with the second background, positioned on the same place like the original ul */
.description-page #main-navigation ul:before {
background: url(../images/top-bar1.png) no-repeat center 0px;
content: "";
height: 86px;
left: 0;
position: absolute;
top: 0;
width: 451px;
z-index: -1;
}