This question already has answers here:
How do I select an element only when inside another element?
                                
                                    (3个答案)
                                
                        
                        
                            List of HTML5 elements that can be nested inside P element?
                                
                                    (1个答案)
                                
                        
                                2个月前关闭。
            
                    
这是一个漂亮的问题,我想解决不寻常的问题。

这是关于访问CSS中的特定html标记。所以这是html文件中的场景

<DOCTYPE html>
   <html>
      <head>
         <link rel="stylesheet" href="MyCssCode.css"/>
      </head>
      <body>
         <p>
            Some Text!
            <h1>Even More Text!</h1> <!-- what i want to access -->
         </p>
         <h1> Lorem Ipsum Dolor Sit Amet.</h1>
      </body>
   </html>


现在问题来了,当在CSS中时,我想专门将规则应用于h1标记内的p。而不影响下面的其他h1(在p标记之外)。我已经尝试过选择器,例如+>,但在此在线证明中我找不到真正的答案,这是不可能的。

最佳答案

问题是因为将h1标记放在p标记内是不合适的,如果在浏览器控制台中查看,则会看到代码损坏。

html - 是否可以将规则应用于标签嵌套中的特定标签?-LMLPHP

您可以像这样使用div标签来代替使用p标签

<html >
<body>
    <div>
        Some Text!
        <h1>Even More Text!</h1> <!-- what i want to access -->
    </div>
    <h1> Lorem Ipsum Dolor Sit Amet.</h1>
</body>




然后您可以应用这样的样式

div h1{/* all h1 tag inside div*/}
div+h1{/* the h1 next to div*/}
div>h1{ /* h1 tags direct child of a div*/}

关于html - 是否可以将规则应用于标签嵌套中的特定标签? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58861247/

10-12 12:34
查看更多