CSS3 's Calc () uses

Source: Internet
Author: User

Transferred from: http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html

Calc () may be a stranger to everyone, and it's unlikely to believe that Calc () is part of the CSS. Since it looks like a function, why does the function appear in CSS? This also makes me baffled, today a colleague told me that there is a property in CSS3 to achieve adaptive layout, first let me think of is box-sizing, but with me said can also calculate, which let me think of Calc (). Because earlier on the official website and some blog to see the relevant introduction, but has not been in-depth, and did not test themselves. It took me a whole afternoon to study calc () thoroughly today. So we have this blog, I hope to help you.

Usually in the production of the page, always encounter some elements are 100% of the width. As is known to all, if the element width is 100%, it does not have the other box model property set itself OK, if anything else, it will cause the box to burst. For example, there is a border, or margin and padding, which will make your box burst. In other words, if the width of your element is 100%, as long as you add any value to the element, you will burst the element box (in standard mode, in addition to the IE Border,padding,margin mode). This will be quite troublesome, usually we encounter such phenomenon, is also quite cautious, sometimes even can not solve, only by changing the structure to achieve. Even if you do this in a cumbersome way, there is a browser compatibility that results in inconsistent final results. Although the box-sizing in the CSS3 attribute described above solves this problem to some extent, the function of the Calc () function today is easier to achieve.

What is calc ()?

Before Learning Calc (), we need to know what Calc () is. He's the only one who knows what he is. Better use of him in practical use.

Calc () Literally we can interpret him as a function. In fact, Calc is the abbreviation for the English word Calculate (calculation), is a new function of CSS3, which is used to specify the length of the element. For example, you can use Calc () to set dynamic values for attributes such as border, margin, pading, font-size, and width for an element. Why is it a dynamic value? Because we use the expression to get the value. The biggest benefit of Calc (), however, is that the width of the element can be calculated from Calc () using the fluid layout.

What can calc () do?

Calc () allows you to calculate the elements, you can give a DIV element, using percentages, EM, px and REM unit values to calculate its width or height, such as "Width:calc (50% + 2em)", so you do not have to consider the element div width value is exactly how much, And this annoying task to the browser to calculate.

Calc () syntax

The Calc () syntax is very simple, as we learned to add (+), subtract (-), multiply (*), and (/) as a child, using mathematical expressions to represent:

{  width:calc (expression);}  

where "expression" is an expression that is used to calculate the length of an expression.

Calculation rules for Calc ()

Calc () uses common mathematical rules, but it also provides more intelligent functionality:

    1. Use "+", "-", "*" and "/" arithmetic;
    2. can use percentages, px, EM, REM and other units;
    3. Can be mixed with various units for calculation;
    4. When there are "+" and "-" in the expression, there must be a space before and after it, such as "Widht:calc (12%+5em)", which is not the correct way to make a blank;
    5. When there are "*" and "/" in an expression, there can be no spaces before or after it, but it is recommended that you leave a space.
Browser compatibility

The browser to calc () compatibility is good, in ie9+, ff4.0+, chrome19+, safari6+ are better supported, the same needs to be preceded by the browser manufacturer's identifier, but unfortunately, the mobile browser has not only "Firefox for Android 14.0 "Support, others are annihilated.

You also need to add a browser prefix when you are actually using it.

. Elm {/*firefox*/-moz-calc (expression);  /*chrome safari*/-webkit-calc (expression); /*standard */calc ();}  

Through the above understanding, everyone on the Calc () is not so strange, but the actual use may still not understand, then we will follow up with me, through an example to understand him. First, let's look at one of the most common examples:

<class=<class="box" ></div></div>   

The structure above is simple, that is, a DIV.DEMO element contains a div.box element, then we take a step-by-step look at the changes.

First step: Add a Normal style:

{width:300px;  Background:#60f;} width:100%;  Background:#f60;  Height:50px;}           

The effect is simply that div.box completely obscures the Div.demo, as shown in:

Second step, add border and padding on the Div.box

This is a tricky thing to do, add 10px of padding to the Div.box, and add 5px of border:

. demo{Width 300px; background:  #60f; } .box {width:   100%; background:  #f60; height: 50px;  padding: 10px; border: 5px solid green;          }       

In order to better explain the problem, I added a padding:3px 0 on the Div.demo;

. demo{Width 300px;Background  #60f;  padding: 3px 0;       }.box { Width:  100%; background:  #f60; height: 50px; padding: 10px; border: 5px solid Green;              

At this time, we don't know if we can think of the problem. In fact, it is very simple, this time the width of the div.box is greater than the total width of its container div.demo, thus bursting the container out,:

The third step, the use of Calc ()

In order to solve the problem of bursting containers, previously we can only calculate the width of the div.box, with the container width minus the value of padding and border, but sometimes we do not know the total width of the element, such as adaptive layout, only know a hundred points, but the other values are PX and other values, This is the difficulty, dead stuck. With the advent of CSS3, which uses box-sizing to change the box model of the elements of the actual implementation of the effect, but today we learn the Calc () method is more convenient.

Know the total width is 100%, on this basis minus the width of the Boder (5px * 2 = 10px), minus the width of padding (10px * 2 = 20px), namely "100%-(10px + 5px) * 2 = 30px", the final value is the DIV The width value of the. Box:

. demo{Width 300px;Background #60f;Padding 3px0;}. box{Background #f60;Height 50px;Padding 10px;Border 5px solid Green; width: 90%;/* addressed to browsers that do not support Calc () */width:-moz-< Span class= "function" >calc (100%-(10px + 5px) * 2); width:-webkit-calc (100%-(10px + 5px) * 2); width: calc (100%- (10px + 5px) * 2);       }

In this way, the Div.box does not exceed the width of its container div.demo after calculation by calc ():

The above is a simple example, let's take a look at a sample adaptive layout:

The demo above is a very simple and common layout effect, in this layout, I used the adaptive layout. The entire layout contains "head", "Main content", "sidebar" and "Foot", and "main content" left, "sidebar" to the right, detailed structure see the HTML part of the demo.

Next, we'll focus on the CSS section:

1, set a padding in the body, and attach some basic style, you can make different settings according to their own needs, this example code is as follows:

{    background:color:padding:}   

2. Set the style of the main container "wrapper"

The width of the main container is "100%-20px * 2" and is centered horizontally:

. Wrapper{width:  1024px; /* Fallback for browsers that don ' t support the Calc () function */width:-moz-calc (100%- 40px); width:-webkit-calc (100%-40px); width: calc ( 100%-40px); margin: auto;                /span>             

A fixed-width value of "1024px" is set for browsers that do not support calc ().

3. Set style for headers and footer

The header and footer in this example are simple, add a padding of 20px to them, others are basic styling, and the corresponding width should be "100%-20px * 2":

#header{Background #f60;Padding 20px;Width 984px;/*fallback for browsers that don ' t support the Calc () function*/Width-moz-Calc100%-40px);Width-webkit-Calc100%-40px);Width Calc100%-40px);}#footer{ClearBothBackground #000;Padding 20px;color:  #fff; width: 984px; /* Fallback for browsers, T support the Calc () function */width:-moz-calc (100%-40px); width:-webkit-calc ( 100%-40px); width: calc ( 100%-40px);                /span>             

4. Set the style for the main content

Set a 8px border to the main content, 20px padding, and floating to the left, and set a right margin "" "px, the key point, our main content occupies 75% of the container width, so that the main content should be" 75%-8px * 2-20px * 2 ":

#main{Border 8PX Solid#B8C172;FloatLeftMargin-bottom: 20px;Margin-right: 20px;Padding 20px;Width 704px;/* Fallback for browsers that don ' t support the Calc () function */width: -moz-Calc (75%- 20px * 2- 8px * 2); width: -webkit-Calc (75%- 20px * 2- 8px * 2); width: calc (75%- 20px * 2- 8px * 2); }< /c6> 

4. Set the right column style

To the sidebar set a 25% width, in addition to containing 8px of the border, 10px of the inner distance, there is also the main content 20px to remove, or the whole width and the container will be different 20px, in other words will burst the container fell. So the actual width of the sidebar should be "25%-10px * 2-8px * 2-20px":

#accessory{Border 8PX Solid#B8C172;FloatRightPadding 10px;Width 208px;/* Fallback for browsers that don ' t support the Calc () function */width:-moz-calc (25%- Span class= "number" >10px * 2-8px * 2-20px"); width:-webkit-calc (25%-10px * 2-8px * 2-20px); width: calc ( 25%-10px * 2-8px * 2-20px);                /span>               

As a result, you will see the layout of the demo shown above. After this example of learning, people will feel that using calc () for adaptive layout is super cool AH. At this time there are a lot of students will feel it, bitter force of ie6-8 not support, dare not to use.

Finally, we provide an example of HEIHGT:

After a long time did not write blog, do not know everyone to Calc () whole understand no, if not, continue to write a few examples of it. If you have a better share, remember to tell us yo.

CSS3 's Calc () uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.