前端学习(3)—— CSS实现热搜榜

发布于:2025-05-14 ⋅ 阅读:(14) ⋅ 点赞:(0)

效果展示

具体的展示效果如下,可以直接在浏览器显示:

页面分为两部分,一部分是 body 标签里的 html 结构,一部分是 style 标签里的CSS代码(页面布局的部分数据直接在代码里显示了) 

一,html结构

除去CSS后的单独html页面如下:

这个就有点像我们加载某些网页没加载完就中断加载后,页面显示的样子,我们访问一些国外网站时容易遇到,因为在实际开发中是把 css 和 html 分开的,两个单独用文件实现的,不过我们这个热搜比较简单,就直接嵌套在 head 标签里了

html代码如下:

<body>
    <table cellspacint="0px"> <!--这个属性表示单元格之间的空间-->
        <th class="title col-1">热搜</th>
        <th class="title col-2"><a href="#">换一换<span class="icon"></span></a></th>
        <tr class="content">
            <td class="col-1"><span class="one">1</span><a href="https://github.com/">GitHub</a></td>
            <td class="col-2">666万</td>
        </tr>
        <tr class="content">
            <td class="col-1"><span class="two">2</span><a href="https://www.csdn.net/">CSDN</a></td>
            <td class="col-2">666万</td>
        </tr>
        <tr class="content">
            <td class="col-1"><span class="three">3</span><a href="https://gitee.com/">Gitee</a></td>
            <td class="col-2">666万</td>
        </tr>
        <tr class="content">
            <td class="col-1"><span class="four">4</span><a href="https://leetcode.cn/">LeetCode</a></td>
            <td class="col-2">666万</td>
        </tr>
    </table>
</body>
  • 首先和个人简历页面一样使用table标签来布局,并设置单元格之间距离为0
  •  然后就是四个行,每一行包括两个单元格,其中排行榜数字和链接是紧挨着的,所以共用一个td标签,表示一个单元格;而后面的“666万”,需要和链接保持一定距离,所以单独用一个 td 标签
  • 对于每一行的两个td,分别用“col-1”和“col-2”来区分
  • 然后就是一些a超链接标签的简单替换,这里不再赘述

二,CSS美化

2.1 标题

html页面中的热搜是在单元格中居中显示的,而在总效果中是居左显示的:

.col-1{
            width: 80%;
            text-align: left;
            /*居左*/
        }

2.2 刷新按钮

  • 首先刷新按钮位于单元格中间
  • 然后就是刷新按钮的图标,也可以在这个网站里下载:iconfont-阿里巴巴矢量图标库
  • 再然后就是刷新按钮和热搜按钮一样是粗体
        .col-2 {
            width: 20%;
            text-align: center;
        }
        .icon {
            background-image: url(./111.png);
            width: 24px;
            height: 24px;
            background-size: 100% 100%;
            display: inline-block;
            /*加上后图片才能显示出来*/
            vertical-align: bottom;
            /*使垂直对齐*/
        }
        .title .col-1 {
            font-size: 20px;
            font-weight: bolder;
        }

2.3 序号

  • 首先观察序号,背景颜色的数值我们暂时不关心,可以看到数字都是白色的,所以我们直接设置数字为白色
  • 然后我们直接通过padding属性设置内边距然后填充颜色即可,这里我们左右边距都设置4像素,这样我们的白色数字就显示在中间了
        .num {
            font-size: 20px;
            color: #fffff3;
        }
        #one {
            background-color: #f54545;
            padding-right: 4px;
            padding-left: 4px;
        }

        #two {
            background-color: #ff8547;
            padding-right: 4px;
            padding-left: 4px;
        }

        #three {
            background-color: #ffac38;
            padding-right: 4px;
            padding-left: 4px;
        }

        #four {
            background-color: #81b9f5;
            padding-right: 4px;
            padding-left: 4px;
        }

2.4 超链接

  • 超链接字体为蓝色,可以根据需要设置粗体
  • 然后热搜下面有下划线
  • 然后对于链接的集中操作显示的颜色,可以使用链接伪类选择器设置,这里就不设置了,可以前往上篇文章的复合选择器部分:前端学习(2)—— CSS详解与使用-CSDN博客
        a {
            color: blue;
            text-decoration: none; /*这里取消掉超链接的划线,因为会和下面的下划线重复*/
        }
        a:hover {
            text-decoration: underline;
        }

        .content .col-1,
        .content .col-2 {
            /*给每一行的下面的下划线设置颜色*/
            border-bottom: 2px solid #f3f3f3;
        }

三,完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>热搜</title>
    <style>
        .content {
            font-size: 18px;
            line-height: 30px;
        }
        table {
            width: 536px
        }
        .col-1{
            width: 80%;
            text-align: left;
            /*居左*/
        }
        .col-2 {
            width: 20%;
            text-align: center;
        }
        .icon {
            background-image: url(./111.png);
            width: 24px;
            height: 24px;
            background-size: 100% 100%;
            display: inline-block;
            /*加上后图片才能显示出来*/
            vertical-align: bottom;
            /*使垂直对齐*/
        }
        .title .col-1 {
            font-size: 20px;
            font-weight: bolder;
        }
        .num {
            font-size: 20px;
            color: #fffff3;
        }
        #one {
            background-color: #f54545;
            padding-right: 4px;
            padding-left: 4px;
        }

        #two {
            background-color: #ff8547;
            padding-right: 4px;
            padding-left: 4px;
        }

        #three {
            background-color: #ffac38;
            padding-right: 4px;
            padding-left: 4px;
        }

        #four {
            background-color: #81b9f5;
            padding-right: 4px;
            padding-left: 4px;
        }
        a {
            color: blue;
            text-decoration: none; /*这里取消掉超链接的划线,因为会和下面的下划线重复*/
        }
        a:hover {
            text-decoration: underline;
        }

        .content .col-1,
        .content .col-2 {
            /*给每一行的下面的下划线设置颜色*/
            border-bottom: 2px solid #f3f3f3;
        }

    </style>
</head>

<body>
    <table cellspacint="0px"> <!--这个属性表示单元格之间的空间-->
        <th class="title col-1">热搜</th>
        <th class="title col-2"><a href="#">换一换<span class="icon"></span></a></th>
        <tr class="content">
            <td class="col-1"><span class="num" id="one">1</span><a href="https://github.com/">GitHub</a></td>
            <td class="col-2">666万</td>
        </tr>
        <tr class="content">
            <td class="col-1"><span class="num" id="two">2</span><a href="https://www.csdn.net/">CSDN</a></td>
            <td class="col-2">666万</td>
        </tr>
        <tr class="content">
            <td class="col-1"><span class="num" id="three">3</span><a href="https://gitee.com/">Gitee</a></td>
            <td class="col-2">666万</td>
        </tr>
        <tr class="content">
            <td class="col-1"><span class="num" id="four">4</span><a href="https://leetcode.cn/">LeetCode</a></td>
            <td class="col-2">666万</td>
        </tr>
    </table>
</body>

</html>