CSS 圆角快速完成:这是一项糟糕的技术吗?
这是我一直在尝试的一种快速而肮脏的圆角技术。
<!-- assuming the div isn't statically positioned -->
<div>
<img src="box_TL.png" style="position:absolute;top:0;left:0;"/>
<img src="box_TR.png" style="position:absolute;top:0;right:0;"/>
<!-- other content -->
<img src="box_BL.png" style="position:absolute;bottom:0;left:0;"/>
<img src="box_BR.png" style="position:absolute;bottom:0;right:0;"/>
</div>
是的,它很丑,但是速度很快,角落是流畅的,它避免了嵌套的 div 并且不需要 JavaScript。角图像和内容顺序没有区别,但我认为以这种方式排序角和内容可能更直观。
问题:这个技术有多可怕?还可以吗,还是应该彻底放弃?
Here's a quick and dirty round corners technique I've been playing around with.
<!-- assuming the div isn't statically positioned -->
<div>
<img src="box_TL.png" style="position:absolute;top:0;left:0;"/>
<img src="box_TR.png" style="position:absolute;top:0;right:0;"/>
<!-- other content -->
<img src="box_BL.png" style="position:absolute;bottom:0;left:0;"/>
<img src="box_BR.png" style="position:absolute;bottom:0;right:0;"/>
</div>
Yeah it's ugly, but it's fast, the corners are fluid, it avoids nested divs and requires no javascript. The corner images and content order makes no difference, but I thought it might be more intuitive to order corners and content this way.
Question: How terrible is this technique? Is it passable or should I abandon it completely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会使用 jQuery Corner 插件。它非常快,并且可以在所有现代浏览器中运行,也在 IE6 中。
I'd use jQuery Corner Plugin. It's very fast and works in all modern browsers, and also in IE6.
太可怕了。您的标记应该是内容,您的布局应该是风格。不杂乱。您应该使用:
和样式:
是的,当然,某些浏览器不会出现圆角。但是,如果您编写了一个解决方案,即使在不支持该功能的浏览器中也能提供适当的圆角,那么您将得到一个复杂的解决方案,而且您的网站很可能在其他一些浏览器中根本无法工作。因此,您应该问自己:网站在某些浏览器 X 中正常运行,还是在其他浏览器 Y 中获得圆角,哪一个更重要?
添加:使用另一个答案中提到的 jQuery 插件(或其他一些预打包的解决方案)可能是可以接受的。只要它不需要任何额外的
、
或其他标签。
It's terrible. Your markup should be content, and your layout should be in the style. Not intermingled. You should go with:
and the style:
Yes, sure, some browsers won't get rounded corners. But if you hack up a solution that will give properly rounded cornsers even in browsers that does not support that, you will have a complex solution, and odds are that your site will not work att all in some other browsers. So you should ask yourself: What is more important, that the site works at all in some browser X or that you get rounded corners in some other browser Y?
Addition: Using the jQuery plugin mentioned in another answer (or some other pre-packaged solution) might be accepptable. As long as it does not require any extra
<div>
,<img>
or other tags.这是一个糟糕的解决方案,抱歉:-)
确实,您不需要任何 JavaScript 或嵌套
div
元素。无论如何,JavaScript 是很容易避免的。但是四个不相关的img
元素是否比几个嵌套的div
元素更好呢?img
元素应该包含图像内容,使用它进行布局基本上与使用表格进行布局相同。是的,它有效,但它是错误的,并且它破坏了所有语义价值。如果我是你,我会这样做(请原谅那些愚蠢的类名,它们只是为了说明):
标记
CSS
明白了吗?等一下,我会在某个地方举一个例子:-)
编辑:刚刚提出了一个一个例子!
无论如何,这种方法将确保盒子的高度和宽度具有完全的灵活性,并且盒子内的布局始终按其应有的方式工作,不受角落的影响。
是的,它为您提供了一些嵌套的 div,除了布局之外没有其他用途 - 但话又说回来,这就是 DIV 的用途。 IMG 应始终是与内容相关的图像。
您可以将所有角设为 15*15px 并设置容器的背景颜色。然而,当像这样拉伸这些图像时,您有机会添加阴影、渐变或其他效果。这是我习惯做的,所以我对拉伸图像这样做了。
这种方法已经过充分测试,据我所知/记得至少在 IE 5.5 上都可以正常工作。
It's a terrible solution, sorry :-)
It's true that you don't need any JavaScript or nested
div
elements. The JavaScript is easily avoidable, no matter what. But is four irrelevantimg
elements better than a few nesteddiv
elements? Theimg
element is supposed to contain image content, using it for layout purposes is basically the same as using tables for layout. Yes, it works, but it's wrong, and it ruins all semantic value.If I were you, I'd do it this way (excuse the silly class-names, they are just there to illustrate):
The markup
The CSS
Got it? Hang on, I'll put up an example somewhere :-)
Edit: Just threw up an an example!
Anyhow, this method will ensure a complete flexibility regarding height and width of the box, and the layout within the box always works the way it should, unaffected by the corners.
Yes, it gives you some nested divs with no purpose other than the layout - but then again, that's what DIVs are used for. IMGs should always be content-related imagery.
You could do it with all the corners being 15*15px and setting the background-color of the container. However, when stretching these images like this, you get the opportunity to add shadows, gradients or other effects. This is what I'm used to do, so I did this this way with the stretched images.
This method is well tested out, and should as far as I know/remember work fine at least all the way back to IE 5.5.
这是一个非常古老的话题,但由于它重新出现在首页上,我将添加一条评论。
在过去的几个月里,出现了一种新的圆角解决方案,它解决了所有相关版本的 IE(6、7、8)的问题。
CSS3Pie 是 IE 的“hack”,它允许您在 CSS 中设置圆角,而不必在其他地方担心它。您可以一次性扔掉标记中所有多余的 div 和 jquery 插件,只需在样式表中按照应有的方式指定它即可。
所有其他浏览器都支持 CSS 中的圆角,并且已经这样做了足够长的时间,因此您无需担心旧版本。
CSS3Pie 还可以帮助处理 IE 中的 CSS 渐变和框阴影,因此这对于跨浏览器开发人员来说是一个巨大的胜利。
This is a very old topic, but since it's re-appeared on the front page, I'll add a comment.
In the last few months, a new solution has appeared for rounded corners, which solves the issue for all relevant versions of IE (6,7,8).
CSS3Pie is a 'hack' for IE which allows you to set up rounded corners in your CSS and not worry about it anywhere else. At a stroke, you can throw away all those extra divs in your markup and those jquery plugins, and just specify it in your stylesheet the way it should be.
All other browsers support rounded corners in CSS, and have done so for long enough that you don't need to worry about older versions.
CSS3Pie also helps with CSS gradients and box shadows in IE too, so it's a very big win for cross-browser developers.
使用 PNG 时,您会遇到 IE6 的问题,因此您需要向 div 添加正确的 CSS 过滤器背景而不是图像,或者使用 javascript 帮助将 png 图像转换为添加了 png 背景的透明 gif。
http://www.twinhelix.com/css/iepngfix/
you'll come into issues with IE6 using PNGs so you will either need to add the correct CSS filter background to divs instead of images or use javascript to help turn the png images into transparent gifs with the png background added.
http://www.twinhelix.com/css/iepngfix/
如果框是固定宽度的,那么您可以使用一个有趣的技巧,该技巧适用于 IE8 和其他浏览器(但不适用于 IE7):
不幸的是,这不适用于任何具有流动宽度的内容,并且与旧版 IE 不兼容浏览器,但我仍然喜欢它:)
If the box is fixed width, then there's an interesting trick you can do which works in IE8 and the rest (but not IE7-):
Unfortunately this doesn't work with anything that has a fluid width, and it's not copatible with older IE browsers, but I still like it :)