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

居中面试问题

前端常问居中面试问题

css文本居中

文本水平居中

<div class="father"><div class="child"><div>
<div>

子类元素为行内元素,则给父类元素定义text-align:center

如果子元素是块元素,则给子元素定义margin:0 auto;

如果子元素是块级元素,可以通过display将子元素定义成行内元素,在给子元素定义text-align:center

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Document</title><style>.father {width: 800px;height: 800px;background-color: skyblue;}.child {display: block;text-align: center;background-color: orange;}</style>
</head>
<body><div class="father"><span class="child">我是一棵小小小小草奥</span></div>
</body>
</html>

在这里插入图片描述

如果margin:0 auto;加在父元素上,则父元素相对与body水平居中。text-align是作用于自身

文本垂直居中

<div>hello word </div>

例如这个高为100px,让文本居中你需要两个条件

div {vertical-align:middledisplay:table-cell;
}

这里display必须是table-cell

是table-cell会控制这个盒子的宽度为里面text的宽度,并不是真正可以设置宽高

另一种方法是,对单行文本的垂直居中可以将行高line-height设置为盒子的高度

给盒子做居中

盒子水平居中

margin:auto

盒子垂直居中,不能用margin,可以用定位或者flex布局实现或者浮动

用定位实现常见的父子盒子都是块元素的情况
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Document</title><style>.father {width: 800px;height: 800px;background-color: skyblue;position: relative;}.child {width: 200px;height: 200px;top: 50%;left: 50%;transform: translate(-50%,-50%);position: absolute;background-color: orange;}</style>
</head>
<body><div class="father"><div class="child"></div></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Document</title><style>.father {width: 800px;height: 800px;background-color: skyblue;position: relative;//父元素开启相对定位}.child {width: 200px;height: 200px;top: 0;left: 0;right: 0;bottom: 0;margin: auto; //居中关键就是外边距为autoposition: absolute; //子元素开启固定定位background-color: orange;}</style>
</head>
<body><div class="father"><div class="child"></div></div>
</body>
</html>

方式不一样但是呈现的样式是一样的

在这里插入图片描述

弹性盒子居中

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Document</title><style>.father {width: 800px;height: 800px;display: flex; //这里必须要开启弹性盒子justify-content: center; //水平居中align-items: center;//垂直居中background-color: skyblue;}.child {width: 200px;height: 200px;background-color: orange;}</style>
</head>
<body><div class="father"><div class="child"></div></div>
</body>
</html>

在这里插入图片描述

http://www.lryc.cn/news/280385.html

相关文章:

  • 网页设计-用户体验
  • docker应用:vocechat
  • linux 02 vmware的快照,文件管理
  • 项目架构之Zabbix部署
  • RocketMQ源码阅读-Message消息存储
  • 《C语言学习》---郝斌版---笔记
  • Python(32):字符串转换成列表或元组,列表转换成字典小例子
  • CentOS 7 安装私有平台OpenNebula
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • 基于STM32微控制器的四轮智能小车控制系统设计
  • JPA的复杂查询包括一对多多对一和多对多的查询
  • 电脑文件mfc100u.dll丢失的解决方法分析,怎么修复mfc100u.dll靠谱
  • 从DETR到Mask2former(2): 损失函数loss function
  • Java21 + SpringBoot3集成WebSocket
  • 鲸鱼优化算法WOA改进预告
  • Nightingale 夜莺监控系统 - 告警篇(3)
  • 【LeetCode2696】删除子串后的字符串最小长度
  • VMware安装CentOS7虚拟机
  • Linux第22步_安装CH340驱动和串口终端软件MobaXterm
  • Elasticsearch 地理空间搜索 - 远超 OpenSearch
  • USB micro输入口中三个问题详解——差分信号、自恢复保险丝SMD1210P050TF、电容滤波
  • mysql原理--undo日志1
  • Zookeeper系列(一)集群搭建(非容器)
  • 【高等数学之泰勒公式】
  • 奇异值分解在图形压缩中的应用
  • C++深入学习之STL:1、容器部分
  • Javascript——vue下载blob文档流
  • C# 的SequenceEqual
  • 第九部分 使用函数 (一)
  • 【JUC进阶】14. TransmittableThreadLocal