Vertically aligning a div and proper scrolling overflow, is this possible? - css

I've discovered that I can have one or the other, but can't seem to figure out both.
My HTML is as follows:
<div id="middle">
<div id="middleinner"></div>
</div>
And the CSS goes a little something like this (z-indexes set for something else on the page, left out here because it's irrelevant, plus there's a few commented out things as I tried to figure it out as I went along):
html, body{
font-family: helvetica, sans-serif;
font-size: 1em;
height: 100%;
overflow: hidden;
}
#middle{
/* display: table;*/
display: table;
height: 80%;
width: 90%;
/* position: fixed;*/
position: absolute;
top: 10%;
left: 5%;
right: 95%;
bottom: 90%;
color: #000;
z-index: 0;
}
#middleinner{
padding: 0.5em 1em 0.5em 1em;
background-color: #F9F9F9;
display: table-cell;
/* display: inline-block;*/
border-radius: 1.5em;
vertical-align: middle;
/* margin-left: auto;
margin-right: auto;*/
text-align: center;
/* position: relative;*/
position: absolute;
width: 100%;
height: 100%;
overflow: auto;
z-index: 20;
}
Anyway, if I change middleinner's position to relative instead of absolute, I've got vertical alignment at the cost of proper overflow handling. If I set it to absolute, I've got proper overflow handling at the cost of vertical alignment.
Is there a pure-CSS way that I can handle this?

Yes, a combination of line-height, inline-block, and vertical-align can do it.
#middle {
width: 80%;
margin: 10px auto;
border: 1px dashed black;
height: 500px;
line-height: 500px;
vertical-align: middle;
text-align: center;
}
#inner {
height: 50px;
width: 80%;
overflow: auto;
display: inline-block;
line-height: 1.1em;
}
Demo
To have it work with a dynamic height element, you'll have to use some JavaScript, since you're using jQuery, I'll go with that. It's perfectly possible to use vanilla JS for this one too.
resize = function(el) {
el.css({lineHeight: el.height() + "px"})
};
$(document).ready(function() {
var $middle = $("#middle");
resize($middle);
$(window).on("resize", function() {
resize($middle);
})
})

Related

bottom: 0 doesn't work in Firefox with fixed positioning

I have a simple page with a textarea that I want to fill the entire screen, except for a bit at the top. It works perfectly in Chrome but doesn't stretch to the bottom of the window in Firefox. This is the CSS I'm using:
body#pad textarea {
position: fixed;
top: 3em;
right: 0;
bottom: 0;
left: 0;
width: 100%;
box-sizing: border-box;
background-color: #222;
color: #fff;
display: block;
font-size: 1.2em;
letter-spacing: 0.6px;
padding: 1em 2em 2em;
resize: none;
}
When I add height: 100% it does reach 100% in height, but that's not what I want, since it needs a little space at the top of the screen. Is there any pure CSS way to fix this? I'd really like to make it look and function like it does in Chrome without any Javascript.
As MDN
When both top and bottom are specified, and height is unspecified or
either auto or 100%, both the top and bottom distances are respected.
In all other situations, if height is constrained in any way, the top
property takes precedence and the bottom property is ignored.
So I suggest this trick to do, just remove top: 3em; and add height:calc(100% - 3em);
textarea {
position: fixed;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: calc(100% - 3em);
box-sizing: border-box;
background-color: #222;
color: #fff;
display: block;
font-size: 1.2em;
letter-spacing: 0.6px;
padding: 1em 2em 2em;
resize: none;
}
<textarea placeholder="write..."></textarea>
Don't know why FF misbehave so here is a workaround using CSS calc()
Firefox appear to follow this rendering rule and by doing so actually have a preset height (and width) for the textarea element. It is this preset that overrides the top-bottom/left-right settings and therefore become constrained causing the constraint rules (top-bottom / left-right) to apply.
To make all browsers behave the same, a height and width need to be set explicit, where I used CSS calc() to get the correct height.
(the right and bottom properties can be removed as they no longer affect the rendered result)
html, body {
margin: 0;
height: 100%;
}
textarea {
position: fixed;
top: 3em;
left: 0;
width: 100%;
height: calc(100% - 3em);
box-sizing: border-box;
background-color: #222;
color: #fff;
display: block;
font-size: 1.2em;
letter-spacing: 0.6px;
resize: none;
}
<textarea>
write more...
</textarea>
In some situations the top-bottom/left-right properties is needed when using positioning, so for a textarea, one can use a wrapper like this
html, body {
margin: 0;
height: 100%;
}
div {
position: fixed;
top: 3em;
left: 0;
right: 0;
bottom: 0;
}
textarea {
position: absolute;
width: 100%;
height: 100%;
box-sizing: border-box;
background-color: #222;
color: #fff;
display: block;
font-size: 1.2em;
letter-spacing: 0.6px;
resize: none;
}
<div>
<textarea>
write more...
</textarea>
</div>
this css seem to work [https://jsfiddle.net/ubc4nz9k/]https://jsfiddle.net/ubc4nz9k/ set padding-top to your needs
#pad textarea {
width: 100%;
height:100%;
box-sizing: border-box;
background-color: #222;
color: #fff;
font-size: 1.2em;
letter-spacing: 0.6px;
padding: 1em 2em 2em;
resize: none;
}
#pad {
padding-top: 20px;
position: fixed;
width:100%;
height:100%;
left:0;
right:0;
}

Text won't fill the container in a column

I've been experimenting with various columns techniques and I'm liking this one. I plan on having one more image under them and centered. I have the images flipping once selected. My problem is the text on the back (.flip-item-desc) is inheriting the 50% width from the container (#top div). I can't figure out how to make the text div fill the back of the image.
#top div, #bottom div{
background-color: aqua;
text-align: center;
margin: 1% auto;
width: 50%;
height: auto;
}
.flip-item-desc{
font-family: sans-serif;
font-weight: bold;
color: white;
text-align: center;
font-size: 80%;
line-height: 1.237;
background: rgba(0,0,0,0.5);
width: 96%;
height: auto;
margin: 2% 2%;
padding: 2%;
position: absolute;
top:0;
left: 0;
overflow: hidden;
}
I copied and pasted my code with hosted image here: https://jsfiddle.net/digi57/ojLtu049/1/
I added a few lines in your CSS code -
.flip-item-desc {
min-width: 96%;
}
.back {
overflow: hidden;
}
here is the fiddle - https://jsfiddle.net/ojLtu049/2/

How do I resize a fixed footer background image to my desired resolution?

Please what do I have to add or change here to reduce the footer-bg to a pixel with width: 100; and height: 18;?
I'll be glad if this is looked on now as I need this help now.
Gracias.
#footer-bg {
background:#1f512e;
position: relative;
display: block;
width: 100%;
padding-bottom: 0;
min-height: 43px;
}
.footer-top {
height: 12px;
}
You can apply your dimensions using
width: 100px;
height: 18px;
http://jsfiddle.net/f9p6xbrn/2/
#footer-bg {
background:#1f512e;
position: relative;
display: block;
width: 100px;
height: 18px;
padding-bottom: 0;
min-height: 43px;
}
.footer-top {
height: 12px;
}
Simple enough. If you want the green bar to be centered and take up 960px, do this. My changes are at the bottom.
#footer-bg {
background:#1f512e;
position: relative;
display: block;
padding-bottom: 0;
min-height: 43px;
width: 960px;
margin: 0 auto;
}
If you want to set your footer to 100% and have it match the rest of your content, you would have to create some sort of container to put all your content including the footer in. Then set that container to a width of 960px with margin: 0 auto.

div template with 3 random size columns and bottom

I use this template
<style>
#block_center{
position: absolute;
right: 210px;
left: 210px;
text-align: left;
border: 1px solid #000;
overflow: auto;
height: auto;
}
#block_right{
width: 200px;
border: 1px solid #000;
position: relative;
right: 3px;
text-align: left;
float: right;
}
#block_left{
position: relative;
left: 3px;
width: 200px;
border: 1px solid #000;
text-align: left;
float: left;
}
#block_content{
clear: both;
float: none;
width: 100%;
text-align: center;
overflow-y:auto;
overflow-x:auto;
height: auto;
/* margin-bottom: -50px; */
margin: auto;
}
#block_buttom {
background-color: blue;
/* z-index: -10; */
width: 100%;
height: 50px;
clear: both;
}
.clear {
clear:both;
overflow:hidden;
}
</style>
<div id="block_content">
<div id="block_center"> ARTICLE <br> article_ajax_content </div>
<div id="block_right"> Artile links </div>
<div id="block_left"> banner </div>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div id="block_buttom"> some text info about site and 31px height img </div>
Problem that I having is that Article not only can be random height size but also there is ajax block of random size content going after it and I simply can't absolutely stick bottom div to stay in bottom after all content regarding browser window size, content size, ajax block size...
Can any one help me with how css (I do not want to use jQuery to pin bottom block to a fix y coordinate) should look like for my pattern of use?
Make all blocks relatively positioned and give the heights and widths using percentage rather than pixels.
Make sure the sum of all your height percentages is 100%(in case you want to cover the whole screen).
This ensures your page content covers the whole screen, irrespective of the screen resolution.
The relative sizes of each block is also kept the same across all resolutions.
The key is to use PERCENTAGE values and not PIXEL values.
To solve the Dynamic sized article data, just CSS the article div to have a scroll bar.
this is currently does want i want
<style>
#block_buttom {
margin-top: 10pt;
/* z-index: -10; */
width: 100%;
display: inline-block;
height: auto;
clear: both;
}
.page-buffer {
padding-top: 50px inherit;
height: 50px;
clear: both;
width: 100%;
}
.clear {
clear:both;
font-size:0;
overflow:hidden;
}
#block_content {
width:100%;
text-align:center;
}
#block_left {
position: relative;
top: 0px;
text-align: left;
float:left;
width: 10%;
min-width:210px;
height: auto;
padding: 3px;
}
#block_center {
text-align: left;
display: inline-block;
margin:0 auto;
width:70%;
min-width: 640px;
height: auto;
padding: 3px;
}
#block_right {
position: relative;
text-align: left;
float:right;
width: 10%;
min-width:210px;
height: auto;
padding: 3px;
}
</style>
on high resolution it looking very nice, on lower - still require some tuning with finding balance of center block size and spaces between fixed size left\right blocks

vertical-align css not working even with line-height specified

I'm trying to vertically center an image using vertical-align: middle; but I can't seem to get vertical-align to have any effect at all. I've set the line-height and height, but nothing has an effect.
SASS:
.lot-images {
position: relative;
float: left;
min-height: 300px;
*height: expression(this.scrollHeight < 300 ? "300px" : "auto");
padding-right: 65px;
.viewer-wrapper {
height: 415px;
width: 450px;
line-height: 415px;
position: relative;
.main {
position: relative;
display: block;
text-align: center;
height: 415px;
line-height: 415px;
width: 100%;
max-width: 450px;
max-height: 415px;
font-size: 0;
cursor: pointer;
&.small {
img {
display: inline-block;
max-height: 100%;
max-width: 100%;
height: auto;
width: auto;
vertical-align: middle;
}
}
}
}
HAML:
.lot-images
%div.viewer-wrapper
.facebook
%a.pinterest{ :href => '#', :target => '_pinterest' }
.main.small
%img
%div.gallery-pager.prev.hide
%i
%div.gallery-pager.next.hide
%i
.thumbnails
This whole container is inside a modal.
Let me know if you need more info. Thanks!
It's not sass, it's scss.
And your code works. Look my example: jsfiddle
Can you explain more what's the problem? And show your html

Resources