You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在纯HTML/CSS滑块上添加固定文本与按钮?

解决方案:为纯CSS滑块添加顶部固定文本按钮并修复问题

先修复现有滑块的bug

  1. 修正切换选择器错误:原代码中第一个滑块的切换CSS写错了,缺少类名前缀和正确层级,导致第一个滑块切换无效,把#btn1:checked~slider-inner slides改成#btn1:checked~.slider-inner .slides
  2. 调整滑块垂直位置:原transform: translateY(40%)容易和固定header重叠,改成用margin-top适配头部高度,避免内容被遮挡
  3. 补全语法错误:修复search::placeholder缺失的font-size属性,给切换按钮label增加间距避免拥挤

添加顶部固定文本与按钮

在.slider容器内新增一个绝对定位的.slider-overlay层,用来承载文本和按钮,通过position: absolute固定在滑块上方,设置z-index确保在轮播图上层显示,同时添加文本阴影提升可读性。

修改后的完整代码

HTML代码

<html lang="en">
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Shop</title>
  <meta charset="UTF-8">
  <script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header class="header">
  <a href="" class="logo">Fresh market</a>
  <input class="menu-btn" type="checkbox" id="menu-btn" />
  <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
  <ul class="menu">
    <li><a href="#work">Home</a></li>
    <li><a href="#about">Shop</a></li>
    <li><a href="#careers">About</a></li>
    <li><a href="#contact">Contact</a></li>
    <div class="icons">
<input class="search" placeholder="Search.." type="text">
<ion-icon name="search-outline"></ion-icon>
<ion-icon name="person-circle-outline"></ion-icon>
<ion-icon name="bag-handle-outline"></ion-icon>
</div>
  </ul>
</header>

<div class="slider">
  <!-- 新增顶部文本按钮层 -->
  <div class="slider-overlay">
    <h2>Fresh Organic Vegetables</h2>
    <p>Get the best fresh produce delivered straight to your door</p>
    <button class="cta-btn">Shop Now</button>
  </div>

  <input type="radio" name="switch" id="btn1" checked>
  <input type="radio" name="switch" id="btn2">
  <input type="radio" name="switch" id="btn3">
  
  <div class="switch">
    <label for="btn1"></label>
    <label for="btn2"></label>
    <label for="btn3"></label>
  </div>
  
  <div class="slider-inner">
    <div class="slides">
      <img src="https://static.wixstatic.com/media/ad420a_f2ae964f4e3d4bb8af8b6cdda0ad86bd~mv2.jpg/v1/fill/w_1112,h_601,q_90/ad420a_f2ae964f4e3d4bb8af8b6cdda0ad86bd~mv2.webp"/>
      <img src="https://static.wixstatic.com/media/ad420a_d2d8311f21fe4d7bb4836f82c5011a46~mv2.jpg/v1/fill/w_1112,h_601,q_90/ad420a_d2d8311f21fe4d7bb4836f82c5011a46~mv2.webp"/>
      <img src="https://static.wixstatic.com/media/ad420a_01886647b6df44198b05bc86420472c0~mv2.jpg/v1/fill/w_1112,h_601,fp_0.73_0.29,q_90/ad420a_01886647b6df44198b05bc86420472c0~mv2.webp"/>
    </div>
  </div>
</div>
</body>
</html>

CSS代码

body {
    margin: 0;
    font-family: Helvetica, sans-serif;
    background-color: #ffffff;
  }
 
  a {
    color: #000;
  }
  
  /* header */
  
  .header {
    background-color: #ffff;
    position: fixed;
    width: 100%;
    z-index: 99;
  }
  
  .header ul {
    margin: 0;
    padding: 0;
    list-style: none;
    overflow: hidden;
    background-color: #fff;
  }
  
  .header li a {
    display: block;
    padding: 20px 20px;
    border-right: 1px solid #f4f4f4;
    text-decoration: none;
  }
   
  .header li a:hover,
  .header .menu-btn:hover {
    color:green;
  }

  
  .header .logo {
    display: block;
    float: left;
    
    font-size: 2em;
    padding: 10px 20px;
    text-decoration: none;
    color:green;
    
  }

  .logo::before {
   
    content:url('imgonline-com-ua-Resize-HhpRdaDieHxuk8.png');
            display: inline-block;
            padding-right: 3px;
            vertical-align: middle;
          }
  
  /* menu */
  
  .header .menu {
    clear: both;
    max-height: 0;
    transition: max-height .2s ease-out;
  }
  
  
.icons{
  display:flex;
  gap:20px;
  float:right;
  padding:16px;
  margin-right:50px;
}
ion-icon{
  font-size:25px;
}

.search{
  border:none;
}
.search::placeholder{
  font-size: 14px;
}


  /* menu icon */
  
  .header .menu-icon {
    cursor: pointer;
    display: inline-block;
    float: right;
    padding: 28px 20px;
    position: relative;
    user-select: none;
  }
  
  .header .menu-icon .navicon {
    background: #333;
    display: block;
    height: 2px;
    position: relative;
    transition: background .2s ease-out;
    width: 18px;
  }
  
  .header .menu-icon .navicon:before,
  .header .menu-icon .navicon:after {
    background: #333;
    content: '';
    display: block;
    height: 100%;
    position: absolute;
    transition: all .2s ease-out;
    width: 100%;
  }
  
  .header .menu-icon .navicon:before {
    top: 5px;
  }
  
  .header .menu-icon .navicon:after {
    top: -5px;
  }
  
  /* menu btn */
  
  .header .menu-btn {
    display: none;
  }
  
  .header .menu-btn:checked ~ .menu {
    max-height: 240px;
  }
  
  .header .menu-btn:checked ~ .menu-icon .navicon {
    background: transparent;
  }
  
  .header .menu-btn:checked ~ .menu-icon .navicon:before {
    transform: rotate(-45deg);
  }
  
  .header .menu-btn:checked ~ .menu-icon .navicon:after {
    transform: rotate(45deg);
  }
  
  .header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
  .header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
    top: 0;
  }
  
  /* 48em = 768px */
  
  @media (min-width: 48em) {
    .header li {
      float: left;
    }
    .header li a {
      padding: 20px 30px;
    }
    .header .menu {
      clear: none;
      float: center;
      max-height: none;
    }
    .header .menu-icon {
      display: none;
    }
  }
  
.slider {
  position: relative;
  width: 680px;
  margin: 120px auto 50px; /* 适配固定header高度 */
  box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}

/* 顶部文本按钮层样式 */
.slider-overlay {
  position: absolute;
  top: 30px;
  left: 30px;
  z-index: 10;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
}

.slider-overlay h2 {
  font-size: 2.2em;
  margin: 0 0 10px;
}

.slider-overlay p {
  font-size: 1.1em;
  margin: 0 0 20px;
}

.cta-btn {
  background-color: #2ecc71;
  color: #fff;
  border: none;
  padding: 12px 24px;
  font-size: 1em;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.cta-btn:hover {
  background-color: #27ae60;
}

.slider input[name="switch"] {
  display: none;
}

.switch {
  position: absolute;
  left: 0;
  bottom: -40px;
  text-align: center;
  width: 100%;
}

.switch label {
  display: inline-block;
  width: 8px;
  height: 8px;
  cursor: pointer;
  margin: 0 5px;
  box-shadow: 0 0 2px 0 rgba(0, 0, 0, .8);
  border-radius: 50%;
  border: 5px solid #2f363c;
  background-color: #738290;
}

#btn1:checked~.switch label[for="btn1"] {
  background-color: white;
}

#btn2:checked~.switch label[for="btn2"] {
  background-color: white;
}

#btn3:checked~.switch label[for="btn3"] {
  background-color: white;
}

.slider-inner {
  overflow: hidden;
}

.slides {
  width: 300%; /* 3张图每张占100%,更合理 */
  transition: all 0.5s;
}

.slides img {
  width: 680px;
  height: 340px;
  float: left;
}

#btn1:checked~.slider-inner .slides { /* 修复选择器错误 */
  transform: translate(0);
}

#btn2:checked~.slider-inner .slides {
  transform: translate(-680px);
}

#btn3:checked~.slider-inner .slides {
  transform: translate(-1360px);
}

内容的提问来源于stack exchange,提问作者Garfild Raven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:50:52