CSS前端:元素的布局技巧

【备注】在线测试网站,点此进入

一、内边距和外边距

【总结】padding增加本元素和子元素的距离,margin增加本元素和父元素的距离。

二、居中和对齐

【需求】

【HTML】

html 复制代码
<div id="container">
    <p id="top">我在div容器的顶部</p>
    <div id="inner-container">
      <p id="left">我在div容器的最左边</p>
      <p id="center">我在div容器的最中心</p>
      <p id="right">我在div容器的最右边</p>
    </div>
    <p id="bottom">我在div容器的底部</p>
  </div>

【CSS实现一】绝对位置:top和left

css 复制代码
 #container {
    width: 600px;
    height: 300px;
    background-color: yellow;
    margin: 0 auto;//整个元素在页面中居中
  }

  #container p {
    position: absolute; /* 绝对定位 */
  }

  #top {
    top: 0; /* 顶部定位 */
    left: 50%; /* 居中 */
    transform: translateX(-50%); /* 水平居中 */
  }

  #left {
    top: 50%; /* 垂直居中 */
    left: 0; /* 左边 */
    transform: translate(100%, -100%); /* 垂直居中 */
  }

  #center {
    top: 50%; /* 垂直居中 */
    left: 50%; /* 水平居中 */
    transform: translate(-50%, -100%); /* 居中 */
  }

  #right {
    top: 50%; /* 垂直居中 */
    right: 0; /* 右边 */
    transform: translate(-100%, -100%); /* 垂直居中 */
  }

  #bottom {
    bottom: 0; /* 底部定位 */
    left: 50%; /* 居中 */
    transform: translate(-50%, -100%); /* 水平居中 */
  }

备注:

1.需要微调一下距离

2.居中语法:

css 复制代码
margin: 0 auto

【CSS实现二】Flex布局:指定排列方式

HTML要改,最中间三个元素要一起打包:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Flexbox布局示例</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div id="container">
    <p id="top">我在div容器的顶部</p>
    <div id="inner-container">
      <p id="left">我在div容器的最左边</p>
      <p id="center">我在div容器的最中心</p>
      <p id="right">我在div容器的最右边</p>
    </div>
    <p id="bottom">我在div容器的底部</p>
  </div>
</body>
</html>

CSS:

css 复制代码
#container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  width: 600px;
  height: 300px;
  background-color: yellow;
  margin: 0 auto;
  padding: 10px;
}

#inner-container {
  display: flex;
  justify-content: space-between;
  width: 100%;
}

#top {
  margin-top: 0;
}

#left {
  margin-left: 0;
}

#right {
  margin-right: 0;
}

#bottom {
  margin-bottom: 0;
}

备注,这里设置了padding所以有点出入,设置为0即可

三、元素的覆盖

【需求】

【HTML】

html 复制代码
<div id="container">
  <div id="reddiv"></div>
  <div id="bluediv"></div>
</div>

【实现】

css 复制代码
#container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: yellow;
}

#reddiv, #bluediv {
  position: absolute;
  width: 200px;
  height: 200px;
}

#reddiv {
  background-color: red;
  z-index: 1; /* 红色div位于上层 */
  top: 100px;
  left: 0px;
}

#bluediv {
  background-color: blue;
  z-index: 2; /* 蓝色div位于更上层 */
  top: 100px;
  left: 100px;
}

举例下面的例子,指的是reddiv距离父级元素顶部100距离,叠放排序是1(越大越靠上)

css 复制代码
#reddiv {
  background-color: red;
  z-index: 1; /* 红色div位于上层 */
  top: 100px;
  left: 0px;
}
相关推荐
漂流瓶jz1 天前
Webpack中各种devtool配置的含义与SourceMap生成逻辑
前端·javascript·webpack
前端架构师-老李1 天前
React 中 useCallback 的基本使用和原理解析
前端·react.js·前端框架
musenh1 天前
css样式学习
css·学习·css3
木易 士心1 天前
CSS 中 `data-status` 的使用详解
前端·css
明月与玄武1 天前
前端缓存战争:回车与刷新按钮的终极对决!
前端·缓存·回车 vs 点击刷新
牧马少女1 天前
css 画一个圆角渐变色边框
前端·css
zy happy1 天前
RuoyiApp 在vuex,state存储nickname vue2
前端·javascript·小程序·uni-app·vue·ruoyi
小雨青年1 天前
Cursor 项目实战:AI播客策划助手(二)—— 多轮交互打磨播客文案的技术实现与实践
前端·人工智能·状态模式·交互
小光学长1 天前
基于Vue的儿童手工创意店管理系统as8celp7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js
meichaoWen1 天前
【Vue】Vue框架的基础知识强化
前端·javascript·vue.js