CSS定位中的必须深究的常用技法_html/css_WEB-ITnose
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">关于CSS定位,有人很多时候都是随便用用,符合自己的要求就行。但是CSS中的position等属性确实有很多需要认真考究的地方。</span>
1.position:static
static属性是position的默认值,也就是说,当一个元素没有为其设定position属性时,它的默认值就是static。
2.position:absolute
这是一个经常会被用到的position属性值。如果为某个元素设定了absolute,则该元素脱离原来的文档流。形象一些说,比如a元素被定义了position:absolute,那么这个元素就不会与这个页面中的其他元素发生位置上的关系,而是凌驾于整个页面之上的漂浮状态。页面中的其他元素的位置变化、大小变化等,都不会影响a元素的位置,相当于一个局外人。
3.position:relative
relative是最有用的定义方法。设置了relative属性表示,该元素相对于自己原来位置发生的变化。比如,我们定义了一个b元素,给它设定如下css样式:
#b{ position: relative; width:100px; height:100px; top:100px;}
4.position:fixed
fixed定位用的不多,但是它非常适用于固定模式的部分制作,比如顶部菜单。定义了fixed属性后,元素的位置不会随着任何行为发生变化。
5.relative+position
同时使用这两个定位,是一种很常用的手法,新手也可能会在此处遇见很多麻烦。总体来说,如果一个元素绝对定位后,其参照物是以离自身最近元素是否设置了相对定位,如果有设置将以离自己最近元素定位,如果没有将往其祖先元素寻找相对定位元素,一直找到html为止。比如,下面的代码利用二者的结合实现了一个两列布局;
<span style="white-space:pre"> </span>#div-1 { position:relative; } #div-1a { position:absolute; top:0; right:0; width:200px; } #div-1b { position:absolute; top:0; left:0; width:200px; }
6.clear:both清除浮动
有的时候定位会出现塌陷现象,即子元素在父元素中,但是父元素的大小不会随着子元素的大小而被“”撑开“,导致了父元素的塌陷效果。这种bug的出现是由于子元素设定了 float属性,导致父元素的坍塌。要想解决这种bug,需要为父元素设定清除浮动。示例代码如下:
<span style="white-space:pre"> </span> #div-1a { float:left; width:190px; } #div-1b { float:left; width:190px; } #div-1c { clear:both; }

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



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 article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

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.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

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

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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