HTML元素居中

ops/2024/9/24 8:11:18/
htmledit_views">

⾏内元素⽔平垂直居中 设置⽗级标签。 ⽔平居中: 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.ppmy.cn/ops/115199.html

相关文章

锁策略, cas 和 synchronized 优化过程

1.1 常見的鎖策略 預測鎖衝突概率 樂觀鎖&#xff1a;加鎖的時候&#xff0c;假設出現鎖衝突的概率不大。圍繞加鎖做的工作會更少。 悲觀鎖&#xff1a;加鎖的時候&#xff0c;假設鎖出現衝突的概率很大。圍繞加鎖做的工作會更多。 synchronized “自適應” 初始是樂觀的。鎖…

WordPress建站钩子函数及使用

目录 前言&#xff1a; 使用场景&#xff1a; 一、常用的wordpress钩子&#xff08;动作钩子、过滤器钩子&#xff09; 1、动作钩子&#xff08;Action Hooks&#xff09; 2、过滤器钩子&#xff08;Filter Hooks&#xff09; 二、常用钩子示例 1、添加自定义 CSS 和 JS…

解决Vue 3中Element Plus el-color-picker 组件消失的问题

概述 在使用Vue 3和Element Plus框架构建应用程序时&#xff0c;你可能会遇到el-color-picker组件无法正常显示的情况。这可能是由于多种原因造成的&#xff0c;包括但不限于CSS加载问题、JavaScript错误、版本兼容性等。本文将指导你如何排查并解决这个问题。 在 el-color-p…

Java之String 类的学习

目录 1. String类的重要性 2. 常用方法 2.1 字符串常用3种构造 2.2 String对象的4种比较 1. 比较是否引用同一个对象 2. boolean equals(Object anObject) 方法&#xff1a;按照字典序比较 3. int compareTo(String s) 方法: 按照字典序进行比较 4. int compar…

Blender软件三大渲染器Eevee、Cycles、Workbench对比解析

Blender 是一款强大的开源3D制作平台&#xff0c;提供了从建模、雕刻、动画到渲染、后期制作的一整套工具&#xff0c;广泛应用于电影、游戏、建筑、艺术等领域。 渲染101云渲染云渲6666 相比于其他平台&#xff0c;如 Autodesk Maya、3ds Max 或 Cinema 4D&#xff0c;Blende…

平衡二叉树(AVL树):原理、常见算法及其应用

目录 引言 AVL树的基本概念 常见算法 插入节点 查找节点 删除节点 AVL树的应用场景 1. 数据库索引 2. 符号表 3. 字典和词汇表 4. 动态集合 5. GPS导航系统 6. 计算机辅助设计&#xff08;CAD&#xff09; 结论 引言 平衡二叉树&#xff08;Balanced Binary Tre…

解决启动docker desktop报The network name cannot be found的问题

现象 deploying WSL2 distributions ensuring main distro is deployed: checking if main distro is up to date: checking main distro bootstrap version: getting main distro bootstrap version: open \wsl$\docker-desktop\etc\wsl_bootstrap_version: The network name…

golang学习笔记3-变量的声明

注&#xff1a;本人已有C&#xff0c;C,Python基础&#xff0c;只写本人认为的重点。 一、变量的三种声明方式 func main() {//方式1&#xff0c;指定数据类型&#xff0c;声明后若不赋值&#xff0c;使用默认值//比如int的默认值是0&#xff0c;string的默认值是空串var i in…