/    Sign up×
Community /Pin to ProfileBookmark

Cross-Browser Vertical Align Divs

I am having a heck of a time with an alignment issue. I am attempting to vertically center align items of a non-fixed height (images and text from a database). The number of items(e.g. columns) varies, depending upon the database. The text varies in length based on the database.

All I want to do is vertically align all the information within the div class hz. As alluded to, I cannot specify a height, for inevitably it will be wrong since it is database fed. Because the number of “columns” varies, I have everything float to allow for proper positioning. HELP – please!!!

[CODE]<HTML><HEAD><meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<STYLE TYPE=”text/css”>
#ctnr { width: 762px ; margin-left: auto ; margin-right: auto ; background-color:#FFFFCC; border:3px solid; color:#71371C; }
#qr { padding:15px; background-color:#FFFFCC;font-family: “Trebuchet”, Verdana, sans-serif;}
.h{text-decoration: none; font-size: 18px; color: #71371C; font-weight: bold; }
.qs{padding-top:16px;padding-bottom:8px;text-decoration: none; font-size: 16px; color: #71371C; font-weight: bold; clear:left;}
.hzsubq{text-decoration: none; font-size: 16px; color: #71371C; font-weight: normal; float:left;width:250px;}
.hz{margin-left:0px;text-decoration: none; font-size: 16px; color: #71371C; font-weight: normal; clear:left;}
.hz1,.hz2,.hz3,.hz4,.hz5,.hz6,.hz7,.hz8,.hz9,.hz10 {float:left;clear:none;text-align:center;}
.hz1{width:100px;}
.hz2{width:100px;}
.hz3{width:100px;}
.hz4{width:100px;font-size:14px;}
.hz5{width:85px;font-size:14px;}
.hz6{width:70px;font-size:12px;}
.hz7{width:60px;font-size:12px;}
.hz8{width:40px;font-size:12px;}
.hz9{width:30px;font-size:12px;}
.hz10{width:30px;font-size:12px;}
.fftxt{margin-left:150px;}
.btn {text-align:center;}
.btn input{font-size=16px;color: #71371C; border: 1px solid; background: #F0EBE7; margin: 3px; padding: 2px;}
input, textarea, select { color: #71371C; border: 1px solid; background: #F0EBE7; margin: 3px; padding: 2px; }
img {border-style:none;}
body{background-color: white;}</STYLE>
</HEAD><BODY onContextMenu=’return false’><FORM method=”POST” action=”q.asp” onSubmit=”return cpl(1);”>
<div id=”ctnr”><div id=”qr”>
<DIV class=qs>In general, how useful are the following</DIV>
<div class=hz><div class=hzsubq>&nbsp;</div><div class=hz5>Not at All Useful</div><div class=hz5>Not Very Useful</div><div class=hz5>Somewhat Useful</div><div class=hz5>Very Useful</div><div class=hz5>Do Not Recall</div></div>
<div class=hz><div class=hzsubq>your brain</div><div class=hz5><IMG border=”0″ name=”cxF14ax248″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14ax249″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14ax250″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14ax251″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14ax252″ src=”yelhorizup.gif”></div></div>
<div class=hz><div class=hzsubq>your extremities (this is a sample that includes some longer text to help emphsize the problem)</div><div class=hz5><IMG border=”0″ name=”cxF14bx248″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14bx249″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14bx250″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14bx251″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14bx252″ src=”yelhorizup.gif”></div></div>
<div class=hz><div class=hzsubq>your feet</div><div class=hz5><IMG border=”0″ name=”cxF14cx248″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14cx249″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14cx250″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14cx251″ src=”yelhorizup.gif”></div><div class=hz5><IMG border=”0″ name=”cxF14cx252″ src=”yelhorizup.gif”></div></div>

<div style=”clear:both;padding-top:25px;”></div><DIV class=btn><INPUT type=”submit” name = “Continue” value = “Continue”></DIV>
</div></div></FORM></BODY></HTML>
[/CODE]

to post a comment
CSS

17 Comments(s)

Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — I have run into a similar issue today, and have looked on the internet for answers. Experimenting with examples from the internet (which did not work with Internet Explorer), I have come up with this Firefox/MSIE compatible centering thing. I was really determined to do this without tables.

I do not know exactly how your code is built, and I do not feel like trying to figure it out at this moment, so I will post my example here so that you might be able to use portions of it to do what it is you are trying to do...

[code=html]<div Style="display:inline-block; position:relative; width:630px; height:630px; background:gray; text-align:center;">
<span style="display:inline-block; vertical-align:middle; height:100%"></span>

<span Style="display:inline-block; background:black; color:white; width:250px; vertical-align:middle; text-align:center;">
This is a test.
</span>
</div>[/code]
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 13.2009 — That works when you can define the height, but what about when a height cannot be specified?
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — Are you talking about centering it on the page? If so, set the height and width to 100%. If not, how is anything supposed to be vertically centered without first defining a height?
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 14.2009 — how is anything supposed to be vertically centered without first defining a height?[/QUOTE]
Relative to each other... just like the valign=middle with tables.
Copy linkTweet thisAlerts:
@TheTeenScripterJan 14.2009 — the code posted does work like a table-cell
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 14.2009 — Understood that it will work in the situation you mentioned. But the method you described will not work when aligning text and images with a float, without having a predetermined height for the container.

In the picture I posted above, you can see what I am attempting to align. Basically text and images, which have items aligned vertically and horizontally. It works, except that within rows, items need to be vertically centered.

Does that make more sense?
Copy linkTweet thisAlerts:
@drhowarddrfineJan 15.2009 — You will never get IE to attempt to perform like other more modern browsers without a proper doctype. Use this one on your first line:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

Then validate your html and css for any errors listed there.

Always use the more modern browsers, such as Firefox, as your reference for how things should work. Then see if IE manages to do the same. Never, ever use IE as a reference for anything.
Copy linkTweet thisAlerts:
@TheTeenScripterJan 15.2009 — Truthfully, I do not actually see anything that looks wrong with the attached picture or the end result from you code... The only thing that I could say is that it is not horizontally centered...
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 15.2009 — You will never get IE to attempt to perform like other more modern browsers without a proper doctype. Use this one on your first line:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">[/QUOTE]

Thanks. I actually have that line in there, but not in the one I posted. My mistake. I am designing with Firefox, then testing with Opera, Safari, Chrome, IE6 and IE7.
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 15.2009 — Truthfully, I do not actually see anything that looks wrong with the attached picture or the end result from you code... The only thing that I could say is that it is not horizontally centered...[/QUOTE]
See the line that says "your extremities (this is a sample that includes some longer text to help emphsize the problem)." Each are in independent divs, so the images align horizontally and vertically. The intent is to have the images to the right of the indicated text ("your extremities...") vertically centered with this text.

To demonstrated what is occuring, I added some borders. The hz5 class items are not taking the height of the parent (hz) and are not centering. I have used many tweaks to try to force this to occur. See the freshly attached code and image.
[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1252">
<style type="text/css">
#ctnr {
border: 3px solid ;
width: 762px;
margin-left: auto;
margin-right: auto;
background-color: #ffffcc;
color: #71371c;
}
#qr {
padding: 15px;
background-color: #ffffcc;
font-family: "Trebuchet",Verdana,sans-serif;
}
.qs {
padding-top: 16px;
padding-bottom: 8px;
text-decoration: none;
font-size: 16px;
color: #71371c;
font-weight: bold;
clear: left;
}
.h {
text-decoration: none;
font-size: 18px;
color: #71371c;
font-weight: bold;
}
.hz {
margin-left: 0px;
text-decoration: none;
font-size: 16px;
color: #71371c;
font-weight: normal;
clear: both;
overflow: auto;
width: 100%;
vertical-align:middle;
/*display:table-row;*/
/*border: 1px solid red;*/
}
.hzsubq {
text-decoration: none;
font-size: 16px;
color: #71371c;
font-weight: normal;
width: 250px;
vertical-align: middle;
height: 100%;
float:left;
border: 1px solid black;
overflow: auto;
/*display:table-cell;*/
}
.hz1, .hz2, .hz3, .hz4, .hz5, .hz6, .hz7, .hz8, .hz9, .hz10 {
clear: none;
text-align: center;
vertical-align: middle;
height: 100%;
float:left;
overflow: auto;
border: 1px solid black;
/*display:table-cell;*/
}
.hz1 {
width: 100px;
}
.hz2 {
width: 100px;
}
.hz3 {
width: 100px;
}
.hz4 {
width: 100px;
font-size: 14px;
}
.hz5 {
width: 85px;
font-size: 14px;
}
.hz6 {
width: 70px;
font-size: 12px;
}
.hz7 {
width: 60px;
font-size: 12px;
}
.hz8 {
width: 40px;
font-size: 12px;
}
.hz9 {
width: 30px;
font-size: 12px;
}
.hz10 {
width: 30px;
font-size: 12px;
}
.fftxt {
margin-left: 150px;
}
.btn {
text-align: center;
}
.btn input {
border: 1px solid ;
margin: 3px;
padding: 2px;
background: #f0ebe7 none repeat scroll 0%;
color: #71371c;
}
input, textarea, select {
border: 1px solid ;
margin: 3px;
padding: 2px;
background: #f0ebe7 none repeat scroll 0%;
color: #71371c;
}
img {
border-style: none;
}
body {
background-color: white;
}
</style>
<title></title>
</head>
<body>
<form method="post" action="q.asp"
onsubmit="return cpl(1);">
<div id="ctnr">
<div id="qr">
<div class="qs">In general, how useful are the following</div>
<div class="hz">
<div class="hzsubq"
style="border: 1px solid rgb(255, 255, 204);">&nbsp;</div>
<div class="hz5">Not at All Useful</div>
<div class="hz5">Not Very Useful</div>
<div class="hz5">Somewhat Useful</div>
<div class="hz5">Very Useful</div>
<div class="hz5">Do Not Recall</div>
</div>
<div class="hz">
<div class="hzsubq">your brain</div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14ax248" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14ax249" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14ax250" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14ax251" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14ax252" border="0"></div>
</div>
<div class="hz">
<div class="hzsubq">your extremities (this is a sample
that includes some longer text to help emphsize the problem)</div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14bx248" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14bx249" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14bx250" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14bx251" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14bx252" border="0"></div>
</div>
<div class="hz">
<div class="hzsubq">your feet</div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14cx248" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14cx249" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14cx250" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14cx251" border="0"></div>
<div class="hz5"><img src="yelhorizup.gif"
name="cxF14cx252" border="0"></div>
</div>
<div style="clear: both; padding-top: 25px;"></div>
<div class="btn"><input name="Continue"
value="Continue" type="submit"></div>
</div>
</div>
</form>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@TheTeenScripterJan 15.2009 — ohh ok i see now ill try and see if i can do something?
Copy linkTweet thisAlerts:
@TheTeenScripterJan 15.2009 — OK all you have to do is wrap each row with another div (minimum height or leave blank), making that row it's own block. Then, all you need to do is add the vertical-alignment code to each block containing an image, and set it's height to 100&#37; (the div's height gets set by the text and the height of the block that holds the image gets set by the height of the div).

Is that straight-forward enough for you or would you like me to make an example?
Copy linkTweet thisAlerts:
@TheTeenScripterJan 15.2009 — here is an example of the image inheriting the height of the text (i know it's not the image you want sized but this could just as easily be done with another <div>)..
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 16.2009 — OK all you have to do is wrap each row with another div (minimum height or leave blank), making that row it's own block. Then, all you need to do is add the vertical-alignment code to each block containing an image, and set it's height to 100% (the div's height gets set by the text and the height of the block that holds the image gets set by the height of the div).

Is that straight-forward enough for you or would you like me to make an example?[/QUOTE]

Isn't this what the div class=hz is doing? Shouldn't it be creating a row, exactly like you described?

I guess I need an example... Here is a snippet from the existing code that references my question above....
[CODE]
<div class=hz>
<div class=hzsubq>your extremities (this is a sample that includes some longer text to help emphsize the problem)</div>
<div class=hz5><IMG border="0" name="cxF14bx248" src="yelhorizup.gif"></div>
<div class=hz5><IMG border="0" name="cxF14bx249" src="yelhorizup.gif"></div>
<div class=hz5><IMG border="0" name="cxF14bx250" src="yelhorizup.gif"></div>
<div class=hz5><IMG border="0" name="cxF14bx251" src="yelhorizup.gif"></div>
<div class=hz5><IMG border="0" name="cxF14bx252" src="yelhorizup.gif"></div>
</div>
[/CODE]


THANKS again!!
Copy linkTweet thisAlerts:
@TheTeenScripterJan 16.2009 — No, it's supposed to be doing that but for 1, your float:left attribute is making the text go outside of it and 2, the images arent going in it for some reason.

set the border of the hz class and you will see what I am talking about

[CODE].hz{margin-left:0px;text-decoration: none; font-size: 16px; color: #71371C; font-weight: normal; clear:left; border-width:1px; border-style:solid; border-color:black; }[/CODE]

If you wrap each one in another div, it should fix that problem..
Copy linkTweet thisAlerts:
@junkyardmonkeyauthorJan 17.2009 — If you wrap each one in another div, it should fix that problem..[/QUOTE]

Okay, I wrappd the hz class div inside another div (with and id of "extra")for the second row which contains images. All of the elements within have a vertical-align:middle attribute (hz, hz5 and hzsubq) and.... nothing is different. I added a border to the extra div.

What am I not understanding? Also, if this were to fix it, wouldn't it fail for IE6 and IE7 given the lack of vertical-align compliance?
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type"
content="text/html; charset=windows-1252"&gt;
&lt;style type="text/css"&gt;
#ctnr {
border: 3px solid ;
width: 762px;
margin-left: auto;
margin-right: auto;
background-color: #ffffcc;
color: #71371c;
}
#qr {
padding: 15px;
background-color: #ffffcc;
font-family: "Trebuchet",Verdana,sans-serif;
}
.qs {
padding-top: 16px;
padding-bottom: 8px;
text-decoration: none;
font-size: 16px;
color: #71371c;
font-weight: bold;
clear: left;
}
.h {
text-decoration: none;
font-size: 18px;
color: #71371c;
font-weight: bold;
}
.hz {
/*border: 1px solid #999999;*/
margin-left: 0px;
text-decoration: none;
font-size: 16px;
color: #71371c;
font-weight: normal;
clear: left;
overflow: auto;
width: 100%;
vertical-align: middle;
}
.hzsubq {
text-decoration: none;
font-size: 16px;
color: #71371c;
font-weight: normal;
width: 250px;
vertical-align: middle;
height: 100%;
float: left;
overflow: auto;
}
.hz1, .hz2, .hz3, .hz4, .hz5, .hz6, .hz7, .hz8, .hz9, .hz10 {
clear: none;
text-align: center;
vertical-align: middle;
height: 100%;
float: left;
overflow: auto;
}
.hz1 {
width: 100px;
}
.hz2 {
width: 100px;
}
.hz3 {
width: 100px;
}
.hz4 {
width: 100px;
font-size: 14px;
}
.hz5 {
width: 85px;
font-size: 14px;
}
.hz6 {
width: 70px;
font-size: 12px;
}
.hz7 {
width: 60px;
font-size: 12px;
}
.hz8 {
width: 40px;
font-size: 12px;
}
.hz9 {
width: 30px;
font-size: 12px;
}
.hz10 {
width: 30px;
font-size: 12px;
}
.fftxt {
margin-left: 150px;
}
.btn {
text-align: center;
}
.btn input {
border: 1px solid ;
margin: 3px;
padding: 2px;
background: #f0ebe7 none repeat scroll 0% 50%;
color: #71371c;
}
input, textarea, select {
border: 1px solid ;
margin: 3px;
padding: 2px;
background: #f0ebe7 none repeat scroll 0% 50%;
color: #71371c;
}
img {
border-style: none;
}
body {
background-color: white;
}
&lt;/style&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" action="q.asp"
onsubmit="return cpl(1);"&gt;
&lt;div id="ctnr"&gt;
&lt;div id="qr"&gt;
&lt;div class="qs"&gt;In general, how useful are the following&lt;/div&gt;
&lt;div class="hz"&gt;
&lt;div class="hzsubq"&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class="hz5"&gt;Not at All Useful&lt;/div&gt;
&lt;div class="hz5"&gt;Not Very Useful&lt;/div&gt;
&lt;div class="hz5"&gt;Somewhat Useful&lt;/div&gt;
&lt;div class="hz5"&gt;Very Useful&lt;/div&gt;
&lt;div class="hz5"&gt;Do Not Recall&lt;/div&gt;
&lt;/div&gt;
&lt;div class="hz"&gt;
&lt;div class="hzsubq"&gt;your brain&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14ax248" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14ax249" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14ax250" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14ax251" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14ax252" border="0"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="extra" style="border: 1px solid black;"&gt;
&lt;div class="hz"&gt;
&lt;div class="hzsubq"&gt;your extremities (this is a sample
that includes some longer text to help emphsize the problem)&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14bx248" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14bx249" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14bx250" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14bx251" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14bx252" border="0"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="hz"&gt;
&lt;div class="hzsubq"&gt;your feet&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14cx248" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14cx249" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14cx250" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14cx251" border="0"&gt;&lt;/div&gt;
&lt;div class="hz5"&gt;&lt;img src="yelhorizup.gif"
name="cxF14cx252" border="0"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both; padding-top: 25px;"&gt;&lt;/div&gt;
&lt;div class="btn"&gt;&lt;input name="Continue"
value="Continue" type="submit"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@TheTeenScripterJan 18.2009 — I think you may just be better off using tables in this situation.. Any fix to this would dirty up the code way too much and would probably not even work in very many browsers.
×

Success!

Help @junkyardmonkey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...