写在前面
文中错误或不足之处欢迎指正批评,共同交流!
在项目中写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>
<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>}
但是问题就来了,再给一个按钮加一条侧边的边框,那么就分行显示了。如果每个按钮宽度只给到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>; }
下面就来说一说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>; }

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

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

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool