This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Center image using text-align center?
(28 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
I want to have a button image centred in a div of fixed size both horizontally and vertically.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#fixed-size {
width: 800px;
height: 600px;
border: 1px solid red;
display: block;
}
button {
padding: 0;
margin: auto;
display: block;
width: 256px;
height: 256px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div id="fixed-size">
<button>
<img src="aqua.png">
</button>
</div>
</body>
</html>
What should I write instead of button { /* ... */ } for the button to be centered in a div and not in a whole page?
ive changed your code to where the button is relative and the parent makes sure the button inside is centered, using flexbox styling
justify-content: center
and
align-items: center
make sure that all the children of the display flex parent are aligned in the middle both horizontally and vertically.
if you want to learn more about it, check this website: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#fixed-size {
width: 800px;
height: 600px;
border: 1px solid red;
display: flex;/* changed */
justify-content: center;/* added */
align-items: center;/* added */
}
button {
padding: 0;
margin: auto;
display: block;
width: 256px;
height: 256px;
position: relative;/* changed */
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div id="fixed-size">
<button>
<img src="aqua.png">
</button>
</div>
</body>
</html>
Related
This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
How to center a "position: absolute" element
(31 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Closed 24 days ago.
I want to my text to be centered both horizontally and vertically within a div.
I'm using the inset property to fill up the entire div with my p tag.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div class="wrapper">
<div class="zone">
<p>Hello World</p>
</div>
</div>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: bordered-box;
}
.wrapper {
background: red;
}
.zone {
min-height:10em;
text-align: center;
position: relative;
}
.zone p {
position: absolute;
inset:0;
}
However I don't get it to work. Can anyone give me a hint on how to achive this with the inset property?
If you require the inset property you can do the following, but there are many better ways to center text in a div.
* {
margin: 0;
padding: 0;
box-sizing: bordered-box;
}
.wrapper {
background: red;
}
.zone {
min-height: 10em;
text-align: center;
position: relative;
}
.zone p {
font-size: 20px;
position: absolute;
inset: calc(50% - 10px) 0 0 0; /* The minus value needs to be half the font-size */
}
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div class="wrapper">
<div class="zone">
<p>Hello World</p>
</div>
</div>
</body>
</html>
This question already has answers here:
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed last month.
How can I center an image vertically an horizontally in my html page ? The image should kept in center irrespective of the size of the screen changes .I tried something like below ,but it is not centering the image vertically.
<body>
<img src="img/blogg.PNG" height="20%" width="20%" style="display: flex;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
justify-content: center;
position: relative;
padding-top: 15%;
margin: auto;
align-items: center;
" />
</body>
You can try to use this styling
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
You can use the flex display properties with a few small rules, in this way you can avoid to set the absolute position which can cause the image to overlap with other elements if you don't check all the media queries well
body{
min-height: 100vh
}
.img-centered{
height:100%;
display: flex;
justify-content: center;
align-items:center;
}
<body>
<div class="img-centered">
<img src="https://picsum.photos/id/237/200/300" height="300" width="200" />
</div>
</body>
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 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.
This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 8 years ago.
I want to Have a Text Box at the center. I tried with the code below. But it is not putting it at the center. Image of output can also be found below. Please help me by pointing errors in my code.
<html>
<head>
<style>
#form
{
margin-top:40%;
margin-left:40%;
}
</style>
</head>
<body>
<div id="form">
<form method = "post" action = "test.htm" >
UserName: <input type="text" name = "username" />
</form>
</div>
</body>
</html>
My output from the above code is:
you can use the below css code to make your text box in horizontally and vertically center.
#form {
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width: 100px;
height: 100px;
}
Here you go.
WORKING DEMO
The CSS Change:
div#form form {
background: none repeat scroll 0 0 #808080;
display: block;
height: 100px;
line-height: 100px;
margin: 0 auto;
text-align: center;
vertical-align: middle;
width: 100%;
}
Hope this helps.
To get it vertically AND horizontally centered do this for the parent element:
position:relative;
Then the class/ID you want centered:
position:absolute;
top:0;left:0;bottom:0;right:0;
margin:auto;
/*and make sure this element has a width + height: example:!*/
width:100px;
height:100px;
Like this: Codepen