This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 7 years ago.
How can I center horizontally and vertically a text? I don't want to use position absolute because I try with it and my other div getting worse. Is there another way to do that ?
div {
height: 400px;
width: 800px;
background: red;
}
<div>
<h1>This is title</h1>
</div>
you can use display flex it enables a flex context for all its direct children, and with flex direction establishes the main-axis, thus defining the direction flex items are placed in the flex container
div{
height: 400px;
width: 800px;
background: red;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
Just do this, which works on every browser:
div{
height: 400px;
width: 800px;
background: red;
line-height: 400px;
text-align: center;
}
The line-height property specifies the line height (which centres it vertically), and the text-align:center place it directly in the centre horizontally.
<div>
<style>
div {
position: fixed;
top: 50%;
left: 50%;
}</style>
<h1>This is title</h1>
</div>
With position: fixed you set the position to a fixed value, and with top and left both set to 50% you'll get it in the middle of the screen.
Good luck coding!
Here is some more information: https://css-tricks.com/quick-css-trick-how-to-center-an-object-exactly-in-the-center/
.container {
display: table;
}
.centered {
display: table-cell;
height: 400px;
width: 800px;
background: red;
text-align: center;
vertical-align: middle;
}
<div class="container">
<div class="centered">
<h1>This is title</h1>
</div>
</div>
Related
This question already has answers here:
Using margin:auto to vertically-align a div
(15 answers)
Why don't margin-top: auto and margin-bottom:auto work the same as their left and right counterparts?
(2 answers)
Closed last year.
I think I've got some confusion on how margin auto works. In this case, I'm creating a parent div and a children div inside of it, and both div boxes are given width & height. Then, I tried to use margin auto to make the inner div horizontally & vertically aligned.
<div id="parent">
<div id="child"></div>
</div>
#parent {
width: 200px;
height: 200px;
background: red;
}
#child {
width: 50%;
height: 50%;
background: blue;
margin: auto;
}
Result:
Somehow the result was out of my expectation. Anyone knows why?
Just use display: flex
#parent {
width: 200px;
height: 200px;
background: red;
display: flex;
justify-content: center;
align-items: center;
}
#child {
width: 50%;
height: 50%;
background: blue;
}
<div id="parent">
<div id="child"></div>
</div>
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 1 year ago.
// css
.table-art {
background-image:url("../img/table-decorated1.jpg");
background-size: cover;
width: auto;
height: 500px;
}
.box-test {
border: 5px solid red;
width: 150px;
height: 50px;
margin: auto;
}
// html
<div class="table-art">
<div class="box-test">
</div>
</div>
The red box is placed like this when I use the css. However, if I'm not wrong, due to the 'margin: auto' it should superposition the box, from left right down and up within the div with the picture, however it only seems to be doing it for left and right, horizontally it works fine, however I want to have this box vertically centered as well.
.box-test {
border: 5px solid red;
width: 150px;
height: 50px;
margin: auto;
position: absolute;
top: 50%;
left: 50%;
}
I tried to have it centered using the ol' top and left 50%. Which in theory should put it straight in the center. However, as you can see, it's slightly off to the down and right. Increasing the size of the box div will always go right and down, but it doesn't seem to take into account the left and upper part of itself.
I tried also using right and down: 50% but it's always in that particular location. It's like the website thinks this is really the center, is it due to the image?
I've tried looking at different solutions such as using padding but that didn't work either, what should I do to make sure it's box size always remains in the center within this filled out div?... Or, is there a better way to doing this and I'm just hurting myself for nothing? What's the deal here?
*The image's size in question is 765x510
Also using firefox navigator, but chrome also does the same thing.
Thank you for reading, any help will be very much appreciated!
You could use flex or grid - like
.table-art {
background-image:url("../img/table-decorated1.jpg");
background-size: cover;
width: auto;
height: 500px;
/* center items */
display: grid;
place-items: center;
}
.box-test {
border: 5px solid red;
width: 150px;
height: 50px;
margin: auto;
}
Add display flex to the parent div and then margin:auto to the inner div
CSS
METHOD 1 :
By using the position relative for parent div and absolute for child element you can easily cneter the child element using top and left props of css ( of child with absolute position ) as 50% and finally center the child element with transform prop
/*Using positioning and transform method*/
.table-art {
background-image: url("../img/table-decorated1.jpg");
background-size: cover;
width: auto;
height: 500px;
background-color: blue;
position: relative;
}
.box-test {
border: 5px solid red;
width: 150px;
height: 50px;
position: absolute;
left: 50%;
top:50%;
transform: translate(-50% , -50%);
}
METHOD 2 :
Using the grid or flex you can easily center the child div by setting the parent div's display to flex or grid and then using the justify-content ( for flex and place-content for grid ) and aligning the div vertically center with align-items to center ( grid doesn't needs this prop to center the content ! )
/*Using flex or grid to center the content*/
.table-art {
background-image: url("../img/table-decorated1.jpg");
background-size: cover;
width: auto;
height: 500px;
background-color: blue;
display: flex;
align-items: center;
justify-content: center;
}
.box-test {
border: 5px solid red;
width: 150px;
height: 50px;
}
HTML
<div class="table-art">
<div class="box-test">
</div>
</div>
Just add below to your .table-art class
display:flex;
align-items: center
.table-art {
background-image: url("https://picsum.photos/300/300");
background-size: cover;
width: auto;
height: 500px;
display:flex;
align-items: center
}
.box-test {
border: 5px solid red;
width: 150px;
height: 50px;
margin: auto;
}
<div class="table-art">
<div class="box-test">
</div>
</div>
This question already has answers here:
Make wrapper take maximum width of child image? [duplicate]
(2 answers)
How can I horizontally center an element?
(133 answers)
Closed 4 years ago.
I'm trying to center an image and put some text under it. I can get the text to the center but when I try to get it to the left or the right, it flows past the image. I'm not sure how to keep that text within the image border.
HTML:
<p align="center"><img src="/someimage.jpg" height="300" width="600">
<b>text under it</b></p>
I understand this centers the image but also the text but if i try to wrap the text in a span and tell it to float to the left or right, that's when it goes way past the image.
Is this the kind of thing you are thinking of?
I've just added display: flex to the wrapper and then flex-direction: column makes sure that they are stacked in a column (not a row).
You can then change text-align property on the b to whichever you'd like (left, center or right).
p {
display: flex;
flex-direction: column;
width: 600px;
margin: auto;
}
img {
width: 600px;
height: 150px;
}
b {
text-align: left;
}
<p align="center"><img src="http://via.placeholder.com/600x150">
<b>text under it</b></p>
is this what you want? Look at the JSFiddle below.
img {display: block;}
https://jsfiddle.net/ybq3d692/
Placing both the image and the text within a parent div of same width as that of image and giving any of text-align properties i.e., text-align: center or left or right will work..
for reference...
https://jsfiddle.net/e653vfdk/2/
/* here is the html */
<div class="container">
<div class="holder">
<img src="https://www.bmw.com.mt/content/dam/bmw/common/all-models/4-series/gran-coupe/2017/images-and-videos/images/BMW-4-series-gran-coupe-images-and-videos-1920x1200-12.jpg.asset.1519121502869.jpg" class="img1">
<div class="text">Lorem Ipsum is simply dummy text of the printing </div>
</div>
</div>
/* And here is the css */
.container
{
border: 1px solid;
padding: 20px;
}
.img1
{
width: 500px;
height: 500px;
}
.holder
{
width: 500px;
margin: 0 auto;
}
.text/*Uncomment any of the below 3 lines to see the text left-aligned, right-aligned or center-aligned whatever u want*/
{
text-align: left;
/* text-align: right; */
/* text-align: center; */
}
This is how I would approach this question:
#container {
position: relative;
display: block;
width: 600px;
margin: 0 auto;
}
#image {
position: relative;
display: block;
width: 600px;
height: 300px;
background-image: url("https://www.w3schools.com/w3css/img_lights.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
#text {
position: relative;
display: block;
font-size: 2em;
color: red;
}
<div id="container">
<div id="image"></div>
<div id="text">
This is some text this is some text
This is some text this is some text
This is some text this is some text
</div>
https://jsfiddle.net/evkg2yz6/1/
This question already has answers here:
How to vertically center a container in Bootstrap?
(10 answers)
Closed 5 years ago.
By default the row aligns to the top.
I tried to put margin-top: auto; and margin-bottom: auto; but doesn't work.
Also vertical-align: middle; also does not work.
Is there an easy fix to this?
Thanks
.container {
background-color:black;
height: 100px;
}
.row {
background-color:#fff;
height: 50px;
}
<div class="container">
<div class="row">
</div>
</div>
You can use flexbox for this:
.row {
display: flex;
align-items: center;
justify-content: center;
}
Basically, this question is a duplicate. There are various centering methods for Bootstrap explained in that question. This will work specifically for your scenario with the row inside the container. Here are 2 different methods..
// method 1 flexbox
.container-flex {
display: flex;
align-items: center;
}
// method 2 translatey
.v-center {
position: relative;
transform: translatey(-50%);
top: 50%;
}
http://www.codeply.com/go/b6wTQTzoe1
Set width and height for row and
.row {
margin: 0 auto;
}
<div class="inner">
<p>
Text
</p>
</div>
.inner {
height: 200px;
width: 300px;
background: orange;
vertical-align: middle;
display: table-cell;
}
The "Text" should be vertically aligned.
Display table-cell works with vertical-align.
I think it should be possible to do it with inline-elements too.
This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 7 years ago.
As I said in the title, I am trying to center the text on a div - vertically and horizontally, but without success. Please check my code and help me to see where the problem is.
HTML Code:
<div id="wrapper">
<div id="top">
<div class="container-fluid text-center">
<div id="top-rectangle" class="mid">
<p>
Hello, text and text.
</p>
<p>
More text
</p>
</div>
</div>
</div>
CSS Code:
body {
height: 100%;
margin: 0;
}
#wrapper {
display: table;
width: 100%;
}
#top {
height: 0;
display: table-row;
vertical-align: middle;
}
#top-rectangle {
height: 450px;
background-image: url("http://www.planwallpaper.com/static/images/recycled_texture_background_by_sandeep_m-d6aeau9_PZ9chud.jpg");
background-size: cover;
color: white;
}
.midSection {
text-align: center;
vertical-align: middle;
display: table-cell;
}
JSFIDDLE:
https://jsfiddle.net/krgtdomh/
Note: Please know that I searched an answer and I saw some, but I still don't understand whats wrong with my code that the text doesn't center.
Check this: https://jsfiddle.net/krgtdomh/1/
I've added:
.outer {
display: table;
position: absolute;
height: 450px;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: /*whatever width you want*/;
}
And I've added display:block; to #top-rectangle and changed the height to 450px.
I used this answer to get there
It looks like your CSS code is fine, but there is a mistake:
In CSS you use: midSection
In html: class="mid"
As you can see this way the names are different and css is not used, here is a fix:
https://jsfiddle.net/krgtdomh/2/
I've also added a width to your div:
#top-rectangle {
height: 450px;
width: 450px;
background-image: url("http://www.planwallpaper.com/static/images/recycled_texture_background_by_sandeep_m-d6aeau9_PZ9chud.jpg");
background-size: cover;
color: white;
}
For center the div .midSection vertically and horizontally
CSS
.midSection{
position:absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%,-50%);
-moz-transform:translate(-50%,-50%);
-ms-transform:translate(-50%,-50%);
-o-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
}