I've been working with a simple Java EE project using JSF.
<h:form id="phoneForm"> <h:dataTable id="phoneTable"> </h:dataTable> </h:form>
I tried to set CSS via #phoneTable { ... }
, however it doesn't work. Upon inspection of the HTML source in client side, it appears that the JSF-generated HTML table gets a client ID in form of id="phoneForm:phoneTable"
. I can't apply CSS via #phoneForm:phoneTable { ... }
, because the colon indicates the start of a pseudoselector and causes an error.
How can I use it anyway in CSS selectors?
A CSS pseudo-element is a keyword added to a CSS selector that lets you style a specific part of the selected HTML element. In CSS3, they are usually denoted by two colons — for example, ::first-line — to differentiate them from pseudo-classes. In contrast, CSS2 syntax uses one colon (e.g., :first-line ).
The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
The :
is a special character in CSS identifiers, it represents the start of a pseudo class selector like :hover
, :first-child
, etc. You would need to escape it.
#phoneForm\:phoneTable { background: pink; }
This only doesn't work in IE6/7. If you'd like to support those users as well, use \3A
instead (with a trailing space behind!)
#phoneForm\3A phoneTable { background: pink; }
Above works in all browsers.
There are several other ways to solve this:
Just wrap it in a plain HTML element and style via it instead.
<h:form id="phoneForm"> <div id="phoneField"> <h:dataTable id="phoneTable">
with
#phoneField table { background: pink; }
Use class
instead of id
. E.g.
<h:dataTable id="phoneTable" styleClass="pink">
with
.pink { background: pink; }
or
table.pink { background: pink; }
Additional advantage is that this allows much more abstraction freedom. The CSS is reusable on multiple elements without the need to add selectors and/or copypaste properties when you want to reuse the same properties on another element(s).
Since JSF 2.x only: change the JSF default UINamingContainer
separator by the following context param in web.xml
. E.g.
<context-param> <param-name>javax.faces.SEPARATOR_CHAR</param-name> <param-value>-</param-value> </context-param>
So that the separator character becomes -
instead of :
.
#phoneForm-phoneTable { background: pink; }
Disadvantage is that you need to ensure that you don't use this character yourself anywhere in the ids and this is thus a very brittle approach. I do not recommend this approach. This is a bad practice.
Since JSF 1.2 only: disable prepending of the form id
.
<h:form prependId="false"> <h:dataTable id="phoneTable">
so that you can use
#phoneTable { background: pink; }
Disadvantage is that <f:ajax>
won't be able to find it and that it is considered poor practice: UIForm with prependId="false" breaks <f:ajax render>. I do not recommend this approach. This is a bad practice. Moreover, this attribute does not exist in all other UINamingContainer
components, so you still have to deal with them the right way (#1 and/or #2 here above).
In your specific case, I think turning it into a CSS class as described in #2 is the most appropriate solution. A "phone table" namely doesn't seem to represent a website-wide unique element. Real website-wide unique elements such as header, menu, content, footer, etc are usually not wrapped in JSF forms or other JSF naming containers, so their IDs wouldn't be prefixed anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With