简体   繁体   English

这些DIV真的有必要吗?

[英]Are these DIVs really necessary?

I sometimes find myself creating a div which serves no other purpose than to hold another element. 我有时发现自己创造了一个div,除了拥有另一个元素之外别无其他目的。

For example when creating a menu, I would create a div and assign it a background colour, position etc. Then within it I would create an unordered list. 例如,在创建菜单时,我会创建一个div并为其指定背景颜色,位置等。然后在其中我将创建一个无序列表。

I could surely assign all the properties I assigned to the DIV to the UL and not use a div at all. 我当然可以将我分配给DIV的所有属性分配给UL,而根本不使用div。

Any ideas of what is best practice and reasons for it. 什么是最佳实践和原因的任何想法。

Thanks 谢谢

Zenna 泽纳

DIVs can be useful for grouping semantically related elements. DIV可用于对语义相关元素进行分组。 If you are simply wrapping a single element that is also a block element, then you are simply adding bytes to the file. 如果您只是包装一个也是块元素的元素,那么您只需将字节添加到文件中。

No, they are not. 不,他们不是。 The purpose of a div element is to create block level structure in the document . div元素的目的是在文档中创建块级结构 If you can lose them just lose them. 如果你失去他们就会失去他们。 Never use divs to solve design purposes, css is for that. 永远不要使用div来解决设计目的,css就是为了这个目的。 Use html elements each like list, data definitions or tables (which were overabused in the past and used as the divs are now for css purposes). 使用html元素,如列表,数据定义或表格(过去过度使用并用作div现在用于css目的)。 The more diverse your HTML knowledge is the less you are using divs all over the place. HTML知识越多样化,你就越少使用div。

I use often divs to keep child-padding/margins from ruining parent-width. 我经常使用div来保持子填充/边距不会破坏父宽度。 But you need to be careful with this type of stuff - you could end up adding a bunch of nonsense. 但是你需要小心这类东西 - 你最终可能会添加一堆废话。

The real issue is that we are using HTML in ways that its creators had never imagined. 真正的问题是我们正在以其创作者从未想象过的方式使用HTML。 The need for 'all those divs' is because some really smart people have found some very creative ways to take a very old standard and do some very modern things with it. “所有这些div”的需求是因为一些非常聪明的人已经找到了一些非常有创意的方法来采用一个非常古老的标准并用它做一些非常现代的事情。

Best practice should be to use as few div elements as possible. 最佳做法应该是使用尽可能少的div元素。 If you've got a div elements with only one child, chances are it's a useless div . 如果你有一个只有一个孩子的div元素,很可能是一个无用的div The div element should really only be used when you need a block element and there is no semantic pre-defined element at hand. div元素实际上只应在需要块元素时才使用,并且手头没有语义预定义元素。 This includes grouping elements as Renesis suggests. 这包括Renesis建议的分组元素。

Div's are really a necessary evil without a more semantically rich set of tags. Div是真正必要的邪恶,没有更丰富的语义丰富的标签。

Their purpose is to server as a generic container. 它们的目的是将服务器作为通用容器。 Therefore, I suppose you could say they do their job well. 因此,我想你可以说他们做得很好。

If you can do what you're trying to do without the additional div, then leave it out. 如果你可以在没有附加div的情况下做你想做的事情,那么就把它留下来。 If it's an important part of your design that you can't fix with some clever CSS, then it's still a whole lot better than using tables... 如果它是你的设计的一个重要部分,你无法修复一些聪明的CSS,那么它仍然比使用表格好很多...

I use DIVS primarily for at least one of two main reasons: 我使用DIVS主要是出于以下两个主要原因中的至少一个:

  • I need it to provide a new physical level of CSS in some way (either child padding inside a fixed width element, a shadow or a double border around an image, etc.) 我需要它以某种方式提供一个新的物理级别的CSS(固定宽度元素内的子填充,图像周围的阴影或双边框等)
  • It logically groups the elements it contains (for future portability and semantics in both HTML and CSS - IE "div#menu .label". This way I can use the "label" class several places but have a specific style applied to labels within the "menu" div). 它逻辑上对它包含的元素进行分组(为了HTML和CSS中的未来可移植性和语义 - IE“div#menu .label”。这样我可以在几个地方使用“label”类,但是有一个特定的样式应用于“菜单”div)。

For example, if you were to transfer all attributes to the UL, but then decided you wanted a caption or image above or below the menu but in the same location, you would have to create the DIV again to place the new element inside, and transfer half of the attributes back to it. 例如,如果您要将所有属性传输到UL,但后来确定您想要在菜单上方或下方但在同一位置的标题或图像,则必须再次创建DIV以将新元素放入其中,并且将一半属性转移回它。

In the case of a UL, yes, the DIV is unnecessary. 在UL的情况下,是的,DIV是不必要的。 They are both block elements, so anything you can do with a DIV wrapped around the outside you can do directly to the UL itself. 它们都是块元素,因此您可以使用围绕外部的DIV进行任何操作,您可以直接使用UL本身。

However, because of the Box Model problem with some IE browsers, some people tell you to add these DIVs as a workaround. 但是,由于某些IE浏览器存在Box模型问题,有些人会告诉您添加这些DIV作为解决方法。 When you combine padding and width, IE6 disagrees with other browsers about what the final size of the element will be. 当您组合填充和宽度时,IE6不同意其他浏览器关于元素的最终大小。 So one workaround is to put padding but no width on an inner element, and width but no padding on an outer element. 因此,一种解决方法是在内部元素上放置填充但没有宽度,宽度但在外部元素上没有填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM