Reputation: 12847
I have 5 buttons without any Class or ID. Here's my code:
HTML
<button>Btn1</button>
<button>Btn2</button>
<button>Btn3</button> <!-- I don't want this elem to get the CSS styles -->
<button>Btn4</button>
<button>Btn5</button>
CSS
button {
width: 100px;
height: 45px;
background-color: #12aeef;
border: none;
box-shadow: 0px 5px 3px #888888;
}
How can I make the 3rd element not get the CSS styles?
Upvotes: 4
Views: 39624
Reputation: 20850
As an alternative to Mihai's answer, if you'd like to be able to use it on more than element, add a class
to any appropriate button
:
<button class="nostyle">
Then in your CSS, simply change button
to button:not(.nostyle)
.
That's it! ...what, you were expecting more work?
Here's the updated code:
button:not(.nostyle) {
width: 100px;
height: 45px;
background-color: #12aeef;
border: none;
box-shadow: 0px 5px 3px #888888;
}
<button>Btn1</button>
<button>Btn2</button>
<button class="nostyle">Btn3</button>
<button>Btn4</button>
<button>Btn5</button>
Upvotes: 13
Reputation: 16107
We want to remove styling (i.e.: the styling was already applied to the element and we want it removed) without any Classes or ID (i.e.: HTML must not be touched).
This requires some knowledge of the defaults ex.:
width: auto; // the default for sizes is auto
But also some investigation regarding the rest of your css because you may have some reset stylesheet included into your project which tells all elements everywhere to have behave diferently (this usually applies to paddings / margins / borders etc.).
We can use :nth-child()
, a CSS3 pseudo selector that finds child elements which ocupy the nth offset within the parent regardless of element type.
button:nth-child(3) {
width: auto;
height: auto;
background: none;
/* ... */
}
Try Connors last fiddle for the most efficience and elegance, or his first for full compatibility.
Upvotes: 4
Reputation: 7332
CSS pseudo clsses may help you. More info on nth child pseudo class can be obtained from Click Here
button:nth-child(3) {
// Your CSS Here
}
Upvotes: 2
Reputation: 20189
i don't think there is a cross browser way to do this in css without class
or id
so For Cross Browser Compatability you can do something like this
<div class="container">
<button>text 1</button>
<button>text 2</button>
<div class="no-style">
<button>text 3</button>
</div>
<button>text 4</button>
<button>text 5</button>
</div>
with this CSS
.no-style{
display:inline;
}
.container > button {
background:skyblue;
}
But if your not worried about browser compatability you can use
button:not(:nth-child(3)){
background:skyblue;
}
Upvotes: 3
Reputation: 3900
If you cannot change the HTML or the CSS stylesheet, you can try this jQuery (untested):
<script type='text/javascript' src='http://code.jquery.com/jquery.min.js'></script>
<script type='text/javascript'>
var buttons = $('button');
var styles = {'width': 'auto', 'height': 'auto', 'background': 'none', 'box-shadow': ''};
buttons[2].css(styles);
</script>
Upvotes: 2