当前位置: 首页 > article >正文

HTML元素居中

⾏内元素⽔平垂直居中 设置⽗级标签。 ⽔平居中: text-align: center 垂直居中: line-height:盒⼦⾼度

⽔平垂直都居中

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      position: relative;
      width: 200px;
      height: 200px;
      border: 1px solid black;
    }

    .child {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 100px;
      height: 100px;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="child"></div>
  </div>
</body>
</html>

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

这三个需要一起用实现居中

 绝对定位 + 四个方向 margin: auto

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      position: relative;
      width: 200px;
      height: 200px;
      border: 1px solid black;
    }

    .child {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      width: 100px;
      height: 100px;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="child"></div>
  </div>
</body>
</html>

      top: 0;

      left: 0;

      right: 0;

      bottom: 0;

      margin: auto;这5个需要一起用

flex 布局

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 200px;
      height: 200px;
      border: 1px solid black;
    }

    .child {
      width: 100px;
      height: 100px;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="child"></div>
  </div>
</body>
</html>

      display: flex;

      justify-content: center;

      align-items: center;这三个需要一起用

table-cell

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      display: table-cell;
      text-align: center;
      vertical-align: middle;
      width: 200px;
      height: 200px;
      border: 1px solid black;
    }

    .child {
      width: 100px;
      height: 100px;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="child"></div>
  </div>
</body>
</html>

      display: table-cell;

      text-align: center;

      vertical-align: middle;这三个需要一起用


http://www.kler.cn/news/326855.html

相关文章:

  • 一款好用的远程连接工具:MobaXterm
  • Xcdoe快速更新安装的小Tips
  • 工业制造场景中的设备管理深度解析
  • QT-文件创建时间修改器
  • 安全运营 -- GPO审计
  • Chrome Cookie最大有效期
  • Web3的愿景:如何构建去中心化的互联网
  • Llama微调以及Ollama部署
  • 阿里云k8s如何创建可用的api token
  • 腾讯云SDK产品优势
  • 4.2.1 通过DTS传递物理中断号给Linux
  • 全面指南:探索并实施解决Windows系统中“mfc140u.dll丢失”的解决方法
  • NAND Flash虚拟层坏块管理机制
  • python爬虫案例——抓取链家租房信息(8)
  • Solaris11.4配置远程桌面登录
  • STM32 软件触发ADC采集
  • 基于MATLAB的苹果外观特征检测
  • 【计算机网络 - 基础问题】每日 3 题(二十六)
  • 针对考研的C语言学习(定制化快速掌握重点4)
  • IDEA几大常用AI插件
  • Splashtop 加入 Microsoft 智能安全协会
  • VBA解除Excel工作表保护
  • 浅析Android中的View事件分发机制
  • 19.2 编写dockerfile和k8s yaml
  • 迁移学习案例-python代码
  • Redis一些简单通用命令认识常用数据类型和编码方式认识Redis单线程模型
  • OJ题之单链表排序
  • 智慧城市运营模式--联合公司运营
  • ThinkPHP发送邮件教程:从配置到发送指南!
  • ChatGPT的150个角色提示场景实测(9)讲故事