问题描述:父元素给定宽度,子元素给定宽度,设置子元素position为absolute/fixed后,无法使用margin:auto使子元素在父元素中水平居中

html代码如下:

    <div class="test">
<div class="m-fixed">
</div>
</div>

css代码:

.test{
height: 200px;
font-size: 14px;
width: 100%;
background-color: #D8BFD8
}
.m-fixed{
position: fixed;
top:;
width: 80%;
background-color: #00FF00;
height: 200px;
margin: auto;
}

效果图:

解决办法:

在子元素设置:left:0;right:0

如果像我一样宽度用的是百分比的话可以直接设置子元素:left:(1-子元素占父元素宽度百分比)/2,我这里是:left:10%

实现效果:

问题导致原因和解决办法实现原理个人还不是很明白,后面搞清楚了以后补上。

day2-设置position:fixed/absolute无法使用margin:auto调整居中的更多相关文章

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

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

  2. 当父级是body时,子元素设置position:absolute;定位是根据body还是html呢?

    position:absolute 元素相对最近的 position 为 absolute / relative / fixed 的祖先元素(包含块)定位,如果没有这样的祖先元素,则以初始包含块进行定 ...

  3. margin auto 实现居中,与text-align:center的区别

    本文导读:一个元素在水平方向上所占的长度,由width ,padding ,和margin 决定.这些值中,只有width和margin-left,margin-right可以设为auto,text- ...

  4. 设置position(absolute,fixed)导致flex布局不生效

    个人解决办法:将要设置display:flex的dom外面在套一个div,并且设置宽度,就可以了.

  5. IE6和IE7下绝对定位position:absolute和margin的冲突问题解决

    绝对定位的Position:absoulte的元素,会让相邻的兄弟元素的margin-top失效.而如果去掉了兄弟元素的高度又会正常. <div id="layer1" st ...

  6. position为absolute的元素如何实现居中

    当给div设置absolute时,该元素已经脱离文档流,呈现浮动状态,只能通过left,top,right,bottom来设置属性,要实现居中有两种方法: 一.css法 <div class=& ...

  7. margin重叠现象与margin auto自适应居中

    上下相邻的(算一种兄弟关系)普通元素,上下边距并非简单的相加,而是取其中最大的边距值:而浮动的盒子边距是相加的:父子div也会发生重叠,并不是bug: <style>#test1{ wid ...

  8. IE8 margin: auto 无法居中

    需要给body元素添加属性 body { text-align: center; width: 100%; } ok,可以正常居中.

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

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

随机推荐

  1. IIS7 部署MVC 运行不了 注意事项

    经常安装完iis7或者7.5后,部署MVC3.运行后提示 403 找不到目录. 解决办法:需要重新注册下iis C:\Windows\Microsoft.NET\Framework64\v4.0.30 ...

  2. WARNING: inbound connection timed out (ORA-3136)

    WARNING: inbound connection timed out (ORA-3136) WARNING: inbound connection timed out (ORA-3136) Ta ...

  3. MVC和MVP区别

    从这幅图可以看到,我们可以看到在MVC里,View是可以直接访问Model的!从而,View里会包含Model信息,不可避免的还要包括一些业务逻辑. 在MVC模型里,更关注的Model的不变,而同时有 ...

  4. ORA-00904: "B"."METHOD": 标识符无效,00904. 00000 - "%s: invalid identifier"

    1 SELECT COUNT(1) FROM (SELECT a.id AS "id", a.district AS "district", a.company ...

  5. Statement、 PreparedStatement 、CallableStatement 区别和联系

    Statement. PreparedStatement .CallableStatement 区别和联系 1. Statement.PreparedStatement和CallableStateme ...

  6. python 学习笔记(三)根据字典中值的大小对字典中的项排序

    字典的元素是成键值对出现的,直接对字典使用sorted() 排序,它是根据字典的键的ASCII编码顺序进行排序,要想让字典根据值的大小来排序,可以有两种方法来实现: 一.利用zip函数将字典数据转化为 ...

  7. Python之异常处理合集

    PermissionError: [Errno 13] Permission denied open(filePath)中的filePath是一目录路径,而非目录路径 先前打开的file文件对象未被关 ...

  8. EncryptHelper加密对象-工具类

    using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.W ...

  9. Tor路径选择说明

    Tor Path Specification Roger Dingledine Nick Mathewson Note: This is an attempt to specify Tor as cu ...

  10. golang 数据类型之间的转换

    一.基本数据类型之间的转换 1.string到int int,err:=strconv.Atoi(string) 2.string到int64 int64, err := strconv.ParseI ...