使用CSS定位DIV的第一个元素 [英] Target first element of DIV using CSS

查看:932
本文介绍了使用CSS定位DIV的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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 a ul, because li is not a valid child of div.
    • 我已经在上面的演示中修复了这些问题.

      I've fixed these things in the demo above.

      这篇关于使用CSS定位DIV的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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