来自: https://segmentfault.com/a/1190000004500652
了解 CSS 中属性的值及其特性, 透彻分析问题和需求才可以选择和设计最适合的布局解决方案。
多列布局在网页中非常常见(例如两列布局),多列布局可以是两列定宽,一列自适应, 或者多列不定宽一列自适应还有等分布局等。
定宽-自适应
有如下布局
<div class="parent"> <div class="left"> <p>left</p> </div> <div class="right"> <p>right</p> <p>right</p> </div></div>
float+margin
.left { float: left; width: 100px; } .right { margin-left: 100px /*间距可再加入 margin-left */ }
优点:容易理解
缺点:IE6中会有3像素的BUG, 解决方法可以在.left 加入 margin-left:-3px 。
因为left是浮动元素,right是没有浮动的,如果right内容中有清楚浮动就会产生bug。
改进:float + margin + (fix)
<div class="parent"> <div class="left"> <p>left</p> </div> /*外层在包裹一个容器*/ <div class="right-fix"> <div class="right"> <p>right</p> <p>right</p> </div> </div></div><style> .left { float: left; width: 100px; } .right-fix { float: right; width: 100%; margin-left: -100px; } .right { margin-left: 100px /*间距可再加入 margin-left */ }</style>
兼容性很好,适用于多版本浏览器(包括 IE6)但是多了层right-fix的结构。但left不可选择,被right-fix盖住。需要设置left的 position: relative; 来提高层级。
float+overflow
.left{ float: left; width: 100px; margin-right: 20px; } .right{ overflow: hidden; //触发BFC }
overflow:hidden 使得div产生了BFC 模式(Block Formatting Context)块级格式化文本,根据BFC的布局规则之一,div那个产生的BFC区域不会与左边浮动的元素所重叠。具体什么是BFC可以看看这篇 文章
table
.parent { display: table; width: 100%; table-layout: fixed; } .left { display: table-cell; width: 100px; } .right { display: table-cell; /*宽度为剩余宽度*/ }
table 的显示特性为每列的单元格宽度合一定等与表格宽度。 table-layout: fixed; 可加速渲染,也是设定布局优先。
table-cell 中不可以设置 margin 但是可以通过 padding 来设置间距。
flex
.parent{ display: flex ; } .left { width: 100px; margin-right: 20px; } .right{ /*等价于flex:1 1 0; 增长因子和收缩因子都为1, 基础宽度为0,那么剩余宽度都分配给right*/ flex: 1 ; }
flex-item 默认为内容宽度。
缺点: CSS3 兼容性。根据内容判断,性能会有问题,做小范围布局,不适合大范围布局。
不定宽-自适应
float + overflow
.left{ float: left; width: 200px; margin-right: 20px; } .right{ overflow: hidden; //触发BFC }
table
.parent{ display:table; width: 100%; } .left, .right { display: table-cell; } .left{ width:0.1%; //写的够小就行,不写1px的原因是IE8会有问题 padding-right:20px; } .left p{ width : 200px;} //用内部元素撑开,宽度可以不定
flex
.parent{display:flex;}.left{margin-right:20px;}.right{flex:1;}
三列不定宽 + 自适应 与两列的做法一样
等分布局
每一列的宽度和间距均相等。有如下HTML结构
<div class="parent"> <div class="column"><p>1</p></div> <div class="column"><p>2</p></div> <div class="column"><p>3</p></div> <div class="column"><p>4</p></div></div>
float
.parent{ margin-left :-20px; //为父元素增加20px宽度 } .column{ float : left; width: 25%; padding-left : 20px; box-sizing : border-box; }
缺点:结构和样式稍微有点耦合性
table
.parent-fix{ margin-left :-20px; //为父元素增加20px宽度 } .parent{ display : table; width : 100%; table-layout : fixed; } .column{ float : left; width: 25%; padding-left : 20px; box-sizing : border-box; }
fix
.parent{ display : flex; } .column{ flex: 1; } .column+.column{ margin-left: 20px; } //选择2,3,4
等高需求
table
上面自适应的table布局就具有登高的布局
flex
如上面自适应的例子,flex 天然等高
float
.parent{ overflow : hidden; } .left, .right{ padding-bottom : 9999px; margin-bottom : -9999px; }
优点是兼容性比较好缺点是伪等高 不是正真意义上的登高

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

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.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
