页面高度为视口的100%?

时间:2013-10-30 21:27:28

标签: css html5 css3 height

我首先要说的是,我对整个网络开发非常陌生,这是我的第一个响应式网站所以请温柔并牢记这一点,我是noob这个词的定义在这个阶段。寻找答案一段时间没有运气我希望有人能帮助我。

我正试图为这个网站制作一个主页。设计只是页面左侧的一个块,显示顶部的徽标,然后是下面的一系列链接,所有链接都在同一背景上。在右边是一个填满屏幕其余部分的大图像。我希望整个页面在浏览器窗口中填充它所查看的任何设备,因此绝对不需要滚动,即宽度和高度都是视口的100%。页面的宽度完全没有给我带来任何悲伤,可以根据需要随意调整到不同的屏幕尺寸,侧边栏宽度为20%,主图像为80%。

然而,高度是另一回事。在目前为止我尝试过的CSS的任何组合中,我无法看到能够达到100%视口的高度。侧边栏太短,主图像太长或两者都太长等等。主图像我想保持纵横比,只是让它溢出它的div,以保持大部分的显示和侧栏我只想适应页面高度的100%。这是我目前的代码:

<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
html
{
height: 100%;
margin: 0;
padding: 0;
}

body
{
height: 100%;
margin: 0;
padding: 0;
}

#page 
{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}

#sidebar
{
float: left;
width: 20%;
height: 100%;
padding-bottom: 10;
margin: 0;
background: url(/Images/bg.jpg);
}

#slideshow
{
float: right;
width: 80%;
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}

#logoimg
{
width: 80%;
margin-top: 10%;
margin-left: 10%;
margin-right: 10%;
}

#mainimg
{
width: 100%;
overflow: hidden;
}

.link
{
font-family: courier;
font-size: 1.3em;
text-align: center;
padding-top: 7%;
padding-bottom: 1%;
color: rgba(255,255,255,1.00); 
}

@font-face
{
font-family: courier;
src: url(/courier_new-webfont.ttf);
src: url(/courier_new-webfont.eot);
src: url(/courier_new-webfont.woff);
}

</style>
</head>

<body>
<div id="page"><!--Whole page container-->
<div id="sidebar"><!--Side bar container-->
    <div class="link" id="logo"><img id="logoimg" src="/Images/logo.png"></div>
    <div class="link" id="homelink">Home<!--Home link--></div>
    <div class="link" id="aboutlink">About<!--About link--></div>
    <div class="link" id="gallerylink">Gallery<!--Gallery link--></div>
    <div class="link" id="priceslink">Prices<!--Prices link--></div>
    <div class="link" id="reviewslink">Reviews<!--Reviews link--></div>
    <div class="link" id="contactlink">Contact<!--Contact link--></div>
    <div class="link" id="clientslink">Clients<!--Clients link--></div>
</div>
<div id="slideshow"><img id="mainimg" src="/Images/main.jpg"><!--Image slideshow container-->
</div>
</div>
</body>
</html>

任何对此的帮助都会非常感激,并且毫不犹豫地指出任何大规模的业余错误。我愿意接受任何批评并从中吸取教训。感谢

4 个答案:

答案 0 :(得分:43)

以下是HTML的简化代码示例:

<div id="welcome">
    your content on screen 1
</div>
<div id="projects">
    your content on screen 2
</div>

这是使用vh的CSS:

div#welcome {
    height: 100vh;
    background: black;
}

div#projects {
    height: 100vh;
    background: yellow;
}

从这里开始:http://stanhub.com/how-to-make-div-element-100-height-of-browser-window-using-css-only/

它对我有用。

答案 1 :(得分:19)

我已经为你做了一个基本的设置来展示你如何设计这个风格。我发现将高度设置为100%的最佳方法是使用jQuery / Javascript。您可以找到窗口的高度,然后使用它将其输入到css中。

这样做的方式是var wH = $(window).height();找到高度并将其转换为数字。然后当你使用$('.sideBar').css({height: wH});时,你将高度输入sideBar的css。

<强>的jQuery

function windowH() {
   var wH = $(window).height();

   $('.sideBar, .mainImg').css({height: wH});
}

windowH();

我写的这个函数给这两个元素提供了窗口的高度。这将允许这两个元素成为任何浏览器窗口的100%。

我还建议将nav转换为我在小提琴中包含的ul,以说明这是可能的。

JSFIDDLE(删除网址末尾的'show'以查看代码)

您需要研究的下一件事是media queries来调整内容以更好地适应移动设备。在移动设备上时,请考虑将sideBar更改为水平导航。

如果你想要纯粹的CSS方法,那么你可以做这样的事情,

html, body {
   height: 100%;
   width: 100%;
   margin: 0;
   padding: 0;
}

通过在html / body中将高度和宽度添加到100%,您可以在其他元素上使用height: 100%来填充整个页面。

请参阅此JSFIDDLE以查看其工作原理。

Helpful read about responsive web design

答案 2 :(得分:1)

在Chrome上,只需在display: flex上添加body就可以了。

在Firefox上,您必须添加height: 100%才能获得所需的结果。并且margin: 0将摆脱烦人的滚动条。

<body style="display:flex; height: 100%; margin: 0;">
  <div style="background-color: red; flex:1;"></div>
  <div style="background-color: green; flex:2;"></div>
  <div style="background-color: blue; flex:1;"></div>
</body>

答案 3 :(得分:0)

确切覆盖页面高度的示例代码。

HTML

<div class="container">  
  <div class="header">
    <h1>Header</h1>
  </div>
  <div class="content">
    Main content
  </div>
</div>

CSS

html, body {
 height: 100%;
 width: 100%;
 margin: 0;
 padding: 0;
}
.container {
 max-width: 1020px;
 margin: auto;
 height: 100%;
 background: #ddd;
 padding:16px;
 box-sizing:border-box
}
.header,.content{
 background:#fff;
 padding:16px
}
.content{
 margin-top:16px;
 min-height:calc(100% - 160px);
}

示例链接: https://codepen.io/rahdirs/pen/jeRVod

相关问题