【问题标题】:Have div maintain screen width and height让 div 保持屏幕宽度和高度
【发布时间】:2017-06-17 14:43:37
【问题描述】:

我希望创建一个网站,其中有一个主 div,其中宽度和高度始终是屏幕的大小。无论这个 div 中包含什么,它都必须包含这个宽度和高度。

如果您查看this 我想要的示例。

我的目标是将导航部分包含在左侧的蓝色大部分中,并将主页内容显示在白色框中,并且此白色 div 只能横向滚动。我已经搜索了几天试图让它工作,但总是出错。我尝试过的例子是:

How to make the main content div fill height of screen with css how to make a full screen div, and prevent size to be changed by content? How to make a div to fill a remaining horizontal space?

如果有人可以帮助解决这个问题,或者至少为我指明正确的方向以获得指导,那就太棒了。提前致谢。

【问题讨论】:

  • 欢迎来到 Stackoverflow。只是一个开发技巧:清楚地定义问题。 一个定义明确的问题已经解决了一半。你说“总有事情出错”。因此,请向我们展示您的代码的某个版本,并具体说明它有什么问题。

标签: javascript jquery html css responsive


【解决方案1】:

1。使用 CSS3 弹性框

/*QuickReset*/ *{margin:0; box-sizing:border-box;} html,body{height:100%;}

/*
OP: -I am looking to create a website in which it has a
main div in which both the width and the height is
always the size of the screen
A: -let this be body!
*/
body{
  background: #0ae;
  display: flex;
  flex-flow: row nowrap;
  padding: 40px;
}

/*
OP: -My aim is for the navigation section to be
contained in the large blue part on the left
A: -Thanks to CSS3 flexbox on body all you need is a desired menu width:
*/
aside{
  width: 140px;      /* or any size you want, px, %, ... */
}

/*
OP: -and for the main page content to be displayed within
the white box and for this white div to only scroll sideways
A: -add overflow:auto; to make it scroll and flex:1 to grow to available size
*/
article{
  background: #fff;
  overflow: auto;    /* make element scrollable */
  flex: 1;           /* let the browser grow this element */
}

/* just to demonstrate sideways scroll */
article{
  display: flex;
  flex-flow: row nowrap;
}
section{
  min-width:100%;
}
<aside>
  Menu
</aside>

<article>
  <section>"and I want this CONTENT area to scroll sideways"</section>
  <section style="background: #eee;">YEY</section>
</article>

2。 老派方式(兼容到 IE8)

/*QuickReset*/ *{margin:0;box-sizing:border-box;} html,body{height:100%;}


body{ /* your app */
  padding:40px;
  background: #0ae;
}

#aside{ /* your menu */
  float:left;
  width: 100px;
}
#content{ /* white content area */
  overflow: auto;
  margin-left: 100px;  /* menu is 100px remember? */
  height:100%;
  background: #fff;
}

/* Just to test horizontal scrollability */
#content{
  white-space:nowrap;
}
.page{
  white-space: normal;
  display: inline-block;
  vertical-align: top;
  width: 100%;
  height:100%;
  margin-right: -4px;
  /* Todo: make scroll vertically each page if needed */
}
<div id="aside">
  Menu
</div>

<div id="content">
  <div class="page">Content A</div>
  <div class="page" style="background:#eee;">Content B</div>
</div>

【讨论】:

  • “老派”? calc 是 CSS3。老派需要JavaScript。不过,我仍然喜欢这个答案!
  • @PHPglue wooooops :) 已编辑 :) 我急于创建 flexbox 示例,但计划删除那个 CSS3 calc!感谢您的关注!
  • 是的,这个答案应该可以!
  • 我一直在父元素上使用视口单位,ei VW,VH。然后在孩子中使用 flexbox 和百分比单位
  • 很抱歉这么晚才回复这个答案,但这正是我想要的,而且效果很好!谢谢你的回答
【解决方案2】:

按照您喜欢的方式设置主 div css 溢出属性后, 你总是可以用javascript(如给定的例子)——它会完全向后兼容。

<div id=[mainDivID]>
<script>
[maindDivID].style.width = screen.width +"px";
[maindDivID].style.height = screen.height +"px";
</script>
<!-- main div content -->
</div>

【讨论】:

  • 其实设置html和body高度为100%之后就不需要JS了
  • @RokoC.Buljan,哦,我知道我不知道 - 但无论文档窗口的其他大小如何,OP 确实需要内容始终扩展到屏幕大小body 可能会因用户操作而缩小。这是你不能用宽度 100% 做的事情。因为它是一个相对值。
  • 啊哈,我想我明白了。如果“白色”DIV 总是需要与实际视口完全相同的大小,那么可以!你是对的,虽然我最好忘记向后兼容性并使用100vh100vw,否则你的解决方案还需要一个在窗口调整大小时调用的函数......并且慢慢地你会变得不必要的复杂性。 ;)
  • @RokoC.Buljan 这就是你错的地方,专业人士永远不会忘记向后兼容性,并且总是会尝试使用最少的资产来获得最大的输出。再一次,OP 并不关心运行他的应用程序的窗口大小——他想要的是在任何给定时间将底层内容完全扩展到设备上可用的最大分辨率。百分比是不行的——每个人都知道这一点。
  • 我认为您没有很好地阅读 OP 的问题。如果有帮助,我会逐步编辑我的答案以包括 OP 的问题。在您阅读之后,我只能遗憾地得出结论: 1. 新手很难理解和实现您的代码。 2. 没有完成任务(按照 OP 的要求)。 3.您没有提供处理浏览器调整大小的解决方案。所以...感谢您提醒我有关专业人士的信息;)
猜你喜欢
  • 2012-11-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
相关资源
最近更新 更多