CSS渐变边框无法正确显示

时间:2017-12-05 18:43:41

标签: html css css3 gradient linear-gradients

我想为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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:5)

您需要在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>

答案 1 :(得分:1)

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

&#13;
&#13;
.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;
}
&#13;
<div>
    <form method="post">
    <input type="Search" class="search" placeholder="Search">
    </form>
    </div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这个渐变线上有一个拼写错误,它应该是这样的:

  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>