
在网页设计中,我们经常需要为输入框添加视觉效果以提升用户体验。`box-shadow`是实现元素阴影效果的常用属性,但它通常只在元素的外部边缘生效。有时,设计需求可能要求阴影的颜色仿佛“渗透”到输入框内部,与输入框的背景融为一体,形成一种带有偏移感的内部填充效果。本文将深入探讨如何通过css的巧妙组合,实现这种既有外部阴影又有内部“填充”的独特输入框样式。
要实现阴影颜色“透过”输入框可见的效果,我们需要结合两种CSS技术:
我们将通过一个具体的输入框示例来演示如何实现这种效果。假设我们有以下HTML结构:
<input type="text" class="input-field" name="day" required placeholder="DD"> <input type="text" class="input-field" name="month" required placeholder="MM"> <input type="text" class="input-field" name="year" required placeholder="YYYY">
为了方便演示,我们使用一个通用的类名input-field。
首先,为输入框设置基本样式,包括尺寸、边框、字体颜色等。核心在于设置透明背景,并应用box-shadow和background-image。
立即学习“前端免费学习笔记(深入)”;
.input-field {
  width: 176px;
  height: 60px;
  border: 2px solid #FFFFFF; /* 白色边框 */
  background: rgba(0, 0, 0, 0); /* 关键:设置背景透明 */
  /* 内部填充效果:使用linear-gradient创建纯色块 */
  background-image: linear-gradient(#54B3A1, #54B3A1); 
  /* 内部填充的定位:偏移量 = box-shadow偏移量 - 边框宽度 */
  background-position: 12px 12px; 
  background-repeat: no-repeat; /* 防止背景重复 */
  /* 外部阴影效果 */
  box-shadow: 14px 14px 0px 0px #54B3A1; /* 外部阴影,与内部填充颜色一致 */
  margin-right: 40px;
  color: white;
  font-size: 18px;
  font-family: futuraptbook; /* 假设的字体 */
  text-align: center;
}解析:
当输入框获得焦点时,通常需要改变其样式以提供视觉反馈。在这个示例中,我们希望在焦点状态下移除阴影和内部填充,并可能调整输入框的尺寸。
.input-field:focus {
  box-shadow: 0px 0px 0px 0px black; /* 移除阴影 */
  width: 218px; /* 宽度增加 */
  margin-right: -1px;
  margin-left: -1px;
  border-radius: 0px;
  background-image: linear-gradient(transparent, transparent); /* 移除内部填充 */
}解析:
当输入框内容符合required属性或其他验证规则时,可以恢复或应用特定的样式。在这个例子中,我们假设在内容有效时,输入框恢复到带有阴影和内部填充的原始状态,但可能保持焦点状态下调整后的尺寸。
.input-field:valid {
  box-shadow: 14px 14px 0px 0px #54B3A1; /* 恢复阴影 */
  background-image: linear-gradient(#54B3A1, #54B3A1); /* 恢复内部填充 */
  background-position: 12px 12px; /* 恢复内部填充位置 */
  background-repeat: no-repeat;
  width: 218px; /* 保持焦点状态下的宽度 */
  margin-right: -1px;
  margin-left: -1px;
  border-radius: 0px;
}解析:
为了更清晰地展示,我们将相关的CSS和HTML合并如下:
<!DOCTYPE html>
<html>
<head>
  <title>输入框盒阴影与内部填充效果</title>
  <style>
    /* 假设的字体定义,实际项目中需引入字体文件 */
    @font-face {
      font-family: futuraptbook;
      src: url(../fonts/futuraptbook.ttf); /* 请替换为实际路径 */
    }
    @font-face {
      font-family: futuraptdemi;
      src: url(../fonts/futuraptbook.ttf); /* 请替换为实际路径 */
    }
    body {
      background: black;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
      font-family: sans-serif;
    }
    .container {
      background: none;
      padding: 20px;
      border-radius: 8px;
      text-align: center;
    }
    .age-label {
      font-size: 15px;
      text-align: left;
      line-height: 20px;
      font-family: futuraptdemi;
      color: white;
      letter-spacing: 1.5px;
      display: block;
      margin-bottom: 5px;
    }
    .input-wrapper {
      display: flex;
      gap: 20px; /* 调整输入框之间的间距 */
      margin-top: 10px;
      justify-content: center;
    }
    .input-group {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
    }
    /* 通用输入框样式 */
    .input-field {
      width: 176px;
      height: 60px;
      border: 2px solid #FFFFFF;
      background: rgba(0, 0, 0, 0); /* 关键:背景透明 */
      background-image: linear-gradient(#54B3A1, #54B3A1); /* 内部填充色 */
      background-position: 12px 12px; /* 内部填充偏移量 */
      background-repeat: no-repeat;
      box-shadow: 14px 14px 0px 0px #54B3A1; /* 外部阴影 */
      color: white;
      font-size: 18px;
      font-family: futuraptbook;
      text-align: center;
      transition: all 0.3s ease; /* 添加过渡效果 */
    }
    /* 焦点状态 */
    .input-field:focus {
      box-shadow: 0px 0px 0px 0px black; /* 移除阴影 */
      width: 218px; /* 宽度增加 */
      margin-right: -1px; /* 微调位置 */
      margin-left: -1px; /* 微调位置 */
      border-radius: 0px;
      background-image: linear-gradient(transparent, transparent); /* 移除内部填充 */
      outline: none; /* 移除默认焦点轮廓 */
    }
    /* 有效状态 (当输入框内容有效时) */
    .input-field:valid {
      box-shadow: 14px 14px 0px 0px #54B3A1; /* 恢复阴影 */
      background-image: linear-gradient(#54B3A1, #54B3A1); /* 恢复内部填充 */
      background-position: 12px 12px;
      background-repeat: no-repeat;
      width: 218px; /* 保持焦点状态下的宽度 */
      margin-right: -1px;
      margin-left: -1px;
      border-radius: 0px;
    }
    /* 为了演示方便,添加一个按钮 */
    .btn {
      border-radius: 27px;
      background: #D94016;
      width: 300px;
      height: 50px;
      margin-top: 50px;
      color: white;
      font-size: 18px;
      font-family: futuraptdemi;
      border: none;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1 style="color: white; font-family: futuraptdemi;">WHEN WERE YOU BORN</h1>
    <div class="input-wrapper">
      <div class="input-group">
        <label class="age-label" for="day">DAY (DD)</label>
        <input type="text" class="input-field" id="day" name="day" required placeholder="DD">
      </div>
      <div class="input-group">
        <label class="age-label" for="month">MONTH (MM)</label>
        <input type="text" class="input-field" id="month" name="month" required placeholder="MM">
      </div>
      <div class="input-group">
        <label class="age-label" for="year">YEAR (YYYY)</label>
        <input type="text" class="input-field" id="year" name="year" required placeholder="YYYY">
      </div>
    </div>
    <button class="btn">I AM OF LEGAL DRINKING AGE</button>
  </div>
</body>
</html>通过巧妙地结合box-shadow和background-image(使用linear-gradient创建纯色块),我们可以突破box-shadow仅限于元素外部的限制,实现一种独特的、带有内部填充感的阴影效果。这种技术为UI设计师和前端开发者提供了更丰富的视觉表现力,能够创建更具吸引力和交互性的输入框组件。理解background-position与box-shadow偏移量及边框宽度的关系,是掌握此技术的关键。
以上就是CSS技巧:实现输入框内部可见的盒阴影与外部阴影融合效果的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号