美文网首页
绝对定位的应用场景

绝对定位的应用场景

作者: xinhui9056 | 来源:发表于2019-07-13 14:49 被阅读0次
//html部分
<h1>A fake article about spaceships</h1>
<p>This is a fake article about spaceships. Remember when you did presentations in
 middle school, and you told the class about what your assignment was about, then 
read aloud from the paper you had written, including the title?
  ”My assignment is about spaceships. Spaceships. Spaceships are very large, 
and fly in space...”.
</p>
<aside class="comment" id="comment-1">
    I’ve never done this. Is that really true?
</aside>
<p>You may think that spaceships are fake, like this article, but they’re not. 
There are actual spaceships,  flying in space right now, probably. For example,
 there’s the International Space Station, which is a spaceship of sorts. Well, 
actually it’s a space station, which is even cooler!
</p>
<figure>
  <img src="images/spaceship.jpg" alt="The Dragon spaceship in orbit around Earth.">
  <figcaption>The ”Dragon” spaceship, created by SpaceX. Image from 
    <a href="https://www.flickr.com/photos/spacexphotos/16787988882/">Flickr.com</a>
  </figcaption>
</figure>
<p>There’s various government organizations and companies that are building
 spaceships. One of them is SpaceX. An image of one of their spaceships appears 
on this very page.
</p>
<aside class="comment" id="comment-1">
    It’s only a mockup, actually.
</aside>
<p>I can't be bothered to write any more about spaceships so the rest of the 
text will just be nonsense. This isn’t a school assignment after all.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi saepe harum, 
excepturi dolorum voluptatem hic amet nemo. Incidunt tenetur dignissimos laborum 
molestiae reiciendis ipsum quas temporibus nisi dolor, ea libero!</p>
<p>A assumenda, et eius odit, rerum delectus placeat dolores eveniet quis. 
Iure neque deserunt mollitia impedit illo corporis odio quod, velit ducimus!</p>
//css
body {
      font-family: Georgia, Times New Roman, serif;
      line-height: 1.5;
      padding: 2em 8em;
      max-width: 35em;
      margin: 0 auto;
    }
    h1 {
      font-family: Avenir, Franklin Gothic, sans-serif;
    }

    figure {
      background-color: #eee;
      margin: 0;
      padding: 1em;
      float: right;
      max-width: 17em;
      position: relative;
      right: -8em;
      margin-left: -7em;
    }
    figure img {
      width: 100%;
      display: block;
      margin-bottom: .5em;
    }
    figcaption {
      font-style: italic;
      font-size: .875em;
    }
    .comment {
      position: absolute;
      width: 7em;
      margin-left: -9.5em;
      margin-top: -2.5em;
      font-size: .875em;
      padding: .5em;
      border-radius: .25em;
      background-color: #dcf0ff;
      font-style: italic;
    }
    .comment:after {
      position: absolute;
      content: '';
      display: block;
      width: 0;
      height: 0;
      border: .5em solid #dcf0ff;
      border-bottom-color: transparent;
      border-right-color: transparent;
      position: absolute;
      right: -1em;
      top: .5em;
    }
最终效果
利用初始位置

绝对定位的元素默认会待在自己静态定位时的地方,如果想实现向左、向上偏移,则必须使用偏移属性(top、right、bottom、left),那既要用到定位的上下文,又要设置确定的偏移量,在这里我们就可以用到负边距来移动元素,这种做法和相对定位很相似。

.comment {
      position: absolute;
      width: 7em;
      margin-left: -9.5em;
      margin-top: -2.5em;
}
定位与z-index:堆叠内容的陷井
image.png

容器A、B、C、D都是绝对定位,其中C是B的子元素。容器C和D设置了z-index,但由于容器B的opacity值 小于1,所以它又创建了一个新独立的堆叠上下文,于是,就算C的z-index值 再大,它也不会跑到D的上方

相关文章

  • 绝对定位的应用场景

    利用初始位置 绝对定位的元素默认会待在自己静态定位时的地方,如果想实现向左、向上偏移,则必须使用偏移属性(top、...

  • 1、Selenium -- 环境配置与基本用法

    概要: 应用场景 环境配置 基本应用基本定位js定位鼠标事件屏幕截图 一、应用场景 爬虫; 绕过登录(cookie...

  • iOS-CoreLocation文集目录

    CoreLocation应用场景:定位iOS8.0之前的定位iOS8.0定位iOS9.0定位定位总结指南针效果区域...

  • css calc()

    常用场景 1.元素绝对定位居中显示 .foo { position: absolute; to...

  • 定位BFC边距合并

    1、有几种定位方式,分别是如何实现定位的,参考点是什么,使用场景是什么? 绝对定位:absolute(相对于 st...

  • 对 'CoreLocation' say so

    应用场景 定位 地图CoreLocation : 用于地理定位, 地理编码, 区域监听等(着重功能实现)MapKi...

  • 前端第五天

    前端第五天 目录 文档流 浮动布局 清除浮动 流式布局 定位布局应用 相对定位 绝对定位 固定定位 一、文档流 1...

  • 面试官问我swoole的应用场景,我懵了!

    应用场景简介 与硬件设备连接通讯(定位设备) IM系统(用于直播页面的聊天通讯) 场景1 - 实时收集定位数据实时...

  • 定位总结

    定位总结 一. 定位的应用场景 二. 开发经验 ** 由于定位非常耗电; 所以为了给用户省电, 你可以遵守以下小经...

  • css: About Position

    position:relative 相对定位 1.仍在文档流中2.参照物为元素本身** 使用场景:绝对定位元素的参...

网友评论

      本文标题:绝对定位的应用场景

      本文链接:https://www.haomeiwen.com/subject/uvopkctx.html