开发者

Flexible full screen layout with css

I'm creating a full screen (html, body {height: 100%}) web application and h开发者_如何学运维ave a screen which has a form in the top (approximately) half, and some other information with two buttons in the bottom (approximately) half.

What I'm wanting to do (being a touch screen in an industrial environment) is to make these buttons as big as possible. So they have height: 50% inside the bottom container.

The question is: how do I get the top half to take the height it requires, and the bottom to take the rest? i.e. is it possible with CSS (2.1 preferably, but 3 is good too)?


There's no way to make an element in CSS 2.1 to take up the rest of the space vertically. Block elements, like Div tags, will automatically stretch out to fill a space horizontally, but won't do it height-wise. This means that you can't get something, like a content page or your buttons, to stretch out to fill rest of the empty space.

The best way to achieve something like this is with tricks, or knowing exactly how high each element will be. For instance, if you know the exact percentage that the other elements will be, you can hard-code a percentage into your stylesheet as described, here. Another trick would be by making the bottom element fill the entire window, and hiding the top half with the form.

Tables, however, are the only elements which will stretch to fill a vertical space. That might be the only solution available to you. An example of this is shown below:

<form ...>
<table id="container">
  <tr><td id="top">Form elements go here</td></tr>
  <tr><td>Buttons go here</td></tr>
</table>
</form>

And the CSS:

#container {
  height: 100%;
  width: 100%;
}

#top {
  height: 200px; /* Replace this with the appropriate height, or remove altogether. */
}

.buttons {
  height: 100%; /* Used to stretch the buttons to fill the element. */
}


the HTML:

<div id="c">
    <div id="topHalf"></div>
    <div id="bottomHalf"></div>
</div>

the CSS:

#c {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
}
#topHalf, #bottomHalf {
    height: 50%;
    width: 100%;
    background: #00f;
}
#bottomHalf {
    background: #f00;
}

You can place your buttons inside the bottom half.


try something like this :-

<html style="height: 100%">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="height: 100%">
    <div id="top" style="background-color: #cccccc; height: 50%">form here</div>
    <div id="bottom" style="background-color: #eeeeee; height:50%">buttons here</div>
</body>

essentially height:100% just tells the div to be as big as its parent, and this carries on up the chain of parent objects. you'll notice that if you remove the height:100% on the html tag that all the inner children will just collapse up.

hth


EDIT: I just realised this is appropriate if using tables. If using a div then it's a little harder... a JavaScript function to manipulate the height of the bottom element using the style property in the element. Have a look at this previous question that may help with the JavaScript

ORIGINAL ANSWER

Try putting in the CSS for the bottom half of the application

min-height:50%;

Then specify no height in the top half section.

This will mean the bottom half with the buttons will be at least 50% of the screen area being able to become bigger as required and the bottom half will take the remaining section.

Personally I make this a little smaller than what I expect to use, e.g. instead of 50% I may use 30%, this means I'm getting the most out of my screen real estate but it may not be appropriate in your app.

I hope this helps ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜