📜  div 类和 id 在 HTML 中有何用处?

📅  最后修改于: 2021-09-01 02:15:43             🧑  作者: Mango

在本文中,我们将讨论 id、div 和 class 在 HTML 中的作用。 Id、Div 和 class 是构建 HTML 页面的重要概念。让我们一一讨论。

    要理解本文,您可以阅读以下这些文章以供参考。
  • HTML 中的 ID 属性
  • HTML 中的 Div 标签
  • HTML 中的类属性

现在,考虑一个用例,您想要实现任何书籍的目录并想要直接转到您想要阅读的主题。在这种情况下,您可以在 HTML5 中实现 Id 属性,并且可以将所有主题与目录链接。现在,您可以直接点击并直接转到您想阅读的主题。



  

    Id Div and Class

  

    
                 
           
            
                    
  1.                     Example Topic-1                     
                        Example Heading 1                     

                            Hi, This is an example of                          Example Heading 1.                     

                    
  2.             
        
           
            
                    
  1.                     Example Topic-2                     
                        Example Heading 2                     

                            Hi, This is an example of                          Example Heading 2.                     

                    
  2.             
        
           
            
                    
  1.                     Example Topic-3                     
                        Example Heading 3                     

                            Hi, This is an example of                          Example Heading 3.                     

                    
  2.             
        
           
            
                    
  1.                     Example Topic-4                     
                        Example Heading 4                     

                            Hi, This is an example of                          Example Heading 4.                     

                    
  2.             
        
           
            
                    
  1.                     Example Topic-5                     
                        Example Heading 5                     

                            Hi, This is an example of                          Example Heading 5.                     

                    
  2.             
        
    
  

可以通过执行上面的代码来验证结果。现在,让我们看看页面结构的输出。
输出:

在上面的输出屏幕中,您可以直接看到 Id 和 Div 是如何工作的,并且您还可以看到 Div 如何用于构建您的网页。

现在,在这里您将看到如何在 Id、Div 和 class 的帮助下使用 CSS 为您的网页添加更多功能。



  

    Id Div and Class
  
    

  

    
        
                         
               
                
                    
                            
  1.                                                              Example Topic-1                             
                                Example Heading 1                             

                                    Hi, This is an example of                                  Example Heading 1.                             

                            
  2.                     
                
            
               
                
                    
                            
  1.                             Example Topic-2                             
                                Example Heading 2                             

                                    Hi, This is an example of                                  Example Heading 2.                             

                            
  2.                     
                
            
               
                
                    
                            
  1.                             Example Topic-3                             
                                Example Heading 3                             

                                    Hi, This is an example of                                  Example Heading 3.                             

                            
  2.                     
                
            
               
                
                    
                            
  1.                             Example Topic-4                             
                                Example Heading 4                             

                                    Hi, This is an example of                                  Example Heading 4.                             

                            
  2.                     
                
            
               
                
                    
                            
  1.                             Example Topic-5                             
                                Example Heading 5                             

                                    Hi, This is an example of                                  Example Heading 5.                             

                            
  2.                     
                
            
        
    
  

可以通过执行上面的代码来验证结果。现在,让我们看看页面结构的输出。
输出:

在上面的例子中,你可以直接看到,通过使用Id,我们可以为不同的标题添加单独的功能,而class是通用的,我们可以编写一次CSS脚本,可以在任何地方使用它,它也有助于可重用性。