Development CSS Height to expand in relation to content

Discussion in 'Software' started by Draxin, 13 Oct 2008.

  1. Draxin

    Draxin Seeker of Photons

    Joined:
    29 Nov 2001
    Posts:
    965
    Likes Received:
    5
    im still learning CSS so forgive me if this is a very basic question

    i have the following CSS
    Code:
    
    #comment {
    	width: 99%;
    	height: auto;
    	position: relative;
    	border: 1px solid #99FFCC;
    	clear: both;
    	text-align: center;
    	overflow: visible
    }
    
    #com_icon {
    	position: absolute;
    	top: 5px;
    	left: 10px;
    	height: 75px;
    	width: 75px;
    	vertical-align: middle;
    	float: left;
    }
    
    #com_title {
    	position: absolute;
    	top: 5px;
    	width: 100%;
    	height: 10px;
    	font-weight: bold;
    	text-decoration: underline;
    	text-align: center;
    	float: right;
    }
    
    #com_what {
    	height: auto;
    	position: absolute;
    	top: 25px;
    	left: 95px;
    	float: left;
    	text-align: justify;
    }
    
    used for comments.

    the #com_what ID holds the comment text.
    I set height of ID comment to auto in hopes that the container would stretch to be the same size as the text content filling it, but it doesnt. it just spills over.
    iv tried using min-height and max-height and still it isnt stretching.

    Im obviously missing something basic but havent had any luck figuring it out.

    id appreciate it if someone could educate me in this
     
  2. NiHiLiST

    NiHiLiST New-born car whore

    Joined:
    18 Aug 2001
    Posts:
    3,987
    Likes Received:
    6
    Absolutely positioned elements are outside of the flow of the document. Basically that means their height isn't taken into account when positioning other elements, so the browser sees the contents of the #comment element as having a height of 0, regardless of what's in its three children.

    Is there any reason why you need to use absolute positioning for the child elements?
     
  3. badders

    badders Neuken in de Keuken

    Joined:
    4 Dec 2007
    Posts:
    2,635
    Likes Received:
    72
    Would adding
    Code:
    overflow:visible;
    to the #com_what CSS do it?
     
  4. NiHiLiST

    NiHiLiST New-born car whore

    Joined:
    18 Aug 2001
    Posts:
    3,987
    Likes Received:
    6
    No :) Overflow is just that, overflowing outside of the containing box, and as such not affecting its dimensions or layout.
     
  5. Draxin

    Draxin Seeker of Photons

    Joined:
    29 Nov 2001
    Posts:
    965
    Likes Received:
    5
    so if i changed #com_what to relative would that work?

    well i tried that and it kind of worked, it pushed the other elements below it down but the border didnt stretch and the #comments below it over lap some

    because if i dont they position wrong
     
    Last edited: 13 Oct 2008
  6. Draxin

    Draxin Seeker of Photons

    Joined:
    29 Nov 2001
    Posts:
    965
    Likes Received:
    5
    ok heres what i want

    Code:
    -------------------------------------------------------------------------
    | ------------               #com_title                                 |
    | | ICON     |  ------------------------------------------------------  |
    | |          |  |                                                    |  |
    | ------------  |           #com_what                                |  | <- #comment
    |               |                                                    |  |
    |               |                                                    |  |
    |               |                                                    |  |
    |               ------------------------------------------------------  |
    -------------------------------------------------------------------------
    
    i want #comment to stretch vertically with #com_what

    #com_what is wraping around ICON (#com_icon) if i use relative positioning

    I want to get the hang of CSS but stuff like this make me remember why i like tables, design once works across all browsers, does what you tell it, BUT unmaintainable, unflexable
     
  7. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    Here's a sample, should get you in the right direction:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    		<title>ab</title>
    		<style type="text/css" media="screen">
    			.comment {
    			width: 500px;
    			border: 1px solid #000;
    			}
    			.comment .com_title {
    			display: block;
    			width: 100%;
    			font-size: x-large;
    			text-align:center;
    			}
    			.comment .com_icon {
    				display:block;
    				float:left;
    			width: 50px;
    			height: 50px;
    			border:1px solid #000;
    			}
    			.comment .com_what {
    				display:block;
    				float:left;
    				width:200px;
    			background-color: lightyellow;
    			}
    			div.clear {
    				clear:both;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="comment">
    		<div class="com_title">some title</div>
    		<div class="com_icon">icon would go here</div>
    		<div class="com_what">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    		<div class="clear"></div>
    		</div><!--.comment-->
    		
    	</body>
    </html>
    
    Positioning always works out a little funky unless you're... well, lucky, usually, or have an unusually good idea of what you're doing. I avoid it whenever possible, though it's the only way to do some things (thickboxes, mostly).

    The above floats your icon and text divs to the left within the comment box, so they line up inline. You need the clearing div underneath them so that there's something that sets the height property of the comment wrapper div. Unfortunately, it's damn near impossible to get working reliably with fluid-width setups, only because percent-based systems just fail abysmally for images. Readability and consistency aside, it's probably a good part of why so many sites are fixed-width. Too bad you can't do width: 100% - 75px; or something (at least w/out javascript, which should stay the hell away from layout except in very, very rare circumstances).

    Styles and widths and whatnot in there are just to make it a bit easier to see what's going on, obviously you can change them to whatever you need. Only really important stuff is that .com_icon and .com_what have float:left;, their combined width (using the box model, so border+padding+margin) is less than the width of .comment, and that the div.clear is present.
     
    Last edited: 13 Oct 2008
  8. koola

    koola Minimodder

    Joined:
    11 Jul 2004
    Posts:
    2,401
    Likes Received:
    10
    My attempt.

    Code:
    <html>
    <head>
    <style>
    .comment
    {
    	width: 650px;
    	background-color: #000;
    }
    .wrap_left
    {
    	float: left;
    	width: auto;
    	background-color: #DDD;
    }
    
    .wrap_right
    {
    	float: left;
    	width: 500px;
    	background-color: #DDD;
    }
    
    .com_icon
    {
    	width: 70px;
    	height: 70px;
    	padding: 5px;
    	background-color: #999;
    }
    .com_title
    {
    	width: 100%;
    	padding: 5px;
    	background-color: #999;
    }
    .com_what
    {
    	width: 100%;
    	padding: 5px;
    	background-color: #DDD;
    }
    
    .clear
    {
    	clear:both;
    }
    </style>
    </head>
    <body>
    	
    <div class="comment">
    	<div class="wrap_left">
    		<div class="com_icon">icon</div>
    	</div>
    	<div class="wrap_right">
    		<div class="com_title">Comment title</div>
    		<div class="com_what">
    			<p>
    				Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin sed erat. Duis pulvinar.
    				Ut tincidunt turpis sed risus. Sed nec velit in risus pellentesque mollis. Nulla facilisi.
    				Nam eu ligula. Mauris aliquet arcu nec velit. Etiam erat. Proin dictum, ligula ut gravida convallis,
    				velit lacus vehicula diam, sit amet hendrerit odio sem sed nisl. Nunc tincidunt, mauris at iaculis laoreet,
    				ipsum nibh rutrum erat, eu mollis diam sapien et eros. In porta tellus congue neque.
    			</p>
    		</div>
    	</div>
    	<div class="clear"></div>
    </div>
    <br />
    <div class="comment">
    	<div class="wrap_left">
    		<div class="com_icon">icon</div>
    	</div>
    	<div class="wrap_right">
    		<div class="com_title">Comment title</div>
    		<div class="com_what">
    			<p>
    				Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin sed erat. Duis pulvinar.
    				Ut tincidunt turpis sed risus. Sed nec velit in risus pellentesque mollis. Nulla facilisi.
    				Nam eu ligula. Mauris aliquet arcu nec velit. Etiam erat. Proin dictum, ligula ut gravida convallis,
    				velit lacus vehicula diam, sit amet hendrerit odio sem sed nisl. Nunc tincidunt, mauris at iaculis laoreet,
    				ipsum nibh rutrum erat, eu mollis diam sapien et eros. In porta tellus congue neque.
    			</p>
    		</div>
    	</div>
    	<div class="clear"></div>
    </div>
    </body>
    </html>
    
    The background colours are there for debug purposes and can be removed.
     
    Last edited: 13 Oct 2008
  9. Draxin

    Draxin Seeker of Photons

    Joined:
    29 Nov 2001
    Posts:
    965
    Likes Received:
    5
    firehed your my new hero.

    this has been driving me up the wall.

    thank you
     
  10. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    Glad to be of service.

    That'll be $200 ;)
     
  11. Draxin

    Draxin Seeker of Photons

    Joined:
    29 Nov 2001
    Posts:
    965
    Likes Received:
    5
    well if you put it that way, ill send you some PSDs and pay you $200 each to make them cross browser CSS just so i dont have to do them

    payment on browsershots verification across all A-class browsers (IE 5.5+, safari, firefox, opera)
     
  12. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    Depends how complex the PSDs are, but if I had a copy of IE5.5 I'd probably take you up on that. PM me or email eric@ the domain noted in my sig if you want to try working something out.

    edit - the first domain. forgot I link out to two lol
     
  13. NiHiLiST

    NiHiLiST New-born car whore

    Joined:
    18 Aug 2001
    Posts:
    3,987
    Likes Received:
    6
    IE 5.5... A-class? :hehe:

    Is there a specific reason you need your sites to support IE 5.5? It has a negligible market share, hasn't been supported by Microsoft since the end of 2005 and is an absolute pig to get well-built sites working in. Just in case that comes across the wrong way, I'm not being confrontational, just curious :)
     
  14. Draxin

    Draxin Seeker of Photons

    Joined:
    29 Nov 2001
    Posts:
    965
    Likes Received:
    5
    ok 5.5 was just included because people still use it, scrap that, ie 6 - 8
     

Share This Page