stuffs
stuffs
Geek Answers Handbook

CSS selector for numbered class / id

I have the following situation:

<div id="myMenu">
   <div id="menu0">stuffs</div>
   <div id="menu1">stuffs</div>
   <div id="menu2">stuffs</div>
   ...... and so on
</div>

My requirement is access to all divs with an identifier $=menuinside myMenuexcept menu0, since my menu can have from 10 to 15 elements, so one way:

 #myMenu > menu1 {style}
 #myMenu > menu2 {style}
 so on... 15 times

but since I have to give the same style to all of them, this seems unnecessary, I am looking for a CSS selector that fits correctly for my requirement, which also has IE8 compatibility.

Any help is appreciated.

+4
html css cross-browser css-selectors css3
Vivek Nov 12 '14 at 13:40
source share
5 answers

If you always have an element #menu0, you can use the general sibling selector compatible with IE8:

    #menu0 ~ [id^="menu"] {
        color: red;
    }
<div id="myMenu">
   <div id="menu0">stuffs</div>
   <div id="menu1">stuffs</div>
   <div id="menu2">stuffs</div>
</div>
Run codeHide result

classes ( id s), .

+4
Andrea Ligios 12 . '14 13:53

css3 # menu0:

div#myMenu > div:not(#menu0)
{
}

:

div#myMenu > div
{ 
    /*new values*/ 
}

div#myMenu > div#menu0
{ 
    /*reset with the original values*/ 
}

div, , reset # menu0 .

+4
C Bauer 12 . '14 13:42

, :

#myMenu div[id^="menu"]:not(#menu0) {
  color: red;
}
<div id="myMenu">
  <div id="menu0">stuffs</div>
  <div id="menu1">stuffs</div>
  <div id="menu2">stuffs</div>
  <div id="menu3">stuffs</div>
  <div id="menu4">stuffs</div>
  <div id="menu5">stuffs</div>
</div>
Hide result

id, 'menu' id #myMenu, id #menu0

, . ie8 :

#myMenu div[id^="menu"] {
  color: red;
}
#myMenu #menu0 {
  color: #000;
}
<div id="myMenu">
  <div id="menu0">stuffs</div>
  <div id="menu1">stuffs</div>
  <div id="menu2">stuffs</div>
  <div id="menu3">stuffs</div>
  <div id="menu4">stuffs</div>
  <div id="menu5">stuffs</div>
</div>
Hide result

id .

+4
Alex Char 12 . '14 13:43

:

<div id="myMenu">
   <div id="menu0">stuffs</div>
   <div id="menu1" class="sub">stuffs</div>
   <div id="menu2" class="sub">stuffs</div>
   ...... and so on
</div>

:

#myMenu > .sub{ ... }

#myMenu  .sub{ ... }
+1
AvrilAlejandro 12 . '14 13:44

, , :

/* selects all the <div>s with an id beginning with 'menu',
   that follow a <div> with an id beginning with menu, that
   are the direct-children of the element with an id of 'myMenu': */
#myMenu > div[id^=menu] + div[id^=menu] {
    /* css here */
}

:

/* selects all <div> elements that are not the :first-child
   that are direct children of <div id="myMenu">: */
#myMenu > div:not(:first-child)
    /* css here */
}

:

/* selects all <div>s with an id beginning with menu that
   have a previous sibling <div> with an id beginning with
   'menu' that is the direct child of <div id="myMenu">: */
#myMenu > div[id^=menu] ~ div[id^=menu] 
    /* css here */
}
+1
David Thomas 12 . '14 13:47

More articles:

  • cordova readAsText returns a json string that cannot be parsed for a JSON object - json
  • Android - three-dimensional pie chart - android
  • do i need connection.commit () after executeBatch ()? - java
  • TinyMCE Enable button in read-only mode - javascript
  • search words by line and format changes - php
  • Why does he always print magazines with a level of reliability of information? - c ++
  • Filtering ng options based on user selection - angularjs
  • https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1563619/opencv-capturing-desktop-screen-live&usg=ALkJrhjbvPfFITC-fAobLF2p16yOa7-v8A
  • Is my batch really running as expected if autocommit is set to true? - java
  • The order of the IP addresses in the gethostbyname function - c ++

All Articles

Geek Answers | 2019