CSS units, Em,rem, and the width and height of elements

Source: Internet
Author: User

I. EM and REM

When it comes to adaptive layouts, you have to mention REM as a unit.

To put it simply

    • em: is the font size, depending on the element's own font size, if it does not define the font size, then inherit the parent element's font size, that is, 1em = 1 font-size;
    • REM: Similar to EM, can be seen as Root-em, is based on the root element of the font size to define, that is, the HTML set font size to define, the default HTML font size is 16px;

Use a demo to illustrate the best:

<style>HTML{font-size:50px;}#wrapper{font-size:40px;Background-color:Red;width:2em;Height:2em;    }#outter{font-size:20px;Background-color:Yellow;width:2em;Height:2em;    }#inner{font-size:10px;background:Blue;width:2em;Height:2em;    }#wrapper-1{font-size:40px;Background-color:Red;width:2rem;Height:2rem;    }#outter-1{font-size:20px;Background-color:Yellow;width:2rem;Height:2rem;    }#inner-1{font-size:10px;background:Blue;width:2rem;Height:2rem;    }</style><Body>    <DivID= ' wrapper '>        <DivID= ' Outter '>            <DivID= ' inner '>0</Div>        </Div>    </Div>    <DivID= ' wrapper-1 '>        <DivID= ' outter-1 '>            <DivID= ' inner-1 '>1</Div>        </Div>    </Div></Body>

The result, on the use of EM, for REM

It can be seen that the size of EM varies according to the size of its own elements, while REM does not, and has always been the font-size of the root.

Second, width and height

I want to get the width of the above elements, I used to use:

// The result is ""

Find the information and find that this is used to find the properties of CSS elements set, and generally should use the following methods:

1.clientWidth and ClientHeight

var width = el.clientwidth; var height = el.clienheight;

Description: Padding and scroll changes, only change

2.scrollWidth and Box.scrollheight

var width = el.scrollwidth; var height = el.scrollheight;

Description, 1) border changes, different browsers have different changes 2) padding change, change 3) margin change, no change

3.offsetWidth and Offsetheight

var width = el.offsetwidth; var height = el.offsetheight;

shows that padding and border are changed.

CSS units, Em,rem, and the width and height of elements

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.