{"id":22925,"date":"2024-03-14T21:48:00","date_gmt":"2024-03-14T20:48:00","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/selectores-pseudoelementos-en-css-todo-lo-que-necesitas-saber\/"},"modified":"2024-06-03T17:29:58","modified_gmt":"2024-06-03T16:29:58","slug":"selectores-pseudoelementos-en-css-todo-lo-que-necesitas-saber","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/","title":{"rendered":"Pseudoelement Selectors in CSS: Everything you need to know"},"content":{"rendered":"<p>In the world of web development, CSS is one of the fundamental languages to give style and design to our pages. One of the most powerful features of CSS is selectors, which allow us to select HTML elements and apply different styles to them. But within selectors, pseudo-elements are an even more interesting and versatile option.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#%C2%BFQue_son_los_pseudoelementos_en_CSS\" >What are pseudo elements in CSS?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#Selectores_de_pseudoelementos_mas_utilizados\" >Most used pseudo-element selectors<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#_before_y_after\" >::before and ::after<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#_first-letter_y_first-line\" >::first-letter and ::first-line<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#_selection\" >::selection<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#%C2%BFCual_es_la_diferencia_entre_before_y_after_en_CSS\" >What is the difference between ::before and ::after in CSS?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#%C2%BFComo_puedo_subrayar_un_texto_con_CSS\" >How can I underline text with CSS?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#%C2%BFCual_es_la_utilidad_de_los_pseudoelementos_en_CSS\" >What is the use of pseudo elements in CSS?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/pseudoelement-selectors-in-css-everything-you-need-to-know\/#%C2%BFCual_es_la_diferencia_entre_selection_y_before_after\" >What is the difference between ::selection and ::before\/::after?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_son_los_pseudoelementos_en_CSS\"><\/span>What are pseudo elements in CSS?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In CSS, a pseudo-element is a part of an element that is selected and styled independently of the actual content of the element. Basically, they allow you to manipulate and style specific parts of an element without needing to add additional HTML elements.<\/p>\n<p>Pseudo elements are used by adding a colon (::) after the selector. It is important to mention that starting with CSS3, pseudo-elements should be represented with two double colons (::), instead of a single colon (:).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Selectores_de_pseudoelementos_mas_utilizados\"><\/span>Most used pseudo-element selectors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Next, we will look at some of the most used pseudo-element selectors in CSS:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"_before_y_after\"><\/span>::before and ::after<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>These pseudo-elements allow us to add content before or after the actual content of an element. We can use them to add decorative elements, such as arrows or ornaments, or to add additional content dynamically with the property <code>content<\/code>. For example:<\/p>\n<pre>\nelement::before { content: &quot;Hello!&quot;; } element::after { content: &quot;Goodbye!&quot;; }\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"_first-letter_y_first-line\"><\/span>::first-letter and ::first-line<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>These pseudo-elements allow us to select and style the first letter or first line of a text element. For example, we can make the first letter of a paragraph larger or the first line of text underlined:<\/p>\n<pre>\np::first-letter { font-size: 2em; } p::first-line { text-decoration: underline; }\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"_selection\"><\/span>::selection<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This pseudo-element allows us to select and style the text that the user has selected on the page. For example, we can change the background color of the selected text:<\/p>\n<pre>\n::selection { background-color: yellow; color: black; }\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>CSS pseudo-element selectors allow us to have greater control and customization in the stylization of our web pages. With them, we can manipulate specific parts of an element without needing to add additional elements in the HTML. This gives us great flexibility and helps us achieve more creative designs. Always remember to use the two double colons (::) to represent pseudo elements in CSS3.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_diferencia_entre_before_y_after_en_CSS\"><\/span>What is the difference between ::before and ::after in CSS?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The difference between ::before and ::after in CSS lies in their position with respect to the actual content of the element. ::before adds content before the actual content, while ::after adds content after the actual content. Both pseudo elements are used to add decorative elements or additional content.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFComo_puedo_subrayar_un_texto_con_CSS\"><\/span>How can I underline text with CSS?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To underline text with CSS, you can use the ::first-line pseudo-element together with the <code>text-decoration: underline;<\/code>. This will cause the first line of text within the element to be underlined.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_utilidad_de_los_pseudoelementos_en_CSS\"><\/span>What is the use of pseudo elements in CSS?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Pseudo-elements in CSS are very useful because they allow us to select and style specific parts of an element without needing to add additional HTML elements. This gives us greater design flexibility and allows us to create custom effects and styles more efficiently.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_diferencia_entre_selection_y_before_after\"><\/span>What is the difference between ::selection and ::before\/::after?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The difference between ::selection and ::before\/::after in CSS is that ::selection allows us to select and style the text that the user has selected on the page, while ::before\/::after allows us to add content before or after the actual content of an element without needing to add additional elements in the HTML.<\/p>","protected":false},"excerpt":{"rendered":"<p>En el mundo del desarrollo web, CSS es uno de los lenguajes fundamentales para dar estilo y dise\u00f1o a nuestras p\u00e1ginas. Una de las caracter\u00edsticas m\u00e1s poderosas de CSS son los selectores, que nos permiten seleccionar elementos HTML y aplicarles diferentes estilos. Pero dentro de los selectores, los pseudoelementos son una opci\u00f3n a\u00fan m\u00e1s interesante [&hellip;]<\/p>","protected":false},"author":1,"featured_media":22926,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1121],"tags":[31,205,519,477,813,75,589,76],"class_list":["post-22925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-css","tag-que","tag-blog","tag-css","tag-necesitas","tag-pseudoelementos","tag-saber","tag-selectores","tag-todo"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=22925"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22925\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/22926"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=22925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=22925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=22925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}