How to align JSF components to center

怎甘沉沦 提交于 2020-01-10 08:28:38

问题


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.


回答1:


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.




回答2:


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>



回答3:


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.




回答4:


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%; }



来源:https://stackoverflow.com/questions/12759192/how-to-align-jsf-components-to-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!