简体   繁体   English

如何使div调整大小以填充整个浏览器窗口?

[英]How to make div resize to fill the whole browser window?

Please go here: http://demo.thecogworks.co.uk/umbraco/login.aspx 请转到此处: http : //demo.thecogworks.co.uk/umbraco/login.aspx

Login with this account: User: demo Password: password 使用该帐户登录:用户:demo密码:password

Now try to resize the browser window and see the divs resize with the browser window. 现在尝试调整浏览器窗口的大小,并查看浏览器窗口的divs大小。 I suppose it doesn't work with only css. 我想这只对CSS不起作用。 Am I correct? 我对么? How can I create something like that? 我如何创建类似的东西?

It does work with only css (and may be also with "viewport" meta tag if you want it to be shown properly on mobile devices.). 它仅适用于CSS(如果希望在移动设备上正确显示它,也可以与“ viewport”元标记一起使用)。

If you want your html page behave like that you have to use % as width(width:100% for example). 如果您希望html页面具有这种行为,则必须使用%作为宽度(例如,width:100%)。 In this case width of the div will be based (related to) on the size of the viewport. 在这种情况下,div的宽度将基于视口的大小(相关)。

It's so called flexible layout. 所谓的灵活布局。 And It's a one of the element on the way to create responsive web pages. 这是创建响应式网页的一种方式。

You can read more here : 你可以在这里阅读更多 :

You are correct, the page in your supplied example is not using plain css, but uses javascript to render the css on window resize. 没错,您提供的示例中的页面未使用纯CSS,而是使用JavaScript在窗口调整大小时呈现了CSS。

A line from your example (with inline styling in the html output): 您的示例中的一行(在html输出中带有内联样式):

<div id="treeWindow" class="panel" style="height:380px;width:200px;">

So all the measures in the example document styling are assigned with absolute units - in px, and do not adjust on browser window resize. 因此,示例文档样式中的所有度量均以px为单位分配了绝对单位,并且不会在浏览器窗口调整大小时进行调整。 So in terms to resize it uses something like 所以就调整大小而言,它使用类似

var clientWidth=jQuery(window).width();
var leftWidth=parseInt(clientWidth*0.25);

to get the new browser window width and similar for hight 获取新的浏览器窗口宽度和类似的高度

var clientHeight=jQuery(window).height()-48;
var treeHeight=parseInt(clientHeight-5);

and the function 和功能

function resizeGuiWindow(windowName,newWidth,newHeight,window){
   resizePanelTo(windowName,false,newWidth,newHeight);
}

is then applied to the treeWindow object 然后将其应用于treeWindow对象

resizeGuiWindow("treeWindow",leftWidth,treeHeight);

This you can find in the source code of your example page. 您可以在示例页面的源代码中找到此代码。 However, you can do that all just in css, by using relative units in the style. 但是,您可以通过使用样式中的相对单位,仅在CSS中完成所有操作。

Read more on responsive design, adaptive css and fluid layouts. 阅读更多有关响应式设计,自适应CSS和流畅布局的信息。

Edit: I posted the answer almost at the same time as Georgii. 编辑:我几乎在与Georgii相同的时间发布答案。 Even though he did not directly answer the question about your example page, all he says is right and he added some good sources on flexible layouts, where you can find all info to build a similar page only with css. 即使他没有直接回答关于您的示例页面的问题,但他说的都是正确的,他在灵活的布局上添加了一些很好的资源,您可以在其中找到所有信息以仅使用css构建类似的页面。

After resaerching on fluid layout, I finally be able to create the whole layout in CSS (no javascript). 在重新考虑流畅的布局之后,我终于可以在CSS中创建整个布局(没有javascript)。 Here's the working version in jsfiddle: http://jsfiddle.net/Yv4ja/ for anyone who might need it later: 这是jsfiddle中的工作版本: http : //jsfiddle.net/Yv4ja/,供以后可能需要的人使用:

CSS: CSS:

    .left{
    position:absolute;
    width:30%;
    background:red;
    left:0;
    bottom:0px;
    top:100px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
.right{
    overflow:hidden;
    background:green;
    position:absolute;
    right:0;
    width:70%;
    bottom:0px;
    top:100px;
}

html, body{
    min-height:100%;
    height:100%;
    padding:0px;
    margin:0px;
    position:relative;
}



.header{
    width:100%;
    height:100px;
    background:yellow;
}

.left .content {
    background:blue;
    position:absolute;
    top:5px;
    left:5px;
    right:5px;
    bottom:100px;
    overflow:auto;
    padding:5px;
    border:1px solid yellow;
}

.left .bottom-content {
    height:100px;
    position:absolute;
    left:5px;
    right:5px;
    bottom:5px;
    background:black;
}

HTML: HTML:

<div class="header">header</div>
<div class="wrapper">
    <div class="left">
        <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin aliquam adipiscing augue a porta. Sed fringilla sodales nisl ut iaculis. Aenean eu arcu lectus. Vivamus ut felis nibh, eu rutrum magna. In auctor varius ullamcorper. In diam turpis, ornare id pellentesque at, blandit et magna. Integer eget dolor tortor, sed vehicula magna. Quisque metus turpis, mollis porta feugiat vel, condimentum in eros. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec eu mi est, vel euismod velit. Vestibulum ante odio, molestie ut imperdiet eget, aliquet nec elit. Aenean arcu massa, vestibulum eu convallis ut, porttitor ut orci. Fusce eget sem augue, vitae placerat velit. Suspendisse interdum, nulla eget pharetra cursus, nunc elit iaculis nisl, vitae fringilla massa purus nec nunc. Quisque rutrum ornare turpis quis blandit. Mauris venenatis convallis elementum.

Etiam ut magna dolor. Sed eget purus ut nulla feugiat egestas. Quisque quam ligula, dapibus non cursus quis, commodo at quam. Vivamus porttitor, urna nec ultricies scelerisque, massa purus congue ligula, in egestas lectus magna eget est. Ut commodo viverra augue non semper. Suspendisse potenti. In mollis ultrices posuere. Integer mollis, lacus et pretium molestie, elit felis tincidunt elit, at pellentesque turpis diam lacinia quam.

Aenean vehicula, lorem et adipiscing molestie, orci nisl vulputate dui, et tempor erat nibh eget dolor. Nullam posuere lorem eu diam imperdiet et molestie arcu lobortis. Mauris vestibulum mollis lacus non imperdiet. Morbi tortor tortor, pulvinar nec viverra vitae, laoreet nec urna. Quisque in urna turpis. Nullam vel diam dui. Nunc scelerisque, arcu id pulvinar consectetur, purus elit euismod justo, in convallis lacus nisi vel velit.

Aliquam porttitor condimentum sapien at consectetur. Integer ac diam id felis semper pretium. Integer elementum venenatis nunc, ac molestie mauris suscipit sed. Nunc porttitor eleifend ipsum, sit amet ornare lacus dapibus a. Integer adipiscing, quam sed malesuada vehicula, eros erat egestas quam, eu rutrum sem turpis sit amet elit. Pellentesque metus nisl, malesuada et sagittis at, molestie a sapien. Duis vestibulum porta lacus et ultrices.

In auctor vulputate vestibulum. Integer porta quam ante, sed molestie leo. Aliquam cursus enim in augue euismod id porta eros gravida. Mauris lectus ipsum, luctus sit amet consectetur non, venenatis eget ligula. Suspendisse potenti. Vestibulum consequat accumsan orci ut lobortis. Maecenas velit diam, dapibus sit amet tincidunt eu, cursus in erat. Phasellus ac interdum lectus. Phasellus placerat, lacus vitae condimentum placerat, nisl dui varius ipsum, in eleifend metus diam sit amet libero. Vestibulum nec dolor at augue malesuada vestibulum. Praesent euismod, nibh a imperdiet convallis, est leo cursus elit, nec interdum ipsum mauris id urna. Integer at augue diam. Suspendisse tincidunt accumsan porta. Sed vehicula imperdiet velit et sagittis.</div>
        <div class="bottom-content"></div>
        </div>

    <div class="right">right</div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM