当前位置:网站首页>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 .
边栏推荐
- IO error in Oracle11g: got minus one from a read call
- [JS] - [DFS, BFS application] - learning notes
- SOC clock configuration
- 图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换
- Reverse mapping of anonymous pages
- 城联优品向英德捐赠抗洪救灾爱心物资
- 【学习笔记】线性基
- Configuring MySQL multi instance master-slave synchronization for Linux
- Jenkins' common build trigger and hook services (V)
- 十大券商注册开户靠谱吗?安全吗?
猜你喜欢

asp. Net datalist when there are multiple data displays

Prometheus + grafana + MySQL master-slave replication + host monitoring

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

Airflow2.1.1 summary of the pits stepped on in actual combat!!

GPIO configuration of SOC

Activity implicit jump

Generation and verification of JWT token

三角变换公式

MySQL tablespace parsing

Image translation /transformer:ittr: unpaired image to image translation with transformers
随机推荐
软件测试与质量期末复习
Airflow2.x distributed deployment DAG execution failure log cannot be obtained normally
Redis cluster deployment and application scenarios
Devops foundation chapter Jenkins deployment (II)
asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation
关于在cmd中MySQL不能插中文数据的原因
[JS] - [DFS, BFS application] - learning notes
Is it reliable for securities companies to register and open accounts? Is it safe?
Introduction to Devops Basics
Helloword routine for ROS
asp. Net datalist to display product information and pictures
Prometheus deployment alarm docking QQ mailbox
三角变换公式
Doris学习笔记之介绍、编译安装与部署
关于如何在placeholder中使用字体图标
In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde
Connaissez - vous le protocole TCP (2)?
ROS notes (08) - definition and use of service data
Generation and verification of JWT token