Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

::before/::after pseudo class not working on video element

Tags:

html

css

video

I would like my video element to show a title over the video while it's playing, the problem is: it doesn't work. If I switch the <video> tag to a <div> it works perfectly fine. What am I doing wrong?

My HTML markup is this:

<div class="player">     <video src="video.mp4" poster="video-poster.jpg" title="Video Name"></video> </div> 

And my CSS is this:

.player {     position: relative;     width: 852px;     height: 356px; } .player video {     position: relative;     top: 0;     left: 0;     bottom: 0;     right: 0;     display: block; } .player video::after {     content: attr(title);     position: absolute;     top: 10px;     right: 15px;     color: #fff;     display: block;     height: 20px; } 
like image 215
Nathan Bishop Avatar asked Dec 30 '14 06:12

Nathan Bishop


People also ask

When would you use the :: before or :: after pseudo element in your CSS?

Special welcome offer: get $100 of free credit. 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 use a before or after pseudo element on an input field?

The input element has no content in the CSS view, and so has no :before or :after pseudo content. This is true of many other void or replaced elements. There is no pseudo element referring to outside the element.

What does :: Before mean in code?

::before is a pseudo-element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). Definition on Mozilla. ::before creates a pseudo-element that is the first child of the selected element.


1 Answers

Using :before or :after on a <video> tag won't work. You will also find this the case (sadly) with HTML input controls (e.g. textboxes).

This is because of the way :before and :after psuedo elements work. They are box model objects that work e.g. like a div but are placed inside their parent element. Elements like input text boxes and videos cannot contain child elements as far as content is concerned (video has src child tags but that is different).

E.g. if you had HTML:

<div class="awesome-highlight"> ... </div> 

And CSS:

.awesome-highlight::after {     content: 'Hello World'; } 

The rendered effect will be:

<div class="awesome-highlight">  ...  Hello World </div> 

More info:

W3: http://www.w3.org/TR/CSS21/generate.html

Old Stackoverflow answer: Which elements support the ::before and ::after pseudo-elements?

like image 184
Samuel MacLachlan Avatar answered Sep 19 '22 19:09

Samuel MacLachlan