I would like to create a content panel that is in the absolute middle of the page. It can expand both its width and height to a certain percent of the page before scrollbars appear.
I am using Primefaces on a JSF page. I am sure there are many solutions out there to do this. I would like to use JSF’s composition.
Here is my solution:
<p:layout id="layout">
<p:layoutUnit position="center">
<p:panel header="Test" style="width:50%">
<ui:insert name="content"/>
</p:panel>
</p:layoutUnit>
</p:layout>
However, this doesn’t really do anything. Its not centered, what ever I insert into the “content” tag expands to the whole page.
Can anyone assist on how I might accomplish my goal?
EDIT
I have tried the solution posted by Rick Calder. Below is my code. It however does not work for me. I wrote the css into the style.css. I wrote the javascript into resizeContentPanel.js. I load JQUery and the resizeContentPanel.js in the head of my resource.xhtml. I believe these are all the correct steps. Is there anything I could be missing.
As for what i actually wrong, I do not see the content panel. When I debug the javascript, it has a height-margin of -71 and a width-margin of -160. I am assuming this is why I cant see it.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>
<ui:insert name="windowTitle"/>
</title>
<h:outputStylesheet library="css" name="style.css"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<h:outputScript library="scripts" name="resizeContentPanel.js" />
</h:head>
<h:body>
<div class="centeredPanel">
<p:layout id="layout" >
<p:layoutUnit position="center">
<ui:insert name="content"/>
</p:layoutUnit>
</p:layout>
</div>
<h:form>
<div id="stack">
<p:stack id="stackMenu" icon="/resources/images/stack/stack.png" model="#{pageBuilder.stackMenuModel}" />
</div>
</h:form>
<div id="dock">
<p:dock id="dockMenu" model="#{pageBuilder.dockMenuModel}"/>
</div>
</h:body>
style.css
root
{
display: block;
}
body
{
background-image: url('../../resources/images/background/background.jpg');
}
.centeredPanel{
width:25%;
height:25%;
min-width:100px;
min-height:100px;
position:absolute;
left:50%;
top:50%;
}
resizeContentPanel.js
$(document).ready(function(){
var heightMargin = -($('.centeredPanel').height()/2);
var widthMargin= -($('.centeredPanel').width()/2);
$('.centeredPanel').css('margin-left',widthMargin);
$('.centeredPanel').css('margin-top',heightMargin);
});
$(window).resize(function(){
var heightMargin = -($('.centeredPanel').height()/2);
var widthMargin= -($('.centeredPanel').width()/2);
$('.centeredPanel').css('margin-left',widthMargin);
$('.centeredPanel').css('margin-top',heightMargin);
});
To perfectly centre something in the page you really want it to be a fixed size so you can do the calculations.
Demo: http://jsfiddle.net/calder12/qNNvb/
To do it with percentages requires jQuery.
Demo: http://jsfiddle.net/calder12/qNNvb/3/