当前位置:网站首页>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 .
边栏推荐
- Estimation of SQL execution cost by MySQL query optimizer
- 2022第六季完美童模 佛山赛区 初赛圆满落幕
- Activity隐式跳转
- ZYNQ_ IIC read / write m24m01 record board status
- 解决npm ERR! Unexpected end of JSON input while parsing near问题
- Oracle view all tablespaces in the current library
- Redis master-slave structure and application scenarios
- Today's notes 22/1/7
- 软件测试与质量期末复习
- 你了解TCP协议吗(二)?
猜你喜欢
asp. Net upload image path and image name
SQL Master slave Replication Build
抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德
B_ QuRT_ User_ Guide(26)
Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
【尚品汇】项目笔记
Prometheus monitoring (I)
Ambari (V) ---ambari integrated Azkaban (valid for personal test)
你了解TCP協議嗎(二)?
Eslint syntax monitoring off
随机推荐
nlp序列完全可以模拟人脑智能
Study notes 22/1/18
SQL master-slave replication setup
B_QuRT_User_Guide(29)
sql主從複制搭建
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
Host is not allowed to connect to this MySQL server
B_ QuRT_ User_ Guide(30)
B_QuRT_User_Guide(28)
Airflow2.1.1 summary of the pits stepped on in actual combat!!
B_ QuRT_ User_ Guide(28)
Unity 获取当前物体正前方,一定角度、距离的坐标点
MySQL tablespace parsing
Installing mysql5.7 under Windows
Study notes 22/1/17
设置cmd的编码为utf-8
Reverse mapping of anonymous pages
[JS] - [DFS, BFS application] - learning notes
asp. Net registration page
Upgrade HDP spark to spark 2.4.8 without upgrading ambari