The power of CSS is far beyond many web developers' imagination. Over time, the stylesheet language has become more and more powerful, bringing functions to the browser that originally required JavaScript to achieve. This article will introduce eight clever CSS function tips that require no JavaScript at all.
Key points
- CSS functions can be used to create various effects and functions such as tooltips, thumbnail titles, counters, and frosted glass effects without JavaScript.
-
calc()
Functions can be used to create smarter grid systems and alignment of fixed position elements, providing greater flexibility and precision in design.
The - CSS function also allows creative animations using the
cubic-bezier()
function, as well as the potential to use HTML elements as background images using theelement()
function, although the latter is currently only supported by Firefox.
Pure CSS Tooltips
Many websites still use JavaScript to create tooltips, but in fact it is easier to use CSS. The easiest solution is to use data properties in HTML code to provide tooltip text, such as data-tooltip="…"
. With this tag, you can put the following into CSS to display tooltip text within the attr()
function:
.tooltip::after { content: attr(data-tooltip); }
It's very simple and clear, right? Of course, more code is needed to actually style the tooltips, but don't worry, there is a great pure CSS library specifically for this purpose called Hint.css.
(abuse) custom data attributes and attr() function
We have used attr()
for tooltips, but it has some other use cases. Combining data properties, you can build thumbnail images with titles and descriptions using just one line of HTML code:
<a class="caption" href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b" data-title="禿鷲" data-description="..."> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174011191897662.jpg" class="lazy" alt="8 Clever Tricks with CSS Functions " /> </a>
You can now use the attr()
function to display the title and description:
.caption::after { content: attr(data-title); ... }
This is a working example of a thumbnail with hover display animation title: [CodePen example link] (assuming that CodePen link should be inserted here)
Note: The generated content may have problems with accessibility. This article on accessibility support for CSS generated content provides a useful interpretation of this topic.
You can use CSS counters, which seems like magic. It's not the most well-known feature, and most people may think it doesn't support it very well, but in fact, every browser supports CSS counters: [Can I Use css-counters?] (assuming Can I Use should be inserted here Link)
While you should not use CSS counters for ordered lists (<ol>
), counters are great for pagination or displaying numbers under items in gallery. You can also count the number of selected items, which is very impressive considering that you need very little code (and no JavaScript): [CodePen sample link] (assuming that CodePen link should be inserted here).
CSS counters are also great for dynamically changing numbers in the project list, which can be reordered by drag and drop: [CodePen Example Link] (assuming that CodePen link should be inserted here).
As with the last example, remember that the generated content may have problems with accessibility.
With the release of iOS 7, Apple brings us a “frosted glass” effect—transparent, blurry elements that look like frosted glass windows. Inspired by Apple's implementation, this effect began to appear in many places. Reproducing this effect is a bit tricky before you have a CSS filter, you have to use a blurred image to create a frosted glass effect. Now, almost all major browsers fully support CSS filters, so it's much easier to reproduce this effect. [Can I Use css-filters?](assuming that Can I Use link should be inserted here)
In the future, we can use the
and functions to create similar effects, but these two functions are currently supported only by Safari. backdrop-filter
filter()
Usually, you specify a JPEG or PNG file as the background image, or maybe a gradient. But did you know that using the
function, you can also use as the background image? Currently, the element()
function is only supported by Firefox: [Can I Use css-element-function?] (assuming that the Can I Use link should be inserted here) <div>
element()
The possibilities are almost limitless, here is an example from MDN.
Fluid mesh is a great thing, but there are some serious problems. For example, it is not possible to make the spacing between the top and the bottom the same size as the spacing between the left and right. Also, the marking is very confusing, depending on the grid system you are using. Even flexbox itself is not the final solution, but using the
function (which can be used as a value in CSS), the grid can get better. In this tutorial on this website, George Martsoukos shows some practical examples, such as a gallery grid with perfect spacing. Using CSS preprocessors like Sass, building creative mesh systems can be very simple and easy to maintain. Since browser support is almost perfect, is a convenient feature you should definitely use. [Can I Use calc?](assuming that Can I Use link should be inserted here)calc()
calc()
Another use case for a function is to align elements with fixed positions. For example, if you have a content wrapper on the left and right with fluid spacing, and you want to precisely align the fixed elements inside that wrapper - you will have a hard time determining the "right" or "left" attribute selection Which value? With , you can combine relative and absolute values ??to align elements perfectly:
calc()
calc()
.tooltip::after {
content: attr(data-tooltip);
}
To make the UI of a website or application more attractive, you can use animations, but the standard easing options are very limited. For example, "linear" or "ease-in-out". Something like bouncing animation is not possible even with standard options. Using the
function, you can animate elements exactly as you wish.
cubic-bezier()
There are two ways to use
generator. cubic-bezier()
cubic-bezier
Honestly, I will choose the latter.
Skillful use of CSS functions not only solves known problems (such as building a smarter grid system), but also provides you with greater creative freedom. As browser support gets better, you should carefully look at your CSS and improve it using functions like
.
calc()
As a beginner, what basic CSS functions should I understand?
CSS function is used to set the value of the CSS attribute. Some basic functions that every beginner should know include
,, rgb()
, rgba()
, hsl()
, and hsla()
. The calc()
and rgb()
functions are used to define colors, while the rgba()
and hsl()
functions are used to define colors based on hue, saturation, and brightness. The hsla()
function allows you to perform calculations to determine the CSS attribute value. calc()
The
function in in CSS is used to perform calculations that can be used as attribute values. This function can use the " " (addition), "-" (subtraction), "*" (multiple), and "/" (division) mathematical operators. For example, you can use calc()
to create a div that always minus 20 pixels for viewport width 50% as shown below: calc()
. div { width: calc(50% – 20px); }
The main difference between the
and functions in rgb()
CSS is that rgba()
contains an alpha channel that specifies the opacity of the color. The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque). For example, to set translucent red, you can use: rgba()
. color: rgba(255, 0, 0, 0.5);
How to create gradients using CSS functions?
CSS functions can be used to create gradients using the linear-gradient()
and radial-gradient()
functions. For example, to create a linear gradient from red to blue, you can use: background: linear-gradient(red, blue);
. Similarly, to create a radial gradient from the center red to the edge of the blue, you can use: background: radial-gradient(red, blue);
.
Can I convert elements using CSS functions?
Yes, CSS functions can be used to convert elements. The transform
attribute can be used with various functions such as rotate()
, scale()
, skew()
, and translate()
to modify the appearance of an element. For example, to rotate an element 45 degrees, you can use: transform: rotate(45deg);
.
What is the purpose of the attr() function in CSS?
The function in attr()
in CSS is used to return the attribute value of the selected element. This is useful for generating content, etc. For example, you can use attr()
to display the value of the link's "href" property in the tooltip: a:after { content: attr(href); }
.
How to use the var() function in CSS?
The var()
function in CSS is used to insert the value of a custom property (also known as a "CSS variable". For example, you can define a custom property --main-color: blue;
and then use it in CSS as follows: color: var(--main-color);
.
Can I create animations using CSS functions?
Yes, CSS functions can be used to create animations. The animation
attribute in CSS is abbreviated attribute for eight different attributes, including animation-name
, animation-duration
, animation-timing-function
, etc. For example, to create a 2-second animation called "slidein", you can use: animation: slidein 2s;
.
url() function in CSS?
The function in url()
in CSS is used to include files. The most common use of functions is to link to external stylesheets or include images. For example, to set the background image of an element, you can use: url()
. background-image: url('image.jpg');
Yes, the CSS function can be used to create 3D transformations. The
attribute in CSS can be used with functions such as transform
, rotateX()
, rotateY()
, rotateZ()
, scale3d()
, and translate3d()
to create 3D transformations. For example, to rotate an element around the X-axis, you can use: transform: rotateX(45deg);
.
Please note that I have tried my best to pseudo-originalize the original text and preserve the original format and location of the image. Since I cannot access the CodePen and Can I Use websites, I replaced the related link with placeholders. Please find and insert the correct link yourself.
The above is the detailed content of 8 Clever Tricks with CSS Functions. 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

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

CSS,orCascadingStyleSheets,isthepartofwebdevelopmentthatcontrolsawebpage’svisualappearance,includingcolors,fonts,spacing,andlayout.Theterm“cascading”referstohowstylesareprioritized;forexample,inlinestylesoverrideexternalstyles,andspecificselectorslik
