Text not fully centered in Div? [duplicate] - html

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 3 years ago.
I am pretty new to CSS and looked at some guide about how to center a text inside a div, but now it's not fully, but just almost centered...
Image: https://cdn.discordapp.com/attachments/554447633197039627/595281754135199754/unknown.png
CSS:
.info { /* Background (div)*/
background-color: red;
height: 10em;
width: 50em;
position: absolute;
left: 50%;
top: 50%;
margin:-5em 0 0 -25em;
text-align: center;
}
.info-text { /* Text (span) */
font-size: 6em;
display: inline-block;
vertical-align: middle;
}

u can use flex display to the parent with align-items:center and justify-content:space-between
there is other methods but i prefer this one!
*{
margin:0;
padding:0;
}
.center-text{
width:300px;
height:150px;
background:red;
display:flex;
align-items:center;
justify-content:space-around;
}
p{
font-size:30px;
}
<div class="center-text">
<p>Hello World</p>
</div>

you can center vertical and horizontal using the below css
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
below is the working snippet
.parent {
background: red;
height: 100vh
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 23px;
color: #fff
}
<div class="parent">
<div class="center">Vertical and horizontal center</div>
</div>

.info { /* Background (div)*/
background-color: red;
height: 10em;
width: 50em;
position: relative;
}
.info-text { /* Text (span) */
font-size: 6em;
position: absolute;
display: block;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="info">
<div class="info-text">Text</div>
</div>

You can accomplish that by setting the line-height in .info-text with same height as is your .info
In your case, I've changed the unit to px and as you can see, .info has height: 100px; - from that I've added line-height: 100px; in .info-text
It will be in perfect center as long as you keep the line-height same as height.
.info { /* Background (div)*/
background-color: red;
height: 100px;
width: 500px;
position: absolute;
left: 50%;
top: 50%;
margin: -25px 0 0 -250px;
text-align: center;
}
.info-text { /* Text (span) */
position: relative;
font-size: 60px;
line-height: 100px;
display: inline-block;
}
<div class="info">
<span class="info-text">CRUGG</span>
</div>

You don't need absolute positioning at all, just use flexbox to center horizontally and to align the text at the bottom of your container, use text-align:center and relative position for the text. With relative position you will be able to easily move it outside of the container with the bottom property
Example:
.info { /* Background (div)*/
position:absolute;
top:0;
bottom:0;
margin:auto;
background-color: red;
height: 10em;
width: 100%;
display:flex;
justify-content:center;
align-items:flex-end;
z-index:99;
}
.info-text { /* Text (span) */
font-size: 6em;
position:relative;
bottom:-0.5em;
}
<div class="info">
<div class="info-text">Text</div>
</div>
You can set align-items:center if you want your text to be centered vertically too.
Example
.info { /* Background (div)*/
position:absolute;
top:0;
bottom:0;
margin:auto;
background-color: red;
height: 10em;
width: 100%;
display:flex;
justify-content:center;
align-items:center;
z-index:99;
}
.info-text { /* Text (span) */
font-size: 6em;
position:relative;
}
<div class="info">
<div class="info-text">Text</div>
</div>

Related

How to center only one of the children inside a parent?

I have two elements inside a div. How can I vertically center only p element?
.card {
height: 300px;
background-color: aquamarine;
}
<div class="card">
<h1>Hello</h1>
<p>Always in center vertically</p>
</div>
Use flex: display; and justify-content: center;. This will align the p tag to center.
Align h1 with position: absolute; to required position.
You should add position: relative; to .card for the h1 with style position: absolute; to stay inside .card
.card {
height: 300px;
background-color: aquamarine;
display: flex;
flex-direction: column;
justify-content: center;
position: relative;
padding: 50px;
}
h1 {
position: absolute;
width: 100%;
top: 0;
background: beige;
left: 0;
}
<h2>Sample App</h2>
<div class="card">
<h1>Hello</h1>
<p>Always in center vertically</p>
</div>
Since you want to vertically center <p> you can do that by using absolute position. First set your .card to position:relative; so that <p> remains inside it, now give .card p position:absolute; and set top:50%; but this will only center it to its parent element, to make it perfectly center we need to set transform:translateY(-50%);
.card {
height: 300px;
background-color: aquamarine;
position:relative;
}
.card p{
position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0;
}
If you want to make it horizontally center as well then all you need to do is add left:50%; & transform: translate(-50%, -50%); to .card p where you set both translate X and Y axis to -50%.
.card p{
position: absolute;
top: 50%;
transform: translate(-50%,-50%);
margin: 0;
}

How do I right-align a <span> within a <p> and keep the text inside the <p> centered? (HTML) [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
It's hard to ask this question without a visual reference, so I included a picture below (as well as a code snippet). I'm trying to achieve two things:
right-align the blue <span> circle inside the yellow <p> box
keep the text centered in the <p> box, independent of the blue circle
This is my code:
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px
}
<p class='box'>This is centered</p>
<p class='box'>This is not<span class='circle'></span></p>
I'm not super familiar with HTML, but I tried doing align-self: right for the circle, but nothing happened. Not sure what to do.
Here is a picture of what I'm trying to achieve:
just float:right and add margin to center
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block;
float: right;
margin: 5px;
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px;
box-sizing: border-box;
}
.box2 {
padding-left: 30px; /* circle width (20px) + circle margin-left (5px) + margin-right (5px) = 30px */
}
<p class='box'>This is centered</p>
<p class='box box2'>This is not<span class='circle'></span></p>
You can use absolute position.
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block;
/* added */
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 10px;
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px;
position: relative; /* should be relative */
}
<p class='box'>This is centered</p>
<p class='box'>This is not<span class='circle'></span></p>
To center the text vertically and horizontally:
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block;
/* added */
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 10px;
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px;
position: relative; /* should be relative */
/* add these to center text vertically and horizontally */
display:flex;
align-items:center;
justify-content: center;
}
<p class='box'>This is centered</p>
<p class='box'>This is not<span class='circle'></span></p>

How to move div container in the center of the screen, with children divs centralized inside of it

I have the following HTML/CSS code:
<style>.dummy {
color: greenyellow;
}
.middle-container {
position: absolute;
top: 50%;
left: 50%;
}
.middle-container-box {
width: 50px;
height: 50px;
background-color: red;
}
.middle-container-text {
color: red;
font-weight: bold;
font-size: 15px;
}
</style>
<html>
<body>
<div class="dummy">
Lorem Ipsum is simply dummy text...
</div>
<div class="middle-container">
<div class="middle-container-box"></div>
<div class="middle-container-text">
this text needs to be in the center
</div>
</div>
</body>
</html>
In this sandbox: https://codesandbox.io/s/2py075pqnr
I need to have middle-container at the center of the screen and the div and the text elements (which are inside of this container) to be centralized inside of the container.
So it should be moved to the left, something as: https://prnt.sc/n349h1 (we need to move it only to the left). But moving it with fixes values (in pixels) is not an option, since I need this working on all screens resolutions.
You need to use translate to move to box back to the centre and flex to centre it's children:
.dummy {
color: greenyellow;
}
.middle-container {
position: absolute;
top: 50%;
left: 50%;
transform:translate(-50%, -50%); /* this moves this box back to the middle (back up and left 50% of itself) */
display:flex; /* the following four align it's children to the center horizontally and vertically */
flex-direction:column;
align-items:center;
justify-content:center;
}
.middle-container-box {
width: 50px;
height: 50px;
background-color: red;
}
.middle-container-text {
color: red;
font-weight: bold;
font-size: 15px;
}
<div class="dummy">
Lorem Ipsum is simply dummy text...
</div>
<div class="middle-container">
<div class="middle-container-box"></div>
<div class="middle-container-text">
this text needs to be in the center
</div>
</div>
You can do it with transfom
CSS
.dummy {
color: greenyellow;
}
.middle-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
.middle-container-box {
width: 50px;
height: 50px;
background-color: red;
display: inline-block;
}
.middle-container-text {
color: red;
font-weight: bold;
font-size: 15px;
}
DEMO HERE
Change .middle-container, set top, left, right, bottom to 0 then set margin to auto. Your text is already in the center because it's giving the div it's width, what you need to do is put .middle-container-box in the middle setting display to block and margin to 0 auto. See below:
.middle-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
.middle-container-box {
width: 50px;
height: 50px;
background-color: red;
display: block;
margin: 0 auto;
}
.middle-container-text {
color: red;
font-weight: bold;
font-size: 15px;
text-align: center;
}

HTML/CSS How to center DIV inside absolute DIV [duplicate]

This question already has answers here:
How can I center an absolutely positioned element in a div?
(37 answers)
Closed 6 years ago.
I have a problem I can't solve. i m trying to center this black box inside red box which has absolute position. I tried making the black box to relative position but i feel like i am missing something.
Ultimately, i m trying to make the top header.
here is an image header-image.jpg
Help?
body.esc-layout {
min-width: 960px;
margin: 0 auto;
}
.promo-bar {
display: block;
}
.promo-bar .customer-care-wrapper {
float: left;
max-width: 50%;
}
.promo-bar .customer-care {
font-size: 11px;
color: #000;
margin-left: 15px;
display: block;
}
.promo-bar {
width: 100%;
min-height: 32px;
position: relative;
height: auto;
overflow: visible;
z-index: 5;
background-color: #EEE;
overflow: hidden;
}
.promo-bar .service-message-wrapper {
padding-top: 2px;
max-width: 50%;
margin: 0 auto;
position: relative;
}
.service-message-wrapper .service-banner{
position: absolute;
left: 0px;
right: 0px;
text-align: center;
background: red;
}
.caption-wrapper{
position: relative;
background: black;
}
.service-message-wrapper .captions{
font-family: inherit;
font-style: italic;
font-size: 14px;
color: white;
}
<body class="esc-layout">
<div class="promo-bar">
<div class="customer-care-wrapper promo-block">
<div class="customer-care" style="padding-top:10px; padding-bottoms:12px;">
" Contact us 24/7: "
</div>
</div>
<div class="service-message-wrapper promo-block" style="height: 28px;">
<div class="service-banner service-message-1" style="margin-top: 0px;">
<div class="caption-wrapper">
<p class="captions">
<span> Same-day delivery to New York </span>
</p>
</div>
</div>
</div>
</div>
</body>
You can use position: absolute with a combination of top and transform.
The trick is that in top: 50%, the 50% refers to the parent height. In transform, 50% refers to the element's own height.
.outer {
height: 50px;
width: 50%;
position: absolute;
top: 0;
right: 0;
background: red;
}
.inner {
position: absolute;
left: 0;
/* make the top edge of .inner appear in the vertical center of .outer */
top: 50%;
/* move .inner up by half of its height so that its middle is in the middle of .outer */
transform: translateY(-50%);
height: 20px;
width: 100%;
background: black;
}
<div class="outer">
<div class="inner"></div>
</div>
More info: http://howtocenterincss.com/
Centering inside an absolute element, the inner element needs to be absolute give a width and height.
.red-box{
background-color:red;
width:400px;
height:400px;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}
.black-box{
background-color:black;
width:200px;
height:200px;
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
}
<div class="red-box">
<div class="black-box"> </div>
</div>
working sample (click run button)
For center div it is very easy to use flex box.
div.outer {
align-items: center;
background: red none repeat scroll 0 0;
display: flex;
height: 50px;
position: absolute;
right: 0;
top: 0;
width: 50%;
}
div.inner {
background: black none repeat scroll 0 0;
height: 20px;
left: 0;
width: 100%;
}
<html><head>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>
Do not forget using webkit for safari and chrome and in your case I think it's better to set margin:0 for <p> for better control
p.captions{margin:0;}

Vertical Align in Span Doesn't Work? [duplicate]

I want to keep the height of #abc div at 50px and text to align vertically in the middle of the div.
body{
padding: 0;
margin: 0;
margin: 0px auto;
}
#main{
position: relative;
background-color:blue;
width:500px;
height:500px;
}
#abc{
font:Verdana, Geneva, sans-serif;
font-size:18px;
text-align:left;
background-color:#0F0;
height:50px;
vertical-align:middle;
}
<div id="main">
<div id="abc">
asdfasdfafasdfasdf
</div>
</div>
You can use line-height: 50px;, you won't need vertical-align: middle; there.
Demo
The above will fail if you've multiple lines, so in that case you can wrap your text using span and than use display: table-cell; and display: table; along with vertical-align: middle;, also don't forget to use width: 100%; for #abc
Demo
#abc{
font:Verdana, Geneva, sans-serif;
font-size:18px;
text-align:left;
background-color:#0F0;
height:50px;
display: table;
width: 100%;
}
#abc span {
vertical-align:middle;
display: table-cell;
}
Another solution I can think of here is to use transform property with translateY() where Y obviously stands for Y Axis. It's pretty straight forward... All you need to do is set the elements position to absolute and later position 50% from the top and translate from it's axis with negative -50%
div {
height: 100px;
width: 100px;
background-color: tomato;
position: relative;
}
p {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Demo
Note that this won't be supported on older browsers, for example IE8, but you can make IE9 and other older browser versions of Chrome and Firefox by using -ms, -moz and -webkit prefixes respectively.
For more information on transform, you can refer here.
Old question but nowadays CSS3 makes vertical alignment really simple!
Just add to #abc the following css:
display:flex;
align-items:center;
Simple Demo
Original question demo updated
Simple Example:
.vertical-align-content {
background-color:#f18c16;
height:150px;
display:flex;
align-items:center;
/* Uncomment next line to get horizontal align also */
/* justify-content:center; */
}
<div class="vertical-align-content">
Hodor!
</div>
It's simple: give the parent div this:
display: table;
and give the child div(s) this:
display: table-cell;
vertical-align: middle;
That's it!
.parent{
display: table;
}
.child{
display: table-cell;
vertical-align: middle;
padding-left: 20px;
}
<div class="parent">
<div class="child">
Test
</div>
<div class="child">
Test Test Test <br/> Test Test Test
</div>
<div class="child">
Test Test Test <br/> Test Test Test <br/> Test Test Test
</div>
<div>
I found this solution by Sebastian Ekström. It's quick, dirty, and works really well. Even if you don't know the parent's height:
.element {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
Read the full article here.
div {
height:200px;
text-align: center;
padding: 2px;
border: 1px solid #000;
background-color: green;
}
.text-align-center {
display: flex;
align-items: center;
justify-content: center;
}
<div class="text-align-center"> Align center</div>
You can use Line height a big as height of the div.
But for me best solution is this --> position:relative; top:50%; transform:translate(0,50%);
How about adding line-height ?
#abc{
font:Verdana, Geneva, sans-serif;
font-size:18px;
text-align:left;
background-color:#0F0;
height:50px;
vertical-align:middle;
line-height: 45px;
}
Fiddle, line-height
Or padding on #abc. This is the result with padding
Update
Add in your css :
#abc img{
vertical-align: middle;
}
The result. Hope this what you looking for.
.container {
height: 200px;
position: relative;
border: 3px solid green;
}
.center {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
<h2>Centering Div inside Div, horizontally and vertically without table</h2>
<p>1. Positioning and the transform property to vertically and horizontally center</p>
<p>2. CSS Layout - Horizontal & Vertical Align</p>
<div class="container">
<div class="center">
<p>I am vertically and horizontally centered.</p>
</div>
</div>
Try this:
.main_div{
display: table;
width: 100%;
}
.cells {
display: table-cell;
vertical-align: middle;
}
Another method for centering a div:
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {position: relative;}
#child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 50px;
height: 100px;
margin: auto;
}
This Code Is for Vertical Middle and Horizontal Center Align without specify fixed height:
.parent-class-name {
position: relative;
}
.className {
position: absolute;
top: 50%;
left: 0;
right: 0;
margin: 0 auto;
transform: translateY(-50%);
-moz-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
}
Use the translateY CSS property to vertically center your text in it's container
<style>
.centertext{
position: relative;
top: 50%;
transform: translateY(40%);
}
</style>
And then apply it to your containing DIV
<div class="centertext">
<font style="color:white; font-size:20px;"> Your Text Here </font>
</div>
Adjust the translateY percentage to suit your needs. Hope this helps