search
HomeWeb Front-endHTML Tutorial2016.3.14CSS 定位第六天_html/css_WEB-ITnose

CSS定位 CSS 定位机制

CSS中一共有三种基本定位机制:普通流、浮动、绝对定位。
如果不进行专门指定,所有的标签都在普通流中定位。
块级元素从上到下一个接一个的排列,框之间的垂直距离是由框的垂直外边距计算出来。
行内框在一行中水平布置。可以使用水平内边距、边框和外边距来调整它们之间的间距。


position属性

通过position属性,我们可以选择4种不同类型的定位,这会影响元素框生成的方式。
包括4个属性值:static、relative、absolute、fixed

  • static : 元素框正常生成。块级元素生成一个矩形框,作为文档流的一部分;行内元素则会创建一个或多个行框,置于其父元素中。
  • relative : 元素框偏移某个距离。元素仍然保持其未定位前的形状,它原本所占的空间仍然保留。
  • absoulte : 元素框从文档流中完全删除,并相对于其包含块定位。元素原先在正常文档流中所占的空间会关闭,就好像元素不存在一样。元素定位后生成一个块元素,而不论原来它在正常文档流中生成何种类型的框。
  • fixed : 元素框的表现类似将position设置为absoulte,不过其包含块是浏览器窗口。

  • 代码展示

    position: static;

    static是position属性的默认值,无特殊定位。均为正常定位。


    position: reletive;

    HTML代码:

    <p class="p1">    我是p1</p><p class="p2">    我是p2</p><p class="p3">    我是p3</p>

    CSS代码:

    .p1 {    position: relative;    left: 30px;}.p2 {    position: relative;    right: 30px;}

    效果图:

    解释:

  • position: relative;参考的是自己原来得位置。
  • 第一行文字设置postion为relative,这并不会造成什么反应,但是我们还给p1设置了left: 30px;,这就会使元素距离左侧产生30px的间距。
  • 同样,第二行文字设置right: 30px,会使元素距离右侧30px的间距,就产生了跑到屏幕外面的情况
  • 第三方没有做任何处理,正常显示
  • 注意,一定要设置好position: relative;,否则top、right、bottom、left是不起作用的。

  • position: absoulte;

    HTML代码:

    啦啦啦啦啦<h1 id="我是h-大标题">我是h1大标题</h1>

    CSS代码:

    .h1 {    position: absolute;    top: 100px;    left: 100px;}

    没有设置CSS样式的样子:

    设置了CSS样式后的样子:

    解释:

  • postion: absolute;参考的是自身的包含块,也就是自己的父视图
  • 当设置了position: absolute;属性之后,标签的位置就变得绝对了。这个时候我们设置top、right、bottom、left其中的任何一个属性,都可以设置标签的位置。
  • 注意,一定要设置好position: absolute;,否则top、right、bottom、left是不起作用的。

  • position: fixed;

    HTML代码:

    <p class="one">    我是p one</p><p class="two">    我是p two</p>

    CSS代码:

    .one {    position: fixed;    top: 100px;    left: 30px;}.two {    position: fixed;    top: 50px;    right: 30px;}

    效果展示:

    解释:

  • position: fixed;参考系是浏览器的窗口
  • 当给标签设置了position: fixed;属性之后,这些标签就只会相对于浏览器窗口进行位置的设定,忽略网页的滚动
  • 同样,如果不设置postion: fixed;属性,而是直接设置top、right、bottom、left都不会起作用
  • 参考文章:http://www.w3school.com.cn/css/css_positioning.asp

    关于界面排布优先级的问题 z-index

      <meta charset="UTF-8">  <title>Document</title>  <style media="screen">    div{      width: 100px;      font-size: 50px;      position: absolute;      height: 100px;    }    .a{      background-color: red;      left: 0;      top: 0;        /*设置优先级,数字越大,放置越靠前*/      z-index: 3;    }    .b{      background-color: blue;      left: 40px;      top: 40px;      z-index: 2;    }    .c{      background-color: green;      left: 80px;      top: 80px;      z-index: 100;    }  </style><div class="a">1</div><div class="b">2</div><div class="c">3</div>

    关于界面元素框偏移
    偏移前:


    偏移后:

        <meta charset="UTF-8">    <title>Document</title>    <style type="text/css">    .wrap{        width: 300px;        height: 300px;        border: 1px solid red;        margin: 100px;        padding: 100px;        position: relative;        padding-left: 0;    }    .inner{        width: 200px;        height: 200px;        background-color: green;        padding: 50px;        position: relative;    }    .content{        width: 50px;        height: 50px;        background-color: red;        position: absolute;        left: 0;    }    </style>    <!--     position:absolute;默认是相对于窗口进行定位     -->    <div class="wrap">        <div class="inner">            <div class="content"></div>        </div>    </div>
    Statement
    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
    The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

    HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

    Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

    See all articles

    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

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools