【问题标题】:CSS gradient border not showing correctlyCSS渐变边框未正确显示
【发布时间】:2017-12-05 18:43:40
【问题描述】:

我想为border-color 的text 输入设置一半的蓝色和橙色。

我尝试使用渐变,但它不起作用。

我的代码有什么问题?

.search {
  width: 550px;
  height: 50px;
  margin-left: 350px;
  border-radius: 20px;
  outline: none;
  margin-top: 70px;
  border: solid;
  border-image: linear-gradient(to right, rgb(254, 113, 53) 50%, rgb(55, 154, 214) 50%);
  font-size: 20px;
  text-align: center;
  transition: all 0.2s linear;
}

.search:hover,
.search:focus {
  border: #4cbea5 solid;
}
<div>
  <form method="post">
    <input type="Search" class="search" placeholder="Search">
  </form>
</div>

【问题讨论】:

    标签: html css linear-gradients


    【解决方案1】:

    您需要像这样在border-image 中指定切片值:

    .search {
      width: 550px;
      height: 50px;
      border-radius: 20px;
      outline: none;
      margin-top: 70px;
      border: solid;
      border-image: linear-gradient(to right, rgb(254, 113, 53) 50%, rgb(55, 154, 214) 50%) 2;
      font-size: 20px;
      text-align: center;
     }
    <form method="post">
        <input type="Search" class="search" placeholder="Search">
      </form>

    你可以阅读更多关于border-image

    顺便说一句,您不能将border-radius 与border-image 一起使用,但您可以使用多个背景并通过调整background-clip 来实现相同的效果。这也将允许您具有悬停行为:

    .search {
      width: 550px;
      height: 50px;
      border-radius: 20px;
      outline: none;
      margin-top: 70px;
      border: 2px solid transparent;
      background: 
        linear-gradient(#fff,#fff) content-box,
        linear-gradient(to right, rgb(254, 113, 53) 50%, rgb(55, 154, 214) 50%) border-box;
      font-size: 20px;
      text-align: center;
      transition: all 0.2s linear;
    }
    
    .search:hover,
    .search:focus {
      border-color:#4cbea5;
    }
    <form method="post">
        <input type="Search" class="search" placeholder="Search">
      </form>

    相关:Border Gradient with Border Radius

    【讨论】:

      【解决方案2】:

      要在您正在寻找的边框上获得半橙色、半蓝色渐变,请使用border-image-slice 属性并将蓝色和橙色边框图像应用于搜索类。您可以查看干净的边框渐变和悬停时的平滑过渡。 希望,它会有所帮助。

      .search{
        width: 550px;
        height: 50px;
        margin-left: 350px;
        border-radius: 20px;
        outline: none;
        margin-top: 70px;
        border: solid;
        border-image: linear-gradient(to right, #4cbea5 0%, orange 100%);
        border-image-slice: 1;
        font-size: 20px; 
        text-align: center;
         transition: all 0.2s linear;
      }
      .search:hover , .search:focus{
      border: #4cbea5 solid;
      }
      <div>
          <form method="post">
          <input type="Search" class="search" placeholder="Search">
          </form>
          </div>

      【讨论】:

        【解决方案3】:

        你在这个渐变线上打错了,应该是这样的:

          border-image: linear-gradient(to right, rgb(254, 113, 53), rgb(55, 154, 214)) 1 20%;
        

        查看运行演示:

        .search {
          width: 550px;
          height: 50px;
          margin-left: 50px;  /* adjust as needed */
          border-radius: 20px;
          outline: none;
          margin-top: 70px;
          border: solid 5px;  /* made thicker for illustration purposes only */
          border-image: linear-gradient(to right, rgb(254, 113, 53), rgb(55, 154, 214)) 1 20%;
          font-size: 20px;
          text-align: center;
        }
        
        .search:hover,
        .search:focus {
          border: #4cbea5 solid;
        }
        <div>
          <form method="post">
            <input type="Search" class="search" placeholder="Search">
          </form>
        </div>

        【讨论】:

        • 我认为没有错字但缺少数字:) 因为 OP 不想要渐变而是 2 个纯色。所以他的梯度是正确的,他只是缺少一些属性;)
        猜你喜欢
        • 2015-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-27
        • 1970-01-01
        相关资源
        最近更新 更多