make div fill the rest of the browser viewport

眉间皱痕 提交于 2019-12-08 04:51:07

问题


so i have a normal 960px layout, i want to add a div that can use 300px inside that 960 and the rest of the space (browser width) i want to fill with that div...but always stay on the same position inside the wrapper (im not talking about a vertical fixed element)

can i do this without using javascript? but if theres isnt another option its ok

Example:

<div class="wrapper">
    <div class="fill">Something</div>
</div>

CSS:

.wrapper {
    margin: 0 auto;
    width: 960px;
    background: #ddd;
}
.fill {
    margin: 300px 0 0 0;
    width: 300px;
    background: red;    
}

thank you


回答1:


If I well understood what you need: try this fiddle

http://jsfiddle.net/jGmcV/show/
http://jsfiddle.net/jGmcV/ (source)

this should be the effect, but I placed an extra wrapper around your wrapper. The width of red box inside the dark container is always of 300px no matter what the size of the viewport is.




回答2:


You can use margin-left:-10%; and margin-right:-10%; If i understood correctly.

or you can

.wrapper {
    margin: 0 auto;
    width: 960px;
    background: #ddd;
    position:relative;
}
.fill {
    margin: 300px 0 0 0;
    width: 300px;
    background: red;    
    position:absolute;
    top:0px;
    left:-10%;
}


来源:https://stackoverflow.com/questions/9034169/make-div-fill-the-rest-of-the-browser-viewport

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!