The box model in JS

Source: Internet
Author: User

When it comes to box models, the first time you think of CSS box models, the box models in CSS include (content area + inner margin + border). How to get these attribute values in JS? Let's learn the box model in JS.

CSS Styles

{    margin: 0;     padding: 0;}  {    position: absolute;     Margin: 10px;     padding: 10px;     width: 100px;     height: 100px;     Border: 10px solid #000;     line-height: 25px;}

HTML structure

<class= " box" ID= "box">     JS box model of the front-end learning Path The front-end learning path JS box model front-end learning way JS Box model front-end Learning path JS box model </div>

1.client Series

Clientwidth/clientheight if the width of the container is not set, the inner margin is used to open it, and the actual width of the content plus the inner margin is obtained. If you set the width to high, the height is the set width plus the padding.

Clientleft/clienttop width of border

1 var box=document.getelementbyid ("box"); 2 //  - 3 //  - 4 // Ten 5 // Ten

2.offset Series

Offsetwidth/offsetheight (clientwidth+clientleft*2), (clientheight+clienttop*2)

Offsetleft/offsettop margin relative to parent box

OffsetParent Box's parent box

1 //  $ 2 //  $ 3 // Ten 4 // Ten 5 // Body

3.scroll Series

Scrollwidth/scrollheight If the content does not overflow, the value is the same as clientwidth/clientheight. If the content overflows:

ScrollWidth: Actual overflow content width + left padding

ScrollHeight: Actual overflow content height + on fill

ScrollTop: Height of Scroll strip volume

ScrollLeft: Width of scroll strip roll

1 //  - 2 //  - 3 Console.log (box.scrolltop);  // 0 4 // 0

Get, set browser box model information compatible notation

document.documentelement.clientwidth| | Document.body.clientWidth

Encapsulates a function Get set box model

1 function win (attr, value) {2     if (typeof value = = = "undefined") {3         return document.documentelement[attr] | | document.body[attr]; 4     }5     document.documentelement[attr] = value; 6     DOCUMENT.BODY[ATTR] = value; 7 }

This is JS in the box model, often used in development, and these properties are easy to remember confusion, to often practice, to master, tomorrow is the Mid-Autumn Festival, here I wish you a happy Mid-Autumn Festival!!!

<div class= "box" id= "box" > Front-end Learning path JS box model front-end learning way JS Box model front-end learning way JS Box model front-end Learning path JS box model </div>

The box model in JS

Related Article

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.