div上下左右居中

css实现:

<style type="text/css">

.middle{
   width:300px;  
   height:200px;  
   position:absolute;  
   left:50%;  
   top:50%;  
   margin:-100px 0 0 -150px ;
   border:1px solid red;
    }
</style>
</head>

<body>
    <div class="middle"></div>
</body>
</html>

js实现:

<style type="text/css">

.middle{
   width:300px;  
   height:200px;  
   
   border:1px solid red;
    }
</style>
</head>

<body>
    <div class="middle"></div>
</body>
<script type="text/javascript">
$(function(){
    $(".middle").css({
        position:"absolute",
        left:($(window).width()-$(".middle").width())/2,
        top:($(window).height()-$(".middle").height())/2,
    
        })
    })
</script>
</html>

时间: 2024-08-23 23:06:16

div上下左右居中的相关文章

css:子元素div 上下左右居中方法总结

最近在面试,不停地收到了知识冲击,尤其是对于一些基础的css.html.js问题居多,所以自我也在做反思,今天就css问题,如何让一个子元素div块元素上下左右居中 (以下总结方法,都已得到验证). 情景一:一个浏览器页面中,只有一个div模块,让其上下左右居中 解决方案:  { position:fixed;  left:0; right:0; top:0; bottom:0; margin:auto; } 备注:此处小编使用position:fixed为最佳方案,因为position:fix

css div上下左右居中

相信大家都会遇到这样的问题,要求一个块上下左右居中,在这里我总结了几个好用的方法 1.已知要居中的块width height 假设  content 要在f里上下左右居中 <div class="f"><div class="content"></div></div> <style> .f{ width: 800px; height: 800px; position:relative; } .content

元素div 上下左右居中方法总结

情景一:一个浏览器页面中,只有一个div模块,让其上下左右居中 解决方案:  { position:fixed;  left:0; right:0; top:0; bottom:0; margin:auto; } 情景二:一个父元素div,一个已知宽度.高度的子元素div(200*300) 解决方案: 1.position布局 { position:absolute/fixed; top:50%; left:50%; margin-left:-100px; margin-top:-150px;}

关于一个div上下左右居中的css方法

1:通过position:absolute定位,上下左右的值都设为0,margin:auto:需要知道div的宽高 { width: 64px; height: 64px; border: 1px solid red; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; } 2:需要知道div的宽高,通过定位移动的页面的宽高一半的位置,再通过margin-top和margin-left的移动该div的宽高

原生 js 让div上下左右居中

html 里写出一个div, css给div 定宽高和背景色: <div class="boxes"> </div> <style type="text/css"> .boxes{ width: 200px; height: 200px; background: #CCCCCC; } </style> 上面绘出的是一个宽高给为200的背景色为浅灰色的框框. 现在要让这个div 相对于浏览器居中,通过一段javascri

让一个div 上下左右居中

方法1:.div1{         width:400px;         height:400px;         border:#CCC 1px solid;         background:#99f;         position:absolute;         left:50%;         top:50%;         transform: translate(-50%,-50%);}                        <div class=&quo

jquery设置一个不固定高度的div 上下左右居中

$(window).resize(function(){ $(".mydiv").css({ position: "absolute", left: ($(window).width() - $(".mydiv").outerWidth())/2, top: ($(window).height() - $(".mydiv").outerHeight())/2 }); }); // 然后在页面加载时 $(function(){

div块上下左右居中

实现div块的上下左右居中: <!DOCTYPE html> <html> <head>     <meta charset="utf-8" />     <title>div块上下左右居中</title> </head> <body>     <div id="div2">     </div>     <style>     *{p

div元素上下左右居中

1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>div元素上下左右居中</title> 6 <style type=