Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same style for id's that end with the same letters

Tags:

css

I have in my screen a few tables that their id ends with "test": studentsTest, marksTest, classesTest etc.

I want them all to have the same style. Is there a way to define a style for all the objects which thier id ends with the same characters?

Thanks Devora

like image 805
Dvora Avatar asked Mar 23 '11 08:03

Dvora


People also ask

How do you write id in style?

The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id. The syntax for id is: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.

What is an id style?

A Type ID personality is one of the 160 combination styles identified in the Extended DISC assessment. People with an ID personality type tend to be optimistic, talkative, and goal-oriented, focusing on the big picture. They are likely to have an easy and relaxed manner when speaking.

Can two elements have the same id?

This is because an id value can be given to only one HTML element, in other words, multiple elements cannot have the same id value on the same page.

How do you use id in style tag?

To use an ID selector in CSS, you simply write a hashtag (#) followed by the ID of the element. Then put the style properties you want to apply to the element in brackets.


2 Answers

If you are able to change your mark-up, the easiest, and most cross-browser compliant way of acheiving this is to use a 'test' class, for example:

.test {background-color:#FC0;}

<table id="students" class="test">...</table>
<table id="marks" class="test">...</table>
<table id="classes" class="test">...</table>

If you're not able to change the mark-up but you are able to use jQuery you can style IDs that end with 'test' like so:

$(document).ready(function(){

    $('table[id$=test]').css('background-color','#FC0');

});

Your final option, if you can't or won't use jQuery (or similar) is to use pure CSS3. This uses the same syntax as above but will only work in the more modern browsers:

table[id$=test] {background-color:#FC0;} 
like image 76
ajcw Avatar answered Sep 28 '22 01:09

ajcw


According to W3C it's theoretically possible but only in CSS 3.

E[foo$="bar"] an E element whose "foo" attribute value ends exactly with the string "bar"

However I'd say this is far from teh best way of going about it because it will only work in the latest browsers. I also doubt the performance would be up to much. I'd suggest you give the elements that you need to share a common style a class and then style the class.

like image 40
GordonM Avatar answered Sep 28 '22 00:09

GordonM