This question already has answers here:
How do I vertically align text in a div?
(34 answers)
Closed 9 years ago.
I was wondering what would be the best way to vertically center a div inside another div. The only constraint I have is that .anim needs to remain relative! I lsited the current code I have now down below. Thanks guys!
HTML:
<div class="anim">
<div class="spacer">
<p>CONTENT</p>
<p>more content</p>
</div>
</div>
CSS:
.anim {
position:relative;
width:75%;
margin:auto;
height:50%;
}
.spacer{
position:absolute;
height:300px;
top:0;
bottom:0;
}
.anim{display:table;}
.spacer {display:table-cell; vertical-align:middle;}
would have it vertically aligned
An easy way to do this is to give your .anim div a fixed height of, let's say, 500px.
Then you can just add a margin-top :
.anim {
margin-top: 100px; // (500 - 300) / 2 = 100
}
demo
according to w3: http://www.w3.org/Style/Examples/007/center.en.html#vertical
CSS level 2 doesn't have a property for centering things vertically. There will probably be one in CSS level 3. But even in CSS2 you can center blocks vertically, by combining a few properties. The trick is to specify that the outer block is to be formatted as a table cell, because the contents of a table cell can be centered vertically.
so the code is really simple
.anim {
display: table-cell;
vertical-align: middle;
height: 200px;
}
here is the demo http://jsfiddle.net/AbTxS/
From your question it looks like you want something like this... div.spacer is vertically centered and div.anim remains relative.
The div.spacer top margin must be negative half of the .spacer height.
This solution only works with a fixed height for .spacer.
CSS
.anim {
position:relative;
width:75%;
margin:auto;
height:800px;
background:#FF0
}
.spacer {
position:absolute;
top:50%;
margin:-150px 0 0;
width:300px;
height:300px;
background:red
}
HTML
<div class="anim spacer">
<p>
Content
</p>
<p>
More content
</p>
</div>
Related
I have a webproject using bootstrap.
There is an Layout with two columns. The left column has an image and the right one has an text. The text should be vertically centered to the image.
The first problem was to get the columns the same height.
I found the following working solution:
.col {
margin-bottom: -99999px;
padding-bottom: 99999px;
height:100%;
}
To get the text centered vertically I inserted the following html in the right row:
<div class="table">
<div class="cell">My text</div>
</div>
CSS:
.table {
height:100%;
width:100%;
display:table;
}
.cell {
display:table-cell;
vertical-align:middle;
height:100%;
}
But I don't get the table and its cell to height 100% of the parent. Position absolute not working too, because bottom:0 is 99999px;
Anybody has an idea what I can do? Thanks
I think that if you can do it, try to use flex-box instead of a fixed grid.
Here's your CodePen for an example: http://codepen.io/anon/pen/RaNYLG
HTML:
<div class="row">
<div class="column">
<p> Here you have some content.</p>
</div>
<div class="column">
<p> Here you can have a lot of text as well, but i will make longer since you need to understand how to vertically align your content. In this case you will see that the content being aliged will be the other. Take this just as an example.</p>
</div>
</div>
And CSS:
.row{
display:flex;
}
.column{
display:flex;
width:50%;
align-items:center;
}
(And to learn how to properly use the flex property: http://flexboxfroggy.com/)
This question already has answers here:
How to set the margin or padding as percentage of height of parent container?
(9 answers)
Closed 7 years ago.
I have very simple markup and style rules that should put my div in the middle of the page, but it does not (actually, the top of the div would be at the middle, not 100% truly centered vertically) when the viewport is of a small enough height.
HTML:
<div>adf</div>
CSS:
div {
background-color:red;
margin-top:50%;
}
You can look at my Fiddle to see this. It will require you shrink the rendered window (bottom right) a bit, as it is correct initially.
Instead of 50% try 50vh
50% doesn't do what you think it would - it actually uses the width of the parent container, not the height to calculate.
Try replacing percentages with vh
div {
background-color:red;
margin-top:50vh;
}
<div>adf</div>
Here are more cool stuff about viewport sized measures
Here is another solution:
http://jsfiddle.net/nb6pq14v/
html, body{height:100%; margin:0;padding:0}
.container-fluid{
height:100%;
display:table;
width: 100%;
padding: 0;
}
.row-fluid {height: 100%; display:table-cell; vertical-align: middle;}
.centering {
float:none;
margin:0 auto;
background: #abc;
height: 25px;
}
<div class="container-fluid">
<div class="row-fluid">
<div class="centering text-center">
</div>
</div>
</div>
This question already has answers here:
Align <div> elements side by side
(4 answers)
Closed 4 years ago.
I have 2 div elements I want centered in my page. The outter div is 100% width and text-align: center. Like so:
div.centerpanel {
font-size: 28px;
width:100%;
height:100%;
text-align: center;
}
The two elements contained within it should appear next to each other but centered (they're both simply divs with text content). Simply put, I want a centered title on my page, each of the two words of the title being a seperate div. I read that you do the following to accomplish this:
float: left;
clear: none;
As far as I can tell, the 'clear' does not have any visible effect. When I add the float: left, to the first of the two elements, it simply causes that element to slide all the way to the extreme left of the outer .centerpanel while the element that follows it remains out in the middle where it should be. So it's definitely aligning it properly but inexplicably sends it all the way to the left.
How do I make it stay vertically aligned with it's following element but keep obeying the outer div's text-align: center?
If I understand your question correctly, something like this is what you want to have. And of course there are more than one ways to skin a cat.
HTML:
<div class="centerpanel">
<div class="left">L</div>
<div class="right">R</div>
<div class="clearfix"></div>
</div>
CSS:
.centerpanel {
font-size: 28px;
width:100%;
height:100%;
text-align: center;
}
.centerpanel div{
width: 50%;
}
.left{
float: left;
}
.right{
float: right;
}
.clearfix{
clear: both;
}
jsFiddle: http://jsfiddle.net/nfpdpmze/2/
Since there are only two divs here, you could also set them display: inline-block without using any flotation.
Side note:
There are of course more modern ways of clearing floating. One of them is by setting up the overflow property on the container of the divs that get floated. More about that here: https://css-tricks.com/all-about-floats/
You can also use display:inline-block; on your child elements. View this Fiddle for an example of how to accomplish this.
HTML:
<div class="centerpanel">
<div class="leftpanel">Left</div><div class="rightpanel">Right</div>
</div>
CSS:
div.centerpanel {
font-size: 28px;
width:100%;
height:100%;
text-align: center;
}
.leftpanel {
background:red;
display:inline-block;
width:50%;
}
.rightpanel {
background:blue;
display:inline-block;
width:50%;
}
Here you go:
<div style="display:flex">
<div style="text-align:center">
<span>Content1</span>
</div>
<div style="text-align:center">
<span>Content2</span>
</div>
</div>
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 8 years ago.
Here's my situation: I'm trying to make a page with two DIVsfilling whole page (height 100%, width 50% each). Also, the content in the DIVs is to be vertically aligned to middle.
Is there an easy way to achieve this without hacks or javascript?
I've tried body,html{height:100%;} .mydiv {display:table-cell;height:100%;vertical-align-middle}
but that doesn't work...and with that code, i have to specify width in pixels instead of percentage
Live Demo
I just made a jsFiddle showing my suggestion to a solution. Here I take into account that you want two <div>s filling 50% of the width each, 100% height, and that you want the content to be vertically aligned in the middle. Here is the simplified working solution with source code.
HTML
<div id="outer">
<div id="table-container">
<div id="table-cell">
This content is vertically centered.
</div>
</div>
</div>
CSS
#outer {
position:absolute;
top:0;
left:0;
width:50%;
height:100%;
}
#table-container {
height:100%;
width:100%;
display:table;
}
#table-cell {
vertical-align:middle;
height:100%;
display:table-cell;
border:1px solid #000;
}
For reference, I used this tutorial.
position: absolute;
top: 0;
bottom: 0;
Will give you a box that fills to 100% height. Make sure your HTML and BODY tags are large enough:
html, body {
height: 100%;
}
Do you want this type of design ? => Example Fiddle
I'm trying to centre align an image using a div and a CSS class (the tag is wrapped in a div class="center" tag). What I'm using at the moment is working in Dreamweaver (when I go to design view) but not when I load the page up in Safari. Here is my code:
.center {
display:inline;
text-align:center;
-webkit-inline;
-webkit-center;
background-color:transparent;
}
Sorry for asking such a simple question, I'm completely new to HTML, my experience is in Objective-C.
text-align: center caused the content to be centered (within the container), and not the container itself being centered.
Since you use display: inline, the size of the container will be the same as its content, and the centering will not have the effect you're after.
Either, you use the margin: 0 auto to center the container (towards its parent container), OR you change display: inline to display: block.
Give text-align:center; to it's .center parent DIV. Write like this:
HTML
<div class="parent">
<div class="center"></div>
</div>
CSS
.parent{
text-align:center;
}
.center {
display:inline;
background-color:transparent;
}
You can use margin : 0 auto , to a vertical align , but if you want a vertical-horizontal align , you need a code like this:
.center{
width:200px;
height:200px;
margin-left:-100px;
margin-top:-200px;
position:absolute;
top :50%;
left:50%;
}
margin: 0 auto. Is what you're looking for. You'll need a width also.
div.center { margin:0 auto; width: 20%;background:orange;}