Home Web Front-end CSS Tutorial Several useful css function tips

Several useful css function tips

Nov 20, 2017 pm 01:19 PM
css Skill

CSS provides a style description for the HTML markup language, defining how elements in it are displayed. CSS is a breakthrough in the field of web design. It can be used to modify a small style to update all page elements related to it. In this article we will introduce 8 useful tips in CSS functions.

1. Pure CSS Tooltip

Many websites still use JavaScript to create Tooltip effects, but in fact it can be achieved more simply through CSS. The easiest way is to add an attribute with the tooltip text in your HTML code, such as data-tooltip="…" . Then you can add the following code to your CSS file to display the tooltip text through the attr() function:

.tooltip::after { content: attr(data-tooltip);
}

Quite simple, right? Of course, we actually need more code to add styles to the hints, but don't worry, there are powerful and pure CSS libraries called Hint.css and Balloon.css designed for this.

2. Use custom data attributes and attr() function

We have learned how to use attr() to create prompts, and there are also some scenarios where this function can be used. Combined with data attributes, you can create thumbnails with a title and description with a simple line of HTML:


Illustration

Now you can display the title and description through the attr() function:

.caption::after { content: attr(data-title);
...
}

The following are specific examples:

Note: This method may have some problems with browser support. For specific content, you can check the article Accessibility support for CSS generated content.

3. CSS Counters

You can achieve awesome features with CSS Counters. This is not a very well-known property, and most people may even think that browsers do not support this property well, but in fact all browsers support this property:

But you should not use CSS counters Used on ordered lists

    , it is more suitable for use on numbers displayed under pagination or image galleries. You can see how to count selected items with very little code (and even no JavaScript) in the following example:

    CSS counters are also great for displaying lists of items that can be reordered via drag and drop Dynamically changing numbers on:

    As with the last example, we need to remember that the content generated through this method may have some accessibility issues.

    4. Frosted effect achieved by CSS filter

    In iOS7, Apple implemented the "frosted glass" effect - translucent, blurred elements that look like they cover a layer of frosted glass. Inspired by Apple, this effect is used in many places. Recreating this effect was a bit tricky before the advent of CSS filters. You have to achieve this frosted glass effect by using Blur Image. But now CSS filters are supported by almost all major browsers, so it is much easier to reproduce this effect.

    In the future, we can achieve this effect through background filters and filter() functions, but currently only Safari supports both functions.

    For more information about CSS filter, you can click here to learn more.

    5. Use HTML elements as background

    Generally we can set a JPEG or PNG file as the background, or we can also set a gradient background. But did you know that you can set a

    as a background image by using the element() function? For now, the element() function is only supported in Firefox:

    The possibilities are endless, here is an example on MDN.

    For relevant introduction to CSS element() function, please click here.

    6. Create a better grid through calc()

    Fluid meshes are great but they still have serious problems. For example, the spacing at the top and bottom is almost never the same as the spacing at the left and right. Also, if a different grid system is used, the markup will be very confusing. Although flex layout is not the final solution, by combining it with calc() (which can be used as a property value in the CSS file), we are able to create a better grid. Here, George Martsoukos lists many examples, such as a gallery grid with perfect spacing. By using a CSS pre-compiled language such as Sass, building a creative grid system can be very simple and easy to maintain. At the same time, browser support for calc() is almost perfect, so calc() is definitely a function you should master.

    For an introduction to the CSS calc() function, click here.

    7. Align position:fixed elements through calc()

    Another function of calc() is to align position:fixed elements. For example, you have a content wrapper with flowing spacing on the left and right. You want to accurately align elements with a fixed position within this content wrapper. However, in this case, it is very difficult to calculate the specific assignment of the left and right attributes. difficulty. With calc(), you can combine relative and absolute values ​​to precisely position your elements:

    .wrapper { max-width: 1060px; margin: 0 auto;
    }.floating-bubble { position : fixed; right: calc(50% - 530px); /* 50% - half your wrapper width */}

    For example:

    You can have a detailed introduction on this aspect Read the article "Aligning position: fixed Elements with CSS calc" by @brnnbrn.

    8. Use cubic-bezier() to implement animation

    In order to make the user interface of a website or APP more attractive, you can use some animations, but you can choose the speed of the transition effect. Curves are quite limited, e.g. linear or ease-in-out. The standard speed curve cannot even achieve the effect of elastic movement. By using the cubic-bezier() function, you can achieve exactly the animation you want.

    There are two ways to use cubic-bezier() - understand the mechanism behind it and create it yourself, or use the cubic-bezier generator.

    To be honest, I use the latter.

    For a detailed introduction to cubic-bezier(), please click here.

    Summary

    Smarter use of CSS functions can not only solve the above problems such as creating a better grid, it can also give you more creative freedom. As browser support gets better and better, you can use CSS functions such as calc() to modify and improve your previous CSS code.

    This article is translated based on @Anselm Urban's "8 Clever Tricks with CSS Functions". The entire translation contains our own understanding and thoughts. If the translation is not good or there is something wrong, please ask friends in the industry for advice. If you want to reprint this translation, please indicate the English source: https://www.sitepoint.com/8-clever-tricks-with-css-functions.

    The above are 8 css function tips. Friends who think they are good should quickly collect them.

    Related recommendations:

    Jquery css function to dynamically manipulate the style of DOM nodes

    ##CSS3 tips for drawing various basic graphics

    The most complete CSS development common skills

    The above is the detailed content of Several useful css function tips. 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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles