蓝桥杯Web应用开发-浮动与定位

浮动与定位

浮动布局比较灵活,不易控制,而定位可以控制元素的过分灵活性,给元素一个具体的空间和精确的位置。

浮动

我们使用 float 属性指定元素沿其容器的左侧或右侧放置,浮动布局常见取值如下:

• left(左浮动)

• right(右浮动)

使用格式如下所示:

html 复制代码
float: left|right;

我们来举个例子吧!

新建一个 index.html 文件,在其中写入以下内容。

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      section {
        border: 1px solid green;
        width: 400px;
        float: left;
      }
      img {
        width: 120px;
        height: 120px;
      }
      img:first-child {
        float: left;
      }
      img:nth-child(2) {
        float: right;
      }
    </style>
  </head>
  <body>
    <section>
      <img src="https://labfile.oss.aliyuncs.com/courses/10532/lotus1.png" />
      <img src="https://labfile.oss.aliyuncs.com/courses/10532/lotus2.png" />
      <p>
        曲曲折折的荷塘上面,弥望的是田田的叶子。叶子出水很高,像亭亭的舞女的裙。层层的叶子中间,零星地点缀着些白花,有袅娜地开着的,有羞涩地打着朵儿的;正如一粒粒的明珠,又如碧天里的星星,又如刚出浴的美人。微风过处,送来缕缕清香,仿佛远处高楼上渺茫的歌声似的。这时候叶子与花也有一丝的颤动,像闪电般,霎时传过荷塘的那边去了。叶子本是肩并肩密密地挨着,这便宛然有了一道凝碧的波痕。叶子底下是脉脉的流水,遮住了,不能见一些颜色;而叶子却更见风致了。
      </p>
    </section>
  </body>
</html>

定位

我们使用 position 属性来对元素的位置进行控制,定位布局可以分为以下四种:

• 静态定位(inherit)

• 相对定位(relative)

• 绝对定位(absolute)

• 固定定位(fixed)

其中,一般的标签元素不加任何定位属性时,默认都属于静态定位,静态定位在页面的最底层属于标准流(普通流),在页面中没有特殊的操作方式和显示效果,在本章节中会重点给同学们讲解其他三种定位方式。

固定定位

fixed 属性值用于固定定位,被固定的元素不会随着滚动条的拖动而改变位置。

使用格式如下:

html 复制代码
position: fixed;

我们来举个例子吧!

新建一个 index1.html 文件,在其中写入以下内容。

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100%;
        margin-left: 15%;
      }
      .ad-l {
        position: fixed;
        top: 100px;
        left: 0;
      }
      .ad-r {
        position: fixed;
        top: 100px;
        right: 0;
      }
    </style>
  </head>
  <body>
    <img src="ad-l.png" class="ad-l" />
    <img src="ad-r.png" class="ad-r" />
    <div class="box">
      <img src="box.png" />
    </div>
  </body>
</html>

相对定位

相对定位是该元素的位置相对于它原始的位置来计算的。position 属性为我们提供了 relative 属性值来设置元素的相对属性。

html 复制代码
position: relative;

我们来举个例子吧!

新建一个 index2.html 文件,在其中写入以下内容。

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100%;
      }
      .ad-l {
        position: relative;

        left: -40px;
      }
      .ad-r {
        position: relative;

        left: 100px;
      }
    </style>
  </head>
  <body>
    <img src="ad-l.png" class="ad-l" />
    <img src="ad-r.png" class="ad-r" />
    <div class="box">
      <img src="box.png" />
    </div>
  </body>
</html>

绝对定位

绝对定位,能把元素精确地放在任意位置。position 属性为我们提供了 absolute 属性值来设置元素的相对属性。

语法格式为:

html 复制代码
position: absolute;

我们来举个例子吧!

新建一个 index3.html 文件,在其中写入以下内容。

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100%;
        margin-left: 180px;
      }
      .ad-l {
        position: absolute;
        left: 50px;
        top: 150px;
      }
      .ad-r {
        position: absolute;
        right: 30px;
        top: 150px;
      }
    </style>
  </head>
  <body>
    <img src="ad-l.png" class="ad-l" />
    <img src="ad-r.png" class="ad-r" />
    <div class="box">
      <img src="box.png" />
    </div>
  </body>
</html>

总结

本节实验给大家介绍了布局相关的属性,浮动和定位。

在浮动中,给大家介绍了左浮动(float:left)和右浮动(float:right)的用法。

在定位中,给大家介绍了四种:

• 静态定位(position:inherit)

• 相对定位(position:relative)

• 绝对定位(position:absolute)

• 固定定位(position:fixed)

相关推荐
百万蹄蹄向前冲30 分钟前
Trae分析Phaser.js游戏《洋葱头捡星星》
前端·游戏开发·trae
朝阳5811 小时前
在浏览器端使用 xml2js 遇到的报错及解决方法
前端
GIS之路1 小时前
GeoTools 读取影像元数据
前端
ssshooter2 小时前
VSCode 自带的 TS 版本可能跟项目TS 版本不一样
前端·面试·typescript
Jerry2 小时前
Jetpack Compose 中的状态
前端
dae bal3 小时前
关于RSA和AES加密
前端·vue.js
柳杉3 小时前
使用three.js搭建3d隧道监测-2
前端·javascript·数据可视化
lynn8570_blog4 小时前
低端设备加载webp ANR
前端·算法
LKAI.4 小时前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
刺客-Andy4 小时前
React 第七十节 Router中matchRoutes的使用详解及注意事项
前端·javascript·react.js