Home  >  Article  >  Web Front-end  >  Why can CSS use fake elements?

Why can CSS use fake elements?

WBOY
WBOYforward
2023-08-30 15:01:02415browse

为什么 CSS 可以使用假元素?

As a front-end developer, you should be familiar with CSS pseudo-elements, including their functionality and various presentation and structural applications. Basic CSS selectors and their many properties are fun to use, but understanding pseudo-classes and pseudo-elements is the next step to becoming a CSS expert.

In addition to CSS pseudo-elements, there are also some HTML elements commonly called pseudo-elements. CSS is sometimes used to ease processing and allow developers to create custom elements in web pages. However, they are not standardized and therefore cannot be used globally.

In this article, we will discuss why CSS can be used with fake elements? This article starts by explaining what fake elements are, why we use them, how they work, and many other basic aspects of fake elements.

What is a false element?

HTML elements that are not defined in the HTML document are called fake elements. It is possible to create fake components. You can give the element any name you choose, but this is not recommended. HTML enables developers to use custom elements in web pages. This element will run smoothly in your page.

Example

View the following examples to see how to use the Fake element in an HTML document.

<!DOCTYPE html>
<html>
<head>
   <title> Fake Elements </title>
   <style>
      *{
         background-color: grey;
         margin: 5px;
         letter-spacing: 1px;
      }
      .para{
         font-family: cursive;
      }
   </style>
</head>
<body>
   <section>
      <heading> Programming Languages </heading>
      <p class= "para"> To communicate with computers, programmers (developers) use a programming language, which is a computer language. It is a set of instructions written in a specific language (such as C, C++, Java, or Python), built to do a certain task. </p>
      <example> Some of the most popular programming languages are: </example> <br>
      <p>
         <ol>
            <li> <h1> C/C++ </h1> </li>
            <li> <h1> Java </h1> </li>
            <li id= "demo"> <h1> Python </h1> </li>
            <li> <h1> JavaScript </h1> </li>
            <li> <h1> PHP </h1> </li>
         </ol>
      </p>
   </section>
</body>
</html>

As we saw in the example above, we used our own elements such as and . The code executes smoothly and the text is displayed. However, we must style them accordingly. For example, if we use the HTML recognition element (h1) instead of , the text will automatically have a larger font size. To do this with fake elements we have to specify the font size using CSS.

Example

In this example, we have declared certain CSS properties to the pseudo-elements and .

<!DOCTYPE html>
<html>
<head>
   <title> Fake Elements </title>
   <style>
      *{
         background-color: yellow;
         margin: 5px;
         letter-spacing: 1px;
      }
      heading{
         color: black;
         text-decoration: underline;
         text-shadow: 4px 4px 4px grey;
         font-size: 28px;
      }
      .para{
         font-family: cursive;
      }
      example{
         color: red;
         font-weight: 900;
      }
   </style>
</head>
<body>
   <section>
      <heading> <h1 class= "head"> Programming Languages </h1> </heading>
      <p class= "para"> Programmers (developers) utilise a programming language, which is a computer language, to communicate with computers. It is a set of guidelines created in any particular language (C, C++, Java, Python), developed to carry out a certain task. </p>
      <example> Some of the most popular programming languages are: </example> <br>
      <p>
         <ol>
            <li> <h1> C/C++ </h1> </li>
            <li> <h1> Java </h1> </li>
            <li id= "demo"> <h1> Python </h1> </li>
            <li> <h1> JavaScript </h1> </li>
            <li> <h1> PHP </h1> </li>
         </ol>
      </p>
   </section>
</body>
</html>

We have added CSS styles to the fake element. The code executes smoothly and there are no issues with the style. The problem is how these fake elements work in HTML documents.

The answer is: As the capabilities of HTML have advanced, most modern browsers have become compatible with many changes or additions to its capabilities. Therefore, unrecognized elements are parsed directly into the DOM tree. However, they do not have any functionality or special features.

Why shouldn't we use fake elements?

Although fake elements work fine in web pages, it is strongly recommended not to use fake elements in HTML documents. Here are some reasons why we should not use fake elements -

  • The HTML specification does not support and recognize these spurious elements.

  • These elements have no specified function. Just like the text in

    has a predefined font size, fake elements have no access to such functionality.

  • They may hinder the functionality of future standard elements (if developed) that have the same name as the fake element.

  • As versions of HTML change rapidly, there may be specific standard elements in the DOM that are best suited for that specific functionality.

  • These tags may cause compatibility issues in different browsers. Additionally, browsers render standard elements faster. This way, your web pages will run smoothly.

  • Standard HTML elements provide various benefits such as SEO (Search Engine Optimization) and speed. They are preferred by popular browsers like Google.

  • Using fake elements shows a lack of professionalism (for some developers). Standard components are easy to maintain. Additionally, it allows for further modifications if required.

  • Using standard HTML elements will make debugging easier. This is because debugging tools can easily access standard elements.

in conclusion

You can create your own elements in HTML documents. However, they do not have any additional semantics or functionality. Furthermore, these elements cannot be globally understood or used by all developers. Therefore, it is better to use standard HTML elements, which have several advantages over fake elements.

The above is the detailed content of Why can CSS use fake elements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete