How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?

解答

 

Negative margins are valid in css and understanding their (compliant) behaviour is mainly based on the box model and margin collapsing. While certain scenarios are more complex, a lot of common mistakes can be avoided after studying the spec.

For instance, rendering of your sample code is guided by the css spec as described in calculating heights and margins for absolutely positioned non-replaced elements.

If I were to make a graphical representation, I'd probably go with something like this (not to scale):

The margin box lost 8px on the top, however this does not affect the content & padding boxes. Because your element is absolutely positioned, moving the element 8px up does not cause any further disturbance to the layout; with static in-flow content that's not always the case.

Bonus:

Still need convincing that reading specs is the way to go (as opposed to articles like this)? I see you're trying to vertically center the element, so why do you have to set margin-top:-8px; and not margin-top:-50%;?

Well, vertical centering in CSS is harder than it should be. When setting even top or bottom margins in %, the value is calculated as a percentage always relative to the width of the containing block. This is rather a common pitfall and the quirk is rarely described outside of w3 docos

How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?的更多相关文章

  1. jsp学习---css基础知识学习,float,position,padding,div,margin

    1.常用页面布局 效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  2. css设置时父元素随子元素margin值移动

    父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. HTML,CSS: <div ...

  3. css使既有浮动又有左右margin的多个元素两端对其

    两端对齐效果 如上图中红色的9个div它们中间有间距,而最左边和最右边是没有间距的,这种布局如果使用css3的flex来实现是非常简单的,而如果要使用float布局就需要一些特殊的技巧了. 实现原理 ...

  4. CSS skills: 6) auto hide the top bar javascript

    //jquery $(document).ready(function(){ $(window).scroll(function() { $(this).scrollTop() > 10 ? $ ...

  5. 每天CSS学习之top/left/right/bottom

    top:值域是数值或百分比,正负都可以.该值表示 距离顶部有多少像素.例如top:10px:即距离顶部10个像素. left/right/bottom与top如出一辙,只是方向不一样而已. 这些属性一 ...

  6. CSS解决ul下面最后一个li的margin

    1.运用css3的nth-child(3n): <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  7. 【转】CSS之Background-Position left right center top bottom属性

    background-position:left top; 背景图片的左上角和容器(container)的左上角对齐,超出的部分隐藏. 等同于 background-position:0,0; 也等同 ...

  8. 如何用CSS快速布局(一)—— 布局元素详细

    要快速进行网页排版布局,则必须对布局的元素有清晰的了解,才不会总是在细节处出错.这一篇先详解有关布局的因素作为布局基础:块级元素and内联元素.盒模型.准确定位.元素对齐.样式继承.下一篇则重点描述快 ...

  9. CSS小全

    CSS 的使用 内联(inline style attribute) 完全不应该这样做 <head> 标签内的 <style> 标签 偶尔可以用 <link> 标签 ...

随机推荐

  1. windows的一些常用指令

    持续更新中..... 1.清除系统内 DNS 的缓冲  :  nslookup baidu.com 2.修改hosts文件  :  位置 运行  ->  C:/windows/system32/ ...

  2. 逐步探究ObjC的Weak技术底层

    前言 之前的文章有说过 Atomic 原子操作的原理,其作为一个特殊的修饰前缀,影响了存取操作. 在属性修饰定义中,还有另一类修饰前缀,他们分别是 strong weak assign copy,这些 ...

  3. 栈 · 有getMin功能的栈O(1)

    题面 实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中的最小元素的操作. 要求 pop(), push(), getMin()时间复杂度都是O(1) 思路 1.准备两个栈,数据栈+最小元素 ...

  4. Oracle学习笔记——Linux下开启Oracle

    1.开启数据库 sqlplus  /  as sysdba startup 2.启动监听:lsnrctl  start; 查看监听状态:lsnrctl status; 3.登入数据库 Linux 设置 ...

  5. zabbix-通过自动发现添加主机

    当生产环境中需要监控海量的机器的时候,特别是像58.赶集这类同城性质的大网站,或者京东.阿里云这样的造节电商,每次活动.大促都需要添加很多机器来应对海量用户流量,每天都有可能上架新的机器.或者添加新的 ...

  6. js 跳出for/for in/each/for each 循环

    写本文原因:最近用到了for in,用return true跳出本次循环,执行下次循环, 结果发现程序没有预期效果,经过调试发现误用了return true, 特此笔记,欢迎指正. 注意:return ...

  7. Image Processing and Analysis_15_Image Registration:HAIRIS: A Method for Automatic Image Registration Through Histogram-Based Image Segmentation——2011

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  8. 【Spring】源码浅析 - ResponseEntity.ok 转载

    https://www.jianshu.com/p/1238bfb29ee1 ResponseEntity.ok具体源码

  9. CSS3媒体查询实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Java锁--Condition

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496716.html Condition介绍 Condition的作用是对锁进行更精确的控制.Condi ...