CSS

[CSS] Pseudo-clase not para esquivar un first-child

[titulo][CSS] Seleccionarlo todo excepto una clase o elemento[/titulo]

En las hojas de estilo disponemos de decenas de tipos de selectores y pseudoclases, cuya intención es facilitarnos escoger a qué elemento aplicaremos un estilo.

Entre las pseudoclases más flexibles tenemos los [codigo]child[/codigo] que nos permiten seleccionar un elemento en función de su posición dentro del DOM.

Así pues:

/*Afecta únicamente al primer párrafo de una página*/
p:first-child {
}

/*Afecta únicamente al segundo div de una página*/
div:nth-child(2) {
}

/*Afecta únicamente al décimo span de una página*/
span:nth-child(10) {
}

/*Afecta únicamente al tercer elemento con la clase "armarios" de una página*/
.armarios:nth-child(3) {
}

 

Pero… ¿y si lo que queremos es lo contrario? ¿Y si queremos aplicar un estilo a TODOS los elementos EXCEPTO uno en concreto?

 

[titulo]La pseudo-clase :not[/titulo]

CSS3 incorporó la pseudoclase [codigo]:not[/codigo] cuyo funcionamiento es aplicar el estilo a todos los elementos excepto al dado en el argumento.

Por ejemplo:

div ul:not(:first-child) {
    background-color: #900;
}

Esto haría que todos los [codigo]ul[/codigo] dentro de [codigo]Divs[/codigo] adquiriedan un color de fondo, excepto los primeros [codigo]ul[/codigo] dentro de cada [codigo]div[/codigo].

Pero este método únicamente puede utilizarse con selectores simples, es decir:

  • selectores de tipo: [codigo]p[/codigo] [codigo]h1[/codigo] [codigo]div[/codigo]
  • selectores universales: [codigo]* [lang^=en][/codigo]  [codigo]*.warning[/codigo]
  • selectores de atributos: [codigo][att][/codigo][codigo][att=val][/codigo]
  • selectores de clase: [codigo].casa[/codigo][codigo].titulo2[/codigo]
  • selectores de ID:  [codigo]#pie[/codigo][codigo]#menu_movil[/codigo]
  • selectores de pseudo-clase: [codigo]::before[/codigo][codigo]::first-line[/codigo]

 

Si queremos aplicarlo a algo más complejo, tendremos que crear un estilo global (en el ejemplo, uno para todos los ul) y luego uno en particular para el elemento al que no queríamos aplicarle ese estilo (Ej: a los ul primeros):

div ul {
    background-color: #900;  /* Se aplica a todos los ul */
}

div ul:first-child {
    background-color: transparent; /* Reescribe el estilo anterior en los primeros ul */
}

Si generase conflicto, podemos aplicar [codigo]!important[/codigo] para imponer un estilo sobre otro:

div ul:first-child {
    background-color: transparent!important; 
}

 

 

Cristian Sarabia Martínez

Desde que a principios de los 90 mi padre desempolvó su Spectrum, no he dejado de probar y experimentar con la tecnología.

Enamorado del mundo web, Full Stack Developer de profesión y diseñador por devoción.

Ahora hago mis pinitos en esto del blogging para compartir con vosotros un poquito de todo lo que la comunidad me ha dado.

Escribir comentario

Haz clic aquí para dejar tu comentario

Flickr

  • Mehringdamm Session for a Dancer
  • EOS
  • Bookshop
  • Rica
  • THF stormy
  • Marta
  • May
  • Selma
  • Kurdes

About Author

ThemeForest

Collaboratively harness market-driven processes whereas resource-leveling internal or "organic" sources. Competently formulate.

Calendar

septiembre 2024
L M X J V S D
 1
2345678
9101112131415
16171819202122
23242526272829
30  

RSS Meks Blog

  • How Adding Slack Bot Boosted Our Culture of Appreciation 3 julio, 2024
    Sweet Kudos is a Slack bot that enhances employee recognition, rewards, and celebrations within your team. It empowers team members to express gratitude and appreciation effortlessly by giving virtual Kudos. The post How Adding Slack Bot Boosted Our Culture of Appreciation appeared first on Meks.
    Dusan Milovanovic
  • 10 Best Knowledge Base & Wiki WordPress Themes 2021 15 septiembre, 2021
    Running a successful online business requires an exceptional WordPress knowledge base theme that organizes documentation and helps customers. Customization options, intuitive navigation, unique layouts, and fast responsiveness are just some of the features you need. The following 10 WordPress wiki themes represent the best options for 2021 and beyond. Explore the full range to determine […]
    Dusan Milovanovic
  • How to increase WordPress Memory Limit (quick fixes) 16 junio, 2021
    Here is a post about how to increase the memory limit in WordPress. Allowed memory size exhausted error message showed up in your WordPress installation? No worries – this is one of the most common errors in WordPress. You can apply an easy fix by increasing the memory limit in your PHP. Table of Contents […]
    Dusan Milovanovic
  • How to use (and why) WordPress sitemap plugin 1 marzo, 2021
    Did you know that by knowing how to use the WordPress sitemap plugin you can significantly improve your site’s visibility and traffic? Although it isn’t mandatory to have a sitemap on your site, having one significantly improves the site’s quality, crawlability and indexing. All this is important for better optimization, which is why we wanted […]
    Ivana Cirkovic
  • 22 free and premium podcast software for your show [2021 edition] 18 enero, 2021
    You’re determined to start or improve your podcast but don’t know which podcast software to use to really make it stand out? We’ve got you! #podcasting Top 22 free and premium podcast software for your show #WordPressTips #podcasting The post 22 free and premium podcast software for your show [2021 edition] appeared first on Meks.
    Ivana Cirkovic
  • Digital storytelling with WordPress – an all-in-one guide to make your web stories pop! 23 noviembre, 2020
    Wondering how to improve digital storytelling with WordPress and build more awareness and exposure of your business? Let our guide lead the way. The post Digital storytelling with WordPress – an all-in-one guide to make your web stories pop! appeared first on Meks.
    Ivana Cirkovic
  • How to use WordPress autoposting plugin to improve your visibility and SEO? 10 septiembre, 2020
    Did you know you can use the WordPress autoposting plugin for your content efforts and improve not only your time management but your business and visibility as well? The post How to use WordPress autoposting plugin to improve your visibility and SEO? appeared first on Meks.
    Ivana Cirkovic
  • How to create a personal branding site? Step-by-step DIY guide 15 agosto, 2020
    Looking for ways and means to create a personal branding site? Well, look no further ’cause we’re giving away all the how-to’s to do it yourselves! The post How to create a personal branding site? Step-by-step DIY guide appeared first on Meks.
    Ivana Cirkovic
  • Top 15 WordPress content plugins and tools to improve your visibility and rankings 16 julio, 2020
    Let’s take a look at some of the must-have WordPress content plugins and tools to use to improve both your UX and rankings. The post Top 15 WordPress content plugins and tools to improve your visibility and rankings appeared first on Meks.
    Ivana Cirkovic
  • WCEU 2020 recap – key takeaways from the biggest online WordPress conference 9 junio, 2020
    Missed WCEU 2020 and all the exciting stuff from there? Here are all the key takeaways and main points to remember so, take notes! The post WCEU 2020 recap – key takeaways from the biggest online WordPress conference appeared first on Meks.
    Ivana Cirkovic

Text

Distinctively utilize long-term high-impact total linkage whereas high-payoff experiences. Appropriately communicate 24/365.

Archives