Home >Web Front-end >HTML Tutorial >Implement triangles and parallelograms with CSS_html/css_WEB-ITnose

Implement triangles and parallelograms with CSS_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:38:331058browse

This article was originally published on my personal blog: The Taste of Chewing

When I was browsing a technology website recently, I felt that the style of the keywords in the article was so cool. ah. So I copied that writing style to my blog. Maybe friends who have browsed my blog recently have discovered where it appears? The pagination style. Here’s a screenshot:

You can also see such a paging bar at the bottom of the homepage; doesn’t it look good? Let’s take a look at how this is implemented~

The first method: use border

The first method is to use the border attribute hack to create a triangle, and then join the two through a rectangle The triangle ultimately creates a parallelogram. Why can triangles be generated using borders? Let’s take a look at a picture first:

After looking at the changing process of the three small shapes in the picture, you should already understand half of it. In fact, hacking only requires two conditions to create a triangle. First, the length and width of the element itself are 0; second, the unnecessary parts are hidden through border-color. Through a similar method, you can also create trapezoids. The codes for the three shapes in the picture above are as follows. (Attached is a CodePen example)

#first {  width: 20px;  height: 20px;  border-width: 10px;  border-style: solid;  border-color: red green blue brown;}#second {  width: 0;  height: 0;  border-width: 10px;  border-style: solid;  border-color: red green blue brown;}#third {  width: 0;  height: 0;  border-width: 10px;  border-style: solid;  border-color: red transparent transparent transparent;}

The next step is to consider how to splice a parallelogram. In the border method, it consists of three parts, namely the left triangle, the rectangle, and the right triangle. It would be too cumbersome to create three elements every time a parallelogram is drawn, so the :before and :after pseudo-elements are a good choice here. Let's achieve this effect:

In order to seamlessly splice triangles and rectangles together, multiple attributes must be consistent, so use CSS preprocessing such as Less, Sass, Stylus, etc. It will be easier to maintain if you write this code in a browser. The Scss version of the code is given below. (CodePen link attached)

//三角形的宽高$height: 24px;$width: 12px;//对平行四边形三部分的颜色进行赋值@mixin parallelogram-color($color) {  background: $color;  &:before { border-color: transparent $color $color transparent; }  &:after { border-color: $color transparent transparent $color; }}//单个三角形的样式@mixin triangle() {  content: '';  display: block;  width: 0;  height: 0;  position: absolute;  border-style: solid;  border-width: $height/2 $width/2;  top: 0;}//平行四边形的样式.para {  display: inline-block;  position: relative;  padding: 0 10px;  height: $height;  line-height: $height;  margin-left: $width;  color: #fff;  &:after {    @include triangle();    right: -$width;  }  &:before {    @include triangle();    left: -$width;  }  @include parallelogram-color(red);}

It should be noted that if the slope of the triangle set through $height and $width is too small or too large, it may cause aliasing in the rendering, so you need to test it more before using it. What is the visual effect obtained by different width and height.

Second method: use transform

The method of using transform to realize a parallelogram is what I saw when I was shopping. The effect is roughly like this:

After seeing it, I thought it was amazing. It turns out that it can only have the outline of a parallelogram. (Because method one can only create parallelograms with filling effects) It is very simple to implement, mainly with the help of transform: skew(...). Let’s take a look at the source code.

<style>.city {  display: inline-block;  padding: 5px 20px;  border: 1px solid #44a5fc;  color: #333;  transform: skew(-20deg);}</style><div class="city">上海</div>

So we got this effect:

You must think this when you see the picture:

Don’t worry, we have indeed distorted the entire div, causing the text in the middle to be slanted, and this is obviously not the effect we want. So we need to add an inner element and reversely distort the inner element to get the effect we want:

The implementation code is as follows, and a CodePen example is attached

<style>.city {  display: inline-block;  padding: 5px 20px;  border: 1px solid #44a5fc;  color: #333;  transform: skew(-20deg);}.city div {  transform: skew(20deg);}</style><div class="city">  <div>上海</div></div>

Summary

The first method uses the border attribute hack to create a triangle, and finally realizes a parallelogram by splicing three elements; while the second method uses transform: skew Obtain a parallelogram. Overall, the second method is much smaller than the first method and is easy to understand. The only drawback is that it cannot construct trapezoids like those used in the pagination of this site. Hope this article is helpful to you.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn