Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. 12.1 The :before and :after pseudo-elements
  2.  
  3. Authors specify the style and location of generated content with the :before and :after pseudo-elements. As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content. The 'content' property, in conjunction with these pseudo-elements, specifies what is inserted.
  4.  
  5. Example(s):
  6.  
  7. For example, the following rule inserts the string "Note: " before the content of every P element whose "class" attribute has the value "note":
  8.  
  9. p.note:before { content: "Note: " }
  10.  
  11. The formatting objects (e.g., boxes) generated by an element include generated content. So, for example, changing the above style sheet to:
  12.  
  13. p.note:before { content: "Note: " }
  14. p.note { border: solid green }
  15.  
  16. would cause a solid green border to be rendered around the entire paragraph, including the initial string.
  17.  
  18. The :before and :after pseudo-elements inherit any inheritable properties from the element in the document tree to which they are attached.
  19.  
  20. Example(s):
  21.  
  22. For example, the following rules insert an open quote mark before every Q element. The color of the quote mark will be red, but the font will be the same as the font of the rest of the Q element:
  23.  
  24. q:before {
  25. content: open-quote;
  26. color: red
  27. }
  28.  
  29. In a :before or :after pseudo-element declaration, non-inherited properties take their initial values.
  30.  
  31. Example(s):
  32.  
  33. So, for example, because the initial value of the 'display' property is 'inline', the quote in the previous example is inserted as an inline box (i.e., on the same line as the element's initial text content). The next example explicitly sets the 'display' property to 'block', so that the inserted text becomes a block:
  34.  
  35. body:after {
  36. content: "The End";
  37. display: block;
  38. margin-top: 2em;
  39. text-align: center;
  40. }
  41.  
  42. The :before and :after pseudo-elements elements interact with other boxes, such as run-in boxes, as if they were real elements inserted just inside their associated element.
  43.  
  44. Example(s):
  45.  
  46. For example, the following document fragment and style sheet:
  47.  
  48. <h2> Header </h2> h2 { display: run-in; }
  49. <p> Text </p> p:before { display: block; content: 'Some'; }
  50.  
  51. ...would render in exactly the same way as the following document fragment and style sheet:
  52.  
  53. <h2> Header </h2> h2 { display: run-in; }
  54. <p><span>Some</span> Text </p> span { display: block }
  55.  
  56. Similarly, the following document fragment and style sheet:
  57.  
  58. <h2> Header </h2> h2 { display: run-in; }
  59. h2:after { display: block; content: 'Thing'; }
  60. <p> Text </p>
  61.  
  62. ...would render in exactly the same way as the following document fragment and style sheet:
  63.  
  64. <h2> Header <span>Thing</span></h2> h2 { display: block; }
  65. span { display: block; }
  66. <p> Text </p>
  67.  
  68. Note. This specification does not fully define the interaction of :before and :after with replaced elements (such as IMG in HTML). This will be defined in more detail in a future specification.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement