Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the only Element with no class or ID

I've seen both this and this question, but they're both Javascript-oriented.

I'm looking to select the only div which has no ID or Class.

Example:

<body>
<div class="X" ... />
<div class="Z" ... />
...
<div class="Y" ... />
<div>Select me</div>
<div id="footnote" ... /> <!-- Notice I have no class. -->
...
</body>

There will only ever be one div with no class and id.
The div may change places, or be not present.
No Javascript. However any language which can compile to CSS like SASS is fine.
The divs in question will only ever be directly under <body>.
I may not always know what classes or IDs there will be.

like image 861
Robobenklein Avatar asked Jun 15 '15 20:06

Robobenklein


People also ask

How do you select an element without class?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How do I select an element in jQuery without an ID or class?

var noSpace= $('*:not([id])*:not([class])'); // 84 ?

How do you select an element by class or ID?

By chaining together class names in the CSS, you can select elements that have both classes at the same time. For example, the CSS selector . color-1. color-2 would only select elements that have an HTML class value that contains both color-1 and color-2 .

How do you select non classes in CSS?

The :not() pseudo-class requires a comma-separated list of one or more selectors as its argument. The list must not contain another negation selector or a pseudo-element.


3 Answers

You can use the :not:

CSS:

div:not([class]):not([id]) {
    height: 200px;
    width: 200px;
    background-color: red;
}

HTML:

<div></div>
<div class="shikaka"></div>
<div id="shikaka2"></div>    

http://jsfiddle.net/qcq0qedj/

like image 131
taxicala Avatar answered Sep 30 '22 04:09

taxicala


You can do:

body > div:not([class]):not([id]) { }

JSFiddle

like image 24
Multitallented Avatar answered Sep 30 '22 04:09

Multitallented


Ah... the beauty of :not. (AKA - not:ugly)

div:not([class]):not([id]) {
    color: #fff;
    padding: 2px 4px;
    background: red;
}
<body>
    <div class="X">...</div>
    <div>Select me</div>
    <div class="Z">...</div>
    <div class="Y">...</div>
    <div>Select me, too!</div>
    <div id="footnote">...</div>
</body>

http://jsfiddle.net/stevenventimiglia/53Lqekod/

like image 32
Steven Ventimiglia Avatar answered Sep 30 '22 03:09

Steven Ventimiglia