Vertically Center - with unknown height - css

I have two div's side by side. On the left is an image, on the right are inputs.
The image varies depending on what the user uploads.
How can I vertically centre align the image and the inputs? I would like the inputs to appear vertically centre to the image.
Both the img and inputs have their own container:
<div class="img-container">
<div class="data-container">
Fiddle:
http://jsfiddle.net/vbLht/

Remove float:left and try display CSS Rule
.img-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
.data-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
Fiddle
Note: You need to set <!-- --> to your mark-up after inline divs as css inline rule will leave white-space between 2 elements, Though this an awkward way but if you don't want to write that crack then you've to adjust width.
Or
Set your li style to display:table with the above CSS Code
li{
display:table; /*remove float*/
}
.img-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
.data-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
Hope this is clear :)

Flexbox makes this very easy.
Just wrap the child elements you want to vertically center in a wrapper div with:
.wpr {
display:flex;
align-items: center; /* align vertical */
}
FIDDLE
Browser support is good in modern browsers - caniuse..
but if CSS3 isn't an option, the display:table approach (As mentioned in the other answer) is fine.

Related

How to remove the space between the children of an element with flex-wrap:wrap applied?

The question is about .text-wrapper, which has display:flex; flex-wrap:wrap applied to it. The reason for using flex-wrap:wrap is that otherwise .text-wrapper and .tabs-wrapper wouldn't stop being on one line, next to each other, like inline elements (though I have no idea why, because divs should be block level elements, no? I'll appreciate if someone can enlighten me on this one as well)
The problem is that I want the children of .text-container to its bottom, and not have more than 20px space between them.
But right now, there is a lot of space between .text-wrapper and .tabs-wrapper. How do I fix this?
JSFiddle here.
OR
html,
body {
height: 100%;
box-sizing:border-box;
}
.the-page {
height:100%;
width:100%;
position:relative;
}
.first-bottom {
height: 100%;
}
.image-container img {
position: fixed;
width: 100%;
height: 100%;
left: 0px;
top: 0px;
display: block;
}
.text-container {
height:100%;
width:100%;
top:0px;
position:relative;
display:flex;
align-items:flex-end;
flex-wrap:wrap;
}
.text-wrapper span {
text-align:center;
color:yellow;
}
.tabs-wrapper {
height:50px;
width:100%;
background-color:pink;
opacity:0.5;
}
.tabs-wrapper-inner {
height:100%;
display:flex;
align-items:center;
justify-content:center;
width:60%;
margin:auto;
}
.tabs-wrapper-inner a {
text-decoration:none;
font:sans-serif;
font-weight:bold;
color:red;
padding:10px;
}
.other-content {
background-color: purple;
opacity: 0.5;
width: 100%;
height: 500px;
}
<div class="the-page">
<div class="first-bottom">
<div class="image-container">
<img src="http://photostry.com/wp-content/uploads/2011/09/highway-arizona-to-utah.jpg" />
</div>
<div class="text-container">
<div class="text-wrapper">
<span>SUN BEACH WARM</span>
</div>
<div class="tabs-wrapper">
<div class="tabs-wrapper-inner">
AMY
BAMY
CAMY
DAMY
EAMY
</div>
</div>
</div>
</div>
<div class="other-content">.</div>
</div><!-- #the-page -->
The question is about .text-wrapper, which has display: flex; flex-wrap: wrap applied to it.
I think you mean .text-container, because there is no .text-wrapper rule in your CSS.
The reason for using flex-wrap: wrap is that otherwise .text-wrapper and .tabs-wrapper wouldn't stop being on one line, next to each other, like inline elements (though I have no idea why, because divs should be block level elements, no? I'll appreciate if someone can enlighten me on this one as well)
When you create a flex container – like you have by declaring display: flex on .text-container – you establish a flex formatting context. In this context, the children of the container become flex items and adhere to a flex layout, not a block layout. By default, flex items are aligned in a single, non-wrapping row (any block or inline display values are overridden by flex rules).
The problem is that I want the children of .text-container to its bottom, and not have more than 20px space between them.
But right now, there is a lot of space between .text-wrapper and .tabs-wrapper. How do I fix this?
To control the alignment of multiple lines in the cross axis, you can use the align-content property.
The reason there is wide space between both lines is because the default value of align-content is stretch, which tells flex lines to distribute free space in the cross axis equally among themselves.
To better understand how this property works I would suggest you add a border (or background, or both) to .text-wrapper and .tabs-wrapper. Then try out the different align-content values: flex-start, flex-end, center, space-between, space-around and stretch.
Also, an important note to keep in mind, align-content only works when there are multiple lines in the cross axis of the flex container. If there is only one line it will have no effect, and you should use align-items instead.
Add this to your CSS:
.text-container {
height:100%;
width:100%;
top:0px;
position:relative;
display:flex;
align-items:flex-end;
align-content: flex-end; /* new */
flex-wrap:wrap;
}
To create a 20px gap between .text-wrapper and .tabs-wrapper simply add a bottom margin to .text-wrapper.
.text-wrapper {
margin-bottom: 20px;
}
Revised Demo
To learn more about flexbox visit:
Methods for Aligning Flex Items
Using CSS flexible boxes ~ MDN
A Complete Guide to Flexbox ~ CSS-Tricks
What the Flexbox?! ~ YouTube video tutorial
You should use flex-direction:column; instead of flex-wrap.
For more information on flex and how to use it, there is this excellent article at CSS Tricks

Display div as centered blocks without 100% width

I know it's a super-basic question, but I'm not able to find a solution. I have 2 div and I would like to display them as blocks (one below the other) without having 100% width. Here's my code.
HTML
<div id="container">
<div class="test">one</div>
<div class="test">two</div>
</div>
CSS
.test {
display:inline-block;
clear: both;
border:1px solid;
}
#container {
clear:both;
text-align:center;
}
Unfortunately this answer doesn't fit to me, since I need to center blocks horizontally (so float cannot be applied in my case). Here's the fiddle. Thanks in advance.
to center them on top of each other without taking 100% width and still use margin:auto; use : display:table;
.test {
display:table;
margin:auto;
border:solid;/* to see it */
}
You can specify the width of the divs, change display to block, and use margin: 0 auto to center them.
JSFiddle
You can also center the div by adding 50% left offset, and then negative margin in amount to half width of the div. I do not know how much is this applicable to your case, but here is an example:
.test {
position: relative;
border:1px solid;
width: 300px;
left: 50%;
margin-left: -150px;
}
You can see it in action here: http://jsfiddle.net/b8LuQ/7/
display:inline-block; is not allow the second line. Therefore I removed it and define width for both div test one two you can resize it and margin:auto is align center the both div in container here is an example

Resizable table-cell to shrink smaller than content

I have set up 2 div tags, the outer one with display:table and the inner one with display:table-cell. Inside these I have an image.
When I resize the box using jQuery UI's resizable() API, I am unable to shrink it smaller than the image.
Markup:
<div class="resizebox">
<div class="content">
<img src="http://placehold.it/300x60">
</div>
</div>
CSS:
.resizebox {
border:1px solid black;
height:100px;
width:320px;
overflow:hidden;
display:table;
}
.resizebox .content {
display:table-cell;
vertical-align: middle;
text-align: center;
}
jsFiddle
I've added another example under the top resizable box to demonstrate the kind of behavior I'm trying to achieve (while keeping the CSS Table)
You beat me to it. Just using regular 'ol width: 100%;
img {
width:100%;
}
Fiddle Example
Try adding the following css
.resizebox .content img {
width:100%;
max-width:100%;
}
JsFiddle
Feeling a big silly now.
Fixed this by adding these CSS Styles:
.resizebox img {
max-width:100%;
max-height:100%;
}
jsFiddle

How can I horizontally and vertically center an <img> in a <div>?

I don't want to center a div, I want the contents of a div to be centered. I would like horizontal and vertical alignment.
Sorry, if this is too easy or whatever, but this is kind of a hassle to get it right.
Grae
I am using IE7
If you know the height and width of your image, position it absolutely, set top/left to 50% and margin-top/left to negative half the height/width of your image.
#foo {
position:relative; /* Ensure that this is a positioned parent */
}
#foo img {
width:240px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin-left:-120px;
margin-top:-100px;
}
Live example: http://jsfiddle.net/Zd2pz/
I know you've said that dont want to center a div but to achieve your requirement in a cross browser way would be easier using a jquery plugin and a fake div that contains your element to be centered.
I have successfully centered almost anything using this very small plugin that can center any block element.
The only other way I know are the answer that you already received from #simshaun & #Prhogz
EDIT: As per comment request
Include the script in your head tag
<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/jquery.center.min.js" )%>"></script>
Now if you have a DIV that you want to center inside your markup simply use it as
$(document).ready(function() {
$("#myDIV").center({ vertical: false });
});
although the following is obsolete, it still works for almost all browsers
<center>
<div>
your html
</div>
</center>
however, visit this link
http://www.110mb.com/forum/vertical-horizontal-alignment-of-image-within-div-t31709.0.html
For horizontal alignment, use text-align:center;
For vertical alignment, see for example the W3 style guide
If you know the inner element's height beforehand,
CSS:
.container {
text-align: center; /* Center horizontally. */
/* For demo only */
border: 1px solid #000;
height: 500px;
margin: 20px;
width: 700px;
}
.container img {
margin-top: -167px;
position: relative;
top: 50%;
}
HTML:<div class="container">
<img src="http://farm6.static.flickr.com/5004/5270561847_7223069d5e.jpg" width="500" height="334" alt="">
</div>
Example

CSS Container DIv Height. Floating DIV questions

Can you force a container DIV height to accomodate two floated div children? Is there a fancy trick I can use to do that? I am trying to make two equally sized divs inside the parent div. I would like them to appear side by side with a little whitespace between them. Child2 tends to pop out and go below Child1. Note Child2 contains a table. Should I be floating?
HTML:
<div id="parent">
<div id="child1"></div>
<div id="child2">
<table><tr><td>content</td></tr></table>
</div>
</div>
CSS:
div#parent
{
background-color: #C6E4E0;
border: solid 3px #017E6F;
font-family: Arial;
font-size: 10pt;
font-weight: bold;
padding-left: 5px;
padding-right: 5px;
width:99%;
}
div#parent div
{
width:49%;
float:right;
padding:3px;
}
div#parent div:first-child
{
float:left;
}
This is not a clearfix issue guys, his problem is that his two floated divs are not appearing side by side.
First of all, you do not need to set the width of the parent div, divs are block elements which means they automatically adjust their width to take up the full width of their parent (in this case, presumably the parent of div#parent is the body).
Because you are setting the width explicitly AND giving it padding, it can potentially extend BEYOND the body. That doesn't really matter, but if you apply this same knowledge to the child floated divs and you can see why the right one might get bumped down to the bottom.
First, if you are explicitly setting the widths of the divs to a percentage, you do not need to add padding. Because you are dealing with percentage widths, it is better to add padding to the content of the divs rather than the divs themselves, because padding is ADDED to the width. Therefore, if you added 10px padding to a div that had a 49% width in a 100px parent, it would have a width of 49px + 10px + 10px (2 sides) for a total calculated width of 69px.
Since you didn't post your markup and content or which browser you are testing in, I can't say exactly why the div is being bumped down. There are two likely possibilities.
You are using IE, which allows tables to extend beyond its parent div which will cause breakage. Try explicitly setting the table width to a percentage of its parent or something like that.
The 49% width + padding = greater than [parent-width] - [left-div-width]. This will cause it to get bumped down because the left div and right div are too wide for the parent width.
I use the clearfix class.
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
display: inline-block;
}
/* Hides from IE-mac \*/
/** html .clearfix {height: 1%;}*/
.clearfix {
display: block;
}
/* End hide from IE-mac */
then just use the class in every floated-element container.
#container { width:200px; }
.floated { width:100px; float:left; }
.clear { clear:both; }
<div id="container">
<div class="floated">A</div>
<div class="floated">B</div>
<div class="clear"></div>
</div>
I am not a fan of clear: both;, I rather do this in Jonathan Sampsons example:
#container { width:200px; overflow: hidden; }
.floated { width:100px; float:left; }
<div id="container">
<div class="floated">A</div>
<div class="floated">B</div>
</div>
By the way, you want
div#parent > div { float:left; }
instead of
div#parent div:first-child { float:left; }
which is still not IE6 friendly, but it will float both child DIVs.

Resources