AAA
AAA

Reputation: 3168

How to control container height?

I have two containers. One with full content (container1) and then on the right I have a box which contains user info (container2):

When i add content to container and if its longer than the height, the content keeps going out of the container. So i decided to add height:auto; that however, will mess up the presentation of the container2. Is there something i can do to make sure they remain consistent?

Code

<div id="container">

<h1>$nameis</h1>

Here will be the content that will be longer and shorter on certain pages.

</div>
<div id="container2">
<div id="welcoming">
<input id="search">
<h3><?php echo $angelinajolie;?></h3>
<h3><a href="/lastlogin.php">Logins</a></h3>
<h3><a href="/stats.php">Stats</a></h3>
</div>
</div>

CSS

 div#container {
 background-color: #FFFFFF;
 margin:0 auto; 
 height: 300px;
 width:60%;
 font-family: "Lucida Grande", Verdana, "Lucida Sans Unicode", Arial, Helvetica, sans-serif;
 border:0px solid #FFFFFF;
 margin-top:25px;
 font-size:15px;
 color: #222222;
 margin-left:60px;
  }

 #container2 {
 margin:0 auto; 
 height: 80px;
 width:20%;
font-family: "Lucida Grande", Verdana, "Lucida Sans Unicode", Arial, Helvetica, sans-serif;
border:0px solid #FFFFFF;
margin-top:25px;
font-size:15px;
color: #E1E1E1;
margin-left: 70%;
 margin-top:-317px;
}

Upvotes: 0

Views: 3154

Answers (4)

SuperSpy
SuperSpy

Reputation: 1314

    height:200px;
    overflow-y:scroll;
    overflow-x:hidden;

Shows a scrollbar when the container overflows. (only in the height direction)

    height:200px;
    overflow:hidden;

Shows no scrollbars.

UPDATE: To crop the text with php:

if (strlen($text) > 200){
    $text= substr($text, 0, 200);
    $text= $text."...";
}else{
    $text;
} 

UPDATE2: This is an example CSS code I use. I'm not good with dynamic heights tough... So maybe somebody can make a better script.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website title</title>
<style>
body {
    position: fixed;
    text-align:center;
    width: 100%;
} 
#wrapper {
    text-align:left;
    margin-left:auto;
    margin-right:auto;
    width:980px;
    height:100%;
    position: relative;
}
#content {
    position:absolute;
    top: 40px;
    left:20px;
    width:712px;
    height:100%;
    overflow-y:scroll;
    overflow-x:hidden;
    padding-right:16px;
}
#sidebar{
    width: 196px;
    position:absolute;
    right:20px;
    top: 40px;
}

.sidebox{
    margin-bottom: 32px;
    position:relative;
}

</style>
<?php 
$username = 'Mr. AAA';
$angelinajolie = 'Angelina is cool';?>
</head>

<body>
<div id="wrapper">
    <div id="content">
        <h1>Welcome to the site <?php echo($username); ?></h1>
    </div>
    <div id="sidebar">
        <div class="sidebox">
            <h1>Search my site:</h1>
            <form action="/search.php">
            <input type="text" name="search" /><input type="submit" />
            </form>
        </div>
        <div class="sidebox">
            <h1>My account info</h1>
            <p>Username: <?php echo($username); ?></p>
            <p>Last login: <?php echo($lasttime); ?></p>
        </div>
    </div>
</div>
</body>
</html>

Upvotes: 4

CronosS
CronosS

Reputation: 3159

You could also use float property. In this example: http://jsfiddle.net/3hnc7/ the height of the container1 follow the height of the text, without disturb the container2.

Upvotes: 1

Konstantin
Konstantin

Reputation: 25389

Try to experiment with overflow property of your container, so you can add scrollbar to the container if content exceeds size of its box. See demo

Upvotes: 1

Abudayah
Abudayah

Reputation: 3875

make container height: 100% and overflow:hidden

Upvotes: 1

Related Questions