header with position: 绝对忽略容器的填充
header with position: absolute ignoring paddings of container
demo of the main problem with header
这是沙箱的 link 因为我无法在此处获取代码 CODEPEN
您好,我已经尝试了所有方法,但是当页面被压缩时 header 仍然粘在屏幕上,请帮助
如果我添加保证金网站就会中断,因为 header 超出了
enter image description here
.body {
font-family: Inter, sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: var(--color-light);
border-radius: 28px;
box-sizing: border-box;
padding: 20px;
position: absolute;
z-index: 1;
width: 100%;
top: 50px;
left: 0;
right: 0;
margin: 0 auto;
}
.header__nav {
display: flex;
}
.header__list {
display: flex;
align-items: center;
list-style: none;
margin: 0;
padding: 0;
}
.header__link {
text-decoration: none;
font-style: normal;
font-weight: 400;
font-size: 16px;
line-height: 22px;
color: var(--color-dark);
transition: color 0.2s linear;
}
.header__link:hover {
color: var(--color-main);
}
.header__item:not(:last-child) {
margin: 0 40px 0 0;
}
.header__item:last-child {
margin: 0;
}
.header__inner {
display: flex;
align-items: center;
justify-content: space-between;
}
.btn {
display: flex;
align-items: center;
justify-content: center;
border: none;
background: transparent;
padding: 15px;
border-radius: 16px;
font-style: normal;
font-weight: 600;
font-size: 16px;
line-height: 22px;
cursor: pointer;
box-sizing: border-box;
font-family: "Inter", sans-serif;
}
.btn_type_bg-none {
border: 2px solid var(--color-light);
color: var(--color-light);
transition: background-color 0.1s linear;
}
.btn_type_bg-none:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.btn_color_purple {
background-color: var(--color-main);
color: var(--color-light);
transition: background-color 0.1s linear;
}
.btn_color_purple:hover {
background-color: #665dac;
}
.btn_color_light {
background-color: var(--color-light);
color: var(--color-main);
transition: background-color 0.1s linear;
}
.btn_color_light:hover {
background-color: rgb(240, 240, 240);
}
.container {
max-width: 1230px;
padding: 0 15px;
margin: 0 auto;
box-sizing: border-box;
}
.hero {
height: 100vh;
background-color: var(--color-main);
border-radius: 0 0 52px 52px;
padding: 230px 0 0 0;
}
.hero__title {
width: 100%;
font-style: normal;
font-weight: 700;
font-size: 75px;
line-height: 105.02%;
color: var(--color-light);
margin: 0 0 35px 0;
}
.hero__text {
display: flex;
flex-direction: column;
max-width: 613px;
width: 100%;
}
.hero__subtitle {
font-style: normal;
font-weight: 400;
font-size: 20px;
line-height: 24px;
color: var(--color-light);
margin: 0 0 50px 0;
}
.hero__parthners {
padding: 0 0 50px 0;
}
.hero__left {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.hero__inner {
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 0 100px 0;
}
.hero__image {
display: block;
width: 100%;
height: auto;
max-width: 400px;
object-fit: cover;
object-position: center;
}
.hero__btns {
display: grid;
grid-template-columns: repeat(2, 1fr);
justify-items: stretch;
align-items: center;
width: 60%;
gap: 30px;
}
.hero__bottom-img-container {
background-color: hsla(0, 0%, 100%, 0.3);
max-width: 1200px;
width: 100%;
box-sizing: border-box;
height: auto;
border-radius: 28px;
margin: 70px auto 150px auto;
padding: 20px;
}
.hero__bottom-img {
width: 100%;
display: block;
object-fit: cover;
}
.modal-signup {
opacity: 0;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
width: 100%;
max-width: 500px;
min-height: 600px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(1.5);
z-index: 999;
text-align: center;
background: linear-gradient(169.29deg, #f8f4f0 7.95%, #ffffff 99.86%);
border-radius: 20px;
transition: opacity 300ms ease-in-out, transform 300ms ease-in-out;
pointer-events: none;
cursor: default;
}
.reset-weight {
font-weight: 400;
}
:root {
--color-main: #8578e6;
--color-light: #fff;
--color-dark: #32313b;
--color-pink: #f48eba;
}
.header__container {
position: relative;
max-width: 1200px;
padding: 0 15px;
width: 100%;
}
<div class="container header__container">
<header class="header">
<div class="header__inner">
<a href="" class="logo logo_position_header">
<img src="images/logo.svg" alt="tines logo" class="logo__image" />
</a>
<nav class="header__nav">
<ul class="header__list">
<li class="header__item">
<a href="#" class="header__link">Products</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Use cases</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Resources</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Company</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Pricing</a>
</li>
</ul>
</nav>
<button class="btn btn__modal-open btn_color_purple" data-modal="1">Sign up for free</button>
</div>
</header>
</div>
尝试删除 width
并更改 left
和 right
值。
.header {
background-color: var(--color-light);
border-radius: 28px;
box-sizing: border-box;
padding: 20px;
position: absolute;
z-index: 1;
top: 50px;
left: 15px;
right: 15px;
margin: 0 auto;
}
您需要将 .header__container
上的 width: 100%
属性 更改为低于 100% 的值。虽然它有一个 max-width: 1200px
值,但一旦它低于 1200px,它就会保持在父容器大小的 100%。
您可以 width: 90%
添加一些 space 或计算值,例如 width: calc(100% - 2rem)
Codepen link https://codepen.io/thechewy/pen/zYRWmQr?editors=1100
demo of the main problem with header
这是沙箱的 link 因为我无法在此处获取代码 CODEPEN
您好,我已经尝试了所有方法,但是当页面被压缩时 header 仍然粘在屏幕上,请帮助
如果我添加保证金网站就会中断,因为 header 超出了
enter image description here
.body {
font-family: Inter, sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: var(--color-light);
border-radius: 28px;
box-sizing: border-box;
padding: 20px;
position: absolute;
z-index: 1;
width: 100%;
top: 50px;
left: 0;
right: 0;
margin: 0 auto;
}
.header__nav {
display: flex;
}
.header__list {
display: flex;
align-items: center;
list-style: none;
margin: 0;
padding: 0;
}
.header__link {
text-decoration: none;
font-style: normal;
font-weight: 400;
font-size: 16px;
line-height: 22px;
color: var(--color-dark);
transition: color 0.2s linear;
}
.header__link:hover {
color: var(--color-main);
}
.header__item:not(:last-child) {
margin: 0 40px 0 0;
}
.header__item:last-child {
margin: 0;
}
.header__inner {
display: flex;
align-items: center;
justify-content: space-between;
}
.btn {
display: flex;
align-items: center;
justify-content: center;
border: none;
background: transparent;
padding: 15px;
border-radius: 16px;
font-style: normal;
font-weight: 600;
font-size: 16px;
line-height: 22px;
cursor: pointer;
box-sizing: border-box;
font-family: "Inter", sans-serif;
}
.btn_type_bg-none {
border: 2px solid var(--color-light);
color: var(--color-light);
transition: background-color 0.1s linear;
}
.btn_type_bg-none:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.btn_color_purple {
background-color: var(--color-main);
color: var(--color-light);
transition: background-color 0.1s linear;
}
.btn_color_purple:hover {
background-color: #665dac;
}
.btn_color_light {
background-color: var(--color-light);
color: var(--color-main);
transition: background-color 0.1s linear;
}
.btn_color_light:hover {
background-color: rgb(240, 240, 240);
}
.container {
max-width: 1230px;
padding: 0 15px;
margin: 0 auto;
box-sizing: border-box;
}
.hero {
height: 100vh;
background-color: var(--color-main);
border-radius: 0 0 52px 52px;
padding: 230px 0 0 0;
}
.hero__title {
width: 100%;
font-style: normal;
font-weight: 700;
font-size: 75px;
line-height: 105.02%;
color: var(--color-light);
margin: 0 0 35px 0;
}
.hero__text {
display: flex;
flex-direction: column;
max-width: 613px;
width: 100%;
}
.hero__subtitle {
font-style: normal;
font-weight: 400;
font-size: 20px;
line-height: 24px;
color: var(--color-light);
margin: 0 0 50px 0;
}
.hero__parthners {
padding: 0 0 50px 0;
}
.hero__left {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.hero__inner {
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 0 100px 0;
}
.hero__image {
display: block;
width: 100%;
height: auto;
max-width: 400px;
object-fit: cover;
object-position: center;
}
.hero__btns {
display: grid;
grid-template-columns: repeat(2, 1fr);
justify-items: stretch;
align-items: center;
width: 60%;
gap: 30px;
}
.hero__bottom-img-container {
background-color: hsla(0, 0%, 100%, 0.3);
max-width: 1200px;
width: 100%;
box-sizing: border-box;
height: auto;
border-radius: 28px;
margin: 70px auto 150px auto;
padding: 20px;
}
.hero__bottom-img {
width: 100%;
display: block;
object-fit: cover;
}
.modal-signup {
opacity: 0;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
width: 100%;
max-width: 500px;
min-height: 600px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(1.5);
z-index: 999;
text-align: center;
background: linear-gradient(169.29deg, #f8f4f0 7.95%, #ffffff 99.86%);
border-radius: 20px;
transition: opacity 300ms ease-in-out, transform 300ms ease-in-out;
pointer-events: none;
cursor: default;
}
.reset-weight {
font-weight: 400;
}
:root {
--color-main: #8578e6;
--color-light: #fff;
--color-dark: #32313b;
--color-pink: #f48eba;
}
.header__container {
position: relative;
max-width: 1200px;
padding: 0 15px;
width: 100%;
}
<div class="container header__container">
<header class="header">
<div class="header__inner">
<a href="" class="logo logo_position_header">
<img src="images/logo.svg" alt="tines logo" class="logo__image" />
</a>
<nav class="header__nav">
<ul class="header__list">
<li class="header__item">
<a href="#" class="header__link">Products</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Use cases</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Resources</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Company</a>
</li>
<li class="header__item">
<a href="#" class="header__link">Pricing</a>
</li>
</ul>
</nav>
<button class="btn btn__modal-open btn_color_purple" data-modal="1">Sign up for free</button>
</div>
</header>
</div>
尝试删除 width
并更改 left
和 right
值。
.header {
background-color: var(--color-light);
border-radius: 28px;
box-sizing: border-box;
padding: 20px;
position: absolute;
z-index: 1;
top: 50px;
left: 15px;
right: 15px;
margin: 0 auto;
}
您需要将 .header__container
上的 width: 100%
属性 更改为低于 100% 的值。虽然它有一个 max-width: 1200px
值,但一旦它低于 1200px,它就会保持在父容器大小的 100%。
您可以 width: 90%
添加一些 space 或计算值,例如 width: calc(100% - 2rem)
Codepen link https://codepen.io/thechewy/pen/zYRWmQr?editors=1100