未加星标

Are we supposed to be able to trust empty DIVs to show in HTML5?

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Having seen advice seemingly change over the years regarding use of empty DIVs (ie. <DIV CLASS="somediv"></DIV> ) I'm confused as to the current thinking over whether or not to use &nbsp; when a DIV will have no inner HTML.

I can find no definitive confirmation over whether we can rely on all modern browsers to display background color and image correctly at the specified width & height when there is no inner HTML, so I'm thinking maybe we can't rely on it - yet it's such a seemingly basic area.

I have even seen suggestions that empty DIVs should never be used - but do specs really state it is 'wrong' to have empty DIVs, or is it just unreliable? (I've tried finding reference to them, but maybe I'm using the wrong terms).

To illustrate, here are 5 areas where I would normally use an empty DIV, in the absence of any recommended alternative:

.somediv:after{content:SOMETEXT}

Maybe there are different answers for each of these, which might explain the complexity over this issue.

I have, of course, tried discovering already, but for example the SO question Is necessary to show an empty <div>? suggests to me there is a huge amount of "IMHO", "probably", "seems to work" in this area. I would expect that by now that some official consensus has been reached on best practice.

So.. should I use &nbsp; and if so should I set font-size to the same as the smaller of DIV width/height to ensure that space is filled in all browsers? Are there any other CSS tricks to ensure this will work in all browsers?

Problem courtesy of: user1543257

Solution

The browser is not going to discard or forget your container just because it does not have any contents (yet).

If you want the container to have a specific placeholder shape, then you might give it min-height , min-width , height and width and make sure it's display: block; .

If you are still unsure, you can fill it with a spacer.gif/png without padding and margin.

http://jsfiddle.net/APxNF/1/

Solution courtesy of: DanFromGermany

Discussion

Short answer. Yes, browsers will render the div even if there is no content.

Long answer, That might now always be the case. I have worked in the web for 8 years now and never had to use these, but here they are anyway.

jsFiddle demo

HTML <div class="empty1"></div> <div class="empty2"></div> <div class="empty3"></div> CSS .empty1 { background: #FBB829; height: 100px; width: 100px; } .empty2:before { content: "\00a0"; } .empty2 { background: #FF0066; height: 100px; width: 100px; } .empty3 { background: #F02311; min-height: 1px; height: 100px; width: 100px; }

Sources:

Experience Empty div with 2px width and background color doesnt show with height as 100% http://csscreator.com/node/36023

Discussion courtesy of: screenmutt

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Are we supposed to be able to trust empty DIVs to show in HTML5?
本站链接:https://www.codesec.net/view/611094.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(72)