粘性页脚

粘性页脚是显示通知的常用方式。放置在页脚中的通知不太可能遮挡导航栏、品牌推广和横幅广告等页面元素。此外,粘性页脚在插入页面时不会导致布局偏移。

HTML

<div id="banner" class="banner">
    <button id="close-button" class="close-button" aria-label="close" tabindex="0">✕</button>
    <div>
        <h1>Notice</h1>
        Lorem ipsum dolor sit amet.
    </div>
</div>

CSS


        body {
    font-family: system-ui;
    padding: 2em;
    overscroll-behavior-y: none;
    background-color: #f4f4f4;
}
.banner {
    position: fixed;
    left: 0;
    bottom: 0;
    right: 0;
    padding: 1rem;
    background-color: white;
    box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
.close-button {
    background: transparent;
    border: none;
    padding: 1em;
    font-size: 1em;
    position: absolute;
    right: 0;
    top: 0;
    cursor: pointer;
}
        

JS


        document.getElementById("close-button").onclick = () => {
    document.getElementById("banner").style.display = "none";
}