是否可以安全地在表行上使用伪元素(:after,:before)? [英] Is it possible to user pseudo-element (:after, :before) on table row safely?

查看:156
本文介绍了是否可以安全地在表行上使用伪元素(:after,:before)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将绝对定位的元素添加为表行的:after(属于:before).

查看此 http://jsbin.com/IGumoye/5/edit

我假设当我添加这样的元素呈现引擎(至少基于Webkit)时,认为它是某种类型的表格单元.

:before在所有浏览器中均无法正常工作.但是:after在Firefox中效果很好,而在webkit中效果很好.在webkit中,它保留了很小的空间,并使整个表的宽度变大了.

-这是困扰Webkit的原因.

如何解决此问题?我在哪里可以知道它为什么发生?

更新1

可能的解决方案:使用此行中的第一个表格单元作为绝对定位元素的主机.

http://jsbin.com/IGumoye/10/edit

更新2

好的,我想我找到了解决最初问题的方法.我认为可能会有更好的选择.但是我想最大程度地使用CSS.问题是-使一些静态文本可编辑,并且如果用户单击其他位置-关闭此编辑模式.

http://jsbin.com/IGumoye/23/edit

解决方案

在此示例中,您使用::after::before伪元素在表行之后或之前添加内容,这实际上会破坏表格布局并导致不可预测的结果.

如果要将生成的内容添加到表单元格中,结果将更加一致.

除了所生成内容的原始规范外,没有太多要引用的内容:

http://www. w3.org/TR/2009/CR-CSS2-20090908/generate.html#before-after-content

此外,请记住,CSS渲染引擎在创建表时会生成匿名框:

http://www.w3. org/TR/2009/CR-CSS2-20090908/tables.html#anonymous-boxes

但是,由于生成的内容不是DOM的一部分,因此整个表呈现过程可能无法以明智的方式处理多余的伪元素.

您正在钻研的区域指定不正确,任何支持都将取决于浏览器.

根据您的布局要求,您可能需要使用JavaScript或jQuery将表的DOM更改为所需的效果.

I want to add absolutely positioned element as an :after (of :before) of a table row.

Look at this http://jsbin.com/IGumoye/5/edit

I assume that when I add such element rendering engine (at least Webkit based) think it is a table cell of some kind.

:before works badly in all browsers. But :after works very well in Firefox and almost good in webkit. In webkit it keep small space and makes whole table width bigger.

- This is what bothers me in webkit.

How to fix this? And where can I read about why it happening?

UPDATE 1

Possible solution: Use first table cell in this row as a host for absolutely positioned element.

http://jsbin.com/IGumoye/10/edit

UPDATE 2

Ok, I think I found solution for my initial problem. And I assume there are might be better one. But I wanted to make using CSS by maximum. Problem is - make some static text editable and if user clicks anywhere else - close this edit mode.

http://jsbin.com/IGumoye/23/edit

解决方案

In this example, you are using the ::after and ::before pseudo-elements to add content after or before a table row, which essentially will break the table layout and lead to unpredictable results.

If you were to add the generated content to a table-cell, the results would be more consistent.

There is not much to refer to except the original specification for generated content:

http://www.w3.org/TR/2009/CR-CSS2-20090908/generate.html#before-after-content

In addition, keep in mind that the CSS rendering engine generates anonymous boxes when creating tables:

http://www.w3.org/TR/2009/CR-CSS2-20090908/tables.html#anonymous-boxes

However, since generated content is not part of the DOM, the whole table-rendering process probably cannot deal with the extra pseudo-element in a sensible way.

You are delving into an area that is not well specified and any support will be browser specific.

Depending on your layout requirements, you might need to use JavaScript or jQuery to alter the DOM of the table to the desired effect.

这篇关于是否可以安全地在表行上使用伪元素(:after,:before)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆