Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8895893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:54:05+00:00 2026-06-14T23:54:05+00:00

I would like to create a content panel that is in the absolute middle

  • 0

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);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:54:06+00:00Added an answer on June 14, 2026 at 11:54 pm

    To perfectly centre something in the page you really want it to be a fixed size so you can do the calculations.

    .centeredPanel{
        width:500px;
        height:500px;
        position:absolute;
        left:50%; //sets left edge at the midway point of it's container
        top:50%; //sets top edge at the midway point of it's container
        margin-left:-250px; //moves the div back half it's width to centre it.
        margin-top:-250px; //moves the div up half it's height to centre it.
    }
    

    Demo: http://jsfiddle.net/calder12/qNNvb/

    To do it with percentages requires jQuery.

    .centeredPanel{
        width:25%;
        height:25%;
        min-width:100px;
        min-height:100px;
        position:absolute;
        left:50%;
        top:50%;
        background-color:red;
    }​
    
    
    $(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);
    });
    

    Demo: http://jsfiddle.net/calder12/qNNvb/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created Extjs.Panel and now I would like to dynamically load a content to
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a class that runs something (a runnable) at regular
I would like to create application with ribbon interface that looks and behaves like
I would like to display data from a database onto a content page using
Here's my situation. Let's say I would like to create a thread that continually
I would like to create a few panel dynamically in my window form application.
I would like to create an Entity Framework 4.0 context when a call is
I would like to create a new unix command to automatically list the contents
i would like create a array of structure which have a dynamic array :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.