Home Web Front-end HTML Tutorial CSS定位中的必须深究的常用技法_html/css_WEB-ITnose

CSS定位中的必须深究的常用技法_html/css_WEB-ITnose

Jun 24, 2016 am 11:23 AM

<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>
Copy after login

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;}
Copy after login
该段代码定义的b元素,它的位置为相对于没有定义position属性的位置向下移动100px的距离。relative属性值的定义就是这样的定位模式。

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;            }
Copy after login
内部的两个子div会根据其外部定位为relative的元素为参照进行绝对定位。

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;            }
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.

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

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.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

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...

See all articles