How to use CSS background-size
attributes to create cool background stripes? This article will share a case that demonstrates how to achieve visual effects of background stripes transitions when mouse over with CSS gradients, blend modes, and background-size
properties.
Usually, we use background-size: cover
to make the background image fill the entire element. But this case requires more advanced background size control: background stripes that transition when the mouse hovers. The effect is as follows (please hover your mouse over the following area):
(The dynamic effect demonstration should be inserted here, which is consistent with the original text)
The key to achieving this effect is to use the gradient and blending modes ingeniously. Let's start with a simple HTML structure:
<div></div>
Initial CSS Style:
div { width: 500px; height: 500px; background: palegreen; }
Create background stripes
We can use CSS linear gradient to create stripes. Due to uneven width of the stripes and the need for transition effects, we cannot directly use repeat gradients. Here, we simulate five stripes by superimposing five linear gradient backgrounds and positioning them to the upper right corner of the container:
div { width: 500px; height: 500px; background: linear-gradient(black, black) top right, linear-gradient(black, black) top 100px right, linear-gradient(black, black) top 200px right, linear-gradient(black, black) top 300px right, linear-gradient(black, black) top 400px right, palegreen; }
To simplify the code, we can use custom properties:
div { --gt: linear-gradient(black, black); --n: 100px; width: 500px; height: 500px; background: var(--gt) top right, var(--gt) top var(--n) right, var(--gt) top calc(var(--n) * 2) right, var(--gt) top calc(var(--n) * 3) right, var(--gt) top calc(var(--n) * 4) right, palegreen; }
--gt
means gradient, and --n
controls the vertical offset of the stripe. Currently the linear gradient is set to pure black, which is for subsequent masking and blending effects. To prevent the background from tiling repeatedly, we need to set background-repeat: no-repeat;
:
div { /* ... */ background-repeat: no-repeat; }
Adjust the size and spacing of stripes
The current stripes overlap and are almost impossible to see. We need to use the background-size
attribute to set the width and height of the stripe. The background-size
attribute supports double-value syntax, and we can set the width and height respectively. The following code sets the width of each stripe, using the default value of height: auto
div { /* ... */ background-size: 60%, 90%, 70%, 40%, 10%; }Since the height is
, the stripes will cover each other. We need to use double value syntax and set the same height: auto
div { /* ... */ background-size: 60% var(--n), 90% var(--n), 70% var(--n), 40% var(--n), 10% var(--n); }To add spacing between stripes, we can slightly reduce the height of each stripe:
div { --h: calc(var(--n) - 5px); /* ... */ background-size: 60% var(--h), 90% var(--h), 70% var(--h), 40% var(--h), 10% var(--h); }Mask and Blending Mode
Change the background color to white:
div { /* ... */ background: var(--gt) top right, var(--gt) top var(--n) right, var(--gt) top calc(var(--n) * 2) right, var(--gt) top calc(var(--n) * 3) right, var(--gt) top calc(var(--n) * 4) right, #fff; /* ... */ }To achieve masking and blending effects, we wrap
in a parent container and add a new one: <div>
<code><div>
Layout with CSS Grid: <pre class="brush:php;toolbar:false"><section>
<div></div>
<div></div>
</section></pre>
<p>
</p>Apply gradient colors on the first <pre class="brush:php;toolbar:false">section {
display: grid;
align-items: center;
justify-items: center;
width: 500px;
height: 500px;
}
section > div {
width: inherit;
height: inherit;
grid-area: 1 / 1;
}</pre>, and apply the previous stripe style on the second <p> and implement screen blending mode using <code><div>: <code><div>
<code>mix-blend-mode: screen;
Mouse hover effect
div:nth-child(1) { background: linear-gradient(to right, red, orange); } div:nth-child(2) { /* ... previous styles ... */ mix-blend-mode: screen; }
Finally, we add a mouseover effect to expand the stripe width to the full width of the container:
The final effect is shown at the beginning. Please note that for a better user experience, it is recommended to consider reducing the settings for sports effects to meet the preferences of different users.
section:hover > div:nth-child(2){ background-size: 100% var(--h); transition: background-size 1s; }
This method is good maintainability and customization, and you can easily change the height, color and orientation of the stripes, etc.
I hope this case can help you better understand and apply the CSS attributes. If you have other implementation methods, please share it in the comment section! background-size
The above is the detailed content of Animated Background Stripes That Transition on Hover. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin
