📜  如何使用 HTML 和 CSS 创建透视文本?

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

在本文中,我们将在主图像下方创建图像错觉。这是一篇简单的文章,我们只能通过使用 HTML 和 CSS 来实现我们的目标。

方法:创建一个主 div,我们在其中添加了一个标题标签,然后使用 CSS 中的选择器来创建效果。

HTML代码:

  1. 首先,我们创建一个 HTML 文件(index.html)。
  2. 创建 HTML 文件后,我们将使用 标签为我们的网页提供标题。它应该放在 <head> 标签内。</li> <li>然后我们将提供所有动画效果的 CSS 文件链接到我们的 html。它也被放置在 <head> 标签内。</li> <li>来到我们 HTML 代码的正文部分。 <ol> <li>首先,我们正在向我们的页面提供标题。</li> <li>在标题内创建 3 个跨度来存储数据。</li> </ol> </li> </ol> <div class="noIdeBtnDiv"> <div> <h5 class="code">HTML</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace"><!DOCTYPE html> <html lang="en"> <head>     <link rel="stylesheet" href="style.css"> </head> <body>     <h1>         <span>geeks</span>         <span>for</span>         <span>geeks</span>     </h1> </body> </html></code></pre> </div> <p></p> <hr/> <h5 class="code">CSS</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">*{     margin: 0;     padding: 0;     box-sizing: border-box; }    body{     height: 100vh;     margin: 0;     display: flex;     justify-content: center;     align-items: center;     background: #000;     color: teal; }    h1{     width: 3em;     height: 1em;     font-size: 6em;     position: relative;     white-space: nowrap;     color: transparent; }    h1 span{     position: absolute;     background-color: rgb(7, 138, 138);     color: #000; }    span:nth-child(odd){     transform: skew(50deg,-20deg) scaleY(0.75); }    span:nth-child(even){     transform: skew(-5deg,-20deg); }    span:nth-child(1){     bottom: 1em;     left: 0.5em; } span:nth-child(2){     top: 0.5em;     left: 2em; }    span:nth-child(3){     top: 2em;     left: 2.5em; }</code></pre> </div> <p></p> <hr/> </div> </div> <p> <strong>CSS 代码:</strong> CSS 用于为我们的 HTML 页面提供不同类型的动画和效果,使其看起来对所有用户都是交互的。在 CSS 中,我们必须记住以下几点——</p> <ul> <li>恢复所有浏览器效果。</li> <li>使用类和 id 为 HTML 元素赋予效果。</li> <li>使用选择器来选择不同的元素!</li> </ul> <div class="noIdeBtnDiv"> <div class="responsive-tabs"> <h2 class="tabtitle"> CSS </h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace">*{     margin: 0;     padding: 0;     box-sizing: border-box; }    body{     height: 100vh;     margin: 0;     display: flex;     justify-content: center;     align-items: center;     background: #000;     color: teal; }    h1{     width: 3em;     height: 1em;     font-size: 6em;     position: relative;     white-space: nowrap;     color: transparent; }    h1 span{     position: absolute;     background-color: rgb(7, 138, 138);     color: #000; }    span:nth-child(odd){     transform: skew(50deg,-20deg) scaleY(0.75); }    span:nth-child(even){     transform: skew(-5deg,-20deg); }    span:nth-child(1){     bottom: 1em;     left: 0.5em; } span:nth-child(2){     top: 0.5em;     left: 2em; }    span:nth-child(3){     top: 2em;     left: 2.5em; } </code></pre> </div> </div> </div> </div> <p><strong>输出:</strong> </p> <p><img class="img-fluid" sizes="100vw" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/How_to_create_Perspective_Text_using_HTML_&_CSS_?_1.jpg" srcset="https://media.geeksforgeeks.org/wp-content/uploads/20210413002825/Screenshot20210413002201-660x415.png 660w, https://media.geeksforgeeks.org/wp-content/uploads/20210413002825/Screenshot20210413002201-768x483.png 768w, https://media.geeksforgeeks.org/wp-content/uploads/20210413002825/Screenshot20210413002201-1024x643.png 1024w, https://media.geeksforgeeks.org/wp-content/uploads/20210413002825/Screenshot20210413002201.png," width="1087"/></p> <div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7"></div> <p></p></div> </div> </div> </div> </div> </div> <footer> <div class="bg-white text-center pb-1"> <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有 <br> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a> <br> Made with ❤️ in Chengdu </p> </div> </footer> <!-- 引入Bootstrap JavaScript库 --> <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <!-- 引入Meilisearch搜索相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script> <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script> <!-- 引入prismjs代码高亮相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script> </body> </html>