简体   繁体   English

Div float保持固定并且视口的高度为100%

[英]Div float keep fixed and 100% height of the viewport

I am trying to make a basic responsive structure for a website with CSS. 我正在尝试为CSS网站制作一个基本的响应结构。 So far I have managed to make three column divs, a menu, a sidebar and one for content. 到目前为止,我已经设法制作了三个列div,一个菜单,一个侧边栏和一个内容。

What I would like to achieve now is to have the menu and the sidebar to be 100% of the viewport height and fixed so that the content div is "scrollable" but the menu and the sidebar stays on top no matter how much content there is in the col content column. 我现在想要实现的是将菜单和侧边栏设置为视口高度的100%并固定,以便内容div是“可滚动的”但是无论有多少内容,菜单和侧边栏都保持在顶部在col content列中。 Naturally, I do not want this to happen in the media query though. 当然,我不希望在媒体查询中发生这种情况。

How can I achieve this most efficiently with CSS. 如何使用CSS最有效地实现这一目标。 Do I have to restructure the divs in HTML or is there any way to achieve this with CSS? 我是否必须重新构建HTML中的div或者有没有办法用CSS实现这一点?

 /* SECTIONS */ .section { clear: both; } /* COLUMN SETUP */ .col { display: block; float: left; } /* GRID OF THREE */ .menu { width: 33%; background-color: #98D2ED } .sidebar { width: 33%; background-color: #D3ADAD } .content { width: 33%; background-color: #C9E4D1 } /* GO FULL WIDTH BELOW 480 PIXELS */ @media only screen and (max-width: 480px) { .menu { width: 100%; } .sidebar { width: 100%; } .content { width: 100%; } } 
 <div class="section"> <div class="col menu"> <p> Menu </p> I want this cloumn to be fixed and full height of the viewport when the screen size is above 480px. </div> <div class="col sidebar"> <p> Sidebar </p> I want this cloumn to be fixed and full height of the viewport when the screen size is above 480px. </div> <div class="col content"> Content </div> </div> 

What I am trying to achieve: 我想要实现的目标: 在此输入图像描述

在此输入图像描述

You can use flexbox, either for known/unknown width and height elements, The key is to set the content area to overflow:auto , and switch the flex-direction to column in media queries. 您可以使用flexbox,对于已知/未知的宽度和高度元素,关键是将内容区域设置为overflow:auto ,并将flex-direction切换到媒体查询中的column

jsFiddle 的jsfiddle

 html, body { height: 100%; } body { margin: 0; display: flex; } .content { flex: 1; overflow: auto; } .menu { background: grey; } .sidebar { background: silver; } @media (max-width: 480px) { body { flex-direction: column; } } 
 <div class="menu">Menu</div> <div class="sidebar">Sidebar</div> <div class="content"> <!-- scroll test --> <div style="height:1000px;">Content</div> </div> 

Or, the traditional way to set the menu and sidebar to position:fixed . 或者,将菜单和侧边栏设置为position:fixed的传统方式position:fixed

jsFiddle 的jsfiddle

 html, body { height: 100%; margin: 0; } body { margin-left: 200px; } .menu, .sidebar { position: fixed; top: 0; height: 100%; overflow: auto; } .menu { left: 0; width: 100px; background: grey; } .sidebar { left: 100px; width: 100px; background: silver; } .content { overflow: auto; } @media (max-width: 480px) { body { margin: 100px 0 0; overflow: hidden; } .menu, .sidebar { left: 0; width: 100%; height: 50px; } .sidebar { top: 50px; } .content { height: calc(100% - 100px); } } 
 <div class="menu">Menu</div> <div class="sidebar">Sidebar</div> <div class="content"> <!-- scroll test --> <div style="height:1000px;">Content</div> </div> 

As I understand, you want your .menu and .sidebar to be stuck to the screen in one place and have the content be scrollable. 据我所知,你希望你的.menu.sidebar在一个地方粘在屏幕上,让内容可以滚动。 And add some more code to other things as well, I know that sounds vague, but it would be a waste of time to write everything down, as I have edited you I finished copy, and I have notes that explain all my changes (and the reasons for doing so) in the code below. 并且还为其他东西添加了更多的代码,我知道这听起来很模糊,但写下所有东西都是浪费时间,因为我已经编辑了你我完成了复制,并且我有笔记解释了我所有的变化(和这样做的原因)在下面的代码中。

I removed the float s and their class es, as I believe those are not necessary, and that the float s do more harm than good. 我删除了float和它们的class ,因为我认为那些不是必需的,并且float的弊大于利。 As well as moved the .content to be in the middle column (between .menu and .sidebar ). 同时将.content移动到中间列( .menu.sidebar之间)。 However, if you need to, feel free to change any or al of these things back. 但是,如果您需要,请随时更改这些内容中的任何一个。


Here's the updated code: (and here's a JSFiddle: JSFiddle ) 这是更新的代码:(这里是一个JSFiddle: JSFiddle

I know that .menu has a weird space above it (when running the snippet and the JSFiddle), but I have it live on my website here , and it behaves perfectly fine, and uses the same code. 我知道.menu上方设有怪异空间(运行段和的jsfiddle时),但我有它住在我的网站在这里 ,和它的行为完全正常,并使用相同的代码。

 * { margin: 0px; /* Added to remove margin from everything */ padding: 0px; /* Added to remove margin from everything */ } .section, .menu, .sidebar, .content { display:inline-block !important; /* Added so they will line up next to each other */ } .section {width:100%;} /* Pretty self explanatory, added to set ".section" to a width of 100% */ /* GRID OF THREE */ .menu { width: 33%; /* Was already here */ background-color: #98D2ED; /* Was already here */ height:100vh; /* Makes it be 100% of the Viewport Height, or 100% of the browser window height */ position: fixed; /* Makes it stay "fixed" to one place on the screen */ } .sidebar { width: 33%; /* Was already here */ background-color: #D3ADAD; /* Was already here */ position:absolute; top:0px; left: 67%; /* To make the element in the right place, add the width of "menu" and "content" */ height:100vh; /* Makes it be 100% of the Viewport Height, or 100% of the browser window height */ position: fixed; /* Makes it stay "fixed" to one place on the screen */ } .content { width: 34%; /* Was already here, but changed it to 34 to make the website fill the page */ background-color: #C9E4D1; /* Was already here */ position:absolute; top:0px; left:33%; /* To make the element in the right place, make this the width of "menu" */ } /* The CSS below this was already here */ /* GO FULL WIDTH BELOW 480 PIXELS */ @media only screen and (max-width: 480px) { .menu { width: 100%; } .sidebar { width: 100%; } .content { width: 100%; } } 
 <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <div class="section"> <div class="menu"> Menu </div> <div class="content"> Content </div> <div class="sidebar"> Sidebar </div> </div> 

Really Hope that helped! 真希望有所帮助!

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

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