1、水平居中
.footer {
height: 10%;
text-align: center;
position: relative;
left: 50%;
transform: translate(-50%, -50%); /*水平居中*/
top: 30px;
} 2、垂直居中:
.footer {
height: 10%;
text-align: center;
position: fixed;
top: 50%;
transform: translate(-50%, -50%); /*水平居中*/
left: 30px;
}
3、万能居中法:

#name{
position:fixed;
left:50%;
top:50%;
transform:translate(-50%,-50%);
z-index:1000;
}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

通常情况下,我们通过操作margin来控制元素居中,代码如下:

1 #name{
2 maigin:0px auto;
3 }

但当我们把position设置为fixed时,例如:

1 #id{
2 position:fixed;
3 margin:0px auto;
4 }

以上代码中的margin:0px auto;会出现失效问题,盒子并未居中。这是因为fixed会导致盒子脱离正常文档流。
解决方法非常简单,只要应用如下代码即可:

1 #name{
2 position:fixed;
3 margin:0px auto;
4 right:0px;
5 left:0px;
6 }

若希望上下也可以居中,可采用如下代码:

1 #name{
2 position:fixed;
3 margin:auto;
4 left:0;
5 right:0;
6 top:0;
7 bottom:0;
8 }

万能居中法(未知大小呦):

1 #name{
2 position:fixed;
3 left:50%;
4 top:50%;
5 transform:translate(-50%,-50%);
6 z-index:1000;
7 }

position fixed 居中的更多相关文章

  1. css3 position fixed居中的问题

    通常,我们要让某元素居中,会这样做: #element{ margin:0 auto; } 假设还想让此元素位置固定呢?一般我们会加入position:fixed,例如以下: #element{ po ...

  2. position:fixed 居中问题

    设置Css属性position:fixed后如何使这个盒子居中呢?其实也很简单: 就是需要设置给这个div盒子设置属性: left:0; right:0; margin:0 auto; ******* ...

  3. 关于position:fixed;的居中问题

    通常情况下,我们通过操作margin来控制元素居中,代码如下: #name{ maigin:0px auto; } 但当我们把position设置为fixed时,例如: #id{ position:f ...

  4. css中position:fixed实现div居中

    上下左右 居中 代码如下 复制代码 div{ position:fixed; margin:auto; left:0; right:0; top:0; bottom:0; width:200px; h ...

  5. position:fixed;如何居中

    div{ position:fixed; margin:auto; left:; right:; top:; bottom:; width:100px; height:100px; } 如果只需要左右 ...

  6. position:fixed div如何居中

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

  7. day2-设置position:fixed/absolute无法使用margin:auto调整居中

    问题描述:父元素给定宽度,子元素给定宽度,设置子元素position为absolute/fixed后,无法使用margin:auto使子元素在父元素中水平居中 html代码如下: <div cl ...

  8. 使用属性position:fixed的时候如何才能让div居中

    css: .aa{ position: fixed; top: 200px; left: 0px; right: 0px; width: 200px; height: 200px; margin-le ...

  9. 元素设置position:fixed属性后IE下宽度无法100%延伸

    元素设置position:fixed属性后IE下宽度无法100%延伸 IE bug 出现条件: 1.div1设置position:fixed属性,并且想要width:100%的效果. 2.div2(下 ...

随机推荐

  1. vue如何加搜狗联盟广告

    搜狗联盟广告和百度联盟广告大同小异,但是好在百度联盟广告的使用方法网上还是可以找到一些,而搜狗联盟广告的教程网上几乎找不到,都是最基本的创建代码位的教程. 在vue中如果要加入搜狗联盟广告,就不能直接 ...

  2. Linux┊理解devfs、sysfs、udev、tmpfs等各种文件系统

    https://www.cnblogs.com/yangliheng/p/6187193.html https://blog.csdn.net/qq258711519/article/details/ ...

  3. ios的AutoresizingMask【转】

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. enum {   UIViewAutoresi ...

  4. vs2012修复问题

    多装了一个.net framework4.5.1结果vs不能拥,借用了下面这个工具将vs2012从注册表中删除了 就能重装了 http://www.auslogics.com/en/software/ ...

  5. Fiddler 抓取 Genymotion 数据包

    对genymotion进行如下设置

  6. C++ 默认构造函数小记

    #include <string> #include <map> using namespace std; class A { public: A(string name):n ...

  7. Netty权威指南之NIO通信模型

    NIO简介:与Socket和ServerSocket类相对应,NIO提供了SocketChannel和ServerSocketChannel两种不同的套接字通道实现,这两种新通道都支持阻塞和非阻塞两种 ...

  8. Mac 使用 launchctl 定时运行程序(转载)

    摘要: 在linux下可以用crontab来定时执行任务,在mac下可以用launchctl来定时执行任务. 在linux下可以用crontab来定时执行任务,在MAC下可以用launchctl来定时 ...

  9. str.split()与re.split()的区别

    str.split(): >>>'hello, world'.split() >>>['hello,','world'] >>>'hello, w ...

  10. iOS开发-UIImageView的contentMode属性

    UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...