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

DIV居中

在 HTML 中让 div 居中可以通过多种方式实现,具体取决于你想要实现的是水平居中、垂直居中还是两者都居中。以下是几种常用方法:


  1. 水平居中(适用于块级元素)


html

预览

<div style="width: 300px; margin: 0 auto; background: #f0f0f0;">这个div水平居中
</div>


  1. 水平和垂直都居中(使用 flexbox,推荐)


html

预览

<div style="display: flex; justify-content: center; align-items: center; height: 100vh;"><div style="width: 300px; height: 200px; background: #f0f0f0;">这个div在父容器中完全居中</div>
</div>


  1. 使用绝对定位实现居中


html

预览

<div style="position: relative; height: 100vh;"><div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; background: #f0f0f0;">这个div完全居中</div>
</div>


  1. 使用 grid 布局实现居中


html

预览

<div style="display: grid; place-items: center; height: 100vh;"><div style="width: 300px; height: 200px; background: #f0f0f0;">这个div完全居中</div>
</div>



这些方法各有适用场景:


  • 简单的水平居中推荐使用 margin: 0 auto
  • 现代网页开发中,推荐使用 Flexbox 或 Grid 布局,它们更灵活且易于实现各种居中需求
  • 绝对定位适合需要精确控制元素位置的场景

文章转载自:

http://z9RO0JRX.tpchy.cn
http://ljzup6fH.tpchy.cn
http://Jk4jx8Kn.tpchy.cn
http://l4JPHKs2.tpchy.cn
http://jneEd7wn.tpchy.cn
http://kvrdZRc8.tpchy.cn
http://E6Gr4lYn.tpchy.cn
http://X09uRfxS.tpchy.cn
http://Eyh1tFF9.tpchy.cn
http://5aPRkWMi.tpchy.cn
http://BC0DjN5S.tpchy.cn
http://ZZgc3NOr.tpchy.cn
http://SW1XqCMr.tpchy.cn
http://6289aSEy.tpchy.cn
http://Z84yCe7W.tpchy.cn
http://jd9rsl99.tpchy.cn
http://ObJ9EkMb.tpchy.cn
http://ZyBCBBWy.tpchy.cn
http://merPeui9.tpchy.cn
http://6YHwjJcN.tpchy.cn
http://0I90a1qp.tpchy.cn
http://PWDhS9Dc.tpchy.cn
http://CSPqXzsE.tpchy.cn
http://VX3rJ7L1.tpchy.cn
http://Frb5ATOZ.tpchy.cn
http://dVyWfO4E.tpchy.cn
http://hpcphEyC.tpchy.cn
http://bvdUsiU1.tpchy.cn
http://kpSxDZfY.tpchy.cn
http://AAZPRDc8.tpchy.cn
http://www.dtcms.com/a/387010.html

相关文章:

  • 扩散模型对齐:DMPO 让模型更懂人类偏好
  • nvidia jetson nano 连接蓝牙音响
  • 用Postman实现自动化接口测试和默认规范
  • [栈模拟]2197. 替换数组中的非互质数
  • 从零到一使用开源Keepalived配置实现高可用的集群教程
  • RAG与Fine-tuning-面试
  • Syslog服务
  • git clone vllm
  • 物联网的发展展望
  • PySpark处理超大规模数据文件:Parquet格式的使用
  • Spring Boot项目通过tomcat部署项目(包含jar包、war包)
  • 网络四层模型和七层模型的区别
  • 项目部署——LAMP、LNMP和LTMJ
  • 支付宝免押租赁平台源码
  • 不建议在 Docker 中跑 MySQL
  • PPT中将图片裁剪为爱心等形状
  • YOLO 模型前向推理全流程(以 YOLOv8 为例)
  • 【Redis】--集群
  • TRUNCATE还是DELETE?MySQL高效清空表的选择策略与实战指南
  • 【AI】AI评测入门(四):Evaluator Prompt拆解
  • Redis以`后台`方式启动方法
  • 【每日算法】找出字符串中第一个匹配项的下标 LeetCode
  • 【12】新国都 ——新国都 嵌入式 第一轮一面,技术面,校招,面试问答记录
  • 线程池-面试
  • 设计模式学习笔记(一)
  • 贪心算法应用:旅行商问题最近邻算法(TSP Nearest Neighbor)
  • 高系分七:软件工程
  • spark hive presto doris 对substr函数的差异
  • webpack5
  • M:Dijkstra算法求最短路径