当前位置:网站首页>Two tips for block level elements
Two tips for block level elements
2022-06-28 08:13:00 【Mediocrity and laziness】
The middle of block level elements
Before writing in the middle, it is usually margin:atuto Or an elastic box , I have to say that the elastic box is really easy to use . But today, I will record the other two centering methods .
The first one is
We all know that one way to center elements is to set the positioning left:50% then transform:translateX(-50%), This method is also commonly used . But today's centring method is a little different from this, but it is simpler .
<style>
.big{
width: 300px;
height: 300px;
border: 1px solid red;
position: relative;
margin: auto;
}
.small{
width: 100px;
height: 100px;
background-color: deeppink;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="big">
<div class="small"></div>
</div>
The renderings are as follows :
You can see that the center of my big box is used margin:auto, So is the little box margin:auto, So why is one centered on the left and right, and the other centered on the top, bottom, left and right .
The reason is that margin:auto The principle of centring , because margin:auto Is based on the flow of elements , In the example above, the big box is a standard block level element , So his width is 100% by default .
Although the big box looks only as wide as I set it , But his stream still defaults to 100% , So I set margin:auto Then it will be centered .
The same goes for small boxes , After positioning, I gave up, down, left and right 0, This will stretch the flow of the small box to the size of the large box , At this time, I set the small box margin:auto The small box will be centered around the large box .
The second kind
The second method actually uses elastic boxes , But it is more direct than setting the center of the main shaft and the secondary shaft .
The code is as follows :
<style>
.big{
width: 300px;
height: 300px;
border: 1px solid red;
display: flex;
margin: auto;
}
.small{
width: 100px;
height: 100px;
background-color: deeppink;
margin: auto;
}
</style>
</head>
<body>
<div class="big">
<div class="small"></div>
</div>
The renderings here are the same as those above, so I won't make a demonstration , Let me just say the reason here .
When an element is set with an elastic box, its child elements automatically become block level elements , The secondary axis of the element is stretched , So the effect is the same as the above .
边栏推荐
- 2022第六季完美童模 佛山赛区 初赛圆满落幕
- Explanation and application of instr() function in Oracle
- Eslint 语法监测关闭
- 图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换
- Airflow2.1.1 ultra detailed installation document
- MySQL installation and environment variable configuration
- ROS notes (08) - definition and use of service data
- 【学习笔记】差分约束
- Soft exam -- software designer -- afternoon question data flow diagram DFD
- Trigonometric transformation formula
猜你喜欢

微内核Zephyr获众多厂家支持!

MySQL implements transaction persistence using redo logs

关于在cmd中MySQL不能插中文数据的原因

Ambari (V) ---ambari integrated Azkaban (valid for personal test)

Ambari (VIII) --- ambari integrated impala document (valid for personal test)

Online WPS tool

Doris学习笔记之介绍、编译安装与部署

SOC timer and interrupt configuration

MySQL installation and environment variable configuration

Prometheus monitoring (I)
随机推荐
Is it reliable for the top ten securities companies to register and open accounts? Is it safe?
B_QuRT_User_Guide(28)
匿名页的反向映射
Redis cerebral fissure
Buffer pool in MySQL
Oracle view all tablespaces in the current library
nlp序列完全可以模拟人脑智能
SQL master-slave replication setup
ROS 笔记(09)— 参数的查询和设置
Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
Reverse mapping of anonymous pages
B_ QuRT_ User_ Guide(26)
Introduction to kubernetes (I)
Ambari (V) ---ambari integrated Azkaban (valid for personal test)
Unity - Pico开发 输入系统等相关API的使用---C#篇
Vagrant installation
券商注册开户靠谱吗?安全吗?
Upgrade HDP spark to spark 2.4.8 without upgrading ambari
Introduction to Devops Basics
sql主從複制搭建