📜  如何使用 HTML 和 CSS 创建图像手风琴?

📅  最后修改于: 2021-11-09 09:10:53             🧑  作者: Mango

在本文中,我们将使用 HTML 和 CSS 创建一个图像手风琴,它基本上用于电子商务网站上的广告目的。

方法:

  • 创建一个 HTML 文件,我们将在其中添加不同类型的广告图像。
  • 创建一个 CSS 样式,为网页元素提供一些动画效果。

HTML代码:

  1. 首先,创建一个 HTML 文件 (index.html)。
  2. 现在在创建我们的 HTML 文件之后,我们将使用 标签为我们的网页提供一个标题。它应该放在 <head> 标签内。</li> <li>然后将提供所有动画效果的 CSS 文件链接到我们的 HTML。这也位于 <head> 标记之间。</li> <li>来到我们 HTML 代码的正文部分。 <ol> <li>然后我们必须添加不同的广告。</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>     <div class="main_box">         <div class="img img1">             <p>gfg 1</p>         </div>            <div class="img img2">             <p>gfg 2</p>         </div>            <div class="img img3">             <p>gfg 3</p>         </div>            <div class="img img4">             <p>gfg 4</p>         </div>     </div> </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">/* Restoring browser properties */ *{     margin: 0;     padding: 0;     box-sizing: border-box;     color: white; }    body{     background-color: rgb(0, 0, 0);     height: 100vh;     display: flex;     justify-content: center;     align-items: center; }    .main_box{     width: 90%;     height: 80vh;     display: flex; }    .img{     flex: 1;     height: 100%;     transform: skew(10deg);     cursor: pointer;     margin: 0 0.125em;     transition: all .3s;     background-repeat: no-repeat;     background-size: cover;     background-position: center;     border: 1px solid pink;     position: relative; }    p{     position: absolute;     bottom: 0;     left: 0;     padding: .75em;     background-color: rgba(0,0,0,0.6);     transform: rotate(-90deg);     transform-origin: 0% 0%;     transition: all 0.3s;     text-transform: uppercase;     white-space: nowrap; }    .img1 {     background-image: url(gfg1.jpg); }    .img2 {     background-image: url(gfg2.png); }    .img3 {     background-image: url(gfg3.jpg); }    .img4 {     background-image: url(gfg4.jpg); }    .main_box:hover .img{     transform: skew(0); }    .img:hover{     flex: 5; }    .img:hover p{     background-color: transparent;     border: .125em solid blue;     color: rgb(255, 38, 0);     transform: rotate(0deg); }</code></pre> </div> <p></p> <hr/> <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>     <style>         /* Restoring browser properties */         * {             margin: 0;             padding: 0;             box-sizing: border-box;             color: white;         }                    body {             background-color: rgb(0, 0, 0);             height: 100vh;             display: flex;             justify-content: center;             align-items: center;         }                    .main_box {             width: 90%;             height: 80vh;             display: flex;         }                    .img {             flex: 1;             height: 100%;             transform: skew(10deg);             cursor: pointer;             margin: 0 0.125em;             transition: all .3s;             background-repeat: no-repeat;             background-size: cover;             background-position: center;             border: 1px solid pink;             position: relative;         }                    p {             position: absolute;             bottom: 0;             left: 0;             padding: .75em;             background-color: rgba(0, 0, 0, 0.6);             transform: rotate(-90deg);             transform-origin: 0% 0%;             transition: all 0.3s;             text-transform: uppercase;             white-space: nowrap;         }                    .img1 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121103/resize16136304481344613575img2.jpg);         }                    .img2 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121132/resize16136304801548620839img1.png);         }                    .img3 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121200/resize1613630503649291282img3.jpg);         }                    .img4 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121224/resize16136305331326755683img4.jpg);         }                    .main_box:hover .img {             transform: skew(0);         }                    .img:hover {             flex: 5;         }                    .img:hover p {             background-color: transparent;             border: .125em solid blue;             color: rgb(255, 38, 0);             transform: rotate(0deg);         }     </style> </head>    <body>     <div class="main_box">         <div class="img img1">             <p>gfg 1</p>         </div>            <div class="img img2">             <p>gfg 2</p>         </div>            <div class="img img3">             <p>gfg 3</p>         </div>            <div class="img img4">             <p>gfg 4</p>         </div>     </div> </body>    </html></code></pre> </div> <p></p> <hr/> </div> </div> <p> <strong>CSS 代码:</strong> CSS 用于为我们的 HTML 页面提供不同类型的动画和效果,使其看起来对所有用户都是交互的。</p> <p></p> <div class="_ap_apex_ad" id="d6f45c53-ff16-4cc5-8b91-eb6ee7037116" style="min-height:280px"></div> <p></p> <p>在CSS中,我们要提醒以下几点——</p> <ul> <li>恢复所有浏览器效果。</li> <li>使用类和 id 为 HTML 元素赋予效果。</li> <li>使用 :hover 来使用悬停效果。</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">/* Restoring browser properties */ *{     margin: 0;     padding: 0;     box-sizing: border-box;     color: white; }    body{     background-color: rgb(0, 0, 0);     height: 100vh;     display: flex;     justify-content: center;     align-items: center; }    .main_box{     width: 90%;     height: 80vh;     display: flex; }    .img{     flex: 1;     height: 100%;     transform: skew(10deg);     cursor: pointer;     margin: 0 0.125em;     transition: all .3s;     background-repeat: no-repeat;     background-size: cover;     background-position: center;     border: 1px solid pink;     position: relative; }    p{     position: absolute;     bottom: 0;     left: 0;     padding: .75em;     background-color: rgba(0,0,0,0.6);     transform: rotate(-90deg);     transform-origin: 0% 0%;     transition: all 0.3s;     text-transform: uppercase;     white-space: nowrap; }    .img1 {     background-image: url(gfg1.jpg); }    .img2 {     background-image: url(gfg2.png); }    .img3 {     background-image: url(gfg3.jpg); }    .img4 {     background-image: url(gfg4.jpg); }    .main_box:hover .img{     transform: skew(0); }    .img:hover{     flex: 5; }    .img:hover p{     background-color: transparent;     border: .125em solid blue;     color: rgb(255, 38, 0);     transform: rotate(0deg); } </code></pre> </div> </div> </div> </div> <p><strong>最终代码:</strong></p> <div class="responsive-tabs"> <h2 class="tabtitle"> HTML </h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace"><!DOCTYPE html> <html lang="en">    <head>     <style>         /* Restoring browser properties */         * {             margin: 0;             padding: 0;             box-sizing: border-box;             color: white;         }                    body {             background-color: rgb(0, 0, 0);             height: 100vh;             display: flex;             justify-content: center;             align-items: center;         }                    .main_box {             width: 90%;             height: 80vh;             display: flex;         }                    .img {             flex: 1;             height: 100%;             transform: skew(10deg);             cursor: pointer;             margin: 0 0.125em;             transition: all .3s;             background-repeat: no-repeat;             background-size: cover;             background-position: center;             border: 1px solid pink;             position: relative;         }                    p {             position: absolute;             bottom: 0;             left: 0;             padding: .75em;             background-color: rgba(0, 0, 0, 0.6);             transform: rotate(-90deg);             transform-origin: 0% 0%;             transition: all 0.3s;             text-transform: uppercase;             white-space: nowrap;         }                    .img1 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121103/resize16136304481344613575img2.jpg);         }                    .img2 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121132/resize16136304801548620839img1.png);         }                    .img3 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121200/resize1613630503649291282img3.jpg);         }                    .img4 {             background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20210218121224/resize16136305331326755683img4.jpg);         }                    .main_box:hover .img {             transform: skew(0);         }                    .img:hover {             flex: 5;         }                    .img:hover p {             background-color: transparent;             border: .125em solid blue;             color: rgb(255, 38, 0);             transform: rotate(0deg);         }     </style> </head>    <body>     <div class="main_box">         <div class="img img1">             <p>gfg 1</p>         </div>            <div class="img img2">             <p>gfg 2</p>         </div>            <div class="img img3">             <p>gfg 3</p>         </div>            <div class="img img4">             <p>gfg 4</p>         </div>     </div> </body>    </html> </code></pre> </div> </div> </div> <h4>输出: </h4> <p><img class="img-fluid" sizes="100vw" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/How_to_Create_Image_Accordion_using_HTML_and_CSS_?_0.jpg" srcset="https://media.geeksforgeeks.org/wp-content/uploads/20210219153830/20210219153708.gif" width="1180"/></p> <p><i>这样,您就可以创建自己的广告版块了!。</i> </p> <div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7" style="min-height:280px"></div> <p></p></div> </article> </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>