How to vertically and horizontally center div [duplicate] - html

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 6 years ago.
I am facing a problem, how to center text (h1) inside div. I want the web page to be full screen and i achieved that with vh units.
.page { height: 100vh;
width: 100%;}
On that page i want div to be centered horizontally and vertically. I tried with
.div { top: 50%;
left: 50%;}
but that just didnt do the trick. I also want this to be "resposive" si that it is centered on any screen.
I am looking to build something like this: http://www.anthonygoodisondesign.com/

use css code as below;
div{height:100vh;
display:flex;
align-items:center;
justify-content:center;
}
<div> <h1>center me</h1></div>

Use this:
text-align: center;
position: relative;
top: 40vh;
transform: translateY(-50%);
https://jsfiddle.net/yak613/pLb8aysd/

Related

How to fix my problems when it comes to centering elements? [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How to center a "position: absolute" element
(31 answers)
How can I horizontally center an element?
(133 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 months ago.
For some reason, I can't seem to get my elements centered on both pages I'm working on. I have the elements to step into the page by 50% the viewport and then translate back 50%, among a few other things.
The notable html :
<body>
<h1><ku>ク</ku><ri>リ</ri><su>ス</su></h1>
</body>
The notable css:
body {
background-color: rgb(0, 0, 0);
margin: 0;
}
h1 {
color: white;
width: fit-content;
height: fit-content;
margin: 0;
position: relative;
top: 50vh;
left: 50vw;
transform: translate(-50%,-50%);
}
P.S:
How do I best position elements on a page freely so page size doesn't affect them, in relation to both centering and other.
I've tried many things I've looked up and sketched it out to see if the math worked out, and I just can't seem to figure it out, but it doesn't seem to work.

HTML CSS Positioning [duplicate]

This question already has answers here:
Setting CSS top percent not working as expected
(4 answers)
"top" CSS property has no effect when using a percentage
(4 answers)
Closed 3 years ago.
So I am just fiddling for some curiosity on jsFiddle.
When I apply style on below HTML:
<p>Hello World</p>
Styles:
p {
position:relative;
top: 50%;
left: 50%;
}
The Hello World comes to the center horizontally but not vertically.
But , when I change the position from relative to absolute,
the element aligns horizontally and vertically as well.
As I understand positioning, top: 50% should change the top of a block element.
Anything I am missing here (conceptually or otherwise)?
p {
position:relative;
top: 50%;
left: 50%;
}
<p>Hello World</p>
For position relative, it parent should have an static height. As I assign height to body it worked!.
p{
position:relative ;
top : 50%;
left : 50%;
}
body{
height:100vh;
}
<p>Hello World</p>
I have this kind of problem lot of time and to use position absolute some times are not a good idea so I have always used flex css.
body{
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
for p no css is required.

Center any sized content and still allow scrolling [duplicate]

This question already has an answer here:
How to center an element vertically in css in a scrollable container
(1 answer)
Closed 6 years ago.
Is there any CSS technique to center an image (or any other element) within container so that:
it appears centered horizontally and vertically initially (that is, its center is in the center of the containing div, no matter the size of the content);
it can be scrolled if it overflows its containing div.
If the content is always smaller than the container, I can simply use this well-known technique:
.content {
position: relative;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
}
This works perfectly for content that 'fits' inside its container, but stops working when the content starts to overflow.
Or I can remove the top, left and transform and let the content appear in the top-left corner of the container, which is potentially fine for very large content, but looks terrible for smaller content.
So is there a way in CSS to center content and allow scrolling for both overflowing and fit content?
The closest I've come is on this fiddle:
https://jsfiddle.net/4k5vp6m6/3/
I kind of suspect that the only way to do this is to inspect the content after loading to see if it fits or not.
Flexbox can do that
.container {
position: relative;
width: 500px;
height: 500px;
border: 3px solid green;
display: flex;
justify-content: center;
align-items: center;
overflow: auto;
}
JSFiddle Demo

Vertically center image in column [duplicate]

This question already has answers here:
Centering images in a div vertically
(3 answers)
Closed 9 years ago.
I have two columns in my HTML page.
<div id="content">
<div id="left"></div>
<div id="right"></div>
</div>
Each of them occupies half of the page
#left {
float: left;
width: 50%;
}
#right {
float: left;
width: 50%;
}
I want to center a picture in the right column. I know that I can make it horizontally centered by doing margin-left: auto; margin-right: auto;. How can I make it vertically centered?
The first issue I see is that there is no height specified for the height of the left and right divs; height should be set to 100% or any value to your liking. To vertically center the image, we can use absolute-positioning. We would set the dimensions for the image (which is good practice in any case) and then set the top:50% and left:50% attributes. This would push the image outside the box though, so we add negative margins that are half the width and height of the image. This will vertically and horizontally align the image in a div every time!
Here's the updated CSS:
#left, #right {
width: 50%;
height:100%;
float:left;
position:relative;
}
#right img {
position: absolute;
top: 50%;
left: 50%;
width: 80%;
height: 80%;
margin-top: -40%; /* Half the height */
margin-left: -40%; /* Half the width */
}
Take a look at this JSFiddle: http://jsfiddle.net/bYF7F/2/.
I know this question has been marked as answered, but you did mention that the height and width on the image was not ideal. So i would like to suggest another solution.
Add:
display: -webkit-flex;
display: flex;
to the right div, and:
margin: auto;
to the image. I think this is what you were after. See fiddle http://jsfiddle.net/Fqa7b/
If you use a TABLE instead of a DIV it will center automatically.

CSS full screen div with text in the middle [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 8 years ago.
I have a css class defined so I can make a div to use all the browser's viewport, the rule is the following:
.fullscreenDiv {
background-color: #e8e8e8;
width: 100%;
height: auto;
bottom: 0px;
top: 0px;
left: 0;
position: absolute;
}
Now I want the text inside the div to be in the exact center of the screen so, vertical align center and horizontal align middle, but I can't seem to find the proper way to do so.
It only needs to work on webkit based browsers.
I already tried to add a P element inside with display set to table-cell (a common way of centering text) without luck.
Any suggestions?
The accepted answer works, but if:
you don't know the content's dimensions
the content is dynamic
you want to be future proof
use this:
.centered {
position: fixed; /* or absolute */
top: 50%;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, -50%);
}
More information about centering content in this excellent CSS-Tricks article.
Also, if you don't need to support old browsers: a flex-box makes this a piece of cake:
.center{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
Another great guide about flexboxs from CSS Tricks; http://css-tricks.com/snippets/css/a-guide-to-flexbox/
The standard approach is to give the centered element fixed dimensions, and place it absolutely:
<div class='fullscreenDiv'>
<div class="center">Hello World</div>
</div>​
.center {
position: absolute;
width: 100px;
height: 50px;
top: 50%;
left: 50%;
margin-left: -50px; /* margin is -0.5 * dimension */
margin-top: -25px;
}​
DEMO
There is no pure CSS solution to this classical problem.
If you want to achieve this, you have two solutions:
Using a table (ugly, non semantic, but the only way to vertically align things that are not a single line of text)
Listening to window.resize and absolute positionning
EDIT: when I say that there is no solution, I take as an hypothesis that you don't know in advance the size of the block to center. If you know it, paislee's solution is very good
text-align: center will center it horizontally as for vertically put it in a span and give it a css of margin:auto 0; (you will probably also have to give the span a display: block property)