css不定宽高水平垂直居中

 时间:2024-10-14 03:09:16

1、新建一个html文件,在这个html文件上创建一个<div>,然后对这个<div>添加一个样式类,最后在这个<div>里再创建一个子元素并在这个元素中添加要垂直居中的内容。代码:<div class="paernt"> <div> 不宽高的水平垂直居中<br/> 不宽高的水平垂直居中<br/> </div> </div>

2、设置父元素样式。使用Flexbox弹性盒子让子元素自动水平垂直居中;使用justify-content设置子元素水平居中,使用align-items设置子元素垂直居中。css代码:<style type="text/css"> .parent{ height: 300px; background-color: #96b97d; justify-content: center; align-items: center; display: flex; } </style>

css不定宽高水平垂直居中

4、可以直接复制所有代码到新建的html文件上,粘贴保存后运行即可看到效果。所有代码:<!DOCTYPE html><稆糨孝汶;html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .parent{ height: 300px; background-color: #96b97d; justify-content: center; align-items: center; display: flex; } </style> </head> <body> <div class="parent"> <div> 不宽高的水平垂直居中<br/> 不宽高的水平垂直居中<br/> </div> </div> </body></html>

  • 生产许可证办理
  • 如何申请QT四位数ID
  • 英雄联盟神装谁单挑第一?
  • HBuilder如何新建Uni-app组件页面
  • 游戏藏宝湾攻略
  • 热门搜索
    贵阳的旅游景点 元阳梯田旅游攻略 旅游ppt背景图片 智圣汤泉旅游度假村 东莞旅游景点排行 江西三清山旅游 西安旅游攻略4天 丽江最佳旅游时间 温州洞头旅游 重庆到武隆旅游攻略