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; }
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.
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.
::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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With