Vertical align in CSS, no heights known - css

Here's my markup:
<div id="container">
<img id="image" src="image.jpg" />
<div id="contents">Contents</div>
</div>
The height of #container equals the height of #image.
All the heights are dynamic (they change on window resize).
The image can not be set via background property.
How can I have Contents over the image and vertically centered in #container?

Is the height of #contents known? In that case this should do it (jsfiddle demo):
#container{
position:relative;
/* For demo only */
height:500px;
border: 5px solid red;
}
#image{
position:absolute;
/* For demo only */
height:500px;
}
#contents{
position: absolute;
top:50%;
height:100px;
margin-top: -50px; /* Half of #contents height */
/* For demo only */
background-color: blue;
}

​This ought to do what you are looking for. I have just set the height of the container and image in css, but if they are the same set in html or using javascript, the result should be the same. The background colour is just there for clarity.
#container {
background-color: #333;
height: 200px;
}
#image{
height: 200px;
float: left;
}
#contents{
line-height: 200px;
float: left;
position: fixed;
}
​
EDIT: Here is a fiddle of a solution using the old classic margin auto trick. The only thing that may cause problems here is that the parent needs to be position: fixed; which may cause issues for you elsewhere. The main thing is it works, and no heights are set using pixels.
link
Here is the code from the fiddle for a pure css solution with no fixed heights.
<div id="container">
<img id="image" src="https://www.google.co.uk/logos/2012/juan_gris-2012-hp.jpg" />
<div id="contents">Contents</div>
</div>
#container {
position: fixed;
}
#contents{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 50%;
height: 10%;
margin: auto;
}

If you know the height of #contents you can set up
#container{position:relative;}
#contents{
position:absolute;
top:50%; /*50% of parent*/
margin-top:/*negative one-half of container height i.e. if contaner is 4 em -2em*
}
You say you don't know the height of #contents, though.
Another option is to set the display: to table-cell and use vertical-align: middle
#container{
display: table-cell;
vertical-align: middle;
}
But depending on what browsers you are targetting for support, that may cause display issues as well.
The more sure fire way is to combine the first option with some jquery or javascript to find the element's height and set its margin-top:
content= document.getElementById('content')
content.style.marginTop = '-' + content.offsetHeight + 'px';
http://jsfiddle.net/h76sy/
EDIT: Sorry, had a bug in my javascript, try it now

Related

when css position sticky stops sticking

I was wondering why position: sticky works for some x-axis-scrolling, but once you scroll past the initial width of the screen width, your 'sticky div', stops sticking.
In this example, I have a left-side-bar that sticks to the left (please note that I cannot use position: fixed or position: absolute, because in my actual project both the left-div and the right-div need to scroll up and down along the y-axis, hence we only want left-side-sticking)
is there an additional CSS parameter I can define, such as
left-sticky-distance=999999%
or something like that?
some test code illustrating is below
<html>
<body>
<div style='
position:sticky;
z-index:1;
left:0;
width:100px;
height:200px;
overflow: hidden;
background-color:#ff0000;
opacity:0.8;'
>
</div>
<div style='position: absolute; top: 10; left: 10; width: 200; height:50px; background-color: blue'>B</div>
<div style='position: absolute; top: 10; left: 110; width: 200; height:50px; background-color: blue'>C</div>
<div style='position: absolute; top: 10; left: 210; width: 200; height:50px; background-color: blue'>D</div>
</body>
<html>
After I add the height: auto; into body's CSS attributes as below, this auto-hiding problem is fixed.
body {
background: #fff;
color: #444;
font-family: "Open Sans", sans-serif;
height: auto;
}
Hope it will be helpful to you. :)
This question: https://stackoverflow.com/a/45530506 answers the problem.
Once the "sticky div" reaches the edge of the screen, it is at the end of the viewport of the parent element. This causes the sticky element to stop and stay at the end of parent's viewport. This code pen provides an example: https://codepen.io/anon/pen/JOOBxg
#parent{
width: 1000px;
height: 1000px;
background-color: red;
}
#child{
width: 200px;
height: 200px;
background-color: blue;
position: sticky;
top: 0px;
left: 0px;
}
body{
width: 3000px;
height: 3000px;
}
<html>
<div id="parent">
<div id="child">
</div>
</div>
</html>
What i've just realized is that is stops sticking because you haven't captured an overflow. if you've specified an overflow: hidden;, then check that all content within that axis fits perfectly on all screen sizes and if not then make the necessary adjustments to make the content fit. This also happens when you have specified the height of a div and the content overflows past that height in a certain screen sizes.
I hope this helps anyone that made the same mistake i did.

Absolute position element on equal height columns - how?

Here's what I can't do by any means, using only CSS:
Columns should have equal heights.
And, on a given column, a absolute positioned element should be present relative to that column:
The HTML:
<div class="wrapper">
<div class="inner">
<div class="column">
<img src="http://www.english3.com/wp-content/uploads/2013/05/DFI-Logo-300px-X-200px.png"/>
</div>
<div class="column info">
<p>Some text here yeah :).</p>
<p>Some text here yeah :)</p>
<p>Some text here yeah :)</p>
<a class="link" href="#">I should be absolute.</a>
</div>
</div>
</div>
THE CSS:
.wrapper {
overflow: hidden;
position: relative;
}
/*added */
.inner {
width: 100%;
display: table;
}
.column {
display: table-cell;
width: 50%;
vertical-align: top;
background-color:red;
}
.column img {
max-width:100%;
display: block;
}
.info {
background-color: blue;
padding-bottom: 25px;
}
.link {
display: block;
position: absolute;
left: 50%;
bottom: 3%;
color: yellow;
}
Fiddle to play:
Here's the try with table-cell; and a relative inner container:
http://jsfiddle.net/BuuFv/98/
TRIES AND FRUSTRATIONS:
1) - Display table
For equal heights I can't pull this out, due to FF issues.
Tried with an extra relative parent container
But left image doesn't shrink according to it's container on FF.
Tried giving height: 100%
http://davidwalsh.name/table-cell-position-absolute
But didn't work either, the image doesn't shrink or expands using max-width;
2 - Huge positive padding and negative margin values
Will not work, because the absolute positioned element will not stay in place.
3 - Faux Columns
Seems to be of any help, due to the fact that, we are not playing with solid background colours, and we have an image on the left column instead.
A picture:
Any help, please?
1)
On this case, adding an extra relative container with position relative attribute (.inner), did the trick.
2)
The fact that the image didn't shrink or grow on FF was because it wasn't considering the max-width declaration as it should.
Fix:
.inner {
width: 100%;
display: table;
table-layout:fixed; /* <<-- ADD */
}
the table-layout fixed algorithm made Firefox recognize the max-width in the cell and this let the image shrink.
Credits on this solution should be shared with Paul O'B.
Here is my take on it http://jsfiddle.net/E6UDD/
So basically, uncoment padding and negative margin hack, and add the height of the column to be the same amount.
Then, add relative positioning to the .column.
Then, position of the absolute positioned link should be top: 50% to have it at the very bottom, or 44% to mimic the 3% you originally indended.
DEMO
.wrapper {
position:relative;
overflow: hidden;
white-space:nowrap;
outline:1px solid red;
height:auto;
}
.column {
position:relative;
display:inline-block;
vertical-align:top;
background-color:red;
width:50%;
}
.column img {
max-width:100%;
min-width:30px;
min-height:20px;
}
.info {
position:absolute;
bottom:0;
top:0;
background-color: blue;
}
.link {
clear:left;
display: block;
position: absolute;
bottom: 3%;
color: yellow;
}
http://jsfiddle.net/BuuFv/75/
You can try changing the link from absolute positioned to another table-cell. Not 100% on your use case but it seems to accomplish what you are asking.
.link {
height:100px;
display:table-cell;
vertical-align:bottom;
color: yellow;
}

CSS vertically align div while maintaining aspect ratio

I used the JsFiddle at jsfiddle.net/5tzk3/10. I changed it to display the div as square shaped dialog (both horizontally and vertically centered). The result is at jsfiddle.net/5tzk3/548.
As you see, centering horizontally was easy, but I could not get it centered vertically. Anyone who knows how to do that with pure CSS?
Edited code below:
<div class="blind">
<div class="wrapper">
<div class="main">
I'm your div with an aspect-ratio of 1:1!
</div>
</div>
</div>
html, body, .blind {
height: 100%;
width: 100%;
}
.blind {
left: 0;
position: fixed;
text-align: center;
top: 0;
}
.wrapper {
display: inline-block;
margin: auto;
position: relative;
width: 50%;
}
.wrapper:after {
content: '';
display: block;
padding-top: 100%;
}
.main {
background-color: rgb(0, 162, 232);
bottom: 0;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
}
Use display: table for the parent div, and display:table-cell; vertical-align: middle for the content div which you want to vertically center.
The most common way of doing this if you've got an element with known dimensions is to use positioning to firstly position it top: 50% (which places the top edge of the element 50% of the way down) and then use a negative top-margin of half the height of the element (pulling it back up by half it's height).
To give you an example, to absolutely position a 200x200 element dead-centre on the page you would use:
.element{
display: block;
width: 200px;
height: 200px;
position: absolute;
top: 50%;
margin-top: -100px
}
Alternatively, you can use a combination of display: table and then display: table-cell on the parents to open up the ability to use vertical-align although this is a bit nasty when it comes to laying out elements around it.
you can drop absolute positionning and use either display:table or inline-block with pseudo elements.
here is a mixed of the 2 methods
1) html/body as a table one cell
2) inner content with ratio preserved and content as inline box set in the middle.
.ratio1-1 {
width:25%;
vertical-align:middle;
margin:auto;
background:turquoise;
}
.ratio1-1:before {
content:'';
padding:50% 0;
width:0;
display:inline-block;
vertical-align:middle;
}
.ib {
display:inline-block;
vertical-align:middle;
}
/* center body content as a table cell */
html {
height:100%;
width:100%;
display:table;
}
body {
display:table-cell;
vertical-align:middle;
text-align:center;
}
<div class="ratio1-1">
<div class="ib">content in middle</div>
</div>
demo: http://codepen.io/gc-nomade/pen/pubFm

CSS: One column's width relative to resizable 100% height square?

This question is kind of hard so explain so I've created a jsfiddle:
http://jsfiddle.net/zLjHn/ (or see HTML/CSS below)
Basically I want a square shaped video on the right size of the screen with 100% height and the left column to fill the rest of the available screen space. I've been working on this problem for a few hours so any help would be appreciated!
Edit: Left column will contain a number of paragraphs/images that will be centered in the available space (not just a single paragraph as the example code shows.
CSS:
.profile_page {
width: 100%;
height: 100%;
position: absolute;
}
.left {
float:left;
width: 100%;
position: relative;
}
video {
top: 0;
right: 0;
border: 1px solid red;
float: right;
height: 100%;
position: absolute;
max-width: 80%;
}
HTML:
<div class="profile_page">
<div class="left">
<p>This paragraph's width should adjust according to the videos width.</p>
</div>
<video src="http://stream.flowplayer.org/trains/640x360.mp4" />
</div>
Thanks!
Taboo as this sounds, your answer is a table layout. A two-cell table row exhibits the layout behavior you're looking for without any javascript. If you're concerned about semantic HTML (and you should be) you can accomplish this using display:table-cell; and display:table-row; in your css.
This will limit the browsers that correctly display your layout.
<div class="profile_page">
<div class="left">
<p>This paragraph's width should adjust according to the videos width.</p>
</div>
<div class="right">
<video src="http://stream.flowplayer.org/trains/640x360.mp4" />
</div>
</div>
and the style...
.profile_page {
width: 100%;
height: 100%;
position: absolute;
display:table-row;
}
.left {
min-width: 100px;
display:table-cell;
border:1px solid green;
vertical-align:top;
}
.right
{
display:table-cell;
border:1px solid blue;
max-width:80%;
min-width:300px;
}
video {
top: 0;
right: 0;
height: 100%;
width: 100%;
}
See the fiddle: http://jsfiddle.net/NkeLc/1/
If you're looking for a quick fix, you can reorganize to wrap the <p> around the video like this: http://jsfiddle.net/zLjHn/4/
I believe the paragraph and video are competing for "flexibility". Seems like you want the video to flex according to height, and the paragraph to then flex with available width.
I think you can achieve the right flex-priority by leading with the video and wrapping the paragraph around it as the attached fiddle shows.
Try to modify your CSS as following:
.left {
float:left;
max-width:20%;
position: relative;
min-width: 100px;
}
demo: http://jsfiddle.net/4yEmJ/

position: absolute, div under div

I've 3 divs, each of them has position: absolute.
First is header, and its working.
Header has constant height, and second div "content" also works.
Third div is "footer".
"Content" has changeable height and when "content" is higher than web-browser window the "footer" is ON "content". I want to "footer" under "content" irrespective of content height.
My header is 300px height, content has margin-top: 300px. I can't use the same for the footer, because content hasn't got constant height.
I don't want to set one div with position: absolute, and these 3 divs place inside this one.
div#header{
width: 960px;
height: 200px;
left: 50%;
margin-left: -480px;
position: absolute;
}
div#content{
width: 960px;
border: 1px solid black;
left: 50%;
margin-left: -480px;
position: absolute;
margin-top: 200px;
}
div#footer{
width: 960px;
height: 30px;
left: 50%;
margin-left: -480px;
position: absolute;
bottom: 10px; /*with this i've div fixed to the bottom of web-browsers' window */
clear: both;
}
You're over positioning.
You do not need to position everything absolutely unless there's something you aren't sharing.
JSBin Example
If you are willing to use position : relative which is a tad better than position : absolute in cases like this, http://jsfiddle.net/vFTXg/1/ - Try editing the value of your content's height here and your footer will be automatically adjusted.
CSS
.header {
position : relative;
width : 100%;
height : 90px;
background-color : #000;
}
.content{
position:relative;
width : 100%;
min-height : 200px;
background-color : #f00;
}
.footer{
position:relative;
width : 100%;
height : 50px;
background-color : #0f0;
}
HTML
<div class='header'></div>
<div class='content'></div>
<div class='footer'></div>
I would recommend using CSS floats
Do something like this:
<div id="wrapper">
<div id="header">...</div>
<div id="content">...</div>
<div id="footer">...</div>
<div class="clear"></div>
</div>
Set the site-width on the wrapper and let the other divs have the same width.
Use float:left on header, content and footer
Set clear:both on the clear-div.
Now you can set the height on the elements you want to have a fixed hight - and you don't have to bother with absolute positioning.. If you insist on using positioning, you could just position the wrapper.
In the future browser can calculate. For your example this could be nice to calculate the min-height for the content to set the foorter to the bottom if content height is low and to set the footer after the content if it has a heigh value. E.g.:
HTML:
<div id="header" class="content">header</div>
<div id="content" class="content">content</div>
<div id="footer" class="content">footer</div>
CSS:
html, body {
height: 100%;
}
.content {
position: relative;
width: 960px;
}
#header {
height: 200px;
}
#content {
border: 1px solid black;
min-height: -moz-calc(100% - 302px);
min-height: -webkit-calc(100% - 302px);
min-height: calc(100% - 302px);
}
#footer {
height: 100px;
}
Unfortunately only firefox and IE9 and higher support calc at the moment, so this is more theoretically. If you want to test it, see this jsfiddle.
If you want to do this with all current browser you need to use javascript.
If you want something to be of constant width and centered try this
#footer,
#header,
#footer {
width: 960px;
margin: 0 auto;
}
and forget about
left: 50%;
margin-left: -480px;
position: absolute;

Resources