Archive for the ‘CSS’ category

CSS position定位

July 29th, 2010

css定位主要通过position属性和float属性

position属性有四个值,分别为static,relative,absolute,fixed

static是默认值,即按元素既定的规则解析排列,因为position不是继承的,所以也很少会有情况用到

relative,相对定位很容易造成误解,首先相对定位是作用元素本身的,但是如果你只设了position:relative但是没有设置left,right,top,bottom的值,那么元素本身的位置是不会变化的。但是设置relative的元素会影响到其子元素的定位,尤其当子元素使用absolute定位时。另外设置了relatvie属性的元素可以使用z-index属性,而且任何relative元素都会在static元素之上,无论是否设置了z-index。

absolute,绝对定位的元素不会参与其他元素的flow,也不会影响他们的定位,可以把绝对定位的元素想象成独立的,

fixed,固定定位,和absolute类似,但是它只相对浏览器窗口(viewport)定位,不会受父元素的局限。而且fixed的元素不会随着窗口页面滚动而滚动。但是fixed元素IE6不支持,所以需要hacker

#top{
position:fixed;
_position:absolute;
bottom:0;
right:20px;
_bottom:auto;
_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
}

通过绝对定位来实现,但是这样在拖动浏览器的时候会出现闪动的现象,解决的方法是,添加

*html{
background-image:url(about:blank);
background-attachment:fixed;
}

这样问题就解决了。

强烈推荐读一下这个,http://www.barelyfitz.com/screencast/html-training/css/positioning/

CSS水平居中

July 29th, 2010

首先,请不要再使用<center>和align来居中页面了,这是Stone Age人们的用法。

其次,请区分元素是block-level还是inline的,具体可以通过wiki查看,list of inline-level elements 和 list of block-level elements.block-level元素不会受text-align属性控制,同时inline元素可以通过display属性控制显示的类别,比如你指定img { display: block; },text-align也不会生效。

水平居中inline内容

如前所属,inline元素只要使用{ text-align: center; }就可以实现,而且该属性是加在父元素上,影响子元素的布局

但需要注意两点,1是IE的Bug,另一个是text-align是继承的。

水平居中block-level元素

使用margin属性,设置{ margin:auto; width:auto}

http://haslayout.net/css-tuts/Horizontal-Centering

CSS Bug Table

July 28th, 2010

http://haslayout.net/css/

问题 浏览器 DEMO 解决方法
Hacking Rules:property:all-ie\9; property:gte-ie8\0;*property:lte-ie7; +property:ie7; _property:ie6;
1 input[button | submit] 不能用 margin:0 auto; 居中 IE8 bugfixed 为input添加width
2 body{overflow:hidden;}没有去掉滚动条 IE6/7 bugfixed 设置html{overflow:hidden;}
3 hasLayout的标签拥有高度 IE6/7 bugfixed *height:0;
_overflow:hidden;
4 form>[hasLayout]元素有margin-left时,子元素中的[input | textarea] 出现2×margin-left IE6/7 bugfixed form > [hasLayout 元素]{margin-left:宽度;}
form div{*margin-left:宽度÷2;}
5 当border-width有1条<边3条时被设置成dotted时,1px的边dotted显示成dashed IE7 bugfixed 不在同一个元素上使用不同宽度的 dotted
6 当子元素有position:relative的时候,父元素设置overflow:[hidden|auto]相当于给子元素设置了position:visible; IE6/7 bugfixed 给父元素设置position:relative;
7 :hover伪类不能改变有position:absolute的子级元素的left/top值 IE7 bugfixed 把top/left的值设置成除0%外的所有百分值;或添加一个margin-[所有方向]除0外的所有值,包括0%
8 :focus + selector {} 选择器失效 IE8 bugfixed 在失效选择器后面添加一个空选择器, :focus{}
9 列表中混乱的浮动:在list中浮动图片时,图片出现溢出正常位置;或没有list-style IE8 bugfixed 用背景图片替换list-style
10 th 不会自动继承上级元素的 text-align IE8 bugfixed 给th添加text-align:inherit;
11 样式(包括link/style/@import(link)) 最多允许个为是:32 IE6-8 ─ 常识 99.99%的情况下,不会遇到
12 :hover 时若background-color为#fff, 失效 IE7 bugfixed 把background-color改成background。或者,非#fff || #ffffff
13 忽略’>’后有注释的选择器:selector> /**/ selector{} IE7 bugfixed 这个bug是官方DEMO有误
14 * html IE6 ─ HACK 只对IE6有效
15 PNG图片中的颜色和背景颜色的值相同,但显示不同 IE6-7 bugfixed 利用 pngcrush 去除图片中的 Gamma profiles
16 margin:0 auto; 不能让block元素水平居中 IE6-8 bugfixed 给block元素添加一个width
17 使用伪类 :first-line | :first-letter, 属性的值中出现!important 会使属性失效 IE8 bugfixed !important is evil, don’t use it anymore
18 :first-letter 失效 IE6 bugfixed 把 :first-letter 移到离{}最近的地方,如 h1, p:first-letter{},而非 p:first-letter h1{}
19 Position:absolute元素中,a display:block, 在非:hover时只有文本可点击 IE6/7 bugfixed 给a添加background, 如果背景透明,使用background:url(‘任何页面中已经缓存的文件链接’),不推荐background:url(#)[官方的解决方法],因为会增加一下HTTP请求
20 float列表元素不水平对齐:li不设置float,a设置display:block;float:[方向],li不水平对齐 IE6/7 bugfixed 给li设置display:inline 或 float:[方向]
21 dt, dd, li 背景失效 IE6 bugfixed dt, dd, li{position:relative;}
22 <noscript />元素的样式在启用javascript的情况下显示了样式 IE6-8 bugfixed 利用js给<noscript />添加display:none;
23 使用filter处理的透明背景图片的透明部分不可点 IE6-8 bugfixed 把background:none变成background:url(‘链接’),链接到本身和图片之外的任何文件
24 li内元素偏离 baseline 向下拉 IE8 bugfixed 给li设置display:inline 或 float:[方向]
25 列表中li的list-style不显示 IE6/7 bugfixed 给li添加margin-left,留空间来显示(不要加在ul上)
26 图片不能垂直居中 IE6/7 bug/fixed 添加一个空标签,并赋给”Layout”, 比如display:inline-block;
27 不能自定义指针样式 IE6-8 bugfixed 给指针文件设置绝对路径
28 背景溢出,拖动滚动条后显示正常 IE6 bugfixed 给父元素添加overflow:hidden防止溢出,并赋予hasLayout,如果添加_zoom:1;
29 高度超过height定义的高 IE6 bug/fixed 添加_overflow:hidden;(推荐)或者_font-size:0;
30 宽度超过width定义的宽 IE6 bug/fixed 添加_overflow:hidden;
31 双倍边距 IE6 ─ 常识 添加display:inline到float元素中
32 margin负值隐藏:hasLayout的父元素内的非hasLayout元素,使用负边距时,超出父元素部分不可见 IE6/7 bug/fixed 去掉父元素的hasLayout;或者赋hasLayout给子元素,并添加position:relative;
33 给两个浮动元素的某中一个的文字设定为斜体,另一个元素下拉在有斜体文字元素的下面 IE6 bug/fixed 给有斜体文字的元素添加overflow:hidden;
35 3px 间隔:在float元素后的元素,会有3px间隔 IE6 bug/fixed 因为是确切的3px,所以,用“暴力破解”吧,比如_margin-left:-3px;
35 text-align 影响块级元素 IE6/7 bug/fixed 整理你的float;或者分开设置text-align

设计是实现html CSS按钮

July 27th, 2010

按钮和超链接

按钮和超链接应该区别对待,很多时候超链接会做成按钮的样式。但是超链接应该主要用于页面间的导航,而按钮是触发某些动作,比如提交表单

基本样式

按钮的基本样式是添加背景图片,边框和间距(padding),分别使用a,button和input产生的按钮

   Sample button
  

.button {
  padding:5px;
  background-color: #dcdcdc;
  border: 1px solid #666;
  color:#000;
  text-decoration:none;
}

Sample button

按钮状态

一般按钮有normal,hover,active三种状态,如果考虑到键盘导航,那么还有focus状态,即当用键盘tab将焦点移动到按钮上时应该样式的变化

Textarea诀窍

July 18th, 2010

1. 使用背景图片做提示

textarea {
  background: url(images/benice.png) center center no-repeat; /* This ruins default border */
  border: 1px solid #888;
}

当输入内容时提示背景消失,可以使用javascript实现,

$('textarea')
  .focus(function() { $(this).css("background", "none") })
  .blur(function() { if ($(this)[0].value == '') { $(this).css("background", "url(images/benice.png) center center no-repeat") } });

2. HTML5 placeholder text
HTML5引入了一个新的属性,placeholder,可以实现相同的效果

3. Placeholder text, HTML5 with jQuery fallback
我们可以判断浏览器是否支持新的属性,如下

function elementSupportsAttribute(element, attribute) {
  var test = document.createElement(element);
  if (attribute in test) {
    return true;
  } else {
    return false;
  }
};

使用JS实现如果有palcehodler属性则用html5实现提示,否则用模拟的方式

if (!elementSupportsAttribute('textarea', 'placeholder')) {
  // Fallback for browsers that don't support HTML5 placeholder attribute
  $("#example-three")
    .data("originalText", $("#example-three").text())
    .css("color", "#999")
    // .attr("val", $("#example-three").text())
    .focus(function() {
        var $el = $(this);
        if ($el[0].value == $el.data("originalText")) {
          $el[0].value = "";
        }
    })
    .blur(function() {
      var $el = $(this);
      // console.log($el.text(), "value: " + $el.attr("val"));
      if ($el[0].value == "") {
          $el[0].value = $el.data("originalText");
      }
    });
} else {
  // Browser does support HTML5 placeholder attribute, so use it.
  $("#example-three")
    .attr("placeholder", $("#example-three").text())
    .text("");
}

4. Webkit浏览器的属性
所有Webkit浏览器,firefox,opera,chrome都有蓝色的边框,可以去掉,不过建议不要去掉

textarea {
  outline: none;
}

5. 删除缩放处理器
所有Webkit浏览器,chrome有缩放大小功能,可以去掉

textarea {
 resize: none;
}

6. 添加缩放
使用resizeable interaction实现

7. 自动增长

使用auto resizing textareas实现

8. 取消自动换行

一般元素使用 #whatever { white-space: nowrap; }就可以了,但是textarea不行,


9. 删除IE上的自动滚动条

IE浏览器上的textarea默认有一个灰色的滚动条,可以删除

textarea {
  overflow: auto;
}

原文地址:http://css-tricks.com/textarea-tricks/