Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Pseudo-elements :before and :after inheriting width/height from original element

I am using the css pseudo elements :before and :after to give an indent-effect on some of my images on a website. However without specifying the width and height, these won't display. This would have me specifying a fixed width and height for each of the images, which I guess would work for a static webpage.

However because these images are generated dynamically with jQuery and are user submitted, images differ in width and height each time. Now I could probably can fix this with Javascript by getting the width from the image and passing it on to the :before, but this seems like it is too much work for something like this.

My question is if there is a way to do this with CSS only, to have the width of containing the image being passed on to the :before on this < li >, so that the :before and :after pseudo-elements inherit the width and height of the orginal element.

The basic page layout:

<ul>    <li>        <img src="foo" />    </li>    </ul>      # css style simplefied ul{ float:left; list-style:none} li{float:left;} li img{float:left} li:before{           content:"":           position:relative;           position:absolute;           float:left;              box-shadow:0 1px 2px rgba(0,0,0,0.4);     } 

PS: compatibility needed is only for mobile Webkit browsers.

EDIT

I could for instance add lines to the CSS with Javascript by using the following lines:

var heightImg = (($('ul li:nth-child(n)').height())) + 'px';     document.styleSheets[1].insertRule('ul li:before { height: ' +  heightImg+ '; }', 0); 

But this would mean that I'll also have to work with dynamic id's. Which won't be hard, but I'm just wondering if there isn't a CSS only way.

like image 489
Marc Hoogvliet Avatar asked Jul 06 '11 13:07

Marc Hoogvliet


People also ask

Do pseudo-elements inherit?

No. The only way that pseudo-elements can inherit values from the parent of their generating element is when the generating element itself is also inheriting from its parent. This is because inheritance occurs from a parent to a child, one level at a time.

How do you use before and after pseudo-elements?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

Can I have multiple before pseudo-elements for the same element?

In CSS2. 1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

What is :: before and :: after?

Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content. Version: CSS2.


2 Answers

:before and :after pseudo-elements are inline boxes as much as I know. Therefore, using display: block; might help you.

like image 199
Saeed Neamati Avatar answered Oct 22 '22 11:10

Saeed Neamati


li{     float:left;     position:relative; } li img{     float:left; } li:before{     content:" ";     position:absolute;     width:100%;     height:100%     box-shadow:0 1px 2px rgba(0,0,0,0.4); } 
like image 40
Hugo Silva Avatar answered Oct 22 '22 11:10

Hugo Silva