I have three divs. I need header and left_side divs to be fixed and content div to scroll. I've been searching for solution and found something with overflow and position. But I can not use it corectly. How can I do this? I will be thankfull for every kind of answer.
我有三个div。我需要修复标题和left_side divs以及滚动内容div。我一直在寻找解决方案,并找到了溢出和位置的东西。但我无法核心使用它。我怎样才能做到这一点?我会感谢各种答案。
HTML
------
<div id="header">
</div>
<div id="left_side">
</div>
<div id="content">
</div
CSS
-----
body {
width: 100%;
height: auto;
padding: 0;
margin: 0px auto;
font-family: Calibri, Georgia, Ubuntu-C;
font-size: 16px;
margin-bottom: 20PX
}
#header{
width: 100%;
height: 139px;
background-image: url('images/Header_grey.gif');
}
#left_side{
width: 210px;
height: 700px;
background-image: url('images/Left_side.gif');
background-repeat:repeat-y;
overflow:hidden;
position:absolute;
font-size: 16px;
}
#content{
height: auto;
padding: 20px;
margin-left: 230px;
margin-right: 20px;
padding-bottom: 30px
}
6 个解决方案
#1
16
overflow: auto;
adds the scroll when need
溢出:自动;需要时添加滚动
#header{
width: 100%;
height: 139px;
background-image: url('images/Header_grey.gif');
overflow: hidden; //code added to prevent scroll
}
#left_side{
width: 210px;
height: 700px;
background-image: url('images/Left_side.gif');
background-repeat:repeat-y;
overflow:hidden; //code added to prevent scroll
position:absolute;
font-size: 16px;
}
#content{
height: auto;
padding: 20px;
margin-left: 230px;
margin-right: 20px;
padding-bottom: 30px;
overflow: auto; //code added
}
#2
8
- at first you will need to have a fixed height for content area.
- 首先,您需要为内容区域设置固定高度。
- then make
overflow:auto
there - 然后溢出:自动在那里
ERROR in your code:: you want to have a scroll bar for a div,but you are declaring that div height as auto
你的代码中的错误::你想要一个div的滚动条,但是你将div高度声明为auto
you cant demand a scroll bar when the height is auto,to have scroll bar you will need to have a fixed height for that div and when the content height will be greater than div height it will introduce scroll bar automatically
当高度为自动时你不能要求滚动条,要有滚动条,你需要为该div设置一个固定的高度,当内容高度大于div高度时,它会自动引入滚动条
NOTE: so the changes in your css will be
注意:所以css中的更改将是
#content{
height: 300px;/*..very important if you want scroll bar...*/
overfow:auto; /*..will introduce scroll bar when needed..*/
padding: 20px;
margin-left: 230px;
margin-right: 20px;
padding-bottom: 30px
}
EXAMPLE :: FIDDLE
例子:: FIDDLE
#3
1
If you want the header and left side to stay in their position while scrolling, you will have to use position:fixed
如果您希望在滚动时标题和左侧保持在其位置,则必须使用position:fixed
#4
1
You can just use position fixed. http://jsfiddle.net/Nrs2u/1/
你可以使用固定的位置。 http://jsfiddle.net/Nrs2u/1/
#header {
position: fixed;
z-index: 2;
left: 0%;
top: 0%;
width: 100%;
height: 10%;
background-color: purple;
}
#side {
position: fixed;
z-index: 2;
left: 0%;
top: 10%;
width: 10%;
height: 90%;
background-color: red;
}
#body {
position: absolute;
left: 10%;
top: 10%;
width: 90%;
height: 300%;
background-color: orange;
}
#5
0
As an learning exercise, I decided to update the answer by using CSS3 Flexbox. I also tried to more closely match the layout that jstorm31 was attempting to create.
作为一个学习练习,我决定使用CSS3 Flexbox更新答案。我还尝试更紧密地匹配jstorm31试图创建的布局。
Ritabrata's answer is the correct one: If you want a specific element to have scroll bars, you need to set its height (and/or width) to a fixed size and overflow to auto.
Ritabrata的答案是正确的:如果你想让一个特定的元素有滚动条,你需要将它的高度(和/或宽度)设置为固定大小并溢出到auto。
Code also to be found here: Plunker
代码也可以在这里找到:Plunker
style.css
style.css文件
#header{
overflow: hidden;
background-color: #880016;
height: 50px;
border-bottom: 4px solid black;
padding: 10px;
font-size: 20px;
}
#side_and_content {
display: flex;
}
#left_side{
flex: 1;
overflow:hidden;
background-color: #ED1B24;
height: 200px;
border-right: 2px solid black;
padding: 10px;
}
#content{
flex: 5;
overflow: auto;
background-color: #FF7F26;
height: 200px;
border-left: 2px solid black;
padding: 10px;
}
index.html
的index.html
<div id="header">
header header header header header header
</div>
<div id="side_and_content">
<div id="left_side">
left side left side left side left side left side
</div>
<div id="content">
CSS3 Flexbox Concepts:
Flexbox consists of flex containers and flex items.
A flex container is declared by setting the display property of an element to either flex
(rendered as a block) or inline-flex (rendered as inline).
Inside a flex container there is one or more flex items.
Note: Everything outside a flex container and inside a flex item is rendered as usual.
Flexbox defines how flex items are laid out inside a flex container.
Flex items are positioned inside a flex container along a flex line.
By default there is only one flex line per flex container.<br>
It is also possible to change the direction of the flex line.
If we set the direction property to rtl (right-to-left), the text is drawn right to left, and also the flex line changes direction, which will change the page layout
</div>
</div>
#6
-1
#left_side{
...
overflow:auto;
}
Set also a padding-right
to create a space between div's inner content and scrollbar
设置一个padding-right,在div的内部内容和滚动条之间创建一个空格
#1
16
overflow: auto;
adds the scroll when need
溢出:自动;需要时添加滚动
#header{
width: 100%;
height: 139px;
background-image: url('images/Header_grey.gif');
overflow: hidden; //code added to prevent scroll
}
#left_side{
width: 210px;
height: 700px;
background-image: url('images/Left_side.gif');
background-repeat:repeat-y;
overflow:hidden; //code added to prevent scroll
position:absolute;
font-size: 16px;
}
#content{
height: auto;
padding: 20px;
margin-left: 230px;
margin-right: 20px;
padding-bottom: 30px;
overflow: auto; //code added
}
#2
8
- at first you will need to have a fixed height for content area.
- 首先,您需要为内容区域设置固定高度。
- then make
overflow:auto
there - 然后溢出:自动在那里
ERROR in your code:: you want to have a scroll bar for a div,but you are declaring that div height as auto
你的代码中的错误::你想要一个div的滚动条,但是你将div高度声明为auto
you cant demand a scroll bar when the height is auto,to have scroll bar you will need to have a fixed height for that div and when the content height will be greater than div height it will introduce scroll bar automatically
当高度为自动时你不能要求滚动条,要有滚动条,你需要为该div设置一个固定的高度,当内容高度大于div高度时,它会自动引入滚动条
NOTE: so the changes in your css will be
注意:所以css中的更改将是
#content{
height: 300px;/*..very important if you want scroll bar...*/
overfow:auto; /*..will introduce scroll bar when needed..*/
padding: 20px;
margin-left: 230px;
margin-right: 20px;
padding-bottom: 30px
}
EXAMPLE :: FIDDLE
例子:: FIDDLE
#3
1
If you want the header and left side to stay in their position while scrolling, you will have to use position:fixed
如果您希望在滚动时标题和左侧保持在其位置,则必须使用position:fixed
#4
1
You can just use position fixed. http://jsfiddle.net/Nrs2u/1/
你可以使用固定的位置。 http://jsfiddle.net/Nrs2u/1/
#header {
position: fixed;
z-index: 2;
left: 0%;
top: 0%;
width: 100%;
height: 10%;
background-color: purple;
}
#side {
position: fixed;
z-index: 2;
left: 0%;
top: 10%;
width: 10%;
height: 90%;
background-color: red;
}
#body {
position: absolute;
left: 10%;
top: 10%;
width: 90%;
height: 300%;
background-color: orange;
}
#5
0
As an learning exercise, I decided to update the answer by using CSS3 Flexbox. I also tried to more closely match the layout that jstorm31 was attempting to create.
作为一个学习练习,我决定使用CSS3 Flexbox更新答案。我还尝试更紧密地匹配jstorm31试图创建的布局。
Ritabrata's answer is the correct one: If you want a specific element to have scroll bars, you need to set its height (and/or width) to a fixed size and overflow to auto.
Ritabrata的答案是正确的:如果你想让一个特定的元素有滚动条,你需要将它的高度(和/或宽度)设置为固定大小并溢出到auto。
Code also to be found here: Plunker
代码也可以在这里找到:Plunker
style.css
style.css文件
#header{
overflow: hidden;
background-color: #880016;
height: 50px;
border-bottom: 4px solid black;
padding: 10px;
font-size: 20px;
}
#side_and_content {
display: flex;
}
#left_side{
flex: 1;
overflow:hidden;
background-color: #ED1B24;
height: 200px;
border-right: 2px solid black;
padding: 10px;
}
#content{
flex: 5;
overflow: auto;
background-color: #FF7F26;
height: 200px;
border-left: 2px solid black;
padding: 10px;
}
index.html
的index.html
<div id="header">
header header header header header header
</div>
<div id="side_and_content">
<div id="left_side">
left side left side left side left side left side
</div>
<div id="content">
CSS3 Flexbox Concepts:
Flexbox consists of flex containers and flex items.
A flex container is declared by setting the display property of an element to either flex
(rendered as a block) or inline-flex (rendered as inline).
Inside a flex container there is one or more flex items.
Note: Everything outside a flex container and inside a flex item is rendered as usual.
Flexbox defines how flex items are laid out inside a flex container.
Flex items are positioned inside a flex container along a flex line.
By default there is only one flex line per flex container.<br>
It is also possible to change the direction of the flex line.
If we set the direction property to rtl (right-to-left), the text is drawn right to left, and also the flex line changes direction, which will change the page layout
</div>
</div>
#6
-1
#left_side{
...
overflow:auto;
}
Set also a padding-right
to create a space between div's inner content and scrollbar
设置一个padding-right,在div的内部内容和滚动条之间创建一个空格