Three CSS tips you must know
The fierce competition between various browsers means that more and more people are now beginning to use devices that support the latest and most advanced W3C Web standards to access the Internet in a more interactive way. This means that we can finally take advantage of more powerful and flexible CSS to create simpler and better-maintained browser front-end code. Now let's take a look at some exciting CSS features that you may not be aware of yet.
Use attr() to display HTML attribute values in CSS
attr()The function has appeared as early as the CSS 2.1 standard, but it is only now becoming generally popular. It provides a clever way to use attributes on HTML tags in CSS, which in many cases can help you save the process that previously required Javascript processing.
To use this feature, you need to use three elements: a :before or :after CSS pseudo-class style, .content attribute, and an attr() expression with the name of the HTML attribute you want to use. For example, if you want to display the value of the data-prefix attribute on the title, you can write like this:
h3:before { content: attr(data-prefix) " "; } <h3 data-prefix="Custom prefix">This is a heading</h3>
Obviously, this example does not display How useful it is, just shows its basic usage. Let's try a more useful example. An excellent application of attr() is to display links to pages when the user prints the page. To achieve this, you can write like this:
@media print { a:after { content: " (link to " attr(href) ") "; } } <a href="example.com">Visit our home page</a>
Once you know this technique, you will be surprised at how convenient it can bring to your work many times!
Tips: In the new version of the CSS3 standard, the attr() function has been extended and can be used in various CSS tags. In CSS2.1 attr() always returns a string. In CSS3 attr() can return many different types.
Use counter() to automatically add serial numbers to the list
Another function already supported in CSS 2.1 is counter(). Using it, you can conveniently Add serial numbers to page titles, blocks, and various other consecutive page content. With it, you don't have to be limited to using to achieve this effect. You can use custom number sequences on the page more flexibly.
Counter-reset definition and usage
The counter-reset attribute sets the value of the counter for the number of occurrences of a certain selector. Default is 0.
Using this property, the counter can be set or reset to any value, either positive or negative. If number is not provided, it defaults to 0.
Note: If "display: none" is used, the counter cannot be reset. If you use "visibility: hidden" you can reset the counter.
Note: Internet Explorer 8 (and later) supports the counter-reset attribute if !DOCTYPE is specified.
counter-resetPossible values
Value | Description |
---|---|
none | default. The selector counter cannot be reset. |
id number | id Defines the selector, id, or class that resets the counter. number Sets the value of the counter for the number of occurrences of this selector. Can be positive, zero, or negative. |
inherit | Specifies that the value of the counter-reset attribute should be inherited from the parent element. |
The key is that it is really simple: add counter( to the content attribute in the :before pseudo-class ):
body { counter-reset: heading; } h4:before { counter-increment: heading; content: "Heading #" counter(heading) "."; }
If you want to know more about this counter zeroing and incrementing method, please refer to the Mozilla
Developer Network page on this topic. There is an excellent example of how to use nested counters.
Using calc() to do arithmetic
Last, but not least, let’s talk about the calc() function. Calc is the abbreviation of the English word calculate. It is a new function of CSS3 and is used to specify the length of elements. This function allows you to perform simple arithmetic calculations, such as calculating the length and width of an element, without having to write unmaintainable Javascript code. This function supports all simple basic arithmetic operations, including addition, subtraction, multiplication and division.
When there are "+" and "-" in the expression, there must be spaces before and after them. For example, "widht: calc(12%+5em)" is wrong to write without spaces; expression When there are "*" and "/", there can be no spaces before and after them, but it is recommended to leave spaces. The browser's compatibility with calc() is pretty good. It is well supported in IE9+, FF4.0+, Chrome19+, and Safari6+. It is also necessary to add the identifier of each browser manufacturer in front of it, but unfortunately, Most mobile browsers do not support it yet, and currently only "firefox for android 14.0" supports it.
Suppose you want to create an element whose width takes up the full width of its parent element, but leave some pixels wide for other uses:
.parent { width: 100%; border: solid black 1px; position: relative; } .child { position: absolute; left: 100px; width: calc(90% - 100px); background-color: #ff8; text-align: center; }
Beautiful, isn’t it? ?
We can find more and more clearly that CSS has matured to the point where it can replace JavaScript in some methods, greatly simplifying the work of web developers. You'd be foolish not to start taking advantage of these features.
The above is the detailed content of Three CSS tips you must know. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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



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.

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.

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.

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

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.

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-

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 use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
