Table of Contents
写在前面
box-sizing属性
Home Web Front-end HTML Tutorial 关于box-sizing属性 - 天上云好白

关于box-sizing属性 - 天上云好白

May 20, 2016 pm 04:50 PM

写在前面

文中错误或不足之处欢迎指正批评,共同交流!

在项目中写css组件时遇到一个问题:

要求两个按钮均分其父元素宽度,且父元素宽度不固定,像这样:

第一反应很自然的想到使用flex布局,但是由于需要兼容ie9以上,以及安卓微信端那个x5浏览器,加之只有两个按钮布局相对简单,所以选择了浮动的方法:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="mask"</span><span style="color: #0000ff;">></span>
  <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="confirm"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="confirm-text"</span><span style="color: #0000ff;">></span>提示信息<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="confirm-btn confirm-btn-sure"</span><span style="color: #0000ff;">></span>确定<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="confirm-btn"</span><span style="color: #0000ff;">></span>取消<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
  <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span></span>
Copy after login
<span style="color: #800000;">.mask</span>{<span style="color: #ff0000;">
    ...
</span>}<span style="color: #800000;">
.confirm</span>{<span style="color: #ff0000;">
    ...
</span>}<span style="color: #800000;">
.confirm-text</span>{<span style="color: #ff0000;">
    ...
    clear</span>:<span style="color: #0000ff;"> both</span>;
}<span style="color: #800000;">
.confirm-btn</span>{<span style="color: #ff0000;">
    float</span>:<span style="color: #0000ff;"> left</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    ...
</span>}
Copy after login

但是问题就来了,再给一个按钮加一条侧边的边框,那么就分行显示了。如果每个按钮宽度只给到49%甚至49.5%倒是可以解决这个问题,但如果点击改变背景颜色的时候还是能被发现,而且非常丑,这个时候就可以使用box-sizing: border-box;使元素边框的宽度包含在元素本身宽度内,这样问题就解决了:

<span style="color: #800000;">.confirm-btn</span>{<span style="color: #ff0000;">
    box-sizing</span>:<span style="color: #0000ff;"> border-box</span>;<span style="color: #ff0000;">
    float</span>:<span style="color: #0000ff;"> left</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    ...
</span>}<span style="color: #800000;">
.confirm-btn:active</span>{<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;"> rgb(235,235,235)</span>;
}<span style="color: #800000;">
.confirm-btn-sure</span>{<span style="color: #ff0000;">
    border-right</span>:<span style="color: #0000ff;"> 1px solid rgb(235,235,235)</span>;
}
Copy after login

下面就来说一说box-sizing这个属性。

box-sizing属性

box,如大家所熟知的,是css布局中最小的单位,所有的布局都是由一个一个矩形的box搭建出来的,就很像是搭积木那样。而每一个box都会由四部分组成,包括:content,padding,border,margin。那么box的高宽是如何计算的呢?css中有一个属性叫box-sizing,该属性取不同的值会决定box高宽不同的计算方式。

先来说说兼容性,目前测试IE9以上版本以及其他现代浏览器都兼容这个属性。

这个属性有三个可取值,分别是:content-box,padding-box,border-box,默认值为content-box,但是padding-box的兼容性似乎存在问题,最新版chrome和safari也不能生效,故本文中暂时不做讨论。

1.content-box(默认值)

 这也就是最常规的计算方式,某个box的高等于它的height+padding+border+margin,宽也是同理,以下不再赘述。

2.border-box

当取值为border-box时,这个box的高就等于它的height+margin了,也就是说该box的height是由content部分的height和padding以及border共同组成的了,换言之,padding和border不再向外延伸,而是往里边挤。

<span style="color: #800000;">.border-box</span>{<span style="color: #ff0000;">
      box-sizing</span>:<span style="color: #0000ff;"> border-box</span>;<span style="color: #ff0000;">
      width</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;">
      height</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;">
      padding</span>:<span style="color: #0000ff;"> 50px</span>;
}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles