freeCodeCamp/guide/english/css/before-selector/index.md

62 lines
2.3 KiB
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Before Selector
---
# Before Selector
The CSS `::before` selector can be used to insert some content, usually cosmetic, *before* the content of an element or elements. It is used by attaching `::before` to the element it is to be used on. It is an inline element by default.
2018-10-12 19:37:13 +00:00
## Examples
2018-10-12 19:37:13 +00:00
Let's look at some examples:
```css
p::before {
content: "";
border: solid 5px #ccc;
2018-10-12 19:37:13 +00:00
}
span.comment::before {
2018-10-12 19:37:13 +00:00
content: "Comment: ";
color: blue;
}
```
```html
<p> To infinity and beyond</p>
<p> I am Buzz Lightyear, I come in peace.</p>
2018-10-12 19:37:13 +00:00
<span class="comment"> May the force be with you</span>
<br/>
<span> Do, or do not. There is no try.</span>
2018-10-12 19:37:13 +00:00
```
In the example above we are prepending a grey border before every paragraph element on a page and we are also prepending the word "Comment: " in blue before every `span` element with the class name `comment`.
2018-10-12 19:37:13 +00:00
> You can check out this demo here https://jsfiddle.net/kxrdswtm/
2018-10-12 19:37:13 +00:00
## Definition and Usage
`::before` is one of the CSS pseudo-elements selectors, which are used to style specified parts of an element. In this case, we can insert content before some HTML element from CSS. Although we will see the content in the page, it is not part of the Document Object Model (DOM), which means that we cannot manipulate it from JavaScript.
2018-10-12 19:37:13 +00:00
```css
p::before {
content: "Hello, ";
}
2018-10-12 19:37:13 +00:00
```
```html
<p>World!</p>
2018-10-12 19:37:13 +00:00
```
This will display `Hello, World!` on the page.
2018-10-12 19:37:13 +00:00
String, images, counters, or even an empty string ("", useful for `clearfix`) can be inserted into the `content` attribute. HTML cannot be inserted.
2018-10-12 19:37:13 +00:00
[See A Whole Bunch of Amazing Stuff Pseudo Elements Can Do!](https://www.w3schools.com/css/css_pseudo_elements.asp)
2018-10-12 19:37:13 +00:00
## Single-colon vs. Double-colon
There's a bit of discussion about the right way of using pseudo-elements: old style single-colon (`:before`), used in CSS specifications 1 and 2, versus CSS3 recommendation, double-colon (`::before`), mainly to "establish a discrimination between pseudo-classes and pseudo-elements". But for compatibility reasons, single-colon is still accepted. IE8 supports the single-colon notation only.
2018-10-12 19:37:13 +00:00
## Additional Resources
- [W3Schools CSS Pseudo-elements](https://www.w3schools.com/css/css_pseudo_elements.asp)
- [CSS-Tricks ::after/::before](https://css-tricks.com/almanac/selectors/a/after-and-before/)