CSS 渐变边框未正确显示

CSS gradient border not showing correctly

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

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

我的代码有什么问题?

.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>

您需要像这样在 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-radiusborder-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-image-slice 属性 并在搜索中应用蓝色和橙色边框图像 class.您可以看到干净的边框渐变和悬停时的平滑过渡。 希望对你有帮助。

.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>

你这条渐变线打错了,应该是这样的:

  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>