user8971711
user8971711

Reputation:

CSS gradient border not showing correctly

I want to set half blue and orange for border-color of text input.

I tried using a gradient but it doesn't work.

what is my code problem?

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

Upvotes: 4

Views: 4654

Answers (3)

Temani Afif
Temani Afif

Reputation: 272703

You need to specify the slice value inside border-image like this :

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

You can read more about border-image

By the way, you cannot use border-radius with border-image but you can achive the same using multiple background and by adjusting background-clip. This will also allow you to have the hover behavior:

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

Related: Border Gradient with Border Radius

Upvotes: 6

user7207170
user7207170

Reputation:

To get the half orange, half blue gradient on the border you are looking for, make use of border-image-slice property and apply the blue and orange border-image on the search class. You can look at the clean border gradient and the smooth transition on hover. Hope, it helps.

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

Upvotes: 2

blurfus
blurfus

Reputation: 14031

You had a typo on this gradient line, it should be like this:

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

See running demo:

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

Upvotes: 0

Related Questions