× 目录 [1]表现 [2]重叠 [3]浮动 [4]定位 [5]应用
前面的话
margin属性在实际中非常常用,也是平时踩坑较多的地方。margin折叠部分相信不少人都因为这样那样的原因中过招。margin负值也是很常用的功能,很多特殊的布局方法都依赖于它。它看似简单,实际上却蛮复杂,本文就margin负值作详细介绍和梳理
[注意]关于margin部分的基础知识移步至此
表现
虽然margin可以应用到所有元素,但display属性不同时,表现也不同
【1】block元素可以使用四个方向的margin值
【2】inline元素使用上下方向的margin值无效
【3】inline-block使用上下方向的margin负值看上去无效
[注意]inline-block使用上下方向的margin负值只是看上去无效,这与其默认的vertical-align:baseline有关系,当垂直对齐的属性值为其他值时,则会显示不同的视觉效果
重叠
margin负值并不总是后面元素覆盖前面元素,它与元素display属性有关系
【1】两个block元素重叠时,后面元素可以覆盖前面元素的背景,但无法覆盖其内容
【2】当两个inline元素,或两个line-block元素,或inline与inline-block元素重叠时,后面元素可以覆盖前面元素的背景和内容
【3】当inline元素与block元素重叠时,inline的元素覆盖block元素的背景和内容
【4】当inline-block元素与block元素重叠时,inline-block元素覆盖block元素的背景,但无法覆盖其内容
浮动
【1】block元素与浮动元素重叠时,其边框和背景在该浮动元素之下显示,而内容在浮动元素之上显示
【2】inline或inline-block元素与浮动元素重叠时,其边框、背景和内容都在该浮动元素之上显示
定位
【1】定位元素(position不为static)覆盖其他元素的背景和内容
【2】将relative属性值应用于inline或inline-block元素后,由于无法改变其行内元素的本质,所以其上下margin依然存在问题
应用
【1】水平垂直居中
由于margin的百分比相对于包含块的宽度,所以在需要居中的元素外面套一个空的
.box{ position:relative; width: 200px; height: 200px; background-color: lightgreen; border: 2px solid black;}.out{ position: absolute; left: 50%; top: 50%;} .in{ height: 100px; width: 100px; background-color: pink; margin-left: -50%; margin-top: -50%;}
<div class="box"> <div class="out"> <div class="in">测试内容</div> </div> </div>
【2】列表项两端对齐
在列表项外面包一层元素,使用margin负值来将最后一个列表项拉回来
ul{ margin: 0; padding: 0; list-style:none;}.box{ width: 200px; background-color: pink; }.list{ overflow: hidden; margin-right: -10px;}.in{ float: left; width: 60px; height: 100px; background-color: lightgreen; margin-right: 10px;}
<div class="box"> <ul class="list"> <li class="in">1</li> <li class="in">2</li> <li class="in">3</li> </ul> </div>
【3】三栏自适应布局
中间的主体使用双层标签,外层
html,body{ height: 100%;}body{ margin: 0;}.main{ width: 100%; height: 100%; float: left;}.main .in{ margin: 0 210px; background-color: pink; height: 100%;}.left,.right{ height: 100%; width: 200px; float: left; background-color: lightgreen;}.left{ margin-left: -100%;}.right{ margin-left: -200px;}
<body><div class="main"> <div class="in"></div></div><div class="left"></div><div class="right"></div></body>
【4】三栏等高布局
给每栏设置大的底部内边距,然后用数值相同的负外边距消除这个高度,然后在外层容器中设置overflow:hidden
body{ margin: 0; overflow: hidden;}ul{ margin: 0; padding: 0; list-style: none;}.list{ overflow: hidden; width: 100%; height: 100%;}.main{ margin: 0 210px; background-color: lightgreen;}.left{ width: 200px; float: left; background-color: pink;}.right{ width: 200px; float: right; background-color: pink;}.main,.left,.right{ margin-bottom: -9999px; padding-bottom: 9999px;}
<ul class="list"> <li class="left">左侧文字比较少</li> <li class="right">右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多</li> <li class="main">中间文字比较少</li> </ul></body>

HTMLtagsareessentialforstructuringwebpages,enhancingaccessibility,SEO,andperformance.1)Theyareenclosedinanglebracketsandusedinpairstocreateahierarchicalstructure.2)SemantictagslikeandimproveuserexperienceandSEO.3)Creativetagslikeenabledynamicgraphics

Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.


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

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

Hot Article

Hot Tools

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version
