Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

css - Responsive two gradient diagonal button

I have a button that is split diagonally. I want to add a different gradient to each side. I've tried splitting in two using :before and :after but that requires a rotate or skew which means changing the deg which isn't very responsive. Basically I need the cut off in the center to stay top left and bottom right at different breakpoints regardless of the width and height. Is there a better way to do this?

.btn {
  background-image: linear-gradient(47deg, #f09 0%, #A09 100%);
  display: inline-block;
  font-size: 1.6rem;
  text-decoration: none;
  text-align: center;
  cursor: pointer;
  max-width: 32rem;
  width: 100%;
  padding: 2.6rem 1rem;
  color: $white;
  text-transform: uppercase;
  vertical-align: middle;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.btn:after {
  content: '';
  width: 100%;
  left: 0%;
  right: 0;
  bottom: 0;
  top: 48%;
  background-image: linear-gradient(47deg, #A09 0%, #f09 100%);
  position: absolute;
  transform: skewY(14deg);
  height: 200%;
  z-index: -1;
}
<a href="" class="btn">Button</a>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can keep one gradient inside the button then consider clip-path on the pseudo element to create a triangle shape and apply the second gradient:

.btn {
  background-image: linear-gradient(to right, yellow,blue);
  display: inline-block;
  font-size: 1.6rem;
  text-decoration: none;
  padding: 2.6rem 1rem;
  color: #fff;
  text-transform: uppercase;
  position: relative;
  z-index: 0;
}

.btn:after {
  content: '';
  position:absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background-image: linear-gradient(to left, yellow,blue);
  -webkit-clip-path:polygon(0 0,100% 0,100% 100%);
  clip-path:polygon(0 0,100% 0,100% 100%);
  z-index: -1;
}
<a href="" class="btn">Button</a>
<a href="" class="btn">Another Button</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...