4 Comments
CSS – Styling With Attribute Selectors
Every now and then, I will be documenting code tips as easy references for myself as I develop Web pages. I found myself in a frustrating situation last week when I needed to style a group of form elements but did not have access to the third party source code to add class selectors. The parent stylesheet also contained default input selectors that caused all input fields, regardless of whether they were hidden or not, to display onscreen. This was a problem for me as hidden fields, should well remain…hidden. The select menu drop downs were also a horrendous sight and needed proper widths set.
I remembered a technique in which you could target an attribute inside of a tag which would then allow you to style the element in question.
<input value="" name="state" type="hidden"> <label for="state">State:</label> <select name="stateName" id="state"> </option><option value="MN">Minnesota </option> …
To keep the hidden field undisplayed, I simply added positioning properties to keep it offscreen. I also added a width property to the select selector.
Solution
<style type="text/css">
input[type="hidden"] { position: absolute; left: -999px; width: 990px; }
select[name="stateName"] { width:250px; }
</style>
<input value="" name="state" type="hidden">
<label for="state">State:</label>
<select name="stateName" id="state">
</option><option value="MN">Minnesota
</option>
…
You could even take it farther by adding a little regex to target even more specific elements in your code.
Add ^ to match any attribute with a certain word at the beginning of the value.
select[name="^stateName"] { width:250px;}
Add $ to match any attribute with a certain word at the end of the value.
select[name="$stateName"] { width:250px;}
Add * to match any attribute with a certain word anywhere of the value.
select[name="*stateName"] { width:250px;}




4 Comments
TomPier says:
May 8, 2010 at 4:24 pmgreat post as usual!
physical therapist says:
June 1, 2010 at 10:43 pmI’ve recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all of your time & work.
Wordpress Themes says:
June 12, 2010 at 5:11 amGood post and this fill someone in on helped me alot in my college assignement. Gratefulness you on your information.
Wordpress Themes says:
July 27, 2010 at 5:38 amAmiable post and this post helped me alot in my college assignement. Say thank you you on your information.