搜索

现代CSS卡片

Aug 23, 2023 pm 01:25 PM

现代CSS卡片

如今,在网站上创建卡片非常重要,可以在网站上展示各种数据。例如,在TutorialsPoint网站的首页上,您会发现以卡片格式展示不同的课程,当您点击卡片时,它会将您重定向到该课程的特定页面。

此外,如果您访问任何电子商务商店,如亚马逊或Flipkart,它们会以卡片格式显示产品。创建卡片的主要好处是我们可以在图片上显示产品的简短信息,并在产品页面上提供完整信息。

在本教程中,我们将学习仅使用HTML和CSS创建不同的卡片。

Example 1 (Basic CSS Cards)

In the example below, we created the ‘card’ div element containing the single image and the ‘card-content’ div element. The ‘card-content’ div element contains the text information.

In CSS, we set the fixed dimensions for the card element. Also, we have given styles such as background color, border radius, and border, etc. Also, we apply the box-shadow effect on the card when users hover over the card.

Furthermore, we fixed the dimensions for the image and set the border radius for the top corners. Also, we set the font size of the text content. In the output, users can observe the resultant card.

<html>
<head>
   <style>
      .card {
         display: flex;
         flex-direction: column;
         border-radius: 5px;
         box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
         transition: box-shadow 0.3s ease-in-out;
         width: 18rem;
         background-color: #fff;
      }
      .card:hover { box-shadow: 0 8px 16px rgba(0, 0, 0, 0.6);}
      .card>img {
         border-radius: 5px 5px 0 0;
         height: 150px;
         width: 100%;
         object-fit: contain;
      }
      .card-content { padding: 20px;}
      .card h3 { font-size: 1.4rem; margin-top: 0;}
      .card p { font-size: 1rem; margin-bottom: 10px;
      }
      .card a {
         padding: 10px 20px;
         background-color: #222;
         border-radius: 10px;
         color: white;
         text-align: center;
         display: inline-block;
         text-decoration: none;
         transition: background-color 0.4s ease-in-out;
      }
      .card a:hover { background-color: #4b4a4a;}
   </style>
</head>
<body>
   <h2 id="Creating-the-i-basic-cards-i-using-the-CSS"> Creating the <i> basic cards </i> using the CSS </h2>
   <div class = "card">
      <img src = "https://www.tutorialspoint.com/static/images/logo.png?v2" alt = "Logo">
      <div class = "card-content">
         <h3 id="Python"> Python </h3>
         <p> Python course by Shubham Vora </p>
         <a href = "#"> Join now </a>
      </div>
   </div>
</body>
</html>

Example 2

的中文翻译为:

示例2

我们在下面的示例中创建了一个类似于第一个示例的卡片。在这里,我们为“card-image” div元素设置了背景图片。同时,我们通过从“Picsum”网站获取随机图片来设置图片。在这个卡片中,我们没有像第一个示例中那样添加“立即加入”的锚点标签。

<html>
<head>
   <style>
      .card {
         display: flex;
         flex-direction: column;
         width: 20rem;
         background-color: white;
         border-radius: 10px;
         box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
      }
      .card-image {
         height: 200px;
         background-image: url("https://picsum.photos/300/200");
         background-size: cover;
         border-radius: 10px 10px 0 0;
      }
      .card-content { padding: 20px;}
      .card-title {
         font-size: 1.5rem;
         font-weight: bold;
         margin: 0 0 10px 0;
      }
      .card-text { font-size: 1rem; margin: 0; }
   </style>
</head>
<body>
   <h2 id="Creating-the-i-basic-cards-i-using-the-CSS"> Creating the <i> basic cards </i> using the CSS. </h2>
   <div class = "card">
      <div class = "card-image"> </div>
      <div class = "card-content">
         <h2 id="Random-Image"> Random Image</h2>
         <p class = "card-text"> This is an random image description. </p>
      </div>
   </div>
</body>
</html>

Example 3

在下面的示例中,我们在卡片上添加了悬停效果,以显示额外的信息。

Here, we created the card container first to create a normal card and styled it using the CSS with ‘position: relative.’ We added the ‘card-first’ and ‘card-second’ div elements inside the card container. The ‘card-first’ div element contains the information on the card, and the ‘card-second’ div element contains the information to show whenever users hover over the card.

Furthermore, we set the dimensions for CSS's ‘card-first’ div element. Also, we set dimensions for the ‘card-second’ div element in the CSS and used the ‘transform: translate(100%)’ CSS property to hide the second part. When users hover over the card element, we use the ‘transform: translateX(-100%)’ CSS property to show the second part of the card.

<html>
<head>
   <style>
      .card {
         position: relative;
         width: 300px;
         height: 200px;
         background-color: rgb(64, 64, 247);
         color: white;
         border-radius: 10px;
         box-shadow: 0px 3px 7px rgba(0, 0, 0, 0.4);
         overflow: hidden;
      }
      .card-first {
         position: absolute;
         width: 100%;
         height: 100%;
         padding: 20px;
         font-size: 1.7rem;
         transition: transform 0.5s ease-in-out;
      }
      .card-second {
         position: absolute;
         top: 0;
         left: 0;
         width: 100%;
         height: 100%;
         padding: 20px;
         transform: translateX(100%);
         transition: transform 0.5s ease-in-out;
      }
      .card:hover .card-first { transform: translateX(-100%);}
      .card:hover .card-second { transform: translateX(0%); }
   </style>
</head>
<body>
   <h2 id="Creating-the-i-hover-effect-on-the-card-i-to-show-additional-information"> Creating the <i> hover effect on the card </i> to show additional information. </h2>
   <div class = "card">
      <div class = "card-first">
         <h3 id="Samsung-s"> Samsung s22 </h3>
         <p> 1,01,000 INR </p>
      </div>
      <div class = "card-second">
         <p> 6.4 inch display </p>
         <p> 8 GB RAM </p>
         <p> 128 GB Storage </p>
         <p> 64 MP Camera </p>
      </div>
   </div>
</body>
</html>

Example 4

在下面的示例中,我们创建了一个名为'parent'的div元素。之后,我们添加了多个包含图片和卡片描述的卡片元素。

在CSS中,我们使用clamp()函数来设置卡片的宽度。clamp()函数接受三个参数。第一个是最小值,第二个是百分比值,第三个是最大值。在我们的例子中,如果屏幕尺寸的20%在300和500像素之间,卡片宽度将为20%。否则,它将是300像素或500像素。

此外,我们将“parent”容器设置为flexbox以正确显示所有卡片。

<html>
<head>
   <style>
      .parent {
         padding: 30px 5%;
         display: flex;
         flex-wrap: wrap;
         justify-content: space-between;
      }
      .card {
         position: relative;
         margin: 20px;
         width: clamp(230px, 20%, 500px);
         height: 250px;
         background-color: green;
         color: white;
         border-radius: 10px;
         transition: all 0.3s ease;
      }
      .card img {
         width: 100%;
         height: 150px;
         border-radius: 10px 10px 0 0;
         object-fit: cover;
      }
      .card-text {
         padding: 20px;
         text-align: center;
         position: absolute;
         bottom: 0;
         left: 0;
         right: 0;
         transition: all 0.3s ease;
      }
      .card-text h3 { font-size: 24px; margin-bottom: 10px;}
      .card-text p { font-size: 16px; margin-bottom: 0;}
   </style>
</head>
<body>
   <h3 id="Creating-the-i-card-with-clamp-function-i-to-manage-card-dimensions-according-to-the-screen-dimensions"> Creating the <i> card with clamp() function </i> to manage card dimensions according to the screen dimensions </h3>
   <div class = "parent">
      <div class = "card">
         <img src = "https://picsum.photos/300/200" alt = "Random image">
         <div class = "card-text">
            <h2 id="Card"> Card 1 </h2>
            <p> This is a card description. </p>
         </div>
      </div>
      <div class = "card">
         <img src = "https://picsum.photos/300/200" alt = "Random image">
         <div class = "card-text">
            <h2 id="Card"> Card 2 </h2>
            <p> This is a card description. </p>
         </div>
      </div>
   </div>
</body>
</html>

Users learned to create modern cards using HTML and CSS. In the first two examples, we created the basic cards; in the third example, we created the cards with the hover effect. Also, we learned to use the clamp() function to handle the card size according to the device’s screen dimensions.

以上是现代CSS卡片的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
有点提醒您伪元素是孩子,有点。有点提醒您伪元素是孩子,有点。Apr 19, 2025 am 11:39 AM

这里有一个带子元素的容器:

菜单具有'动态命中区域”菜单具有'动态命中区域”Apr 19, 2025 am 11:37 AM

飞翔的菜单!您需要在第二个菜单中实现悬停事件以显示更多菜单项的菜单,即您在棘手的领域中。一方面,他们应该

通过Webvtt改善视频可访问性通过Webvtt改善视频可访问性Apr 19, 2025 am 11:27 AM

“网络的力量在于其普遍性。每个人的访问无论残疾是一个重要方面。” - 蒂姆·伯纳斯 - 李

每周平台新闻:CSS :: Marker伪元素,预先渲染的Web组件,向您的网站添加Webmention每周平台新闻:CSS :: Marker伪元素,预先渲染的Web组件,向您的网站添加WebmentionApr 19, 2025 am 11:25 AM

在本周的综述中:datepickers正在让键盘用户头痛,一个新的Web组件编译器,有助于与Fouc进行战斗,我们终于获得了造型列表项目标记,以及在您的网站上获得网络攻击的四个步骤。

使宽度和灵活的物品一起玩得很好使宽度和灵活的物品一起玩得很好Apr 19, 2025 am 11:23 AM

简短的答案:您可能要寻找的是弹性折射和弹性基础。

位置粘性和桌子标头位置粘性和桌子标头Apr 19, 2025 am 11:21 AM

您可以位置:粘性;一个

每周平台新闻:HTML在搜索控制台,全局脚本范围中的HTML检查,Babel Envs添加默认查询查询每周平台新闻:HTML在搜索控制台,全局脚本范围中的HTML检查,Babel Envs添加默认查询查询Apr 19, 2025 am 11:18 AM

在本周的Web平台新闻世界中,Google搜索控制台可以更轻松地查看爬行的标记,我们了解到自定义属性

Indieweb和网络企业Indieweb和网络企业Apr 19, 2025 am 11:16 AM

Indieweb是一回事!他们将举行会议和一切。纽约客甚至在写这件事:

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用