Center a div unequally using CSS
This solution uses display: grid
, it's a new feature so be sure to check the browser support and click here to learn more.
This is the line that controls the top and bottom spaces:
grid-template-rows: 40fr [content-start] auto [content-end] 60fr;
The snippet text content can be edited for you to check that the box keeps centered even if the height changes.
#dialogBoxPanel {
display: grid;
place-content: center;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
grid-template-rows: 40fr [content-start] auto [content-end] 60fr;
}
#dialogBox {
border: 1px solid;
width: 350px;
grid-area: content;
}
<div id="dialogBoxPanel">
<div id="dialogBox" contenteditable>Text</div>
</div>
Use pseudo element and column direction to simulate the white space. Simply adjust the flex-grow
of the pseudo element to control how much free space each one should take. Equal flex-grow will give equal space:
#dialogBoxPanel {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
/* the center */
background:linear-gradient(red,red) center/100% 1px no-repeat;
}
#dialogBox {
width: 350px;
border:1px solid;
}
#dialogBoxPanel:before {
content:"";
flex-grow:4;
}
#dialogBoxPanel:after {
content:"";
flex-grow:6;
}
<div id="dialogBoxPanel">
<div id="dialogBox">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc hendrerit diam eu nisl fringilla ornare. Pellentesque aliquam quam et tellus egestas sodales. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin bibendum,</div>
</div>
You can also use 2
and 3
. We simply need to keep the same ratio:
#dialogBoxPanel {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
/* the center */
background:linear-gradient(red,red) center/100% 1px no-repeat;
}
#dialogBox {
width: 350px;
border:1px solid;
}
#dialogBoxPanel:before {
content:"";
flex-grow:2;
}
#dialogBoxPanel:after {
content:"";
flex-grow:3;
}
<div id="dialogBoxPanel">
<div id="dialogBox">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc hendrerit diam eu nisl fringilla ornare. Pellentesque aliquam quam et tellus egestas sodales. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin bibendum,</div>
</div>
Another idea is to use top value equal to 40%
and rectify the position with translate (same logic with the 50%
when centring)
#dialogBoxPanel {
position: absolute;;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
/* the center */
background:linear-gradient(red,red) center/100% 1px no-repeat;
}
#dialogBox {
position:relative;
top:40%;
width: 350px;
transform:translateY(-40%);
margin:auto;
border:1px solid;
}
<div id="dialogBoxPanel">
<div id="dialogBox">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc hendrerit diam eu nisl fringilla ornare. Pellentesque aliquam quam et tellus egestas sodales. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin bibendum,</div>
</div>