Skip to content
Fix Code Error

not:first-child selector

March 13, 2021 by Code Error
Posted By: Anonymous

I have a div tag containing several ul tags.

I’m able to set CSS properties for the first ul tag only:

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

However, my following attempts to set CSS properties for each other ul tag except the first one don’t work:

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

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

div ul:first-child:after {
    background-color: #900;
}

How can I write in CSS: “each element, except the first”?

Solution

One of the versions you posted actually works for all modern browsers (where CSS selectors level 3 are supported):

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

If you need to support legacy browsers, or if you are hindered by the :not selector’s limitation (it only accepts a simple selector as an argument) then you can use another technique:

Define a rule that has greater scope than what you intend and then “revoke” it conditionally, limiting its scope to what you do intend:

div ul {
    background-color: #900;  /* applies to every ul */
}

div ul:first-child {
    background-color: transparent; /* limits the scope of the previous rule */
}

When limiting the scope use the default value for each CSS attribute that you are setting.

Answered By: Anonymous

Related Articles

  • What are the currently supported CSS selectors…
  • Convert array to nested JSON object - Angular Material tree
  • Change the values ​of a vector based on proximity…
  • backbone.js: How to instantiate models inside…
  • Rails wrong number of arguments error when…
  • The definitive guide to form-based website authentication
  • CSS selector for first element with class
  • Reference - What does this regex mean?
  • How do I keep only the first map and when the game…
  • Fix top buttons on scroll of list below
  • @selector() in Swift?
  • How to properly do JSON API GET requests and assign…
  • 'block in draw' rails 6 routes
  • What is git tag, How to create tags & How to…
  • Does "git fetch --tags" include "git fetch"?
  • What does do?
  • Push git commits & tags simultaneously
  • “tag already exists in the remote" error after…
  • How to properly translate shadow DOM CSS selectors…
  • How do you parse and process HTML/XML in PHP?
  • Neither BindingResult nor plain target object for…
  • How to write :hover condition for a:before and a:after?
  • performSelector may cause a leak because its…
  • Ember Data hasMany async observed property "simple" issue
  • Route's Model Hook with Ember Data "filter" not…
  • don't know what went wrong nav bar with Tailwind CSS…
  • Homebrew install specific version of formula?
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Smart way to truncate long strings
  • Change a HTML5 input's placeholder color with CSS
  • next js with redux and reselect
  • What methods of ‘clearfix’ can I use?
  • CSS selector - element with a given child
  • How to set level logging to DEBUG in Tomcat?
  • Search code inside a Github project
  • Redux Selectors in Mithril
  • Web colors in an Android color xml resource file
  • accessing another models data in ember.js
  • show all tags in git log
  • center 3 items on 2 lines
  • Alpine js get "tag input" as array
  • Adding tags to azure VMs via CSV
  • jQuery - get all divs inside a div with class ".container"
  • Vue.js Filter a multi dimension array in a computed function
  • What is your most productive shortcut with Vim?
  • Ember-Data: Accessing a list of sideloaded resources?
  • Flutter - Insert a Listview between two fixed containers
  • Selecting with complex criteria from pandas.DataFrame
  • jquery data selector
  • Update a Backbone model property of type array by pushing
  • JSP tricks to make templating easier?
  • Start redis-server with config file
  • Playing HTML5 video on fullscreen in android webview
  • How do I include a JavaScript file in another…
  • How can I uniquely identify a specific row in TSQL…
  • How do I install Java on Mac OSX allowing version switching?
  • For-each over an array in JavaScript
  • VUE Error when run test unit
  • List of ANSI color escape sequences
  • powershell logic for no of times the value printed…
  • Aurelia custom component in component
  • Vue CLI 3 / Webpack production build not working on…
  • How to list all Git tags?
  • Centering in CSS Grid
  • What are the undocumented features and limitations…
  • Constructing dynamic lists of lists with Polymer
  • Selector on background color of TextView
  • Better solution for nested Backbone.js collections
  • VueJS 2.0 v-model dynamic target inside v-for
  • What is a "cache-friendly" code?
  • How to separate opening and closing tag by…
  • EXP system for adjusting instance attributes in…
  • Sort table rows In Bootstrap
  • Using in Polymer 2.x
  • Change color iron-icon when tapped
  • jQuery vs document.querySelectorAll
  • VueJS - Initializing a tagsinput form field that was…
  • Simplest way to create Unix-like continuous pipeline…
  • Understanding PrimeFaces process/update and JSF…
  • What is the order of precedence for CSS?
  • Remove local git tags that are no longer on the…
  • Build tree array from flat array in javascript
  • :last-child not working as expected?
  • why my css code is not working in vuejs?
  • How can I manually compile a svelte component down…
  • How to detect page zoom level in all modern browsers?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How can I list all tags for a Docker image on a…
  • Vue doesn't update when computed data change
  • How can I avoid Java code in JSP files, using JSP 2?
  • Backbone.js Handling of Attributes That Are Arrays
  • Is there a CSS parent selector?
  • Logging best practices
  • Backbone.js - Should nested Views maintain…
  • What's the difference between including files with…
  • How does PHP 'foreach' actually work?
  • CSS selectors ul li a {...} vs ul > li > a {...}
  • Navbar not filling width of page when reduced to mobile view
  • How to set deactive other group list if one is…
  • Java - Values are added multiple times in a list

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I count the number of occurrences of a char in a String?

Next Post:

What does “connection reset by peer” mean?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error