`

CSS positioning

    博客分类:
  • css
 
阅读更多

Positioning

The CSS positioning properties allow you to position an element. It can also place an element behind another, and specify what should happen when an element's content is too big.

Elements can be positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the positioning method.

There are four different positioning methods.


Static Positioning

HTML elements are positioned static by default. A static positioned element is always positioned according to the normal flow of the page.

Static positioned elements are not affected by the top, bottom, left, and right properties.


Fixed Positioning

An element with fixed position is positioned relative to the browser window.

It will not move even if the window is scrolled:

Example

p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}

Try it yourself »

Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is specified.

Fixed positioned elements are removed from the normal flow. The document and other elements behave like the fixed positioned element does not exist.

Fixed positioned elements can overlap other elements.


Relative Positioning

A relative positioned element is positioned relative to its normal position.

Example

h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
{
position:relative;
left:20px;
}

Try it yourself »

The content of relatively positioned elements can be moved and overlap other elements, but the reserved space for the element is still preserved in the normal flow.

Example

h2.pos_top
{
position:relative;
top:-50px;
}

Try it yourself »

Relatively positioned elements are often used as container blocks for absolutely positioned elements.


Absolute Positioning

An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>:

Example

h2
{
position:absolute;
left:100px;
top:150px;
}

Try it yourself »

Absolutely positioned elements are removed from the normal flow. The document and other elements behave like the absolutely positioned element does not exist.

Absolutely positioned elements can overlap other elements.


Overlapping Elements

When elements are positioned outside the normal flow, they can overlap other elements.

The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).

An element can have a positive or negative stack order:

Example

img
{
position:absolute;
left:0px;
top:0px;
z-index:-1
}

Try it yourself »

An element with greater stack order is always in front of an element with a lower stack order.

Note: If two positioned elements overlap, without a z-index specified, the element positioned last in the HTML code will be shown on top.

分享到:
评论

相关推荐

    快速提升CSS编码能力的五个必备知识点

    1. **CSS定位(Positioning)** CSS定位是控制元素在页面上位置的关键。包括`static`、`relative`、`absolute`、`fixed`和`inherit`五种方式。其中,`static`是默认值,元素按正常文档流排列;`relative`相对定位,...

    英文原版-AdvancED CSS 1st Edition

    performance, and appearance of your work.You’ll learn how document flow and CSS positioning schemes will help you make your documents more accessible. You’ll discover the great styling ...

    css常用样式.docx

    最后,文档链接指向了一个关于CSS定位(CSS Positioning)的资源,特别是浮动(floating)的概念,这是实现网页元素如图片和文本的布局的重要技术。 总结来说,这个文档主要涵盖了CSS中用于控制元素间距和边框样式...

    (英文)The CSS Anthology: 101 Essential Tips, Tricks & Hacks 2009

    9. **CSS Positioning and Layout** - CSS定位与布局 - 不同定位方式(如相对、绝对、固定定位)的使用场景。 - 如何利用Flexbox和Grid布局创建现代响应式设计。 - 布局中的层叠顺序和盒模型理解。 #### 三、...

    Positioning in CSS Layout Enhancements for the Web epub

    Positioning in CSS Layout Enhancements for the Web 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    字节跳动2017前端工程师实习生笔试题汇总.docx

    4. CSS positioning position: relative 不脱离文档流,而 position: absolute 和 position: fixed 会脱离文档流。 5. 算法问题 小今和小条玩游戏的策略是让小今先取石子,然后让小条取石子,以确保小今必胜。 6. ...

    DHTML.api手册

     DHTML 不是由万维网联盟(W3C)规定的标准。... CSS Positioning (允许您控制元素的定位和可见性)  JavaScript  可视滤镜(允许您向文本和图形应用可视效果)  动态 CSS(允许您控制元素的定位和可见性)

    css-layout-and-positioning-css布局与定位

    在网页设计中,CSS(Cascading Style Sheets)...在“css-layout-and-positioning-master”这个压缩包中,可能包含了一系列关于CSS布局与定位的实例和教程,通过学习和操作这些文件,你可以更好地理解和应用这些概念。

    css2|css3

    2. **定位机制**:支持绝对定位(absolute positioning)、相对定位(relative positioning)和固定定位(fixed positioning),使得元素可以在页面上精确放置。 3. **多列布局**:CSS2允许开发者使用`column-count`...

    div+css手册

    CSS2.0是CSS的第二个主要版本,引入了许多关键特性,如定位(positioning)、浮动(floating)、层叠规则(cascading)、盒模型(box model)等,这些都对网页布局产生了深远影响。 1. **定位(Positioning)**:...

    Positioning in CSS Layout Enhancements for the Web mobi

    Positioning in CSS Layout Enhancements for the Web 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Positioning in CSS Layout Enhancements for the Web 无水印pdf

    Positioning in CSS Layout Enhancements for the Web 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,...

    css禅意花园 CSS Zen Garden

    2. **定位(Positioning)**:使用`position`属性,如`relative`、`absolute`和`fixed`,可以精确控制元素的位置。 3. **Flexbox(弹性盒模型)**:现代布局方案,提供了一种更灵活的方式来排列和对齐元素,尤其适合...

    css教程,CSS编写

    8. **定位(Positioning)**:CSS提供了多种定位方式,如静态定位、相对定位、绝对定位和固定定位,以实现元素在页面上的精确控制。 9. **响应式设计(Responsive Design)**:随着移动设备的普及,CSS3引入了媒体...

    css参考手册(CSS,CSS2.0,CSS3.0)

    2. **定位(Positioning)**:通过position属性,可以设置元素相对于其正常位置或其他元素的位置。 3. **多列布局(Multi-column Layout)**:允许将内容分成多个列,提高文本阅读体验。 4. **背景图像(Background ...

Global site tag (gtag.js) - Google Analytics