vertically centering a input element using padding - html

hey guys a few days back , i was having a small difficulty centering a input element , and somebody proposed the following solution(FIDDLE HERE)
The CSS is pritty straightforward and standard CSS , have a look ::
header {
position: relative;
}
header img {
display: block;
max-width: 100%;
}
.location-search-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.input-group {
margin-top: -17px;
top: 50%;
position: absolute;
}
now this totally acheives what i want to achieve , but the same guy , proposed another unusual solution , using padding , i really liked the padding solution as it was less hassle , but i could't quite understand or grasp what was happening , here is the solution with padding ::
header {
position: relative;
}
header img {
display: block;
max-width: 100%;
}
.location-search-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding-top: 30%;
}
.input-group {
margin-top: -17px;
}
FIDDLE HERE
Notice how padding-top:30% is doing the trick. I would really like to understand how this is working , can somebody give me the basic idea ? this is a much more cleaner and elegant solution i feel . When i asked the same guy how exactly the solution works , he said the following :
As I said, top padding uses the parent's width as a basis. Depending
on aspect ratio, it must be lager or smaller than half the parent
height .
but once again i could't quite understand. can somebody simplify this .
Thank you.
Alex-Z.

The native size of the image is 1200 x 700 pixels. It is resized to fit the window width. For example, suppose that the window is 1000 pixels wide. In that case the height of the image will be 700 x 1000 / 1200, or 583 pixels. In all cases, the ratio of height to width will be 0.583, or, roughly, 60%.
The rule padding:top: 30% tells the browser to add top padding equal to 30% of the container's width. (Padding properties are unusual in that the percentage is always taken as a percentage of width, even though, as in the case of padding-top, it may effect the vertical position and sizing.)
30% is half of (roughly) 60%. Voila!

It is just two variations of the same solution. Absolute position an element and push it a percentage down the page. The cleaner, second one uses a % that removes the need to offset. The original pushed further than needed then pull back a little.
Another approach is to use transform: translateY(-50%); although a little restrictive on the cross browser side of things. There are loads of ways to achieve things in CSS. Some more 'valid' than others.
http://davidwalsh.name/css-vertical-center

Browsers use parents width when calculating percentage for margin values. It seems a bit confusing.
But I think that it is logically correct, because margins are assumed to be used to separate one element from others, and you mostly want this separations to be equal on all sides.
For alignment you can set you elements top, left, bottom, right properties and they should be of position: absolute;.
This properties use non position: static; parents height and width accordingly when used with percentages
In your situation you can use top: 50%; for .location-search-container and assing negative margin-top:-17px; to compensate elements origin to center. Or alternatively set transform: translateY(-50%). This way you don't need to know size of your elements in pixels, and if you are not supporting older browsers.
Here is the [FIDLE][1] example that centers your element veritcally and horizontally. You can change transform property to margin, but you should set half of your elements width and height in pixels

Related

Dealing with multiple images of differing height/width

I have been sent numerous logos of numerous heights and widths (some very tall, others wide with minimal height) that need displaying in table/grid.
Is there a way of setting them all a certain height/width without causing distortion or the images to be chopped off.
Hopefully looking at a CSS solution but even a piece of software that may help.
As you want CSS solution so this may be helpful as it will make your images of same height and width.
<div id="logo"><img src="image.jpg"></div>
#logo { position: relative; height: 100px; width: 200px; }
#logo img { position: absolute; left: 0; top: 0; }
This way you will be showing same images from top and left portion of your image.
This code is taken from another solution, here is full URL to that solution [How to set an image's width and height without stretching it?
[1]: How to set an image's width and height without stretching it? Hope that works for you

CSS Class will not gain height

I have tried multiple options and narrowed it down to a specific class but I cannot seem to figure it out.
Im trying to get "#content-wrapper" to increase height but it seems to be stuck at the browsers height.
This has been pissing me off for the past 3 hours.
#content-wrapper {
width: 100%;
max-width: 1000px;
height: 100%;
min-height: 100%;
margin: auto;
position: relative;
}
JSFiddle Link: http://jsfiddle.net/8hn7uLqr/
If anyone can help me please I will be very grateful, I hope everyone is having a good Christmas Eve!
Delete height:100% in the css for the #wrapper and #content-wrapper. It this case it means the height of your browser without scrolling. Give the height a number of pixels or let the height depend on the content within with height:auto.
So if I understand you correctly, you're not sure why your content shows a height of 703px when it's actually in the 9000's. There are 2 issues here.
is the use of height: 100%. Many people think that means 100% of the content but it's actually 100% of the current screen size unless you set html to be height: 100% as well. You can use min-height: 100% which will allow it to extend past the screen height.
The biggest issue you are having is because of the constant use of position: absolute. I'm not sure if you are aware but using absolute positioning removes the element from the flow of the document. Meaning it no longer adheres to it's parents constrains.
After I removed the multiple position: absolute and the height: 100% you can see form the following screenshot the height is adjusted to the content:
So really you need alot of CSS overhaul to set this up without absolute: position

Define table width & height in CSS, so it has a specific width/height ratio when printed

I have a table on my webpage, that uses a specific print CSS, that removes all the web site elements so that it can be printed on posters.
What I do is:
Visit the page with Google Chrome,
Click: File -> Print -> "Print using system dialog...",
Click PDF -> "Save as PostScript", and
Process with Adobe Distiller (150 dpi & 18” x 24”).
Since I want my table to fill the 18"x24" (18/24 = 3/4) poster nicely, how can I make sure that its width/height aspect ration is 3/4, without putting in specific dimensions that would come to bite me in print?
For now, the only solution that came to my head is the one where you'd have to wrap the table with two additional divs.
Here's the fiddle
As we speak, I'm trying to improve the solution to get rid of the divs.
At the moment I'm using one div as a wrapper with width set to 100%, then inside of it there is another div (innerWrapper) which is positioned absolutely and spread to the wrapper's dimensions. To set the div's height I used a simple trick - added a pseudo :after element to the wrapper which has padding-bottom set to 133%. Padding percentage values are inherited from parent's width (yes!). In case of somebody asking - we can't ommit the innerWrapper div and set its style directly to table becuase top, right, bottom, left method won't work for it (neither width and height set to 100%). Divs are block elements, tables have display: table.
Whole code looks like this:
HTML:
<div id="wrapper">
<div id="innerWrapper">
<table></table>
</div>
</div>
CSS:
#wrapper {
width:100%;
position: relative;
}
#wrapper:after {
content: "";
display: block;
padding-bottom: 133%;
}
#innerWrapper {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
table {
width: 100%;
height: 100%;
}

DIV alignment problem

This is the website I'm having problems with: http://bgflirt.com
I need the menu on the left to have a fixed width and the part with the user pictures should resize when the browser window is resized (width in percent). However, as you can see - the part where the content is refuses to align on the right of the menu, but is instead displayed below it. Can someone help me with this ?
For #content_wrap remove width:100% and float:left. This will make box to stretch to fill all available horizontal space.
You'll need to also clear floats in whatever way you prefer. E.g., add overflow: hidden; to #content_wrap.
This works for me in firebug.
BTW, since you use fixed-width graphics for header and footer (frame with those nice rounded corners), you can't really stretch them.
Try using something like this for your CSS:
.container {
position: relative;
}
.sidebar_wrap {
position: absolute;
top: 0;
left: 0;
width: 130px;
}
.content_wrap {
margin-left: 130px;
}
I believe that is much easier to work with than a float.
A couple of things.
First, get rid of the xhtml doctype and instead start using an html 4.01 strict doctype. xhtml, besides being on it's way out, has inconsistent rendering across a lot of browsers.
Second, this is MUCH easier to accomplish with a table. Just set the width of the table to 100% and the width of the first column to 130px. The layout engine will take care of sizing the other side. Incidentally, this will solve some of the other issues you're going to run into such as making both sides have the same height.
your #content_wrap div has a 100% width, like so it's impossible for it to float left when theres a menu with a 130px width...
You should make the menu's width in % if you really want to make the site resizable... something like
#sidebar_wrap{
width: 15%;
float: left;
}
#content_wrap{
width: 85%;
float: left;
}
note that the sum of the width can't be bigger than 100%, and you should take paddings and borders in consideration.

What is the cleanest way to center a div absolutely (H+V) inside a browser window?

I need to build a webpage of just a humble content (a logo and some text) to be show in the center of a browser window. How do I best achieve this effect targeting today web browsers?
Here's a very simple solution. I take no credit for this:
http://www.infinitywebdesign.com/research/cssverticalcentereddiv.htm
Works very well for a simple body > div situation like yours.
Your other option, although I fear the repercussions from some table-haters around here, is you could wrap the div in a table, and using the vertical-align:middle property on it to take care of the vertical align without javascript. Then use margin: 0 auto; on the div to handle the horizontal center.
If you know the dimensions of the element (do the math for the values):
position: absolute;
left: 50%;
top: 50%;
width: ($WIDTH)px;
height: ($HEIGHT)px;
margin: (-$HEIGHT/2)px 0 0 (-$WIDTH/2)px;
What it does it positions the top left corner of the element at the center of the viewport and then uses negative margins to inch the center point to the center of the viewport.
you can put your content inside a div and provide height & width to this div then apply this simple css like u applied width as 300px & height as 300px::
div {
position: absolute;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
}
then it'll always be in center and make sure u used a valid DOCTYPE.