css中position:fixed怎么实现div居中上下左右居中

发布时间:2021-07-29 23:17:51 作者:chen
来源:亿速云 阅读:356

这篇文章主要介绍“css中position:fixed怎么实现div居中上下左右居中”,在日常操作中,相信很多人在css中position:fixed怎么实现div居中上下左右居中问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”css中position:fixed怎么实现div居中上下左右居中”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

上下左右 居中

代码如下

代码如下:


div{
position:fixed;
margin:auto;
left:0;
right:0;
top:0;
bottom:0;
width:200px;
height:150px;
}


如果只需要左右居中,那么把 bottom:0; 或者 top:0; 删掉即可
如果只需要上下居中,那么把 left:0; 或者 right:0; 即可

下面附一个DIV 元素在浏览器窗口居中

其实,实现这个效果并不复杂,利用 CSS 中的 position 定位就可以轻松搞定了。来看看代码吧:

代码如下
 

代码如下:


<style type="text/css">
.dialog{
position: fixed;
_position:absolute;
z-index:1;
top: 50%;
left: 50%;
margin: -141px 0 0 -201px;
width: 400px;
height:280px;
border:1px solid #CCC;
line-height: 280px;
text-align:center;
font-size: 14px;
background-color:#F4F4F4;
overflow:hidden;
}
</style>
<div class="dialog">我是在窗口正中央的,呵呵!</div>


 
设置的技巧全部在这里:

代码如下

代码如下:


.dialog{
position: fixed;
_position:absolute; /* hack for IE6 */
z-index:1;
top: 50%;
left: 50%;
margin: -141px 0 0 -201px;
width: 400px;
height:280px;
border:1px solid #CCC;
line-height: 280px;
text-align:center;
font-size: 14px;
background-color:#F4F4F4;
overflow:hidden;
}


设置 position: fixed; _position:absolute;
设置 left:50% 和 top:50%;
设置 margin: -(DIV的offsetWidth/2) 0 0 -(DIV的offsetHeight/2)

到此,关于“css中position:fixed怎么实现div居中上下左右居中”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. HTML中怎么引入CSS
  2. css操作控件实现disable效果的方式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css

上一篇:linux系统中如何批量修改图片文件名

下一篇:linux 5.4 nagios监控的搭建方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》