1. HTML 结构:搭建网页的骨架,定义了哪些元素存在。
  2. CSS 样式:为骨架穿上“衣服”,定义了元素的外观、位置和颜色。

最终效果预览

我们将实现一个与百度首页非常相似的界面,包括顶部的导航栏、中间的搜索框和按钮,以及底部的其他链接。

htmlcss仿造百度网页
(图片来源网络,侵删)

第一步:创建 HTML 文件 (index.html)

创建一个名为 index.html 的文件,这是整个页面的基础。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">百度一下,你就知道</title>
    <!-- 引入我们的 CSS 文件 -->
    <link rel="stylesheet" href="style.css">
    <!-- 引入一个流行的图标库,用于显示“设置”等图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="top-nav">
        <div class="nav-left">
            <a href="#" class="nav-link">新闻</a>
            <a href="#" class="nav-link">hao123</a>
            <a href="#" class="nav-link">地图</a>
            <a href="#" class="nav-link">视频</a>
            <a href="#" class="nav-link">贴吧</a>
            <a href="#" class="nav-link">学术</a>
            <a href="#" class="nav-link">更多</a>
        </div>
        <div class="nav-right">
            <a href="#" class="nav-link">登录</a>
            <a href="#" class="nav-link">
                <i class="fas fa-cog"></i> 设置
            </a>
        </div>
    </div>
    <!-- 主要内容区 (Logo, 搜索框, 按钮) -->
    <div class="main-content">
        <!-- 百度 Logo -->
        <div class="logo">
            <img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt="百度Logo">
        </div>
        <!-- 搜索框区域 -->
        <div class="search-box">
            <div class="input-wrapper">
                <input type="text" class="search-input" placeholder="请输入搜索内容">
                <button class="camera-btn">
                    <i class="fas fa-camera"></i>
                </button>
            </div>
            <div class="search-buttons">
                <button class="search-btn">百度一下</button>
            </div>
        </div>
        <!-- 搜索提示 (可选) -->
        <div class="search-tips">
            <span>百度一下,你就知道</span>
        </div>
    </div>
    <!-- 底部链接区域 -->
    <div class="footer">
        <div class="footer-content">
            <div class="footer-links">
                <a href="#" class="footer-link">关于百度</a>
                <a href="#" class="footer-link">使用百度前必读</a>
                <a href="#" class="footer-link">意见反馈</a>
                <a href="#" class="footer-link">投诉中心</a>
            </div>
            <div class="footer-info">
                <span>&copy;2025 Baidu</span>
                <span><a href="#">使用百度前必读</a></span>
                <span><a href="#">意见反馈</a></span>
                <span>京ICP证030173号</span>
            </div>
        </div>
    </div>
</body>
</html>

HTML 代码解析:

  • <!DOCTYPE html><html>: 定义文档类型和根元素。
  • <head>: 包含页面的元数据,如字符集、标题、引入的 CSS 文件等。
  • <link rel="stylesheet" href="style.css">: 这是关键,它告诉浏览器去加载一个名为 style.css 的文件来美化我们的页面。
  • <link rel="stylesheet" href=".../font-awesome...">: 引入 Font Awesome 图标库,方便我们使用一些小图标。
  • <body>: 页面所有可见内容的容器。
  • <div>: 一个通用的容器,用于布局和分组元素,我们给它添加了 class 属性(如 class="top-nav"),这样 CSS 就可以针对这些容器进行样式设置。
  • <a>: 超链接标签,href="#" 表示一个占位符链接。
  • <img>: 图片标签,src 指向图片地址,alt 是图片的替代文本(对SEO和无障碍访问很重要)。
  • <input>: 输入框,type="text" 定义为文本输入,placeholder 定义了输入框内的提示文字。
  • <button>: 按钮标签。

第二步:创建 CSS 文件 (style.css)

在与 index.html 相同的目录下,创建一个名为 style.css 的文件,这个文件将负责所有的样式设计。

/* 全局重置和基础样式 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 13px;
    color: #333;
    line-height: 1.6;
}
a {
    text-decoration: none;
    color: #333;
    transition: color 0.2s;
}
a:hover {
    color: #4169E1; /* 鼠标悬停时变为蓝色 */
}
/* 顶部导航栏样式 */
.top-nav {
    height: 40px;
    font-size: 13px;
    line-height: 40px;
    border-bottom: 1px solid #eee;
}
.nav-left, .nav-right {
    position: absolute;
    top: 0;
}
.nav-left {
    left: 20px;
}
.nav-right {
    right: 20px;
}
.nav-link {
    margin: 0 10px;
    color: #333;
}
区样式 */
.main-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    height: 60vh; /* 使用视口高度,使其垂直居中 */
    padding-top: 100px; /* 给顶部导航栏留出空间 */
}
.logo img {
    width: 270px;
    height: 129px;
    margin-bottom: 40px;
}
/* 搜索框样式 */
.search-box {
    width: 640px;
    margin: 0 auto;
}
.input-wrapper {
    display: flex;
    border: 2px solid #4E6EF2;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.search-input {
    flex-grow: 1;
    height: 44px;
    border: none;
    outline: none;
    padding: 0 16px;
    font-size: 16px;
}
.camera-btn {
    background: none;
    border: none;
    color: #777;
    padding: 0 16px;
    cursor: pointer;
    font-size: 18px;
}
.search-buttons {
    margin-top: 20px;
    text-align: center;
}
.search-btn {
    background-color: #4E6EF2;
    color: white;
    border: none;
    padding: 10px 30px;
    font-size: 17px;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.2s;
}
.search-btn:hover {
    background-color: #4169E1;
}
/* 搜索提示样式 */
.search-tips {
    margin-top: 30px;
    color: #777;
    font-size: 13px;
}
/* 底部链接样式 */
.footer {
    text-align: center;
    padding: 20px 0;
    border-top: 1px solid #eee;
    margin-top: auto; /* 推到底部 */
}
.footer-content {
    max-width: 960px;
    margin: 0 auto;
}
.footer-links {
    margin-bottom: 10px;
}
.footer-link {
    margin: 0 10px;
    color: #777;
}
.footer-info {
    color: #999;
}
.footer-info span {
    margin: 0 5px;
}

CSS 代码解析:

htmlcss仿造百度网页
(图片来源网络,侵删)
  • * { margin: 0; padding: 0; }: 这是常见的“CSS Reset”,用于清除浏览器默认的内外边距,让我们的布局更可控。
  • body: 设置全局字体、颜色和行高。
  • a:hover: 为所有链接添加了鼠标悬停效果,颜色变为蓝色,提升用户体验。
  • .top-nav: 设置了固定高度、下边框,并使用 position: absolute 将左右两侧的链接定位到两端。
  • .main-content:
    • display: flex; flex-direction: column; align-items: center; justify-content; center: 这是现代 CSS 布局的精髓,它让 Logo、搜索框和按钮都能完美地垂直水平居中。
    • height: 60vh;: 使用视口高度(Viewport Height)的 60% 作为高度,确保在不同屏幕尺寸下都能居中。
    • padding-top: 100px;: 为了防止内容被顶部的导航栏遮挡,给顶部添加了内边距。
  • .logo img: 设置了 Logo 图片的宽高,使其不变形。
  • .search-box:
    • .input-wrapper: 使用 display: flex 让输入框和相机图标在同一行。
    • border-radius: 10px;: 为搜索框添加圆角。
    • box-shadow: 添加了轻微的阴影,让搜索框更有立体感。
    • .search-input: flex-grow: 1; 让输入框占据所有剩余空间。
    • .search-input, .camera-btn: 移除了输入框和按钮的默认边框,让它们看起来更统一。
  • .footer: 使用 margin-top: auto; 配合 .main-contentflex 布局,将页脚始终推到页面最底部。

如何运行

  1. 将上述 index.htmlstyle.css 的代码分别保存到两个文件中,并确保它们在同一个文件夹下。
  2. 用你的任意一个现代浏览器(如 Chrome, Firefox, Edge)打开 index.html 文件。
  3. 你就能看到一个仿照百度首页的网页了。

总结与扩展

这个仿造版本已经非常接近百度首页的核心布局和样式了,如果你想进一步学习和扩展,可以尝试:

  • 响应式设计:使用 媒体查询(Media Queries) 让页面在手机、平板和电脑上都能完美显示。
  • 添加更多交互:使用 JavaScript 为搜索框添加自动完成(Autocomplete)功能,或者让“更多”链接点击后展开一个下拉菜单。
  • 细节优化:调整颜色、字体大小、间距等,使其与百度官方的视觉效果更加接近。
htmlcss仿造百度网页
(图片来源网络,侵删)