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;
}
Related
I'm trying to center text over an image; however, whenever I resize it, the text does not stay vertically centered.
.hero-image {
margin: 0 auto;
}
.hero-image img {
width: 100%;
}
.hero-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
<div class="hero-image">
<img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg">
<div class="hero-text">
Here is the hero text
</div>
</div>
Here's what's happening:
Working properly:
Vertical centering is off when resizing:
Your css for your hero text looks good – If I'm understanding your question correctly, I think the image should be modified to use 100 viewport width / height rather than 100%.
body {
margin: 0;
}
.hero-image img {
width: 100vw;
height: 100vh;
object-fit: cover;
}
.hero-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
You need to set the boundary of the container for positioned child. Add position: relative to .hero-image class. Otherwise it's using the next relative parent for it.
.hero-image {
margin: 0 auto;
position: relative;
}
.hero-image img {
width: 100%;
}
.hero-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
color: red;
}
<div class="hero-image">
<img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg">
<div class="hero-text">
Here is the hero text
</div>
</div>
You can try this translate-free solution:
.hero-text {
position: absolute;
top: 33vw;
text-align: center;
width: 100%;
}
If you plan to keep the image stretched over the whole width.
You can do it with "display: flex".
.hero-image {
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg);
background-size: cover;
background-position: center;
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.hero-image img {
width: 100%;
}
.hero-text {
}
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>
I have the following html:
<div class='container'>
<img src="http://lorempixel.com/400/400" />
<div class='button-left'><</div>
</div>
What I would like to achieve is for .button-left to always be in the center of the image, no matter the image size, but instead the div gets positioned according to my html element.
This is my css:
.container img {
position: relative;
}
.button-left {
position: absolute;
top: 50%;
background-color: red;
}
Shouldn't the .button-left div position according to the relative positioned image?
JsBin in case you want to try out a live demo:
https://jsbin.com/cuwaguyiza/edit?html,css,output
You have to set position: relative to the container and not to the image.
Also I suggest translating the button up, so it is perfectly centered.
.container {
position: relative;
}
.button-left {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Dont use position when no need it.
Flexbox.
.container {
display: flex;
align-items: center;
}
<div class='container'>
<img src="http://lorempixel.com/400/400">
<div class='button-left'>Lorem</div>
</div>
Line-height (if u text is single-line)
.container img {
vertical-align: middle;
}
.button-left {
display: inline-block;
line-height: 400px;
}
<div class='container'>
<img src="http://lorempixel.com/400/400"><!-- dont delete this comment
--><div class='button-left'>Lorem</div>
</div>
.container img {
position: relative;
}
.button-left {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
background-color: red;
}
OR
.container img {
position: relative;
display: block;
margin: 0 auto;
}
I have been searching on google and stack overflow for a while, but nothing seems to work.
I am trying to center a div within a div, which is all contained within a page wrapper. I can get it to center horizontally, but not vertically. Any suggestions?
I have tried display:inline-block with vertical-align:middle, but it had no effect.
CSS
html {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
background-color: black;
}
#page-wrapper {
padding: 0 12.5% 0 12.5%;
margin: 0;
}
#outer {
width: 100%;
height: 1000px;
background-color: darkgray;
position: relative;
}
#inner {
position: relative;
width: 50%;
height: 20%;
margin: 0 auto;
background: grey;
}
<body>
<div id="page-wrapper">
<div id="outer">
<div id="inner"></div>
</div>
</div>
</body>
You could:
A. Change the height to be a specific number and add margin-top: -height;
OR
B. Add this into the code for the #inner
position:absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Example of B: CodePen.io
OR
C. If you want to make it fancy use a flexbox (instructions here: Flexbox complete guide) and add:
display: flex;
justify-content: center;
align-items: center;
This may help https://css-tricks.com/centering-css-complete-guide/
change
#inner {
position:relative;
width: 50%;
height: 20%;
margin: 0 auto;
background:grey;
}
to
#inner {
width: 50%;
height: 20%;
margin: 0 auto;
background:grey;
/* magic here */
position:absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
You can do that by using flexbox, together with align-items: center; and justify-content: center;
Have a look at this: https://codepen.io/anon/pen/GOQwre
how to make absolute center horizontal and vertical a div with fluid width and height using css?
Thanks in advance for helping.
#div_parent{
background:#ccc;
position:relative;
}
.div_child{
background-color:#338BC7;
position: absolute;
left: 50%;
width: auto;
height: auto;
padding: 20px;
top:25%;
background: blue;
color: white;
text-align: center;
border:1px solid #ccc;
}
<div id="div_parent">
<div class="div_child">
<p>Centered In The Middle Of The Page.</p>
</div>
</div>
A couple of problems with your code:
You do not have a width and height specified on your html and body, without which any of descendent elements wouldn't have a reference to set their positions and/or dimensions in percent units.
You do not have dimensions (width/height) specified on your #div_parent, without which you cannot position its absolutely positioned child (which is relative to it) to the vertical center. Moreover, as you want to position your .div_child to the center of the page, why do you have a parent wrapped around it anyway.
Apart from fixing the above, a trick which is usually used to align elements both horizontally and vertically is to use transform: translate to shift it back by 50%.
Like this:
.div_child {
position: absolute; left: 50%; top: 50%;
transform: translate(-50%, -50%);
...
}
Fiddle: http://jsfiddle.net/abhitalks/Lnqvqnkn/
Snippet:
* { box-sizing: border-box; paddin:0; margin: 0; }
html, body { height: 100%; width: 100%; }
#div_parent{ height: 100%; width: 100%; background: #ccc; position: relative;}
.div_child {
background-color: #338BC7;
position: absolute; left: 50%; top: 50%;
transform: translate(-50%, -50%);
width: auto; height: auto;
padding: 20px; color: white; text-align: center; border: 1px solid #ccc;
}
<div id="div_parent">
<div class="div_child">
<p>Centered In The Middle Of The Page.</p>
</div>
</div>
When I need fluid width, I prefer using this method:
CSS
.background { display: table; width: 100%; height: 100%; position: absolute; left: 0; top: 0; }
.background > div { display: table-cell; vertical-align: middle; text-align: center; }
HTML
<div>
<div>
<p>Centered In The Middle Of The Page.</p>
</div>
</div>
Demo on jsfiddle.
Hope it works for you.