Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:before and background-image... should it work?

I've got a div and apply :before and :after an image as content. That works perfectly. Now I would need to apply a background image so it does repeat as the div resizes, but it does not seem to work. Is background image on :before and :after supposed to be working?

The current code:

HTML:

<div id="videos-part">test</div> 

CSS:

#videos-part{     background-color: #fff;     height: 127px;     width: 764px;     margin: -6px 0 -1px 18px;     position: relative; } #videos-part:before{     width: 16px;     content: " ";     background-image: url(/img/border-left3.png);     position: absolute;     left: -16px;     top: -6px; } 
like image 523
Michi Avatar asked Aug 26 '11 11:08

Michi


People also ask

Why is my CSS background image not working?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

Should I use image or background image?

Use background-image if you need to improve download times, as with CSS sprites. Use background-image if you need for only a portion of the image to be visible, as with CSS sprites. Use background-image with background-size:cover in order to stretch a background image to fill its entire window.

What is the correct way to set background image?

The most common & simple way to add background image is using the background image attribute inside the <body> tag. The background attribute which we specified in the <body> tag is not supported in HTML5.


2 Answers

@michi; define height in your before pseudo class

CSS:

#videos-part:before{     width: 16px;     content: " ";     background-image: url(/img/border-left3.png);     position: absolute;     left: -16px;     top: -6px;     height:20px; } 
like image 188
sandeep Avatar answered Oct 05 '22 22:10

sandeep


Background images on :before and :after elements should work. If you post an example I could probably tell you why it does not work in your case.

Here is an example: http://jsfiddle.net/namas/3/

You can specify the dimensions of the element in % by using background-size: 100% 100% (width / height), for example.

like image 27
meo Avatar answered Oct 06 '22 00:10

meo