다음 스 니펫 (전체 화면 모드로 볼 필요가 있음)이 있습니다. <main>
요소를 요소 바로 아래 에 배치하려고합니다 <header>
. 나는이 <header>
나는 그것이의 콘텐츠를 통해 사용자가 스크롤로 화면 상단에 있고 싶어하기 때문에 고정 된 위치를 <main>
요소입니다. 내가 아는 모든 것을 시도했지만 가장 좋은 방법은 <header>
요소를 요소 위에 배치 <main>
하여 내용의 큰 부분을 잘라내는 것입니다.
내가 찾은 가장 가까운 솔루션은 <main>
요소 에 추측 된 상단 패딩을 배치하여를 지우는 것 <header>
입니다. 그러나 px 대신 rem 크기 조정을 사용하기 때문에이 솔루션은 다양한 화면 크기를 잘 설명하지 못합니다. <header>
상대 또는 백분율 기반 높이를 사용 하는 여러 요소를 내부에 배치하면 상단 패딩 아이디어가 더욱 악화됩니다 . 한 화면 크기에서는 모든 것이 완벽하게 정렬 될 수 있으며 다른 화면 크기에서는 내용이 사라질 수 있습니다.
마지막으로 jQuery를 사용하여 상단 패딩을 동적으로 설정할 수 있다는 것을 알고 있지만 항상 제대로 작동하지는 않습니다. 궁금한 점은 순수한 CSS / html 솔루션이 있다는 것입니다.
아무도 내가 여기서 잃어버린 것을 말해 줄 수 있습니까? 상단 패딩 방법이 유일하게 실행 가능한 솔루션입니까?
$(document).ready(function() {
$('#navToggle').click(function() {
$("div#bottom-container > nav").slideToggle();
});
$(window).resize(function() {
if(window.innerWidth >= "751") {
$("header > div#bottom-container > nav").show();
}else {
$("header > div#bottom-container > nav").hide();
}
});
$("header > div#bottom-container > nav > ul > li > a").click(function(e) {
if (window.innerWidth <= "750") {
if ($(this).siblings().size() > 0) {
e.preventDefault();
$(this).siblings().slideToggle("slow");
}
}
});
$("header > div#bottom-container > nav > ul > li").hover(function() {
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").stop().show();
}
}
},function(){
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").hide();
}
}
});
});
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
#head-wrap{
margin: 0 auto;
position: relative;
width:100%;
}
#second-wrap{
position: fixed;
width:100%;
z-index:999;
}
main{
height:4000px;
position:relative;
padding-top:13rem;
}
header{
position: absolute;
top:0;
left:0;
width:100%;
overflow-x: hidden;
overflow-y: auto;
height:200rem;
}
#navToggle {
background-color: rgba(0, 0, 0, .15);
position: relative;
right: 0;
top: 0;
z-index:999;
height: 2.6rem;
}
#navToggle>a {
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top:.9rem;
}
#bottom-container {
display: flex;
flex-direction: column;
text-align: center;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
#bottom-container>nav {
background-color: rgb(250, 209, 14);
display: none;
flex: 1;
}
#bottom-container nav>ul {
list-style-type: none;
}
#bottom-container nav>ul>li {
position: relative;
}
#bottom-container nav>ul>li>a {
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
#bottom-container nav>ul>li>a span.toggle {
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
#bottom-container>nav>ul>li>nav {
display: none;
overflow: hidden;
position: absolute;
top:100%;
right: 5%;
width: 90%;
z-index: 100;
background-color: rgb(250, 250, 250);
margin-bottom:10rem;
}
header>nav>ul>li>nav>ul>li>a {
color: rgba(255, 255, 255, .85);
}
/*
/////////////////////////////////////////////////
/////////////////////////////////////////////////
//// THIS IS THE ONLY FIX I KNOW OF //////////
*/
main{
padding-top:5rem;
}
/*
////////////////////////////////////////////////
////////////////////////////////////////////////
*/
/* Medium screens */
@media all and (min-width: 751px) {
header{
overflow-y:visible;
overflow-x:visible;
padding-bottom:0;
}
#navToggle {
display: none;
}
#bottom-container {
background-color: rgb(250, 209, 14);
width: 100%;
border-top: calc(5vh + 2vw) solid yellow;
}
#bottom-container>nav {
display: block;
}
#bottom-container>nav>ul {
display: flex;
justify-content: center;
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li {
position: static;
flex:1;
display: flex;
justify-content: center;
align-items: center;
}
#bottom-container nav>ul>li>a {
padding: 0;
}
#bottom-container nav>ul>li>a span.toggle {
display: none;
}
#bottom-container >nav>ul>li>nav>ul>li{
line-height: 2.5rem;
}
#bottom-container>nav>ul>li>nav {
margin-top:-194.5rem;
}
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div id="head-wrap">
<div id="second-wrap">
<header>
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</header>
</div>
</div>
<main>
<p>content top not visible but should be able to see</P>
<p>line 1</P>
<p>line 2</P>
<p>line 3</P>
<p>line 4</P>
<p>line 5</P>
<p>line 6</P>
<p>line 7</P>
<p>line 8</P>
<p>line 9</P>
<p>line 10</P>
<p>line 11</P>
<p>line 12</P>
<p>line 13</P>
<p>line 14</P>
<p>line 15</P>
<p>line 16</P>
<p>line 17</P>
<p>line 18</P>
<p>line 19</P>
<p>line 20</P>
</main>
</body>
</html>
답변
나는 같은 결과를 얻었지만 (내가 틀렸다면 나를 교정하십시오), js는 없습니다. 그리고 아래 내용에 의해 헤더 높이가 고려되는 것 같습니다.
주요 아이디어 – 포장하지 않는 <header>
과에 적용 position: sticky
, z-index
필요하지도.
정확히 코드를 사용하지 않았지만 결과를 반복하려고했습니다. 문제에 대한 유용한 아이디어를 찾을 수 있기를 바랍니다.
화면 너비가 작고 메뉴를 토글하면 일부 답변의 코드가 기본 콘텐츠를 아래로 푸시합니다. 내 코드에는 해당 문제가 없습니다.
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
header {
width: 100%;
position: sticky;
top: 0;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
.wrapper {
position: relative;
background-color: rgba(0, 0, 0, .15);
}
#navToggle {
display: inline-block;
height: 2.6rem;
}
#navToggle > a {
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top: .9rem;
}
#navToggle:hover + #bottom-container, #bottom-container:hover {
visibility: visible;
opacity: 1;
transition: all 0.2s ease-in-out;
}
#bottom-container {
background-color: rgb(250, 209, 14);
width: 100%;
visibility: hidden;
opacity: 0;
position: absolute;
top: 100%;
left: 0;
}
#bottom-container > nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
#bottom-container > nav > ul {
display: flex;
flex-direction: column;
flex: 1;
align-items: center;
}
#bottom-container > nav > ul li {
display: flex;
justify-content: center;
width: 100%;
}
#bottom-container nav > ul > li > a {
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
.sub1 {
position: relative;
}
.sub1 > nav {
position: absolute;
top: 100%;
left: 0;
visibility: hidden;
opacity: 0;
background-color: rgb(250, 250, 250);
width: 100%;
transition: all 0.2s ease-in-out;
}
.sub1 > nav ul li {
text-align: center;
}
.sub1 > a:hover + nav, .sub1 > a + nav:hover {
visibility: visible;
opacity: 1;
transition: all 0.2s ease-in-out;
}
#bottom-container nav>ul>li>a span.toggle {
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
main {
height:4000px;
}
@media (min-width: 751px){
#bottom-container > nav > ul {
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li>a span.toggle {
display: none;
}
#bottom-container {
height: 100%;
border-top: calc(5vh + 2vw) solid yellow;
visibility: visible;
opacity: 1;
position: static;
}
#navToggle {
display: none;
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<div class="wrapper">
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</div>
</header>
<main>
<p>content top not visible but should be able to see</P>
<p>line 1</P>
<p>line 2</P>
<p>line 3</P>
<p>line 4</P>
<p>line 5</P>
<p>line 6</P>
<p>line 7</P>
<p>line 8</P>
<p>line 9</P>
<p>line 10</P>
<p>line 11</P>
<p>line 12</P>
<p>line 13</P>
<p>line 14</P>
<p>line 15</P>
<p>line 16</P>
<p>line 17</P>
<p>line 18</P>
<p>line 19</P>
<p>line 20</P>
</main>
</body>
</html>
답변
헤더를 줄 바꿈 할 필요가 없으므로 헤더 요소의 상위 2 개 div 컨테이너를 제거하십시오. 헤더 요소는 이미 컨테이너입니다.
height:200rem;
머리글 스타일과 padding-top: 13rem or 5rem
주요 요소 스타일을 제거하십시오 .
마지막으로 헤더 스타일의 위치 속성을 끈적 대신 absolute
및 z 색인을 추가합니다.
아래에서 테스트하고 코드베이스를 업데이트했습니다.
$(document).ready(function() {
$('#navToggle').click(function() {
$("div#bottom-container > nav").slideToggle();
});
$(window).resize(function() {
if(window.innerWidth >= "751") {
$("header > div#bottom-container > nav").show();
}else {
$("header > div#bottom-container > nav").hide();
}
});
$("header > div#bottom-container > nav > ul > li > a").click(function(e) {
if (window.innerWidth <= "750") {
if ($(this).siblings().size() > 0) {
e.preventDefault();
$(this).siblings().slideToggle("slow");
}
}
});
$("header > div#bottom-container > nav > ul > li").hover(function() {
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").stop().show();
}
}
},function(){
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").hide();
}
}
});
});
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
main{
height:4000px;
position:relative;
}
header{
position: sticky;
z-index: 100;
top:0;
left:0;
width:100%;
}
#navToggle {
background-color: rgba(0, 0, 0, .15);
position: relative;
right: 0;
top: 0;
z-index:999;
height: 2.6rem;
}
#navToggle>a {
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top:.9rem;
}
#bottom-container {
display: flex;
flex-direction: column;
text-align: center;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
#bottom-container>nav {
background-color: rgb(250, 209, 14);
display: none;
flex: 1;
}
#bottom-container nav>ul {
list-style-type: none;
}
#bottom-container nav>ul>li {
position: relative;
}
#bottom-container nav>ul>li>a {
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
#bottom-container nav>ul>li>a span.toggle {
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
#bottom-container>nav>ul>li>nav {
display: none;
overflow: hidden;
position: absolute;
top:100%;
right: 5%;
width: 90%;
z-index: 100;
background-color: rgb(250, 250, 250);
margin-bottom:10rem;
}
header>nav>ul>li>nav>ul>li>a {
color: rgba(255, 255, 255, .85);
}
/*
/////////////////////////////////////////////////
/////////////////////////////////////////////////
//// THIS IS THE ONLY FIX I KNOW OF //////////
*/
main{
/* remove padding top */
}
/*
////////////////////////////////////////////////
////////////////////////////////////////////////
*/
/* Medium screens */
@media all and (min-width: 751px) {
header{
overflow-y:visible;
overflow-x:visible;
padding-bottom:0;
}
#navToggle {
display: none;
}
#bottom-container {
background-color: rgb(250, 209, 14);
width: 100%;
border-top: calc(5vh + 2vw) solid yellow;
}
#bottom-container>nav {
display: block;
}
#bottom-container>nav>ul {
display: flex;
justify-content: center;
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li {
position: static;
flex:1;
display: flex;
justify-content: center;
align-items: center;
}
#bottom-container nav>ul>li>a {
padding: 0;
}
#bottom-container nav>ul>li>a span.toggle {
display: none;
}
#bottom-container >nav>ul>li>nav>ul>li{
line-height: 2.5rem;
}
#bottom-container>nav>ul>li>nav {
margin-top:-194.5rem;
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<header>
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</header>
<main>
<p>content top not visible but should be able to see</P>
<p>line 1</P>
<p>line 2</P>
<p>line 3</P>
<p>line 4</P>
<p>line 5</P>
<p>line 6</P>
<p>line 7</P>
<p>line 8</P>
<p>line 9</P>
<p>line 10</P>
<p>line 11</P>
<p>line 12</P>
<p>line 13</P>
<p>line 14</P>
<p>line 15</P>
<p>line 16</P>
<p>line 17</P>
<p>line 18</P>
<p>line 19</P>
<p>line 20</P>
</main>
</body>
</html>
Run code snippet
답변
이것은 일종의 거친 해킹이지만 문서 흐름에 남아있는 실제 헤더 뒤에 숨어있는 두 번째 헤더를 만들 수 있습니다. <main>
.
헤더를 구성하는 요소를 복제하고 더 낮게 z-index
설정하고에서 position: fixed
로 전환하십시오 position: relative
. 그런 다음 숨겨진 <header>
요소 의 높이를 제거하고 padding-top
from을 제거하십시오 <main>
.
$(document).ready(function() {
$('#navToggle').click(function() {
$("div#bottom-container > nav").slideToggle();
});
$(window).resize(function() {
if(window.innerWidth >= "751") {
$("header > div#bottom-container > nav").show();
}else {
$("header > div#bottom-container > nav").hide();
}
});
$("header > div#bottom-container > nav > ul > li > a").click(function(e) {
if (window.innerWidth <= "750") {
if ($(this).siblings().size() > 0) {
e.preventDefault();
$(this).siblings().slideToggle("slow");
}
}
});
$("header > div#bottom-container > nav > ul > li").hover(function() {
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").stop().show();
}
}
},function(){
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").hide();
}
}
});
});
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
#head-wrap,
#hidden-wrap{
margin: 0 auto;
position: relative;
width:100%;
}
#hidden-wrap header {
height: inherit;
position: relative;
}
#second-wrap{
position: fixed;
width:100%;
z-index:999;
}
#second-wrap2{
position: relative;
width:100%;
z-index:998;
}
main{
height:4000px;
position:relative;
/* padding-top:13rem; */
}
header{
position: absolute;
top:0;
left:0;
width:100%;
overflow-x: hidden;
overflow-y: auto;
height:200rem;
}
#navToggle,
#navToggle2{
background-color: rgba(0, 0, 0, .15);
position: relative;
right: 0;
top: 0;
z-index:999;
height: 2.6rem;
}
#navToggle>a,
#navToggle2>a{
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top:.9rem;
}
#bottom-container,
#bottom-container2{
display: flex;
flex-direction: column;
text-align: center;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
#bottom-container>nav,
#bottom-container2>nav{
background-color: rgb(250, 209, 14);
display: none;
flex: 1;
}
#bottom-container nav>ul,
#bottom-container2 nav>ul{
list-style-type: none;
}
#bottom-container nav>ul>li,
#bottom-container2 nav>ul>li{
position: relative;
}
#bottom-container nav>ul>li>a,
#bottom-container2 nav>ul>li>a{
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
#bottom-container nav>ul>li>a span.toggle,
#bottom-container2 nav>ul>li>a span.toggle{
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
#bottom-container>nav>ul>li>nav,
#bottom-container2>nav>ul>li>nav{
display: none;
overflow: hidden;
position: absolute;
top:100%;
right: 5%;
width: 90%;
z-index: 100;
background-color: rgb(250, 250, 250);
margin-bottom:10rem;
}
header>nav>ul>li>nav>ul>li>a {
color: rgba(255, 255, 255, .85);
}
/*
/////////////////////////////////////////////////
/////////////////////////////////////////////////
//// THIS IS THE ONLY FIX I KNOW OF //////////
*/
main{
/*padding-top:5rem;*/
}
/*
////////////////////////////////////////////////
////////////////////////////////////////////////
*/
/* Medium screens */
@media all and (min-width: 751px) {
header{
overflow-y:visible;
overflow-x:visible;
padding-bottom:0;
}
#navToggle,
#navToggle2{
display: none;
}
#bottom-container,
#bottom-container2{
background-color: rgb(250, 209, 14);
width: 100%;
border-top: calc(5vh + 2vw) solid yellow;
}
#bottom-container>nav,
#bottom-container2>nav{
display: block;
}
#bottom-container>nav>ul,
#bottom-container2>nav>ul{
display: flex;
justify-content: center;
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li,
#bottom-container2 nav>ul>li{
position: static;
flex:1;
display: flex;
justify-content: center;
align-items: center;
}
#bottom-container nav>ul>li>a,
#bottom-container2 nav>ul>li>a{
padding: 0;
}
#bottom-container nav>ul>li>a span.toggle,
#bottom-container2 nav>ul>li>a span.toggle{
display: none;
}
#bottom-container >nav>ul>li>nav>ul>li,
#bottom-container2 >nav>ul>li>nav>ul>li{
line-height: 2.5rem;
}
#bottom-container>nav>ul>li>nav,
#bottom-container2>nav>ul>li>nav{
margin-top:-194.5rem;
}
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div id="head-wrap">
<div id="second-wrap">
<header>
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</header>
</div>
</div>
<div id="hidden-wrap">
<div id="second-wrap2">
<header>
<div id="navToggle2"><a href="#">Menu</a></div>
<div id='bottom-container2'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</li>
</ul>
</nav>
</div>
</header>
</div>
</div>
<main>
<p>now you can see this text</p>
<p>line 1</p>
<p>line 2</p>
<p>line 3</p>
<p>line 4</p>
<p>line 5</p>
<p>line 6</p>
<p>line 7</p>
<p>line 8</p>
<p>line 9</p>
<p>line 10</p>
<p>line 11</p>
<p>line 12</p>
<p>line 13</p>
<p>line 14</p>
<p>line 15</p>
<p>line 16</p>
<p>line 17</p>
<p>line 18</p>
<p>line 19</p>
<p>line 20</p>
</main>
</body>
</html>
편집 : 나는 왜 내가 이것을 전에 생각하지 않았는지 모른다. 그냥 설정 #headwrap
과 <main>
에 position: relative
. 그런 다음, 제거 height
에 <main>
와 세트 <body>
에 display: flex
와 flex-direction: column
. 마지막으로의 내용을 <main>
로 묶습니다 <div>
.
여기에서는 스크롤을 보여주기 위해 div의 높이를 4000px로 설정했습니다.
$(document).ready(function() {
$('#navToggle').click(function() {
$("div#bottom-container > nav").slideToggle();
});
$(window).resize(function() {
if(window.innerWidth >= "751") {
$("header > div#bottom-container > nav").show();
}else {
$("header > div#bottom-container > nav").hide();
}
});
$("header > div#bottom-container > nav > ul > li > a").click(function(e) {
if (window.innerWidth <= "750") {
if ($(this).siblings().size() > 0) {
e.preventDefault();
$(this).siblings().slideToggle("slow");
}
}
});
$("header > div#bottom-container > nav > ul > li").hover(function() {
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").stop().show();
}
}
},function(){
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").hide();
}
}
});
});
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: 100vh;
display: flex;
flex-direction: column;
}
#head-wrap{
margin: 0 auto;
position: relative;
width:100%;
}
#second-wrap{
position: relative;
width:100%;
z-index:999;
}
main{
/*height:4000px;*/
position:relative;
overflow: auto;
/* padding-top:13rem; */
}
main>div {
height: 4000px;
}
header{
position: relative;
top:0;
left:0;
width:100%;
overflow-x: hidden;
overflow-y: auto;
/*height:200rem;*/
}
#navToggle{
background-color: rgba(0, 0, 0, .15);
position: relative;
right: 0;
top: 0;
z-index:999;
height: 2.6rem;
}
#navToggle>a{
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top:.9rem;
}
#bottom-container{
display: flex;
flex-direction: column;
text-align: center;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
#bottom-container>nav{
background-color: rgb(250, 209, 14);
display: none;
flex: 1;
}
#bottom-container nav>ul{
list-style-type: none;
}
#bottom-container nav>ul>li{
position: relative;
}
#bottom-container nav>ul>li>a{
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
#bottom-container nav>ul>li>a span.toggle{
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
#bottom-container>nav>ul>li>nav{
display: none;
overflow: hidden;
position: absolute;
top:100%;
right: 5%;
width: 90%;
z-index: 100;
background-color: rgb(250, 250, 250);
margin-bottom:10rem;
}
header>nav>ul>li>nav>ul>li>a {
color: rgba(255, 255, 255, .85);
}
/*
/////////////////////////////////////////////////
/////////////////////////////////////////////////
//// THIS IS THE ONLY FIX I KNOW OF //////////
*/
main{
/*padding-top:5rem;*/
}
/*
////////////////////////////////////////////////
////////////////////////////////////////////////
*/
/* Medium screens */
@media all and (min-width: 751px) {
header{
overflow-y:visible;
overflow-x:visible;
padding-bottom:0;
}
#navToggle{
display: none;
}
#bottom-container{
background-color: rgb(250, 209, 14);
width: 100%;
border-top: calc(5vh + 2vw) solid yellow;
}
#bottom-container>nav{
display: block;
}
#bottom-container>nav>ul{
display: flex;
justify-content: center;
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li{
position: static;
flex:1;
display: flex;
justify-content: center;
align-items: center;
}
#bottom-container nav>ul>li>a{
padding: 0;
}
#bottom-container nav>ul>li>a span.toggle{
display: none;
}
#bottom-container >nav>ul>li>nav>ul>li{
line-height: 2.5rem;
}
#bottom-container>nav>ul>li>nav{
margin-top:-194.5rem;
}
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div id="head-wrap">
<div id="second-wrap">
<header>
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</header>
</div>
</div>
<main>
<div>
<p>now you can see this text</p>
<p>line 1</p>
<p>line 2</p>
<p>line 3</p>
<p>line 4</p>
<p>line 5</p>
<p>line 6</p>
<p>line 7</p>
<p>line 8</p>
<p>line 9</p>
<p>line 10</p>
<p>line 11</p>
<p>line 12</p>
<p>line 13</p>
<p>line 14</p>
<p>line 15</p>
<p>line 16</p>
<p>line 17</p>
<p>line 18</p>
<p>line 19</p>
<p>line 20</p>
</div>
</main>
</body>
</html>
답변
간단한 수정, 내가 사용
padding
에%
보다는rem
나px
$(document).ready(function() {
$('#navToggle').click(function() {
$("div#bottom-container > nav").slideToggle();
});
$(window).resize(function() {
if (window.innerWidth >= "751") {
$("header > div#bottom-container > nav").show();
} else {
$("header > div#bottom-container > nav").hide();
}
});
$("header > div#bottom-container > nav > ul > li > a").click(function(e) {
if (window.innerWidth <= "750") {
if ($(this).siblings().size() > 0) {
e.preventDefault();
$(this).siblings().slideToggle("slow");
}
}
});
$("header > div#bottom-container > nav > ul > li").hover(function() {
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").stop().show();
}
}
}, function() {
if (window.innerWidth >= "751") {
if ($(this).children("nav").size() > 0) {
$(this).children("nav").hide();
}
}
});
});
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
#head-wrap {
margin: 0 auto;
position: relative;
width: 100%;
}
#second-wrap {
position: fixed;
width: 100%;
z-index: 999;
}
main {
height: 4000px;
position: relative;
padding-top: 13rem;
}
header {
position: absolute;
top: 0;
left: 0;
width: 100%;
overflow-x: hidden;
overflow-y: auto;
height: 200rem;
}
#navToggle {
background-color: rgba(0, 0, 0, .15);
position: relative;
right: 0;
top: 0;
z-index: 999;
height: 2.6rem;
}
#navToggle>a {
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top: .9rem;
}
#bottom-container {
display: flex;
flex-direction: column;
text-align: center;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
#bottom-container>nav {
background-color: rgb(250, 209, 14);
display: none;
flex: 1;
}
#bottom-container nav>ul {
list-style-type: none;
}
#bottom-container nav>ul>li {
position: relative;
}
#bottom-container nav>ul>li>a {
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
#bottom-container nav>ul>li>a span.toggle {
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
#bottom-container>nav>ul>li>nav {
display: none;
overflow: hidden;
position: absolute;
top: 100%;
right: 5%;
width: 90%;
z-index: 100;
background-color: rgb(250, 250, 250);
margin-bottom: 10rem;
}
header>nav>ul>li>nav>ul>li>a {
color: rgba(255, 255, 255, .85);
}
/*
/////////////////////////////////////////////////
/////////////////////////////////////////////////
//// Use padding in % //////////
*/
main {
padding-top: 11%;
}
@media (max-width:1200px) {
main {
padding-top: 12.5%;
}
}
@media (max-width:1023px) {
main {
padding-top: 14.5%;
}
}
@media (max-width:767px) {
main {
padding-top: 8%;
}
}
/*
////////////////////////////////////////////////
////////////////////////////////////////////////
*/
/* Medium screens */
@media all and (min-width: 751px) {
header {
overflow-y: visible;
overflow-x: visible;
padding-bottom: 0;
}
#navToggle {
display: none;
}
#bottom-container {
background-color: rgb(250, 209, 14);
width: 100%;
border-top: calc(5vh + 2vw) solid yellow;
}
#bottom-container>nav {
display: block;
}
#bottom-container>nav>ul {
display: flex;
justify-content: center;
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li {
position: static;
flex: 1;
display: flex;
justify-content: center;
align-items: center;
}
#bottom-container nav>ul>li>a {
padding: 0;
}
#bottom-container nav>ul>li>a span.toggle {
display: none;
}
#bottom-container>nav>ul>li>nav>ul>li {
line-height: 2.5rem;
}
#bottom-container>nav>ul>li>nav {
margin-top: -194.5rem;
}
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div id="head-wrap">
<div id="second-wrap">
<header>
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</header>
</div>
</div>
<main>
<p>content top not visible but should be able to see</P>
<p>line 1</P>
<p>line 2</P>
<p>line 3</P>
<p>line 4</P>
<p>line 5</P>
<p>line 6</P>
<p>line 7</P>
<p>line 8</P>
<p>line 9</P>
<p>line 10</P>
<p>line 11</P>
<p>line 12</P>
<p>line 13</P>
<p>line 14</P>
<p>line 15</P>
<p>line 16</P>
<p>line 17</P>
<p>line 18</P>
<p>line 19</P>
<p>line 20</P>
</main>
</body>
</html>
답변
당신은 위치 를 찾고 있지 않습니까? 상단 : 0; ? 오른쪽 스크롤 할 때 사용자를 따르는 메뉴를 원하십니까? 그런 다음 # bottom-container 를 다음과 같이 변경하십시오 .
#bottom-container {
display: flex;
flex-direction: column;
text-align: center;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
position: sticky;
top: 0;
z-index: 1;
}
그리고 그게 전부라고 생각합니다. 그러나 하나는 확실합니다.이 구조를 어떻게 만들 었는지 다시 생각해야합니다. 지저분합니다. 다음과 같은 결과를 얻을 수 있습니다.
html :
<header>
<nav id="mobileMenu"><a href="#">Menu</a></nav>
<nav id='menu'>
<div class="menuItem">
<a href="#">ITEM ONE</a>
</div>
<div class="bigMenuItem">
HOVER ME
<div class="menuItemsCon">
<div class="menuItem"><a href="#">Sub Item 1</a></div>
<div class="menuItem"><a href="#">Sub Item 2</a></div>
<div class="menuItem"><a href="#">Sub Item 3</a></div>
<div class="menuItem"><a href="#">Sub Item 4</a></div>
</div>
</div>
<label class="bigMenuItem" for="inputClick">
CLICK ME
<input type="checkbox" name="input" id="inputClick" style="display:none;">
<div class="menuItemsCon click">
<div class="menuItem"><a href="#">Sub Item 1</a></div>
<div class="menuItem"><a href="#">Sub Item 2</a></div>
<div class="menuItem"><a href="#">Sub Item 3</a></div>
<div class="menuItem"><a href="#">Sub Item 4</a></div>
</div>
</label>
</nav>
</header>
<main>
<p>content top not visible but should be able to see</P>
<p>line 1</P>
<p>line 2</P>
<p>line 3</P>
<p>line 4</P>
<p>line 5</P>
<p>line 6</P>
<p>line 7</P>
<p>line 8</P>
<p>line 9</P>
<p>line 10</P>
</main>
style.css :
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
main{
height:100vh;
position:relative;
}
/*Changed classes*/
header{
position: sticky;
top:0;
z-index: 1;
}
a{
display: block;
color: rgba(0, 0, 0, .65);
text-decoration: none;
width: 100%;
height: 100%;
}
#mobileMenu{
display: none;
background-color: rgba(0, 0, 0, .15);
height: 2.6rem
}
#mobileMenu a{
color:rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top: .9rem;
}
#menu{
background-color: rgb(250, 209, 14);
border-top: calc(5vh + 2vw) solid yellow;
display: flex;
z-index: 999;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
@media only screen and (max-width:751px) {
#mobileMenu{
display: block;
}
#menu{
display: none;
}
}
#menu > .menuItem, #menu > .bigMenuItem{
width: calc(100%/3);
height: 3rem;
text-align: center;
line-height: 3rem;
}
#menu > .bigMenuItem{
position: relative;
cursor: pointer;
}
#menu > .bigMenuItem .menuItemsCon{
position: absolute;
top: 100%;
left: 0;
width: 100%;
z-index: -1;
opacity: 0;
position: relative;
top: -100vh;
transition: all .5s;
}
#menu > .bigMenuItem .menuItemsCon > .menuItem{
height: 3rem;
background-color: #FFF;
}
/* and if you want to click for submenu to show*/
#menu .bigMenuItem input:checked + .menuItemsCon, #menu .bigMenuItem:hover .menuItemsCon:not(.click){
opacity: 1;
top: 0;
}
당신이 그것을 사용할 필요가 없다면 JS cuz도 없습니다. 이게 도움이 되길 바란다.
답변
jQuery 대신 Vanilla-JavaScript를 제공 할 수 있습니다…
페이지가로드 될 때 (onload 이벤트) “header”의 크기를 가져 와서 “main”의 패딩에 추가하십시오.
window.addEventListener("load", function(e){
my_main.style.paddingTop = my_header.clientHeight + "px";
}, 1);
작동하기 위해 “header”와 “main”요소에 “id”를 부여했습니다.
<header id="my_header"> <main id="my_main">
window.addEventListener("load", function(e){
my_main.style.paddingTop = my_header.clientHeight + "px";
}, 1);
* {
margin: 0;
}
@media (min-width: 48rem) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
}
}
body {
background: #eee;
font-family: "HelveticaNeue-Light", Arial;
height: auto !important;
}
header {
width: 100%;
position: sticky;
top: 0;
box-shadow: 2px 5px 10px 1px rgba(0, 0, 0, 0.55);
}
.wrapper {
position: relative;
background-color: rgba(0, 0, 0, .15);
}
#navToggle {
display: inline-block;
height: 2.6rem;
}
#navToggle > a {
color: rgba(255, 255, 255, .85);
display: block;
font-size: 0.85em;
font-weight: 600;
padding: 0 2.5rem;
text-decoration: none;
transition: all 300ms ease;
padding-top: .9rem;
}
#navToggle:hover + #bottom-container, #bottom-container:hover {
visibility: visible;
opacity: 1;
transition: all 0.2s ease-in-out;
}
#bottom-container {
background-color: rgb(250, 209, 14);
width: 100%;
visibility: hidden;
opacity: 0;
position: absolute;
top: 100%;
left: 0;
}
#bottom-container > nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
#bottom-container > nav > ul {
display: flex;
flex-direction: column;
flex: 1;
align-items: center;
}
#bottom-container > nav > ul li {
display: flex;
justify-content: center;
width: 100%;
}
#bottom-container nav > ul > li > a {
display: block;
color: rgba(0, 0, 0, .65);
padding: 1.3rem 0;
text-decoration: none;
}
.sub1 {
position: relative;
}
.sub1 > nav {
position: absolute;
top: 100%;
left: 0;
visibility: hidden;
opacity: 0;
background-color: rgb(250, 250, 250);
width: 100%;
transition: all 0.2s ease-in-out;
}
.sub1 > nav ul li {
text-align: center;
}
.sub1 > a:hover + nav, .sub1 > a + nav:hover {
visibility: visible;
opacity: 1;
transition: all 0.2s ease-in-out;
}
#bottom-container nav>ul>li>a span.toggle {
background-color: rgba(0, 0, 0, .05);
color: rgba(0, 0, 0, .25);
padding: 2px 8px;
}
main {
height:4000px;
}
@media (min-width: 751px){
#bottom-container > nav > ul {
flex-direction: row;
height: 3rem;
}
#bottom-container nav>ul>li>a span.toggle {
display: none;
}
#bottom-container {
height: 100%;
border-top: calc(5vh + 2vw) solid yellow;
visibility: visible;
opacity: 1;
position: static;
}
#navToggle {
display: none;
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header id="my_header">
<div class="wrapper">
<div id="navToggle"><a href="#">Menu</a></div>
<div id='bottom-container'>
<nav>
<ul>
<li><a href="#">ITEM ONE</a></li>
<li class="sub1">
<a href="#">ITEM TWO <span class="toggle">Expand</span></a>
<nav>
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
</ul>
</nav>
<li><a href="#">ITEM THREE</a></li>
</ul>
</nav>
</div>
</div>
</header>
<main id="my_main">
<p>content top not visible but should be able to see</P>
<p>line 1</P>
<p>line 2</P>
<p>line 3</P>
<p>line 4</P>
<p>line 5</P>
<p>line 6</P>
<p>line 7</P>
<p>line 8</P>
<p>line 9</P>
<p>line 10</P>
<p>line 11</P>
<p>line 12</P>
<p>line 13</P>
<p>line 14</P>
<p>line 15</P>
<p>line 16</P>
<p>line 17</P>
<p>line 18</P>
<p>line 19</P>
<p>line 20</P>
</main>
</body>
</html>