Posts Tagged: css


16
五 10

Data URI&MHTML: 用还是不用?

YAHOO!的性能专家Stoyan Stefanov先后发布了

NCZ也有

引起了我们对data uri的关注,国内的也有一些不错的文章,比如:

工作太过饱和一直未深究,而前不久YDN的Performance on Yahoo! Search for Earth Day再次提到,更是撩起了我内心沉睡的欲望…

data URI的优缺点

data uri的主要优点是减少了http请求数,Best Practices for Speeding Up Your Web Site

Inline images use the data: URL scheme to embed the image data in the actual page. This can increase the size of your HTML document. Combining inline images into your (cached) stylesheets is a way to reduce HTTP requests and avoid increasing the size of your pages. Inline images are not yet supported across all major browsers.

还有就是调用起来比css sprite更加灵活,缺点是增加了客户端的资源消耗。

浏览器支持情况

Data URIs explained

Most modern browsers support data URIs:

  • Firefox 2+
  • Opera 7.2+ – data URIs must not be longer than 4100 characters
  • Chrome (all versions)
  • Safari (all versions)
  • Internet Explorer 8+ – data URIs must be smaller than 32k

不能太迷信权威,NET.2971 – The RFC 2397 data URL – Part 1这里说的比较靠谱:

2.3 – Maximum size

The size of data URIs is limited in some browsers. IE8 for example has a limit of 32,768 characters [4].

Filename Size[px] Size[kB] Size[chars]
Wallpaper_1600_1200.jpg 1600×1200 309 699052 – 9199 =

A test was done with IE 8 (8.0.6001.18702), Firefox 3.5.3 and Opera 10.0. A large wallpaper, 1600×1200 px, 309 kB was converted to Base64, resulting in almost 690k characters. This inline image was added to a HTML page and opened in the browser.

IE8 only displays the first part of the image (probably 32,768 characters), Firefox and Opera are showing the complete image.

When searching for information about maximum data size, Opera is often mentioned with a limit of 4100 characters, but this is apparently for older versions.

It is often said that inline images should be limited to small images, for example icons. In practice, however, often much larger images are used. Examples are the Google news pages with 80×80 px standard images (about 3000 characters), some pages contain more than 20 of them. We also see that all modern browsers support large data URLs which is a deviation from earlier policies.

MSDN上的data Protocol

Data URIs cannot be larger than 32,768 characters.

还有段视频Internet Explorer 8 Data URI(不想装淫光的下面有下载链接)

可以简单理解为我们只要把data URI的字符数控制在32768字符内就可以照顾到所有A级浏览器了。

IE6&7使用MHTML并注意MHTML在ie7/vista bug 解决方案就好了,JavaScript组件打包模式里说的比较明确:

MHTML在ie7+/vista缺少结束分割符无法显示,win03sp2缺少Content-Type会有安全提示,原因都MIME不标准,不是所有的东西都可以省。

下面要开始测试了,demo在此,是在WIN7 IE8的IE7模式进行的测试,结合httpwatchfiddler,虽然不是测试所有浏览器但可以反映出一些共同的问题。

PS:demo是在tommyfan的基础上改的,节省了我不少时间,感谢囯+先;99css所在的服务器大猫给开了gzip,有兴趣的朋友可以到 http://miao.in去看看:)

下载速度对比

正常网速

可见,base64稍慢

fiddler模拟低速


base64慢了不少

渲染时间对比

几毫秒可以忽略,不过还是base64偏慢,data:URI + mhtml troubles and solutions中有云:

data:URI takes much more time to be processed (in comparison to CSS Sprites or raw images) – about 10x more (for Firefox, for IE – 2x, for Opera/WebKit almost the same time). So in case of large images’ square (starting from 400×400, or 100k square dots), so you must be careful ibefore you completely convert all graphics to base64.

感官体验

并不是下载速度越快越好,还要考虑访问者的主观感受,比如逐步呈现(Progressiv Rencering),将css放顶部的原因就在于此,测试demo,虽然放底部速度更快,但放顶部可以逐步呈现,详见《High Performance Web Sites: Essential Knowledge for Front-End Engineers》(中译名:《高性能网站建设指南》)第五章的分析。

sprite的demo中页面是逐步呈现的,而base64在css下载完成前一直处于“白屏”状态

Data URIs for CSS Images: More Tests, More Questions走了点弯路,想把data uri的css放底部,可能他没看过《High Performance Web Sites》吧:) (Rob, this is only a joke~)

文件尺寸对比

原始的31个icon加起来16.5KB

sprite的css 1.4KB + 图片4.54KB = 5.94KB

data URI的css 51.2KB, gzip后16.5KB

对于大型站点来说流量 = ¥,这点sprite领先

CPU&内存对比

通过观察Windows任务管理器,base64比sprite多1%左右的CPU,多1M左右的内存

data URI使用建议

  • 能使用sprite的地方尽量使用sprite
  • 不方便使用sprite的时候才使用data uri,比如
    • 宽高不固定且又又要求background-position:center bottom
    • repeat/repeat-x/repeat-y的图片
    • png-24图片,IE6用α滤镜时图片请求不到会导致浏览器一直处于卡死状态,详见Image Optimization, Part 5: AlphaImageLoader,用mhtml代替则可避免此问题(先下载css,再从缓存调用自身)

以上结果跟demo本身的特点有很大关系,希望能看到各位的更多测试结果

除了文中提到的文章,下面的也不错:

另外,我写了个生成器,改天详细介绍:)


28
三 10

图文混排一粒

过需求时一不留神,很爽快的接了个如下图的效果:

条件及要求:

  1. 图片宽度及高度不固定
  2. 分享给好友贴底
  3. 照片描述及分享给好友左侧距图片10px
  4. 照片描述由后台控制输出字数

等做起来发现还挺折磨人,本想下周甩他们一句做不了完事,不过考虑 200,000,000+ P-people  no jobs… 马上变得蛋定,上代码:

<!DOCTYPE html>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>图文混排</title>
<link href="http://yui.yahooapis.com/3.0.0/build/cssreset/reset-min.css" rel="stylesheet">
<style>
.feed{margin:10px;width:500px;}
.feed-img{float:left;position:relative;#zoom:1;margin-right:10px;_margin-right:7px;}
.feed-img img{vertical-align:top;}
.feed-img .act{position:absolute;bottom:0;left:100%;margin-left:10px;white-space:nowrap;}
</style>
</head>

<body>

<div class="feed">
	<div class="feed-img">
		<img alt="犀利哥" height="430" src="http://img1.gtimg.com/cq/pics/29734/29734288.jpg" width="300">
		<p class="act"><a href="##">分享给网友乡民</a></p>
	</div>
	<div class="feed-text">
		<p>cool~</p>
	</div>
</div>

</body>

</html>

——————– 补充提醒分割线 ——————–
评论前确保您已看过减小 HTML 文件


2
三 10

宽度自适应的输入框

YUI中有一种布局思想:栅格决定宽度,内容决定高度,今天讨论的是自适应宽度的输入框,先上代码:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>宽度自适应输入框</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/3.0.0/build/cssreset/reset-min.css">
<style>
.col{padding:10px;width:500px;background-color:yellow;}
.fluid-input{display:inline-block;width:100%;overflow:hidden;}
.fluid-input-inner{display:block;padding-right:10px;#zoom:1;}
.fluid-input .text, .fluid-input textarea{border:2px #ccc solid;padding:3px;width:100%;}
.fluid-input textarea{height:300px;}
</style>
</head>

<body>

<div class="col">
    <b class="fluid-input"><b class="fluid-input-inner"><input class="text" type="text"></b></b>
    <b class="fluid-input"><b class="fluid-input-inner"><textarea></textarea></b></b>
</div>

</body>

</html>

简单说一下原理:

为了有较好的体验,input[type='text']及textarea一般会有border值及padding值(鄙视某些padding设为0滴淫儿),利用如下公式:

.fluid-input-inner{padding-right:输入框左边框 + 输入框右边框 + 输入框左padding + 输入框右padding}

让输入框继承fluid-input-inner的宽度即可实现自适应;而IE6有个宽度超出自动撑开的bug,故再多套一层fluid-input,宽度100%,溢出隐藏即可。

col宽度随意,在宽度自适应布局中默认为auto,这里方便演示定了宽度。

这里由内而外解说,不过命名是由外而内的。

更进一步

focus时border变为4px咋整?

问的好(其实是偶自己在问),输入框focus后增加class=”focus”,即变为:

<textarea class="focus"></textarea>

CSS:

.focus{position:relative;left:-2px;border-width:4px !important;}

———————— 乡民问答分隔线 ————————-

美女@jeannewoo反馈了个bug,IE6下textarea输入文字的时候就自己撑开了(昨晚下班路上@ivane也跟我说过,不过我没当回事,嘿~),我试了一下果真如此,解决方法很简单:触发fluid-input-inner的hasLayout即可。

美女又问为什么用s,我想了一下换成了b,想知道原因么?看这个 http://www.youtube.com/watch?v=vjLdp5tJM0c


5
二 10

IE6/7下划线对不齐bug

这篇帖子《关于IE下英文字体跟中文字体不能对齐的根源以及解决办法》说的已经很详细了,不过不止中英文混合才会出现这个bug,比如《Internet Explorer: Broken Underlines》。

解决方法一般有以下几种:

  1. 对下划线元素使用宋体,缺点是宋体的英文数字比较难看,我个人也比较鄙视这种做法
  2. 触发下划线元素的hasLayout,缺点是inline元素会变成inline-block元素,折行时会整块掉下来
  3. 使下划线元素浮动,缺点是inline元素变成block元素,而且还要清除浮动
  4. 去除下划线元素相邻元素(相邻元素的子元素或父元素等等)的vertical-align(或重写为baseline),垂直居中用position:relative结合top来实现

在使用inline-block元素做sprite容器时经常会遇到这个问题,2、4是比较好的方案,记得要分清楚使用场合哦~

当然IE6下还会出现行高失效bug,old9给出了一种方法《A fix to the IE6 line-height/replaced element bug》,不过在某些追求精确到像素的情况下还是用PIE的方法《Microsoft Internet Explorer 6: Line-height / Replaced Element Bug》比较好。

另外,使用vertical-align:middle来垂直居中在各浏览器中差异也比较大,使用时需谨慎,而用数值则好多了,推荐阅读old9的《CSSTDG读书笔记七


19
十二 09

用display:inline-block做产品列表

由于D2的关系看到这篇文章《[纯技]产品列表到底应该怎么做?》,评论发不成功,又恰逢最近频繁遇到类似的项目,总结如下:

传统的实现方式是用float来实现,换一种思路用display:inline-block,比如这篇《Cross-Browser Inline-Block》(中文翻译)。在Firefox/Safari/Chrome/IE8中会遇到《说说display:inline-block》提到的第一个空隙问题,不过现在已经有了解决方法(详见《Firefox Bug: inline/inline-block的间隙》的评论并略作改进):

ul{letter-spacing:-.25em}
li{letter-spacing:normal;}

同时我也尝试了《Layout of repeating blocks》提到的word-spacing,不过仅Firefox/IE8有效,而在Chrome/Safari中无效

另外要注意的是-.25em是字体设置为Arial时的情况,为其他字体时会有些不同,下面仅列出了常用的几个

常用字体与letter-spacing的关系
宋体/Verdana -.5em
Arial -.25em
Tahoma -.333em

于是display:inline-block加上vertical-align就OK鸟

ul{letter-spacing:-.25em}
li{display:inline-block;#display:inline;#zoom:1;letter-spacing:normal;vertical-align:top;}

如果li中只有img,vertical-align:middle可以实现未知高度图片列表垂直居中,比如Picasa图片列表的效果(单个图片垂直居中请看怿飞的《图片垂直居中的使用技巧,不过个人认为这个通常只会出现在面试题中:P


14
十一 09

自适应圆角

前言

有时候我们不得不面对圆角,也很傻很天真的认为利用 CSS3 的新特性对浏览器分级支持是最好解决方案,但现实≠理想…

需求

  1. 3px 圆角
  2. 宽度自适应(随着文字字数扩展宽度并自动换行)
  3. 换肤
  4. 不使用图片(对可维护性/性能均有影响)

困惑

经典的解决方案看起来像这样:

<!DOCTYPE html>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>传统方案及bug</title>
<style>
body{margin:100px;background-color:red;}
div, p, b{margin:0;padding:0;}
b{display:block;}
.rc{display:inline-block;#display:inline;#zoom:1;}
/* float 效果相同
.rc{float:left;} */
.rc1, .rc2, .rc .bd{border-style:solid;border-color:black;background-color:yellow;}
.rc1, .rc2{height:1px;overflow:hidden;}
.rc2, .rc .bd{border-width:0 1px;}
.rc1{margin:0 2px;height:0;border-width:1px 0 0;}
.rc2{margin:0 1px;}
.rc .bd{padding:0 6px;line-height:1.5;}
</style>
</head>

<body>

<div class="rc">
    <b class="top"><b class="rc1"></b><b class="rc2"></b></b>
    <div class="bd">
        <p>FML!!!</p>
    </div>
    <b class="bottom"><b class="rc2"></b><b class="rc1"></b></b>
</div>

</body>

</html>

使 rc float: left 或 display: inline-block 或许能满足需求,不过 IE 6&7 出现 bug:IE6中依然显示为dispaly:block,而IE7中top 和 bottom缩成一坨,不肯扩展开来,而在 rc1/rc2/rc3 中插入文字

<b class="rc1">XXX</b>

则只能扩展到文字XXX的宽度,无法与 bd 对齐!
如果你能搞定,麻烦留言告诉我一下 :)

解决

试了几个方法均不通,也搜索不到这个 bug,这时想到了 Google 系产品的 1px 圆角按钮:

这是前 Google 视觉设计师 Doug Bowman 的功劳,后转会到Twitter(AD一下:@ytzong),Doug Bowman 为此写过一篇博文《Recreating the buttondemo在此。不过demo 也仅仅是 demo,产品中使用的还是某对天才工程师夫妇的神作,他们有足够的时间来做这项工作:

The magical inline-block solved everything, except in IE. That’s where the genius of Google engineers came in. They knew how to get tricks working in all browsers, and this technique interested a couple of them enough that they dedicated the time to make it work.

打开 Gmail,用 firebug 艹艹看了一下,心里暗骂一句:天才就是天才!
写了个简单的 demo(为了演示方便这里采用了盒谐的颜色):

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>自适应圆角</title>
<style>
body{margin:100px;background-color:red;}
div, p{margin:0;padding:0;}
.div1, .div2, .div3{display:inline-block;#display:inline;#zoom:1;position:relative;border-style:solid;border-color:black;}
.div1{border-width:1px;}
.div2, .div3{#left:-2px;border-width:0 1px;background-color:yellow;}
.div2{margin:0 -2px;}
.div3{margin:1px -2px;padding:0 6px;line-height:1.5;}
.arrow1, .arrow2{position:absolute;top:7px;width:0;height:0;overflow:hidden;border-top:6px transparent dotted;border-bottom:6px transparent dotted;}
.arrow1{left:-9px;border-right:6px black solid;}
.arrow2{left:-8px;border-right:6px yellow solid;}
</style>
</head>

<body>

<div class="div1">
    <div class="div2">
        <div class="div3">
            <p>FML!!!</p>
        </div>
    </div>
    <div class="arrow1"></div>
    <div class="arrow2"></div>
</div>

</body>

</html>

效果如下:

不仅满足了需求,代码量及结构嵌套也很少。

代码说明

  1. div1 实现上下两条边,div2 实现 2px 圆角处的 4个点(一般人只有3点),div3 实现左右两条边,div1 设置左右边框的原因是避免IE 6&7 中的一个盒模型的小 bug,有兴趣研究的话可去掉左右边框看下效果
  2. arrow1 实现小三角的边框,arrow2 实现小三角的背景,此处利用了 border 来模拟小三角,这其中会遇到IE 6 的边框不透明 bug。另外,你也可以看看oocss的写法
  3. 宽度自适应利用的是 display: inline-block,注意IE 6&7 中的处理方式
  4. #left:-2px 之前的 # 为 IE 6&7 hack,多写为*,这里用#一是表明偶的与众不同,二是微软的 Expression Web格式化 CSS 的时候若{}中出现*则会把代码搞乱,此处 IE 6&7 中出现了 margin-left 负值无效的 bug ,通过 position:relative 加 left: -2px 实现

可以看出:IE 6&7 bug 众多的特点在此例中表现的淋漓尽致!

最后的话

阮一峰老师也写过一篇《制作Gmail式按钮》,配合强大的 jquery 来实现按钮各种状态,最后的总结也很精彩,修改一下下:

虽然这种按钮(圆角)的视觉效果比较理想,有很多设计上的优点,但是局限性也很大。一方面,它需要大量的冗余代码,与语义网的原则相违背…

完美方案还是利用浏览器的自身特性提供对不同的浏览器分级支持,渐进增强(比如 IE 下显示为直角,对CSS 3支持较好的浏览器显示为圆角),这才是开发效率、可维护性、语义、性能的最佳平衡。


13
七 09

[翻译]纯 CSS 无 Hack 跨浏览器的多列等高

纯 CSS 打造多列等高并不像想象中那么容易。本文着重讲述多列布局出现的问题,之后提供一个在所有浏览器都正常工作的简单解决方案。这个方法 100% 无 CSS hack,无图片,无 javascript,甚至可以用在最严格编码的网站上。

多列等高的问题

上例中有包含不同内容的 3 列,可以看出存在的问题是列的背景色随着其包含内容的高度而自适应展开。这是我们要解决的问题。如何使所有的列等高?或具体的说,如何使所有列的高度等于最高列的高度?这很棘手,因为我们不清楚每列将会多高,哪一列是最高的。不能简单的给所有列一个固定的高度,如果内容很少将会导致页面底部有大片空白;如果内容太多则会在文字显示完全前关闭。两种情形都不妥。实际上,内容的长度是动态的,所以每列的高度也是动态的。必须意识到Web 上没有固定的东东,乡民们有不同的屏幕分辨率,浏览器中的文字也可能被设置为任意大小,所有这些都会影响内容的高度。

分离列内容与其背景色

解决等高问题的第一步是把能分离的破开。方法是每列用两个 div 替代原来的一个。第一个 div 用来放内容,另一个用来作背景色。分离使我们可以单独控制这些额外的元素,之后用更有效的方法把它们放在一起。答案呼之欲出。

浮动的容器的高度始终取决于其浮动的内容(高度)

这是本文多列等高方法的核心。 使一个 div 的高度等于最高列高度的唯一方法是这个 div 包含所有的列。换句话说,通过把所有的列放在一个容器中,容器的高度就是最高列的高度。这是个非常有用的结构。

3列 HTML div 结构

上例中 3 个内容列在一个 div 容器中。

<div id="container1">
    <div id="col1">Column 1</div>
    <div id="col2">Column 2</div>
    <div id="col3">Column 3</div>
</div>

3 列 CSS

下面是使 div 容器等高于最高列的 CSS。

#container1 {
    float:left;
    width:100%;
}
#col1 {
    float:left;
    width:30%;
    background:red;
}
#col2 {
    float:left;
    width:40%;
    background:yellow;
}
#col3 {
    float:left;
    width:30%;
    background:green;
}

为了让这一结构在所有浏览器中正确工作,容器 div 必须浮动(左或右),同时每一个内容列的 div 也要浮动,哪种方式并不重要。浮动内容 div 的进程使它们在页面中排列在一条水平线上。浮动容器使其自适应到最高列的高度。如果不浮动容器,内容
div 将会从容器底部溢出,容器不会拥有正确的高度。事实上在此例中,容器不浮动的话其最终高度为0。

增加额外嵌套的容器

下一步是增加额外的容器,它们彼此嵌套。我们需要容器的数量等于列的数量:3。这 3 个容器用作各列的背景。请注意,我们去除了原始列的背景色,并将其加至容器上。

3列 HTML div 结构

两个额外的容器加至下面的 HTML 中。

<div id="container3">
    <div id="container2">
        <div id="container1">
            <div id="col1">Column 1</div>
            <div id="col2">Column 2</div>
            <div id="col3">Column 3</div>
        </div>
    </div>
</div>

3 列 CSS

所有元素左浮动,容器宽度设为100%,使他们占满页面的宽度。背景色从内容 div 移除并加至容器上。

#container3 {
    float:left;
    width:100%;
    background:green;
}
#container2 {
    float:left;
    width:100%;
    background:yellow;
}
#container1 {
    float:left;
    width:100%;
    background:red;
}
#col1 {
    float:left;
    width:30%;
}
#col2 {
    float:left;
    width:40%;
}
#col3 {
    float:left;
    width:30%;
}

用相对定位移动容器

现在用相对定位把容器移至新的位置。移动后 div 如下图所示。即等高列背景容器的层叠和位置。为了显示右侧的绿色列 container2 向左移了30%,为了显示中间的黄色列container1 向左移动了40%,与此同时红色部分依然可见作为左侧列。

相对定位的 CSS

下面是添加了相对定位的CSS。

#container3 {
    float:left;
    width:100%;
    background:green;
}
#container2 {
    float:left;
    width:100%;
    background:yellow;
    position:relative;
    right:30%;
}
#container1 {
    float:left;
    width:100%;
    background:red;
    position:relative;
    right:40%;
}
#col1 {
    float:left;
    width:30%;
}
#col2 {
    float:left;
    width:40%;
}
#col3 {
    float:left;
    width:30%;
}

将每列的内容移回

下一步是把每列的内容移回到页面上,使之排列在下面的背景色上。再次使用简单的相对定位来完成它。

最后在最外面的容器 container3 上添加overflow:hidden,砍去超出容器的部分。

相对定位的 CSS

下面是增加了相对定位和溢出的 CSS 规则。请注意 container3 上额外的position:relative; 这是为了解决一个
IE bug ,阻止overflow:hidden;工作。

#container3 {
    float:left;
    width:100%;
    background:green;
    overflow:hidden;
    position:relative;
}
#container2 {
    float:left;
    width:100%;
    background:yellow;
    position:relative;
    right:30%;
}
#container1 {
    float:left;
    width:100%;
    background:red;
    position:relative;
    right:40%;
}
#col1 {
    float:left;
    width:30%;
    position:relative;
    left:70%;
}
#col2 {
    float:left;
    width:40%;
    position:relative;
    left:70%;
}
#col3 {
    float:left;
    width:30%;
    position:relative;
    left:70%;
}

对列增加 padding

最后还需对列增加 padding,这样每列边缘的文字不至于显得拥挤。如果我们增加 padding,一些浏览器中可能正常显示,但不是所有。IE 错误的盒模型,导致其估算拥有padding 的元素宽度异常。一个 200px 宽 20px padding 的 box 在 IE 中被视为 200px 宽,在其他浏览器中则为正确的 240px。padding应该加在元素的宽度上。凸微软!

不过不用担心…我们可以用完全不依赖于 padding 的方法来解决这个问题。相反,我们把列弄窄一点(列宽减去两侧的 padding),之后用相对定位把它们移至正确的位置。在我们的例子中我们用了2% 的 padding,则 30% 的列将减至 26%,40% 的列减至 36%。用相对定位移回列时需谨记,现在列变窄了,所以当它们一起像最初那样左浮动时,每一个需要比上一个移动更远的距离。

完整的CSS

为了使布局保持在小宽度我在每个内容列增加了overflow:hidden; 这将切去超出列宽的东东,并阻止其干扰其他布局。重申一下,这只是IE 的问题,其他所有浏览器会保持正确的布局,不管列内是虾米。如果你真想这样做,可以用 IE 条件注释只对 IE 写规则。

#container3 {
    float:left;
    width:100%;
    background:green;
    overflow:hidden;
    position:relative;
}
#container2 {
    float:left;
    width:100%;
    background:yellow;
    position:relative;
    right:30%;
}
#container1 {
    float:left;
    width:100%;
    background:red;
    position:relative;
    right:40%;
}
#col1 {
    float:left;
    width:26%;
    position:relative;
    left:72%;
    overflow:hidden;
}
#col2 {
    float:left;
    width:36%;
    position:relative;
    left:76%;
    overflow:hidden;
}
#col3 {
    float:left;
    width:26%;
    position:relative;
    left:80%;
    overflow:hidden;
}

好了,就是这样。我希望这篇文章对你有用。可以自己弄一下 CSS 看一下它是如何工作的。你可以搞很多列,只要容器和内容列的数目相等。不要忘记看看我的 demo:2列 3列4列,以及5列


30
五 09

伪清除浮动一法

前言:

彪叔一个多月前总结了《清理浮动的全家》,当时看了之后没什么新鲜的东西也就忽略了,今天偶然又翻到那篇文章,想到了另外一个的方法(N久前在书店翻到的),在网上没看到有人提起过,写出来大家一起讨论下。

HTML:

<ul>
<li>无序列表</li>
<li>无序列表</li>
<li>无序列表</li>
</ul>
<p>段落</p>

CSS:

ul, li{margin:0;padding:0;}
ul{background-color:yellow;}
li{float:left;list-style-type:none;}
p{clear:both;background-color:red;}

Firefox下效果:

可见,ul的黄色背景并没有显示出来。

重点来了:

在CSS理加上如下代码

ul{float:left;}

效果如下:

缺点:

浮动算是清除成功了,但是宽度没有像display:block那样撑开,需要指定一个宽度:

ul{width:100%;}

总结:

浮动带来的一个问题就是清除浮动,虽然有很多方法来解决,但最根本的解决方法还是不要滥用浮动,比如有些情况下完全可以用

selector{display:inline;}

selector{display:inline-block;}

来实现。


16
四 09

说说display:inline-block

前言:

Firefox 3发布后,我开始肆无忌惮的使用inline-blcok…

基本用法:

  1. inline元素的inline-block:
    span{displsy:inline-blcok;}
  2. block元素的inline-block:
    div{displsy:inline-blcok;#displsy:inline;#zoom:1;}

    #开头的为针对IE6/7的hack,多数人用*,用*的话我使用的格式化工具会出错。

遇到的几个问题:

  1. 代码格式化对间距的影响:

    HTML:

    <ul>
        <li class="current">1</li>
        <li>2</li>
        <li>3</li>
    </ul>

    注意代码的缩进为4个空格

    CSS:

    /*请自行引入YUI reset*/
    body{margin:10px;font-size:12px;font-family:Tahoma, Arial, Helvetica, sans-serif;}
    li{display:inline-block;#display:inline;#zoom:1;padding:1px 5px;border:1px #ccc solid;background-color:black;cursor:pointer;color:white;}
    li.current{background-color:#c31909;}

    以下为IE 6/7/8显示效果(Firefox 3/Safari 4/Chrome 2同IE8):

    可见除IE6/7外的浏览器的li之间有4px的间隔。

    HTML将缩进去除则正常:

    <ul>
    <li class="current">1</li><li>2</li><li>3</li>
    </ul>

    以下为IE 6/7/8显示效果(Firefox 3/Safari 4/Chrome 2同IE8):

    由此分析,这4px可理解为一个空格。

    当然,我们的HTML代码是给人看的,加上编辑器有格式化代码工具,缩进不能去除,而且在实际应用中li之间往往有一定的间距,所以我的做法是顺水推舟,仅对IE
    6/7 hack:

    li{#margin-right:4px;}
  2. overflow引起Firefox 3 bug:

    HTML:

    <a href="#">订阅</a>

    CSS:

    /*请自行引入YUI reset*/
    body{margin:10px;font-size:12px;font-family:Tahoma, Arial, Helvetica, sans-serif;}
    a{display:inline-block;width:3.5em;padding:3px 0;background-color:red;font-weight:bold;text-decoration:none;text-align:center;color:white;}
    a:hover{color:black;}

    以下为IE 6/7/8显示效果(Firefox 3/Safari 4/Chrome 2同IE6):

    IE 7/8 的padding有点小问题,不过基本可以忽略。

    如果给a加上overflow:hidden(此处的overflow:hidden没有必要,仅为演示使用)

    /*请自行引入YUI reset*/
    body{margin:10px;font-size:12px;font-family:Tahoma, Arial, Helvetica, sans-serif;}
    a{display:inline-block;width:3.5em;overflow:hidden;padding:3px 0;background-color:red;font-weight:bold;text-decoration:none;text-align:center;color:white;}
    a:hover{color:black;}

    Firefox 3显示效果:

    text-align:center失效!其余浏览器皆正常(这里有个小插曲,开始我误以为是td中的inline-block元素不能居中,后来发现是overflow:hidden的问题),经测试overflow:auto也会出现此问题,不过这个更少用到。

  3. IE 6的line-height失效:

    HTML:

    <p>欢迎使用<span></span>飞信</p>

    CSS:

    /*请自行引入YUI reset*/
    body{margin:10px;font-size:12px;font-family:Tahoma, Arial, Helvetica, sans-serif;}
    p{background-color:yellow;line-height:5;}
    span{display:inline-block;width:16px;height:16px;background-image:url('http://images.n20svrg.139.com/simple2008/coremail/images/simple_icons.png');background-repeat:no-repeat;background-position:-500px -50px;vertical-align:middle;}

    以下为IE 6/7/8显示效果(Firefox 3/Safari 4/Chrome 2同IE8):

    未找到根治方法,可变通一下用padding解决:

    p{background-color:yellow;padding:2em 0;}

总结:

即便如此,我还是会继续使用下去-:P


3
四 09

body标签class属性的妙用(Google Reader前端简单分析)

身为greader的重度用户,今天来简单分析下其中关于body的一点小技巧。

  1. 区分不同浏览器

    IE6:

    <body class="ie ie6 loaded">

    IE7:

    <body class="ie ie7 loaded">

    IE8:

    <body class="ie ie8 loaded">

    GECKO内核(firefox/flock等):

    <body class="gecko loaded">

    WEBKIT内核(safari/chrome等):

    <body class="webkit loaded">

    以上值通过JS判断当前浏览器后给body的class赋值。

    当然,并不是每种浏览器都利用到了相应的class,部分是预留的,CSS中只用到了以下部分:

    .gecko .goog-inline-block{display:-moz-inline-box;}
    .gecko .goog-inline-block{display:inline-block;}
    .ie6 .goog-inline-block{display:inline;}
    .ie7 .goog-inline-block{display:inline;}

    这段代码是为了实现跨浏览器的display:inline-block,其中只用到了gecko/ie6/ie7这三个class

  2. 布局切换当界面隐藏侧边栏时(以firefox为例):
    <body class="gecko loaded lhn-hidden">

    body增加了lhn-hidden的class:

    .lhn-hidden #chrome-lhn-toggle-icon{border-color:#ebeff9 #ebeff9 #ebeff9 #68e;border-width:5px 0 5px 5px;}
    .lhn-hidden #chrome-lhn-toggle:hover #chrome-lhn-toggle-icon{border-left-color:#fff;}
    .lhn-hidden #nav{display:none;}
    .lhn-hidden #chrome{margin-left:0;}
    .lhn-hidden #chrome-title{display:inline;}
    .lhn-hidden #entries .entry-body{max-width:100%!important;}

    通过给body添加/删除class=”lhn-hidden”来实现侧边栏的显示隐藏,实现了结构、表现、行为的高度分离!

    用jquery的话则变得更加简单,JS只需以下几句:

    $('#chrome-lhn-toggle').click(function(){
        $('body').toggleClass('lhn-hidden');
    })

    这样书写及维护起来非常方便。

  3. 换肤众所周知的GR彩蛋:上上下下左右左右BA,就是通过给 body 增加 class 实现的
    <body class="konami">

    konami 的 css 如下:

    .konami #sub-tree{background:#006f89 url(/reader/ui/1466947354-ninja-sidebar.gif) repeat-y left 0;}
    .konami .lhn-section{background-color:#006f89;}
    .konami .scroll-tree li{background:transparent;}
    .konami .entry .entry-actions .star, .konami .entry .entry-actions .like, .konami .entry .entry-actions .read-state, .konami .entry .entry-actions .broadcast, .konami .entry .entry-actions .broadcast-with-note, .konami .entry .entry-actions .email, .konami .entry .entry-actions .item-link, .konami .entry .entry-actions .tag, .konami .entry .entry-icons .star, .konami .entry-likers-n .number-of-likers{background-image:url(/reader/ui/3207170781-konami-action-icons.png);}
    .konami-like-image{position:absolute;}