css - When to use :before or :after -


before moving question, know how :before , :after selectors work. (not duplicate of what ::before or ::after expression). question in regards use.

i've seen inconsistencies on years these selectors have been used display same thing. same results, different approach. in specific cases, such adding font awesome icon within li before a :before selector makes sense. i'm not inquiring use, since it's intuitive enough understand. take speech bubble tooltip instance. have seen triangle placed :before , :after , in occasions use both! i'm confused.

what determining factor on choosing selector should used attach element such triangle on speech bubble?

allow me demonstrate:

html

<div class="container">     <div class="bubble">this text in bubble using :after</div>     <div class="bubble2">this text in bubble using :before</div> </div> 

css

.bubble{   position: relative;   padding: 15px;   margin: 1em 0 3em;   color: #000;   background: #f3961c;   border-radius: 10px;   background: linear-gradient(top, #f9d835, #f3961c); }  .bubble:after {   content: "";   display: block;    position: absolute;   bottom: -15px;   left: 50px;   width: 0;   border-width: 15px 15px 0;   border-style: solid;   border-color: #f3961c transparent; }   .bubble2 {   position: relative;   padding: 15px;   margin: 1em 0 3em;   color: #000;   background: #f3961c;   border-radius: 10px;   background: linear-gradient(top, #f9d835, #f3961c); }  .bubble2:before {   content: "";   display: block;    position: absolute;   bottom: -15px;   left: 50px;   width: 0;   border-width: 15px 15px 0;   border-style: solid;   border-color: #f3961c transparent; } 

img

enter image description here

please don't tell me it's matter of preference. lol

demo

the nomenclature of ::before , ::after not entirely arbitrary, makes sense when content of pseudo elements displayed inline, before or after content of element attached to.

as use

position: absolute; 

in pseudo element (which totally legitimate), no longer matters whether pseudo element named ::before or ::after.

it might named ::presentational-frill-1 or ::presentational-frill-2.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -