使用CSS定位DIV的第一个元素 [英] Target first element of DIV using CSS
问题描述
我在div内的表格中有一个所见即所得的编辑器,如此...
I have a WYSIWYG editor inside of a table that is placed within a div, as such...
<div class="mydiv">
<li>
<table>My WYSIWYG</table>
</li>
</table>
在所见即所得的里面还有更多的表,但是我只想定位第一个表,我知道我可以为该表分配一个类,但这不是我想要的.
Inside my WYSIWYG there are more tables however I only want to target the first table, I understand I could assign a class to the table but thats not what I want.
我尝试使用以下方法似乎不起作用...
I've tried using the following only it doesn't seem to work...
.mydiv li + table {width:100%;}
推荐答案
您需要.mydiv li table:first-child
.
更多信息.
:first-child
可在IE7 +和所有现代浏览器中使用.
:first-child
works in IE7+ and all modern browsers.
.mydiv li + table
不适用于您,因为它与以下内容匹配:
.mydiv li + table
isn't working for you because that matches something like this:
<div class="mydiv">
<li></li>
<table>My WYSIWYG</table>
</div>
还请注意,您的HTML几乎没有什么毛病:
Also note that there are few things wrong with your HTML:
<div class="mydiv">
<li>
<table>My WYSIWYG</table>
</li>
</table>
-
最后的
-
</table>
应该是</div>
. -
div
应该是ul
,因为li
不是div
的有效子代. </table>
at the end should be</div>
.- The
div
should be aul
, becauseli
is not a valid child ofdiv
.
我已经在上面的演示中修复了这些问题.
I've fixed these things in the demo above.
这篇关于使用CSS定位DIV的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!