Wednesday, March 25, 2015

CSS BASIS

1. Floats
float pushes an element to the sides of a page with text wrapped around it. [When an element is floated, it is shifted to the left or right until it touches the edge of it's containing box or another floated element]
float property:
left, right, none(default), inherit(inherit from parent element)

2. Clear - turning off float:
Elements after the floating element will flow around it. Using "clear" can void that.
For example: with "clear: left", an element will be moved below any floating element on the left side.

3. Display
common properties:
inline: the default value for elements. like <span> <em> <b>
An inline element can wrap texts inside a paragraph without disrupting the flow of that paragraph.
block.
An inline element will accept margin and padding, margin and padding will only push other elements horizontally away, not vertical.


An inline element will not accept height and width.


inline-block: displays an element as an inline-level block container. The inside of this block is formatted as block-level box, and the element itself is formatted as an inline-level box. (can set width and height)








none:
when set display to none, it will render the page as though the element doesn't exist.
Difference with visibility:hidden --> it will hide the element, but the element will still take up the space it would if it was fully visible.
inherit,
list-item: let the element behave like a <li> element
initial: sets this property to its default value
table
display: block
display: block
display: block
display: block
display: inline
display: inline
display: inline
display: inline
display: inline-block
display: inline-block
display: inline-block
display: inline-block

4. Positioning
position: static
this is the default value for every single page element. It just means that the element will flow into the page as it normally would.
position: relative - based on the up-left corner of margin
the element is positioned relative to its normal position.
A page element with relative positioning gives you the control to absolutely position children elements inside of it.
position: absolute - based on the up-left corner of padding
this is used to literally place any page element exactly where you want it. You can use the positioning attributes top, left bottom, and right to set the location. These values will be relative to the next parent element with relative (or absolute) positioning. If there is no such element, it will default all the back to the <html> element itself meaning it will be placed relatively to the page itself. An element with absolute is not affected by other elements and it doesn't affect other elements.
position: fixed
A fixed position element is positioned relative to the viewport, or the browser window itself.
注: 使用relative,后面elements不会补位,而fixed和absolute,后面元素则会占据其之的位置

5. z-index (not finished)
this property controls the vertical stacking order of elements that overlap. [which one appears as if it is physically closer to you.]
z-index only effects elements that you have a position value other than static (the default)
层级关系比较:
(1)对于同级元素,默认情况下(position:static) 文档流后面的元素会覆盖前面的
(2)对于同级元素,position不为static且z-index存在的情况下z-index大的会覆盖z-index小的元素
(3)IE6/7下position不为static,且z-index不存在时z-index为0,除此之外的浏览器z-index为auto
(4)z-index为auto的元素不参与层级关系比较
定位规则:
如果将position设为relative, absolute或者fixed, 这样的节点会覆盖无position属性或者position为static的节点
a会被b覆盖,而a-1在b之上【没有z-index属性干扰时】
<div id="a">
      <div id="a-1" style="position:relative;">A-1</div>
</div>
<div id="b">
</div>
参与规则:
z-index仅在position为relative, fixed, absolute的时候生效
默认值规则:
如果所有节点都定义了position:relative. z-index为0与没有定义z-index在同一层级内没有高低之分,但z-index>=1的节点会遮盖没有定义z-index的节点。z-index: -1的节点被没有定义z-index的节点覆盖
从父规则:
<div id="a" style="position:relative;z-index:1;">
    <div id="a-1">A-1</div>
</div>

<div id="b" style="position:relative;z-index:0;">
    <div id="b-1">B-1</div>
</div>

如果A,B节点都定义了position:relative, A节点的z-index比B节点大,那么A的子节点必定覆盖在B的子节点前面

如果所有节点都定义了position:relative, A节点的z-index和B节点一样大,因为顺序规则(back-to-front),B节点覆盖A节点,就算A的子节点值比B得子节点大,B的子节点还是会覆盖在A的子节点前面

stacking contexts:
每个box都归属于一个stacking context,是元素在z轴方向上定位的参考。根元素形成root stacking context, 其他stacking context由定位元素设置z-index为非auto时产生。
在同一stacking context中, stack level大得显示在上,stack level小的在下
不同stacking context中,元素显示顺序以父级的stacking context的stack level来决定显示的先后情况。

6. sprites
a means of combining multiple images into a single image file for use on a website, to help with performance.

Reason: several images are loaded with a single HTTP request. Browsers limit the number of concurrent requests a  site can make and HTTP requests require a bit of handshaking.

Use:
you can use the same bkg-image on several CSS classes and set the background position and dimensions of the individual classes to display a single portion of the sprite.

#home {
    width: 43px;
    height: 44px;
    background: url(img_navsprites.gif) 0 0;
}

#next {
    width: 43px;
    height: 44px;
    background: url(img_navsprites.gif) -91px 0;
}

</head>
<body>
<img id="home" src="img_trans.gif"><br><br>
<img id="next" src="img_trans.gif">
</body>

7. hiding contents
visibility: hidden;
display: none;   -->inaccessible by screen readers
.hide{
      position: absolute !important;
      top: -9999px !important;
      left: -9999px !important;
}  ---->screen reader accessible
height:0; width:0; overflow:hidden; -->ignored by screen readers


8. css hack
CSS hack的目的就是使你的CSS代码兼容不同的浏览器

9. css selector
元素选择器: p { font-size: 14px;}

复合选择器: 第一个元素必须是元素选择器,第二个必须是类选择器和ID选择器,二者之间无空格   p.special { color: #red;}

后代选择器: 两个选择器间有空格 div h2{ color:#FF0; padding:10px; }

id选择器: #main{font-size: 18px;}

类选择器: .special{font-size: 5px;}

伪类,表示表单元素或者链接的状态
a:link 链接尚未被访问
a:hover,
a:visited,
a:active

通用选择器(不建议使用,加载慢): 对页面上所有的元素应用样式。例如删除每个元素上默认的浏览器内边距和外边距。
* {margin:0; padding:0;}

高级选择器:
(1)子选择器: 选择元素的直接后代
#nav>li {padding-left:20px;}
(2)相邻同胞选择器: select and style every <p> element that are placed immediately after <div> element
div+p {
      background-color:yellow;
}
(3)属性选择器, 根据某个元素的属性是否存在或者属性值来寻找元素
style <a> elements with a target="_blank"
a[target=_blank]{
      background-color:yellow;
}

10 selector优先级
(1)样式的优先级(multiple styles)
一般说来 External style sheet < Internal style Sheet < Inline style
但是如果外部样式放在内部样式后面,外部样式将覆盖内部样式
<head>
<style type="text/css">
h3{color:green;}
</style>
<link rel="stylesheet" type="text/css" href=""/>
</head>
(2)The priority level of the selector is decided in point of combination of selectors.
style attribute = a
number of ID attributes in the selector = b
number of other attributes(classes) and pseudo-classes in the selector = c
number of element names and pseudo-elements in the selectors = d
<body>
  <article>
    <p>This is <span id="red">paragraph</span>.</p>
  </article>
</body>
 
article p span{
   color: blue;
 }
 #red{
   color: red;
 } 

article p span - "a=0, b=0, c=0, d=3 (0003)"
#red - "a=0, b=1, c=0, d=0 (0100)" --->优先级更高



11. box model
http://www.barelyfitz.com/screencast/html-training/css/positioning/








box model includes: content, padding, margin,border
box model has two kinds: one is IE box-mode[content includes border and padding] and the other is W3C box-mode

box_size_width: width+padding-left+padding-right+border-left+border-right
box_size_height: height+padding-top+padding-bottom+border-top+border-bottom




无position属性(position:static)或者position:relative的情况下,块的宽幅是眼神自动填充满它的父元素的宽度区域







当设置浮动float元素和绝对定位元素,他们则会收缩以至包裹紧贴内容













使用相对定位的盒子不仅对父块没有任何影响,对兄弟盒子也没有任何影响

绝对定位,盒子的位置以它的包含框为基准进行偏移。绝对定位的盒子从标准流中脱离。这意味着它们对其后的兄弟盒子的定位没有影响,其它的盒子就好像这个盒子不存在一样。

绝对定位的特殊性质
#outerBox {
    width: 200px;
    height: 100px;
    margin: 10px auto;
    background: silver;
}
#innerBox {
    position: absolute;
    top: 70px;
    width: 100px;
    height: 50px;
    background: orange;
}

<div id="outerBox">
      <div id="innerBox"></div>
</div> 
outerBox没有设置position属性,内部设置了position:absolute, 此时它的基准是浏览器窗口,但是由于没有设置水平偏移,因此水平方向仍然会保持原来位置,左侧与外层盒子左侧对齐。如果设置了绝对定位,而没有设置偏移属性,那么它仍然保持原有的位置。

If the parent element contained nothing but floated elements, the height of it would literally collapse to nothing. 这是因为一个div的范围是由它里面的标准流内容决定的,与里面的浮动内容无关。
 
12. Techniques for clearing floats
(1) If you are in a situation where you always know what the succeeding element is going to be, you can apply the clear:both;
(2) The empty div
 <div style="clear:both;"></div>
e.g.
<div class="father">
    <div class="son1">Box-1<br>Box-1</div>
    <div class="son2">Box-2</div>
    <div class="son3">
        Box-3<br>
        Box-3<br>
        Box-3<br>
        Box-3
    </div>
    <div style="clear:both;border:0;padding:0;margin:0;"></div>
</div>


(3)The overflow method
setting the overflow CSS property on a parent element. If this property is set to auto or hidden on the parent element, the parent will expand to contain the floats. 
另外在 IE6 中还需要触发 hasLayout ,例如为父元素设置容器宽高或设置 zoom:1。
在添加overflow属性后,浮动元素又回到了容器层,把容器高度撑起,达到了清理浮动的效果。 
 .news {
  background-color: gray;
  border: solid 1px black;
  overflow: hidden;
  *zoom: 1;
  }

.news img {https://www.blogger.com/blogger.g?blogID=4622736954821619358#editor/target=post;postID=8538948416929111;onPublishedMenu=posts;onClosedMenu=posts;postNum=0;src=postname
  float: left;
  }

.news p {
  float: right;
  }

<div class="news">
<img src="news-pic.jpg" />
<p>some text</p>
</div>

 
(4)The easy clearing method
use CSS pseudo selector (:after) to clear floats. applay an additional class like clearfix to the parent.
//实现元素末尾添加一个看不见的块元素,
//需要注意的是为了IE6和IE7浏览器,要给clearfix这个class添加一条zoom:1;触发haslayout。 
.clearfix {
  /* 触发 hasLayout */ 
  zoom: 1; 
  }

.clearfix:after{
content:"."
visibility:hidden;
display:block;
height:0;
clear:both;
} 

13. box-sizing property

For display purpose, every element in the page is considered a box. The box model refers to the specification of the box attributes such as the width, padding, border and margin.
 
The box-sizing CSS property is used to alter the default CSS box model used to calculate widths and heights of elements. It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification.

You can change the box model by setting the box-sizing property. Some values are: content-box (default), padding-box, and border-box)

Content-box: width & height includes content but not padding/border/margin

Padding-box: include up to padding

Border-box: include up to border, but not margin
  [reason for setting it to border-box https://css-tricks.com/international-box-sizing-awareness-day/]



14. media queries
screen keyword apply to the device's physical screen or the browser's viewport
//可视区域小于600px的时候被应用
@media screen and (max-width: 600px){
.class{
background: #ccc;
}
} 
如果想链接到一个单独的样式表,可以:
<link rel="stylesheet" media="screen and (max-width: 600px)" href="small.css" /> 

//可视区域大于900px的时候被显示
@media screen and (min-width:900px){
.class{
background: #666;
}
} 
//可视区域大于600小于900的时候被显示
@media screen and (min-width:600px) and (max-width:900px){
.class{
background: #333;
}
} 
//在 max-device-width 是 480px 的设备上触发 [max-width: 可视区分辨率 max-device-width :设备实际分辨率]
@media screen and (max-device-width: 480px){
.class{

}
} 

@media (max-width:700px){}
@media (max-width:700px) and (orientation:landscape){}

//for ipad-->portrait or landscapse
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">


15. List
li{
list-style-type: none,
line-height: 150%, -->values include: normal, number(line-height:3), length(fixed line height in px, pt, cm,etc), %, initial(sets this property to its default value), inherit
list-style-image: url(../img/**.gif); -->use the image as the bullets
}

16. link external file
<link href="myCSSfile.css" rel="stylesheet" type="text/css">
or
<style type="text/css" media="all">
@import "myCSSfile.css";//in order to support really old browsers
</style>

**cascading effect: CSS code on the page itself will override the CSS code in a separate linked file. and CSS declared "inline" one the tag itself would override all other CSS.

17. CSS selector
*{
//will target every single element on the page
}
#container *{
 //target every single element that is a child of the #container
}
#idName{
}
.className{
}
li a{
//descendant selector 后代选择器
}
div#container >ul{
//will only select direct children
}
a {
//target elements
}
a:link{
// pseudo-class to target according to the states
}
a:visited{
}
ul+p{
//adjacent selector
//only the first paragraph after each ul will have red text.
}
ul~p{
//will select any p elements as long as they follow a ul
}
a[title]{
//attribute selector, will only select the anchor tags that have a title attribute
}
可继承的样式: font-size, font-family, color, UI LI DL DD DT
不可继承的样式: border padding margin width height
优先级为: !important > id > class > tag

18. center div/ center floating element
div{
width: 200px;
margin: 0 auto;
}

19 CSS3 new features
border-radius, border-shadow, text-shadow, gradient, transform
transform: rotate(9deg) scale(0.85,0.9), translate(0px, -30px), skew(-9deg,0deg);//旋转,缩放,平移,倾斜
rgba

20.因为浏览器的兼容问题,不同浏览器对有些标签的默认值是不同的,因此一般会先进行初始化

21.手动写动画,最小时间间隔
多数显示器默认频率60hz,即1s60次,理论最小间隔为1/60*1000ms = 16.7ms

22. units
px gives fine grained control and maintains alignment because 1 px or multiple of 1 px is guaranteed to look sharp. px is not cascade, this means if parent font-size is 20px and child 16px. child would be 16px.
em maintains relative size.1em is equal to the current font-size of the element or the browser default. if u sent font-size to 16px then 1em = 16px. The common practice is to set default body font-size to 62.5% (equal to 10px). em is cascade
% sets font-size relative to the font size of the body.
pt(points) are traditionally used in print. 1pt = 1/72 inch and it is fixed-size unit.

23. specificity
specificity is a process of determining which css rule will be applied to an element. It actually determines which rules will take precedence.

24. transition
.transition{
transition: [transition-property] [transition-duration] [transition-timing function] [transition-delay];
}
transition-timing function->how you want to transit (linear, ease, ease-in, ease-out, cubic-bezier)
e.g.
.transition{
transition: width 2s linear 0.5s, background-color 2s linear 0.5s;
}

No comments:

Post a Comment