[html] Can I apply a CSS style to an element name?

I'm currently working on a project where I have no control over the HTML that I am applying CSS styles to. And the HTML is not very well labelled, in the sense that there are not enough id and class declarations to differentiate between elements.

So, I'm looking for ways I can apply styles to objects that don't have an id or class attribute.

Sometimes, form items have a "name" or "value" attribute:

<input type="submit" value="Go" name="goButton">

Is there a way I can apply a style based on name="goButton"? What about "value"?

It's the kind of thing that's hard to find out because a Google search will find all sorts of instances in which broad terms like "name" and "value" will appear in web pages.

I'm kind of suspecting the answer is no... but perhaps someone has a clever hack?

Any advice would be greatly appreciated.

This question is related to html css css-selectors

The answer is


You can use the attribute selector,

_x000D_
_x000D_
input[name="goButton"] {_x000D_
  background: red;_x000D_
}
_x000D_
<input name="goButton">
_x000D_
_x000D_
_x000D_

Be aware that it isn't supported in IE6.

Update: In 2016 you can pretty much use them as you want, since IE6 is dead. http://quirksmode.org/css/selectors/

http://reference.sitepoint.com/css/attributeselector


Text Input Example

_x000D_
_x000D_
input[type=text] {_x000D_
  width: 150px;_x000D_
}_x000D_
_x000D_
input[name=myname] {_x000D_
  width: 100px;_x000D_
}
_x000D_
<input type="text">_x000D_
<br>_x000D_
<input type="text" name="myname">
_x000D_
_x000D_
_x000D_


_x000D_
_x000D_
input[type=text] {
  width: 150px;
  length: 150px;
}

input[name=myname] {
  width: 100px;
length: 150px;
}
_x000D_
<input type="text">
<br>
<input type="text" name="myname">
_x000D_
_x000D_
_x000D_


If i understand your question right then,

Yes you can set style of individual element if its id or name is available,

e.g.

if id available then u can get control over the element like,

<input type="submit" value="Go" name="goButton">

var v_obj = document.getElementsById('goButton');

v_obj.setAttribute('style','color:red;background:none');

else if name is available then u can get control over the element like,

<input type="submit" value="Go" name="goButton">

var v_obj = document.getElementsByName('goButton');

v_obj.setAttribute('style','color:red;background:none');

Using [name=elementName]{} without tag before will work too. It will affect all elements with this name.

For example:

_x000D_
_x000D_
[name=test] {_x000D_
  width: 100px;_x000D_
}
_x000D_
<input type=text name=test>_x000D_
<div name=test></div>
_x000D_
_x000D_
_x000D_


For future googlers, FYI, the method in the answer by @meder , can be used with any element that has a name attribute, so lets say theres an <iframe> with the name xyz then you can use the rule as belows.

iframe[name=xyz] {    
    display: none;
}   

The name attribute can be used on the following elements:

  • <button>
  • <fieldset>
  • <form>
  • <iframe>
  • <input>
  • <keygen>
  • <map>
  • <meta>
  • <object>
  • <output>
  • <param>
  • <select>
  • <textarea>

This is the perfect job for the query selector...

var Set1=document.querySelectorAll('input[type=button]');  // by type

var Set2=document.querySelectorAll('input[name=goButton]'); // by name

var Set3=document.querySelectorAll('input[value=Go]'); // by value

You can then loop through these collections to operate on elements found.


You can use attribute selectors but they won't work in IE6 like meder said, there are javascript workarounds to that tho. Check Selectivizr

More detailed into on attribute selectors: http://www.css3.info/preview/attribute-selectors/

/* turns all input fields that have a name that starts with "go" red */
input[name^="go"] { color: red }

if in case you are not using name in input but other element, then you can target other element with there attribute.

_x000D_
_x000D_
    [title~=flower] {_x000D_
      border: 5px solid yellow;_x000D_
    }
_x000D_
    <img src="klematis.jpg" title="klematis flower" width="150" height="113">_x000D_
    <img src="img_flwr.gif" title="flower" width="224" height="162">_x000D_
    <img src="img_flwr.gif" title="flowers" width="224" height="162">
_x000D_
_x000D_
_x000D_

hope its help. Thank you


have you explored the possibility of using jQuery? It has a very reach selector model (similar in syntax to CSS) and even if your elements don't have IDs, you should be able to select them using parent --> child --> grandchild relationship. Once you have them selected, there's a very simple method call (I forget the exact name) that allows you to apply CSS style to the element(s).

It should be simple to use and as a bonus, you'll most likely be very cross-platform compatible.


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to css-selectors

Angular 2: How to style host element of the component? How to click a href link using Selenium How do I apply a style to all children of an element How to write a CSS hack for IE 11? :last-child not working as expected? Need to find element in selenium by css jQuery select child element by class with unknown path How do I select an element that has a certain class? What is the difference between cssSelector & Xpath and which is better with respect to performance for cross browser testing? What is the mouse down selector in CSS?