Home Web Front-end CSS Tutorial Sharing tips on creating a striped background style for web pages using CSS

Sharing tips on creating a striped background style for web pages using CSS

Mar 09, 2017 pm 05:33 PM

Through the linear-gradient in CSS, stripe effects in different directions can be displayed. Here we will learn about the techniques for making striped background styles for Web pages using CSS. Friends in need can refer to the following

1. Horizontal stripes
The following code:

1

background: linear-gradient(#fb3 20%, #58a 80%)

Copy after login

The above code indicates that the upper 20% and lower 20% of the entire image are corresponding solid colors, only the middle one Some are gradient colors. If the middle part is gradually reduced, when the middle part becomes 0, that is, the seven points and end points of the upper and lower colors are the same, there will be no gradient and it will become a color bar of two colors:

1

background: linear-gradient(#fb3 50%, #58a 50%);

Copy after login

Next, you can set the size of the background to make the background height smaller and the background defaults to repeat, so that stripes appear

1

2

background: linear-gradient(#fb3 50%, #58a 50%);  

background-size: 100% 30px;

Copy after login

We don’t need to set the second one If the starting position of the color is set to 0, the browser defaults to starting with the previous color:

1

2

background: linear-gradient(#fb3 30%, #58a 0);  

background-size:100% 30px;

Copy after login

This forms a stripe with 30% yellow and 70% blue. The shape background
can also be set to multiple colors. Three colors of stripes are set below:

1

2

background: linear-gradient(#fb3 33.3%, #58a 0, #58a 66.6%,yellowgreen 0);  

background-size: 100% 45px;

Copy after login

## 2. Vertical stripes

Just add a prefix to the linear-gradient method. Note that you also need to reverse the settings of background-size length and width

1

2

background: linear-gradient(to rightright, #fb3 50%, #58a 0);    

background-size:30px 100%;

Copy after login


3. Diagonal stripes
You can modify the value of background-size And add an angle to the linear-gradient to achieve diagonal stripes:
background: linear-gradient(45deg, #fb3 50%, #58a 0); //Make the gradient of the background tilted
background-size :30px 30px; //Each small component has a fixed width and height
But the result is that only a small piece of slash will be formed, not the slash of the whole p. We need to divide it into four Small p draws a group of oblique lines, and adds the color decomposition in linear-gradient:

1

2

background: linear-gradient(45deg, #fb3 25%, #58a 0, #58a50%, #fb3 0, #fb3 75%, #58a 0);    

background-size:30px 30px;

Copy after login

4. Use repeat-linear-gradient
for oblique lines For line background drawing, it is more efficient to use the repeat-linear-gradient method. When using this method, the set color changes will automatically repeat until the entire p is covered. The example code is as follows:

1

background: repeating-linear-gradient(45deg, #fb3, #58a 30px);

Copy after login

In this way, the angle can be changed arbitrarily without the adjustment difficulties in the above method

background: repeating-linear-gradient(60deg, #fb3, #fb315px, #58a 0, #58a 30px);
(This method is actually equivalent to combining size control and gradient control)


5. About color Setting
Sometimes we hope that the colors of the stripe background are similar to each other, but it is very inconvenient to manually set this color, and it is also difficult to find out what color to choose. You can use the following method:

1

2

3

4

5

background: #58a;    

background-image: repeating-linear-gradient(30deg,    

hsla(0,0%,100%,.1),    

hsla(0,0%,100%,.1)15px,    

transparent 0,transparent 30px);

Copy after login

The principle of this method is: specify the darkest color in the background, and use transparency to adjust other similar colors in the article

6. Comprehensive examples
The renderings are put together here, corresponding to the following styles one-to-one:

Sharing tips on creating a striped background style for web pages using CSS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

.stripes {  

    height: 250px;  

    width: 375px;  

    float: left;  

 

    margin: 10px;  

 

    -webkit-background-size: 50px 50px;  

    -moz-background-size: 50px 50px;  

    background-size: 50px 50px; /* 控制条纹的大小 */

 

    -moz-box-shadow: 1px 1px 8px gray;  

    -webkit-box-shadow: 1px 1px 8px gray;  

    box-shadow: 1px 1px 8px gray;  

}

Copy after login

1

2

3

4

5

6

7

.horizontal {  

    background-color: #0ae;  

    background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));  

    background-image: -moz-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);  

    background-image: -o-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);  

    background-image: linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);  

}

Copy after login

1

2

3

4

5

6

7

.vertical {  

    background-color: #f90;  

    background-image: -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));  

    background-image: -moz-linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);  

    background-image: -o-linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);  

    background-image: linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);  

}

Copy after login

1

2

3

4

5

6

7

8

9

10

11

.picnic {  

    background-color:white;  

    background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))),  

                      -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5)));  

    background-image: -moz-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),  

                      -moz-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));  

    background-image: -o-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),  

                      -o-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));  

    background-image: linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),  

                      linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));  

}

Copy after login
Copy after login

1

2

3

4

5

6

7

8

9

10

11

.picnic {  

    background-color:white;  

    background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))),  

                      -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5)));  

    background-image: -moz-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),  

                      -moz-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));  

    background-image: -o-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),  

                      -o-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));  

    background-image: linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),  

                      linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));  

}

Copy after login
Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

.angled-135 {  

    background-color: #c16;  

    background-image: -webkit-gradient(linear, 0 0, 100% 100%,  

                            color-stop(.25, rgba(255, 255, 255, .2)), color-stop(.25, transparent),  

                            color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .2)),  

                            color-stop(.75, rgba(255, 255, 255, .2)), color-stop(.75, transparent),  

                            to(transparent));  

    background-image: -moz-linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%,  

                        transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%,  

                        transparent 75%, transparent);  

    background-image: -o-linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%,  

                        transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%,  

                        transparent 75%, transparent);  

    background-image: linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%,  

                        transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%,  

                        transparent 75%, transparent);  

}

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

.checkered {  

    background-image: -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, #555), color-stop(.25, transparent), to(transparent)),  

                      -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.25, #555), color-stop(.25, transparent), to(transparent)),  

                      -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.75, transparent), color-stop(.75, #555)),  

                      -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.75, transparent), color-stop(.75, #555));  

    background-image: -moz-linear-gradient(45deg, #555 25%, transparent 25%, transparent),  

                      -moz-linear-gradient(-45deg, #555 25%, transparent 25%, transparent),  

                      -moz-linear-gradient(45deg, transparent 75%, #555 75%),  

                      -moz-linear-gradient(-45deg, transparent 75%, #555 75%);  

    background-image: -o-linear-gradient(45deg, #555 25%, transparent 25%, transparent),  

                      -o-linear-gradient(-45deg, #555 25%, transparent 25%, transparent),  

                      -o-linear-gradient(45deg, transparent 75%, #555 75%),  

                      -o-linear-gradient(-45deg, transparent 75%, #555 75%);  

    background-image: linear-gradient(45deg, #555 25%, transparent 25%, transparent),  

                      linear-gradient(-45deg, #555 25%, transparent 25%, transparent),  

                      linear-gradient(45deg, transparent 75%, #555 75%),  

                      linear-gradient(-45deg, transparent 75%, #555 75%);  

}

Copy after login


HTML code:


1

2

3

4

5

6

<p class="horizontal stripes"></p>

<p class="vertical stripes"></p>

<p class="picnic stripes"></p>

<p class="angled stripes"></p>

<p class="angled-135 stripes"></p>

<p class="checkered stripes"></p>

Copy after login

The above is the detailed content of Sharing tips on creating a striped background style for web pages using CSS. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles