English 中文(简体)
css columns shrinking 100% pixel value
原标题:

I have a page which is divided up into 3 divs, left center and right. I don t want to display anything in the left and right, they just frame the page.

    #leftDiv
{
    background-color: Gray;
    width: 10%;
    left: 0px;
    top: 0px;
    position: absolute;
    height: 100%;
}

#rightDiv
{
    background-color: Gray;
    height: 100%;
    width: 10%;
    left: 90%;
    top: 0px;
    position: absolute;
    clear:both;
}

The center div has a table, which allows the user to select how many rows to see. If they chose a large value then the body of the table went beyond the bottom of the left and right div.

To correct this I put the following code in

if ($("#leftDiv").length == 1) {
                $("#leftDiv").height($("body").height() + "px");
            }
            if ($("#rightDiv").length == 1) {
                $("#rightDiv").height($("body").height() + "px"); ;
            }

this works fine until the user selects a smaller value than the page size, after selecting a larger value. Then the left and right divs get set to less than 100%. What i need is a way to find out what 100% is in pixels and then I can compare this to the height of the body and decide which is bigger.

Any ideas?

Thanks

John

问题回答

Use margin: 0 auto

Kill your left and right columns, give your main div a width, and then center that div using an auto left and right margin. For example:

#mainDiv {
  width: 80%;
  margin: 0 auto;
}

Why are you creating empty elements to frame the page? How about setting the body background to the colour you require and:

#center_div {width: /* whatever */;
             margin: 0 auto; /* to center in the viewport */
             overflow: auto; /* or visible */
}

You could leave off the overflow property, and simply use min-width in place of width (I can t remember how cross-browser compatible this is) to define the normal width, in such a way that the content will force the div to be larger as required to display the content.

If the left and right divs don t have any contents, then there s no need for them to be separate divs: apply their formatting to your container div instead, and center your contents div using margin: 0 auto. Obviously, you ll need to give the container div a specified width, and a non-transparent background. Then you can let the browser take care of resizing the window as needed - there s no need for you to reinvent the wheel for that part.

CSS:

#container {background-color:gray;}
#content {background-color:white;width:80%;margin:0 auto;}

Html:

...
<body>
<div id="container">
<div id="content">
...your content here...
</div>
</div>
</body>
...

(If your page doesn t have a container div, then you can apply the background color to the body element instead, and save even more code.)





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签