absoulue与relative配合定位盒子居中问题

如何通过absoulue与relative配合把一个盒子或者是把2个div块同时放到页面中央部分?定位完成后为什么又需要margin-left与margin-top各往回走50%的长度,别忘记用z-index定位高度,请看下面代码展示:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <title>absoulue与relative配合定位盒子居中问题</title>
 7     <style type="text/css">
 8     *{
 9         margin: 0;
10         background-color: yellow;
11     }
12     /* 如何把-一个盒子放到页面中央 */
13     .box{
14         width: 100px;
15         height: 100px;
16         background-color: blue;
17         position: absolute;
18         left: 50%;
19         bottom: 50%;
20         margin-top: -50px;
21         margin-left: -50px;
22         z-index: 2;
23     }
24     /*2.如何把2个div块同时居中*/
25     .div1{
26         width: 500px;
27         height: 300px;
28         background-color: red;
29         position: absolute; ;
30         left: 50%;
31         top: 50%;
32         /*居中的只是一个点,所以需要往左走250,往上走150*/
33         margin-left: -250px;
34         margin-top: -150px;
35          }
36     .div2{
37         width: 200px;
38         height: 100px;
39         background-color:green;
40         position: absolute;
41         top: 50%;
42         left: 50%;
43         margin-left: -100px;
44         margin-top: -50px;
45     }
46     </style>
47     <script>
48         window.onload=function(){
49             var obj=document.getElementById(‘ceshi‘)
50             obj.onclick=function(){
51                 console.log(‘123‘);
52                 alert(‘我一直在寻找找到了你便找到了整个世界‘)
53             }
54         }
55     </script>
56 </head>
57 <body>
58     <div class="box" id="ceshi"></div>
59     <div class="div1">
60         <div class="div2">
61         </div>
62     </div>
63 </body>
64 </html>

原文地址:https://www.cnblogs.com/dhnblog/p/12347127.html

时间: 2024-08-28 10:04:31

absoulue与relative配合定位盒子居中问题的相关文章

定位的盒子居中显示

一.定位的盒子居中显示 ★:margin:0 auto;  只能让标准流的盒子居中对齐. ★定位的盒子居中:先向右走父元素盒子的一半50%,再向左走子盒子的一半(margin-left:负值.) 二.设置盒子左外边距为auto,将盒子冲到右边 margin-left:auto;

实现盒子居中的方式[经典面试题]

* { margin: 0; padding: 0; } .box1 { float: left; position: relative; width: 200px; height: 200px; border: 1px solid red; } .son1 { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .box2 { /* float: left; */ display: table

使定位元素居中的4种方法例举

例子: 创建两个div,定位父级'box'  和   定位元素'sun'.本文要做的是 'sun' 要在 'box' 中居中显示, 该怎么做呢? 定位前效果图: 定位元素居中后的效果图: HTML代码: <!--定位元素父级--> <div id="box"> <!--定位元素--> <div id="sun"></div> </div> 样式代码: <style type="t

CSS定位和居中问题

定位和居中问题是CSS中经常遇到的.对于一个定长定高的元素,实现其在父元素中的垂直水平居中,可通过position属性实现.当元素大小可变时,这种方法就失效了,我们可以通过flex和table实现居中,这两种方法今后会细致研究,这里只使用position方法. 任务需要完成的效果图如下: 相应代码如下: 1 .main{ 2 position: absolute; 3 top: 50%; 4 left: 50%; 5 width: 400px; 6 height: 200px; 7 margin

css盒子居中

方法1(margin: 0 auto)<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css浮动盒子居中</title> <style> *{ margin: 0; padding: 0; list-style: none; } body{ background: #fffccc; text-

css学习笔记之盒子居中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

HTML连载41-水平居中的注意点、盒子居中和内容居中

一.盒子模型练习 我们有个需求: 创建两个盒子,大盒子嵌套一个小盒子,大盒子是红色的,小盒子是蓝色的,并且小盒子在大盒子中是居中的. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .big{ width:500px; height: 500px;

Web全栈-盒子居中和内容居中

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子居中和内容居中</title> <style> .father{ width: 800px; height: 500px; background-color: red; /*text-align: center;*/ margin:0 aut

内嵌盒子定位和居中问题,在缩放浏览器情况下,不会影响盒子的布局

问题描述:第一眼我们看到上图,可能就想到用浮动来布局,但是在用浏览器调试缩放时“您好中国”盒子会自然成列.那么怎么让他们自适应,让好和中之间始终处于浏览器中间位置? <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin: 0; p