This question already has answers here:
How can I center an absolutely positioned element in a div?
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 4 years ago.
I have a div (child) that is positioned absolute within a parent container (positioned relative). I want this child div to be centered vertically and horizontally within its container both on desktop and mobile, however the child div moves positioning depending on the size of the window. How do apply consistent centered positioning across devices?
HTML
<div class="container">
<div class="child">
</div>
<div class="child-2">
</div>
</div>
CSS
.container {
width: 300px;
height: 300px;
border: solid 1px blue;
position: relative;
}
.child {
position: absolute;
width: 50px;
height: 50px;
background-color: red;
position: absolute;
right: 50%;
left: 50%;
top: 50%;
border-radius: 100%;
}
.child-2 {
border-bottom: solid 1px blue;
width: 300px;
height: 150px;
}
JSFiddle for example - http://jsfiddle.net/hfndkywe/8/
it is always safe to use transform to make the element center, see the following code
.child {
position: absolute;
width: 50px;
height: 50px;
background-color: red;
left: 50%;
top: 50%;
border-radius: 100%;
transform: translate(-50%,-50%);
}
when you use left and right positioning, it is always pushing div from the sides not from the center, so in order to make exactly at center transform is the easy fix.
Related
This question already has answers here:
How to use safe center with flexbox?
(3 answers)
Flexbox align-items overflow text get cuts off at top [duplicate]
(2 answers)
Closed 4 years ago.
I have an element centered in the middle of a page. If the page shrinks to less than the height of the element, I need to still show the top of the element instead of being centered. I would like the element's container to be scrollable.
.card-display {
margin: auto;
top: 50%;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
height: 400px;
background-color: grey;
border-radius: 10px;
}
<div class="card-display" >
<div>
always need top line visible (i.e., if there is enough container height to fit the grey element, it should be vertically centered, otherwise container have scrolling)
</div>
</div>
Check this if it's works for you :
Wrap card-display to apply Flex centered way.
jsfiddle
.container {
display:flex;
align-items: center;
justify-content: center;
position:absolute;
width: 100%;
height: 100%;
}
.card-display {
align-items:center;
position:absolute;
width: 300px;
height: 400px;
border-radius: 10px;
background-color: gray
}
#media screen and (max-height:400px) {
body {
background-color: blue;
}
.container {
align-items:baseline;
}
}
html:
<div class="container">
<div class="card-display">
<div>
always need top line visible
</div>
</div>
</div>
This question already has answers here:
How to center a "position: absolute" element
(31 answers)
How to center absolute div horizontally using CSS?
(9 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 4 years ago.
How can I center align text in a div with the following structure:
.centerText {
position: absolute;
left: 100px;
top: 20px;
text-align: center;
font-size: 25px;
}
<div class="centerText">Hello World</div>
Doing so still aligns the text to the left side; 100px from the left.
To have the text centered while still using left: 100px you need to add right: 100px to automatically set the width of your div and have the same amount of spacing on both sides therefore centring the div too.
.centerText {
position: absolute;
left: 100px;
right: 100px;
top: 20px;
text-align: center;
font-size: 25px;
}
<div class="centerText">Hello World</div>
Try this.
.centerText {
position: absolute;
left: 50%;
top: 20px;
font-size: 25px;
transform: translate(-50%);
}
<div class="centerText">Hello World</div>
to center a text or an element inside a div it has to have a larger width,
take a look at : calc() https://www.w3schools.com/cssref/func_calc.asp
you can set the width of the div according to the page ( dynamically ) and remove the 100px from the left ( and another 100px from the right ) so the div will be centered and the text too ( with text-align:center )
.centerText {
position: absolute;
left: 100px;
top: 20px;
text-align: center;
font-size: 25px;
background: red;
width: calc(100% - 200px);
}
<div class="centerText">Hello World</div>
This question already has answers here:
Element will not stay centered, especially when re-sizing screen
(2 answers)
Closed 5 years ago.
I have a quiet simple html structure but I can't figure out what I have to do to place the 8 on top of the 0 without losing the height of the wrapping div.
If I use for example float or absolute position on both spans, the divs height is reduced to 0. If I use a combination of absolute position and float on the second div, I cant manage to properly center the span horizontally in the container.
I hope you can tell me what I'm doing wrong and how I can move the second span on top of the first one while letting the first span determine the height of the wrapping div.
#wrapper {
position: relative;
width: 100%;
text-align: center;
}
#first {
}
#first {
}
<div id="wrapper">
<span id="first">0</span>
<span id="second">8</span>
</div>
You can center absolutely-positioned elements by using a combination of left: 50% and a negative transform, which will allow you to center the 8 element above the 0:
#wrapper {
position: relative;
width: 100%;
height: auto;
text-align: center;
}
#first, #second {
color: white;
display: block;
}
#first {
background: blue;
height: 50px;
padding: 30px
}
#second {
background: red;
height: 10px;
padding: 10px;
position: absolute;
top: 0;
left:50%;
transform: translateX(-50%);
}
<div id="wrapper">
<span id="first">0</span>
<span id="second">8</span>
</div>
This question already has answers here:
Center and bottom-align flex items
(3 answers)
Closed 5 years ago.
I want to center a div in the absolute center of the page with justify-content: center and align-items: center but the div needs to be the 100% width and height of the page to be in the absolute center.
The problem arrives when you add elements on top or at the bottom of the div, this reduces the height of the div that we want to center and is no longer at the absolute center.
Here is the page https://jsfiddle.net/nybf8tjc/1
I could just add
.typewriter{margin-top: -41px;}
to fix this but I feel like there is must be a better way to do this.
You can make .typewriter an absolutely positioned element and add the usual settings for centering that, like
.typewriter {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
etc.
}
In action: https://jsfiddle.net/de3rf65j/
You can simple achieve it by using this.
div.father {
width: 100vw;
height: 100vw;
position: absolute;
display: flex;
align-items: center;
justify-content: space-around;
}
div.father div.child {
width: 400px;
height: 250px;
background-color: teal;
position: relative;
}
<div class="father">
<div class="child"></div>
</div>
Also you can use the transform property:
div.child {
width: 400px;
height: 250px;
margin:0;
padding: 0;
background-color: teal;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
That should center the container you want to.
Take a look https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This question already has answers here:
Positioning <div> element at center of screen
(14 answers)
Closed 7 years ago.
I found a jquery code that rotate images inside a div and I want to position this div to the center of the page. I have this CSS for the images
#rotating-item-wrapper {
position: relative;
}
and this for the div
.rotating-item {
display: none;
position: absolute;
top: 150px;
left: 200;
}
How I manage to center the whole process to the center?
using margin and position tend to work in these sorts of situations:
HTML:
<div id="center-div"></div>
CSS:
#center-div {
position: relative;
margin: 0 auto;
width: 50px;
height: 50px;
background: red;
}
here's a jsfiddle: http://jsfiddle.net/mwyLz9rt/
Add this code to the div you wish to center:
.centered {
position: absolute;
margin-left: 50vw;
margin-top: 50vh;
transform: translate(-50%, -50%);
}
Also, next time try to provide us with a jsfiddle example so we could understand the problem more vividly :)
Here's a demo of the above code JSFIDDLE