How the ::before and ::after pseudo-element works

Hello guys, sorry to disturb you all; I'm trying to understand how the ::before and ::after pseudo-element work but I have a few question: 1. In the video I'm watching, from what is being told, we should always use the content property irrespective if we have any text content or not; even though the text content is null, we should use empty quotation. My question is why is it so please, why can't we just ignore the content property in this case? 2. If ever we use the content property with empty quotations (that is we will be using the pseudo-element mainly for styling), if we set a height, width and background color, we won't see any visual drawing until we set the position to absolute. My question is, why we should explicitly set the position to absolute? Even if the position is set to relative, it doesn't work, why position of absolute is important here please Here is the codepen where I'm trying to experiment: https://codepen.io/Fakeur/pen/PoMdKZV
42 Replies
b1mind
b1mindā€¢2mo ago
1: because that is how the spec works, it has to have content property to work. 2: you don't have to use position absolute. But by default I think its treated as a *inline element?
b1mind
b1mindā€¢2mo ago
No description
b1mind
b1mindā€¢2mo ago
hope this helps
Faker
FakerOPā€¢2mo ago
hmm when I remove the position absolute, the line is gone:
No description
Faker
FakerOPā€¢2mo ago
ah
b1mind
b1mindā€¢2mo ago
Look at the code I showed
Faker
FakerOPā€¢2mo ago
you set display block
b1mind
b1mindā€¢2mo ago
yes
Faker
FakerOPā€¢2mo ago
hmm how does display block makes the same effect like it reproduces the same thing
b1mind
b1mindā€¢2mo ago
because otherwise its inline and will not have a width
Faker
FakerOPā€¢2mo ago
ahhh I see
b1mind
b1mindā€¢2mo ago
No description
b1mind
b1mindā€¢2mo ago
So its putting it after the word element but without content is 0 width make sense?
Faker
FakerOPā€¢2mo ago
yep I see, thanks !!
b1mind
b1mindā€¢2mo ago
No description
Faker
FakerOPā€¢2mo ago
for the content property thing, I just need to know that we need to use it everytime we use ::after or ::before pseudo-element ?
b1mind
b1mindā€¢2mo ago
Yes
Faker
FakerOPā€¢2mo ago
yep here it has a width
b1mind
b1mindā€¢2mo ago
content="" is required period without it it will not render
Faker
FakerOPā€¢2mo ago
by the way the content property is only used for the before and after pseudo element ?
b1mind
b1mindā€¢2mo ago
yes Well šŸ¤” Yea pretty sure it is xD I was thinking counter used it but its not
Faker
FakerOPā€¢2mo ago
Yep I see, I have a clearer idea now, thanks !!
b1mind
b1mindā€¢2mo ago
np! glad I could help
Faker
FakerOPā€¢2mo ago
last question; consider the 2 images; on the left, we use position absolute and on the right , we use display block; my question is why on the left, the space between the text and the line is smaller compared to that on the right please, for e.g, we can see in the left picture that the 'y' isn't well displayed compared to the right image
No description
No description
b1mind
b1mindā€¢2mo ago
Because absolute removes it from the stack, where as display block has both in the same stack and are aware of each other. So you basically are overlaying vs adding
Faker
FakerOPā€¢2mo ago
yep I see ty !
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
a big difference with position absolute is that the element will be positioned within the first element without position static
Faker
FakerOPā€¢2mo ago
yep, that's why I needed to set position relative to the h1 element
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
yup, or it will leech itself to <body> or <html>
b1mind
b1mindā€¢2mo ago
body is the first relative yes? šŸ„²
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
i dont think so i think it is static too
b1mind
b1mindā€¢2mo ago
yea idk thats why I'm asking hah
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
yes, its static and <html> too
Faker
FakerOPā€¢2mo ago
by the way, when we say "removes it from the stack" , it's like saying "removes it from the normal flow" ? I often saw the wordings "stack context" but never try to figure out what it means exactly šŸ˜…
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
but there are no more parents, which is probably why it doesnt just poof out of existence
b1mind
b1mindā€¢2mo ago
it seems to work off body šŸ¤·ā€ā™‚ļø
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
yes
b1mind
b1mindā€¢2mo ago
You should learn what it means Stacking context is one the most important things for you to learn in CSS
Faker
FakerOPā€¢2mo ago
yep I will
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
he said it right too
Faker
FakerOPā€¢2mo ago
okk, let me try to look for a nice video
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
the element is removed from the normal flow into a new stacking context
Want results from more Discord servers?
Add your server