


Description of using the calc() property in CSS3 to express size values in calculations
This article describes how to use the calc() attribute in CSS3 to express size values in calculations
The usage of calc() is very clever. You can list the formulas like we do math problems in school. Calculate length, width and other values to achieve adaptive layout to a certain extent. Below we will introduce how to use the calc() attribute in CSS3 to express size values in calculations
When we want to implement adaptive layout of the page , usually because of the existence of margin, it is more troublesome; sometimes when you want to implement a width-adaptive input box, it is also quite cumbersome because of the existence of padding or margin, and at the same time, the final effect is inconsistent due to browser compatibility. The new attribute box-sizing added to CSS3 solves the above problem to a certain extent. In today's article, we will use another newly added attribute calc() of CSS3 to implement adaptive layout.
calc() is a newly added attribute of CSS3. It allows you to use an arithmetic expression to express the length value, which means you can use it to define the width of p and set margin, padding, border, etc.
Calc() operation rules:
1. Use the four arithmetic operations "+", "-", "*", and "/";
2. You can use units such as percentage, px, em, rem, etc. ;
3. Various units can be mixed for calculation.
Usage
The calc() syntax is very simple, just like when we learned addition (+), subtraction (-), multiplication (*), and division (/) when we were children, use Mathematical expression to represent:
.haorooms { width: calc(expression); }
In this way, using padding, margin and percentage together, the problem can be solved.
For example, our margin is 20px. Then we can write
.haorooms{ width: calc(100% - 20px); //注:减号前后要有空格,否则很可能不生效!! }
or we can use it like this:
.box { background: #f60; height: 50px; padding: 10px; border: 5px solid green; width: 90%;/*写给不支持calc()的浏览器*/ width:-moz-calc(100% - (10px + 5px) * 2); width:-webkit-calc(100% - (10px + 5px) * 2); width: calc(100% - (10px + 5px) * 2); }
Example
Example 1: Positioning Block elements on the page, with margins
.banner { position:absolute; left: 40px; width: -moz-calc(100% - 80px); width: -webkit-calc(100% - 80px); width: calc(100% - 80px); border: solid black 1px; box-shadow: 1px 2px; background-color: yellow; padding: 6px; text-align: center; }
Example 2: Auto-resizing form, also adapting to the container
input { padding: 2px; display: block; width: -moz-calc(100% - 1em); width: -webkit-calc(100% - 1em); width: calc(100% - 1em); } #formbox { width: -moz-calc(100%/6); width: -webkit-calc(100%/6); width: calc(100%/6); border: 1px solid black; padding: 4px; }
<form> <p id="formbox"> <label>Type something:</label> <input type="text"> </p> </form>
The above is the detailed content of Description of using the calc() property in CSS3 to express size values in calculations. For more information, please follow other related articles on the PHP Chinese website!

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
