스크롤 가능한 콘텐츠가있는 고정 머리글, 바닥 글


87

스크롤 가능한 콘텐츠가있는 고정 머리글, 바닥 글을 어떻게 얻을 수 있습니까? 이 페이지 와 같은 것 . CSS를 얻기 위해 소스를 볼 수 있지만이 작업을 수행하는 데 필요한 최소 CSS와 HTML 만 알고 싶습니다.

여기에 이미지 설명 입력


1
같은 이러한 한정자 질문 : 각 구체화는 ...
mobibob

같은 질문이지만 사이드와 아래로 스크롤 체인까지 바닥 글이 표시되지 않습니다 그리고 당신은 오른쪽에 두 스크롤바와 끝까지하지 않는 경우
Simon_Weaver

답변:


132

이 같은

<html>
  <body style="height:100%; width:100%">
    <div id="header" style="position:absolute; top:0px; left:0px; height:200px; right:0px;overflow:hidden;"> 
    </div> 
    <div id="content" style="position:absolute; top:200px; bottom:200px; left:0px; right:0px; overflow:auto;"> 
    </div> 
    <div id="footer" style="position:absolute; bottom:0px; height:200px; left:0px; right:0px; overflow:hidden;"> 
    </div>
  </body>
</html> 

downvotes와 관련이 없지만 여기에 왔고 이것은 나를 위해 작동하지 않았습니다 (문자 그대로 스타일을 복사했습니다). 아마도 jQuery Mobile이 엉망이 될 것입니다. 그러나 그것이 효과가 있더라도 고정 치수를 하 코딩하고 싶지 않습니다. HTML / css는 기본 UI 구성 요소보다 한 단계 뒤쳐진 것 같습니다. 내가 작업 한 모든 기본 플랫폼에서 스크롤 가능한 컨테이너를 만드는 것은 케이크 조각이었습니다.
미스터 스미스

@MisterSmith 죄송합니다. 컨테이너의 오른쪽을 잠그는 스타일을 추가하는 것을 잊었습니다. 또한이 코드는 컨테이너 크기로만 렌더링됩니다. body 태그 아래에 있고 전체 화면을 원하면 높이와 너비를 100 %로 설정해야합니다. 마지막으로 하드 코딩 된 스타일에 관해서는 이것은 단지 예제 일 뿐이며 프로덕션 릴리스에는 해당되지 않습니다. 이런 인라인 스타일을 릴리스하지는 않지만 대답을 설명하는 더 적절한 방법 인 것 같습니다.
John Hartsock 2013 년

@JohnHartsock 확실히 나쁜 일을하는 것은 JQM이었습니다. 하드 코딩 된 치수의 경우 절대 높이를 픽셀 단위로 설정하는 것이 마음에 들지 않습니다. 내가 함께 작업 한 다른 플랫폼에서는 컨테이너를 만들고 필요한 모든 높이를 사용하도록 지시 할 수있었습니다.
미스터 스미스

@ 미스터 스미스. 치수를 px로 하드 코딩 할 필요가 없습니다. 원하는 경우 백분율을 사용할 수 있습니다. 또한 태그의 스타일 속성을 통해 스타일을 설정할 필요가 없습니다. 대신 ID를 참조하는 CSS 스타일 시트를 설정하기 만하면됩니다. 나는 이해하기 쉽게 만들기 위해 대답에서 이렇게했습니다.
John Hartsock

2
이 인라인 CSS는의와 왜 그냥 사본입니다 그리고 대답 붙여 설명하지 않기 때문에 @JohnHartsock 아마 downvotes
treyBake

63

유연한 상자를 지원하는 브라우저를 대상으로하는 경우 다음을 수행 할 수 있습니다. http://jsfiddle.net/meyertee/AH3pE/

HTML

<div class="container">
    <header><h1>Header</h1></header>
    <div class="body">Body</div>
    <footer><h3>Footer</h3></footer>
</div>

CSS

.container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
}

header {
    flex-shrink: 0;
}
.body{
    flex-grow: 1;
    overflow: auto;
    min-height: 2em;
}
footer{
    flex-shrink: 0;
}

업데이트 : 유연한 상자의 브라우저 지원에
대해서는 "사용할 수 있습니까"를 참조하십시오 .


나는 가변 높이를 가진 머리글과 바닥 글을 사용하고 있으며 이것이 나를 위해 트릭을 할 것이라고 믿습니다. 운 좋게도 최신 브라우저를 대상으로하고 있습니다. 감사합니다!
Jimmie Tyrrell

큰! 그 .body지역 에 또 다른 구조물을 넣을 수있는 방법이 있습니까? 그렇게 할 때 .container안쪽 .body .body이 완전히 아래로 스크롤 되는 즉시 바깥 쪽 이 뷰에서 스크롤됩니다.
philk

1
이것은 제 생각에 가장 좋은 대답이며 이것이 최선의 대답이어야합니다! 감사합니다!
결합

내가 착각하지 않았다면 이것이 다른 질문에 대한 대답입니까? 여기에서 스크롤바는 전체 페이지에 나타나는 반면 John의 답변에서는 스크롤바가 기본 콘텐츠에만 나타납니다 (질문이 묻는 내용)? 적어도 그것이 내가 바이올린에서보고있는 것입니다.
bersling

51

접근 방식 1-플렉스 박스

알려진 높이 요소와 알려지지 않은 높이 요소 모두에 적합합니다. 외부 div를 height: 100%;로 설정하고에서 기본값 margin을 재설정해야 합니다 body. 브라우저 지원 표를 참조하십시오 .

jsFiddle

html, body {
  height: 100%;
  margin: 0;
}
.wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.header, .footer {
  background: silver;
}
.content {
  flex: 1;
  overflow: auto;
  background: pink;
}
<div class="wrapper">
  <div class="header">Header</div>
  <div class="content">
    <div style="height:1000px;">Content</div>
  </div>
  <div class="footer">Footer</div>
</div>

접근법 2-CSS 테이블

알려진 높이 요소와 알려지지 않은 높이 요소 모두. IE8을 포함한 레거시 브라우저에서도 작동합니다.

jsFiddle

접근 3- calc()

머리글과 바닥 글이 고정 높이 인 경우 CSS를 사용할 수 있습니다 calc().

jsFiddle

접근 4-모두에 대한 %

머리글과 바닥 글이 알려진 높이이고 또한 백분율이면 간단한 계산을 수행하여 높이를 100 %로 만들 수 있습니다.

jsFiddle


7

이제 CSS 그리드가 있습니다. 2019 년에 오신 것을 환영합니다.

/* Required */
body {
   margin: 0;
   height: 100%;
}

#wrapper {
   height: 100vh;
   display: grid;
   grid-template-rows: 30px 1fr 30px;
}

#content {
   overflow-y: scroll;
}

/* Optional */
#wrapper > * {
   padding: 5px;
}

#header {
   background-color: #ff0000ff;
}

#content {
   background-color: #00ff00ff;
}

#footer {
   background-color: #0000ffff;
}
<body>
   <div id="wrapper">
      <div id="header">Header Content</div>
      <div id="content">
         Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </div>
      <div id="footer">Footer Content</div>
   </div>
</body>


1
몸의 높이는 필요하지 않습니다. 래퍼 높이를 100vh로 설정하면 여기와 야생에서 모두 작동합니다.
Henrique Erzinger 19

5

2013 년 현재 : 이것이 제 접근 방식입니다. jsFiddle :


HTML

<header class="container global-header">
    <h1>Header (fixed)</h1>
</header>

<div class="container main-content">
    <div class="inner-w">
        <h1>Main Content</h1>
    </div><!-- .inner-w -->
</div> <!-- .main-content -->

<footer class="container global-footer">
    <h3>Footer (fixed)</h3>
</footer>


SCSS

// User reset

* { // creates a natural box model layout
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    box-sizing: border-box; 
} // asume normalize.css


// structure

.container {
    position: relative;
    width: 100%;
    float: left;
    padding: 1em;
}


// type

body {
    font-family: arial;   
}

.main-content {
    h1 {
        font-size: 2em;
        font-weight: bold;
        margin-bottom: .2em;
    }
} // .main-content


// style

    // variables
    $global-header-height: 8em;
    $global-footer-height: 6em;

.global-header {
    position: fixed;
    top: 0; left: 0;
    background-color: gray;
    height: $global-header-height;
}

.main-content {
    background-color: orange;
    margin-top: $global-header-height;
    margin-bottom: $global-footer-height;
    z-index: -1; // so header will be on top
    min-height: 50em; // to make it long so you can see the scrolling
}

.global-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    height: $global-footer-height;
    background-color: gray;
}

나는 2016 년부터 flex-box를 사용할 것입니다. 그래도 항상 상황에 따라 다릅니다. 까다로운 상황! :)
sheriffderek

5

여기에 저에게 효과적이었습니다. 바닥 글이 내 콘텐츠를 차지하지 않도록 margin-bottom을 추가해야했습니다.

header {
  height: 20px;
  background-color: #1d0d0a;
  position: fixed;
  top: 0;
  width: 100%;
  overflow: hide;
}

content {
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 100px;
  margin-top: 20px;
  overflow: auto;
  width: 80%;
}

footer {
  position: fixed;
  bottom: 0px;
  overflow: hide;
  width: 100%;
}

0

CSS 그리드를 사용하면 잘 작동합니다. 처음에는 컨테이너를 수정 한 다음 overflow-y: auto;스크롤 해야하는 중앙 콘텐츠 (예 : 머리글 및 바닥 글 제외)를 제공합니다.

.container{
  height: 100%;
  left: 0;
  position: fixed;
  top: 0;
  width: 100%;
  display: grid;
  grid-template-rows: 5em auto 3em;
}

header{
   grid-row: 1;  
    background-color: rgb(148, 142, 142);
    justify-self: center;
    align-self: center;
    width: 100%;
}

.body{
  grid-row: 2;
  overflow-y: auto;
}

footer{
   grid-row: 3;
   
    background: rgb(110, 112, 112);
}
<div class="container">
    <header><h1>Header</h1></header>
    <div class="body">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
    <footer><h3>Footer</h3></footer>
</div>

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.