Home Web Front-end HTML Tutorial css 绝对定位absolute和相对定位relative实例讲解

css 绝对定位absolute和相对定位relative实例讲解

Jun 01, 2016 am 09:53 AM
css position position

一、如果元素的position属性值为absolute,那么此时分为两种情况:

1. 父级(包括直接父级和间接父级)元素中没有定义position属性(即默认属性static),那么该元素会参照页面,以页面为基准进行定位,top, left等属性会参照页面。

如果该元素的width和height属性是百分数的话,那么这两个属性也是参照页面的宽度和高度。

<code class="language-html">


<meta charset="utf-8">
<style>
*{margin: 0; padding: 0;}
#div1 {
    width: 200px;
    height: 200px;
    background: #ccc;
}
#div2 {
    width: 50%;
    height: 50%;
    background: #aaa;
    top: 100px;
    left: 100px;
    position: absolute;
}
</style>


<div id="div1">
    <div id="div2"></div>
</div>

</code>
Copy after login

以上代码中,div2是绝对定位,而其父级元素没有设置position属性,那么该元素就会以页面为参照进行定位。

大家可以把上面实例代码复制到这里运行一下,查看一下效果。

从运行效果可以看到,div2是相对于页面定位的,而且其宽度和高度是页面宽高的一半。

 

2. 父级元素定义了position属性(relative或absolute),则会参照父级元素。

<code class="language-html">


<meta charset="utf-8">
<style>
*{margin: 0; padding: 0;}
#div1 {
    width: 200px;
    height: 200px;
    background: #ccc;
    position: relative;
    top: 100px;
    left: 100px;
}
#div2 {
    width: 50%;
    height: 50%;
    background: #aaa;
    top: 100px;
    left: 100px;
    position: absolute;
}
</style>


<div id="div1">
    div1
    <div id="div2">div2</div>
</div>

</code>
Copy after login

大家可以把上面实例代码复制到这里运行一下,查看一下效果。

从运行效果可以看到,div2是相对于div1定位的,宽度和高度也是相对于div1

 

二、如果元素的position属性值为relative,那么该元素相对于其正常位置(即position:static)定位。

默认情况下宽度会占满父容器,高度根据本身内容决定。当是父元素的第一个子元素时,位置会在父元素的左上角。

那么设置其位置和大小就会以自己为参照,height的情况特殊,当设置height时,会以充满父容器的状态为参照。

<code class="language-html">


<meta charset="utf-8">
<style>
*{margin: 0; padding: 0;}
#div1 {
    width: 100px;
    height: 100px;
    background: #ccc;
}
#div2 {
    width: 50%;
    height: 50%;
    background: #aaa;
    top: 100px;
    left: 100px;
    position: relative;
}
</style>


<div id="div1">
    <div id="div2"></div>
</div>

</code>
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 Article Tags

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)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

What does placeholder mean in vue

How to write spaces in vue How to write spaces in vue Apr 30, 2024 am 05:42 AM

How to write spaces in vue

How to get dom in vue How to get dom in vue Apr 30, 2024 am 05:36 AM

How to get dom in vue

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

What does span mean in js

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

What does rem mean in js

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

How to introduce images into vue

What is the function of span tag What is the function of span tag Apr 30, 2024 pm 01:54 PM

What is the function of span tag

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

What language is the browser plug-in written in?

See all articles