Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align JSF components to center

In my JSF 2 - Primefaces 3 web application, I am using <p:panelGrid> and <p:panel>. I have multiple components inside them which are left justified. I need to all to be center align. How can we do this I tried to use div but it does not work.

like image 554
Abhishek Dhote Avatar asked Oct 06 '12 11:10

Abhishek Dhote


4 Answers

Look at the generated HTML output and alter CSS accordingly.

If the HTML element which you'd like to center is a block element (<div>, <p>, <form>, <table>, etc, or forced by display: block;), then you first need to give it a known width and then you can center it relative to its parent block element using CSS margin: 0 auto; on the element itself.

If the HTML element which you'd like to center is an inline element (<span>, <label>, <a>, <img>, etc, or forced by display: inline;), then you can center it using CSS text-align: center; on its parent block element.

like image 120
BalusC Avatar answered Nov 05 '22 06:11

BalusC


If you want to set the content of a primefaces:panelGrid to center you can try this:

<h:panelGrid column="1">

   <h:panelGroup style="display:block; text-align:center">

           your contents...

   </h:panelGroup>

</h:panelGrid>
like image 45
Ebrahim Amini Sharifi Avatar answered Nov 05 '22 07:11

Ebrahim Amini Sharifi


We are using RichFaces, but the solution that we used in this case may apply to Primefaces as well. We used to style the inner elements with css.
Once you render the page in the browser, you can look up the source code and find out what HTML elements are rendered. Then create specific CSS classes and style the whole panel or the inner elements in panelGrid to that class.

Most of the time, this was the easiest solution and also sufficient.

like image 32
premma Avatar answered Nov 05 '22 06:11

premma


Try with css and p:panelGrid columnClasses attribute:

<p:panelGrid columnClasses="centered"> ... </p:panelGrid> then in your stylesheet create a class like:

.centered { text-align: center; }

If you have components in your p:panelGrid column other than just text, add the margin attribute to your css class:

.centered { text-align: center; margin-left: 50%; }

like image 28
Akos K Avatar answered Nov 05 '22 07:11

Akos K