Home > Web Front-end > JS Tutorial > How to implement triangles and parallelograms with CSS?

How to implement triangles and parallelograms with CSS?

零下一度
Release: 2017-06-26 15:25:19
Original
1896 people have browsed it

When I was browsing a technical website recently, I felt that the style of the keywords in the article was so cool, 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 achieved~

The first method: using border

The first method is to use the border attribute hack to create a triangle, and then splice 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 figures 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 code for the three shapes in the above picture is as follows.

#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 something like Less, Sass, Stylus, etc. It will be easier to maintain if you use CSS preprocessor to write this code. The Scss version of the code is given below.

//The width and height of the triangle

$height: 24px;

$width: 12px;

//Adjust the colors of the three parts of the parallelogram Assignment

@mixin parallelogram-color($color) {

background: $color;

&:before { border-color: transparent $color $color transparent; }

&:after { border-color: $color transparent transparent $color; }

}

//Style of a single triangle

@mixin triangle () {

content: '';

display: block;

width: 0;

height: 0;

position: absolute;

border-style: solid;

border-width: $height/2 $width/2;

top: 0;

}

//Style of parallelogram

.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 by $height and $width is too small or too large, it may cause aliasing in the rendering, so you need to test the differences before using it. What is the visual effect obtained by the width and height.

If you want to learn and communicate web front-end technologies such as html5, and want to know more about front-end aspects, you can join our QQ learning group: 27062964, learn and communicate together, improve yourself, and have learning materials and source code sharing. Or click on the link to join the group directly:

Second method: Use transform

Use transform to realize a parallelogram. The effect is probably like this:

After seeing it, I thought it was amazing. It turns out that it can only have the outer 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.

.city {

display: inline-block;

padding: 5px 20px;

border: 1px solid #44a5fc ;

color: #333;

transform: skew(-20deg);

}

上海

So we got this effect:

You must think this when you see the picture:

Don’t worry, we did distort 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, with a CodePen attached Example

.city {

display: inline-block;

padding: 5px 20px;

border: 1px solid # 44a5fc;

color: #333;

transform: skew(-20deg);

}

.city div {

transform: skew(20deg);

}

上海

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 Then use transform: skew to get the 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.

If you encounter any problems during the learning process or want to obtain learning resources, you are welcome to join the learning exchange group

The above is the detailed content of How to implement triangles and parallelograms with CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template