网站代码怎么写:结构梳理、四步搭建与齐全示例

网站代码怎么写:结构梳理、四步搭建与齐全示例
2026-10-04 05:43:15 大河网 作者 周一早盘:美股期货走高 大型科技公司财报即将颁布 美联储降息预期强烈 加拿大从美国挖走大量学者 水均益 新浪网官方账号

想搭起一个能展示工作室、介绍文章、适配手机并带有单一交互的网站,,, ,,,,,能够从下面这6段代码起头。。。。。它们别离掌管HTML骨架、基础形状、文章卡片、移动端布局、导航菜单和表单反馈。。。。。按挨次放入对应文件,,, ,,,,,就能组成一个简洁的静态展示站;;; ;;;;;也能够先选用其中一段,,, ,,,,,再逐步补齐其他职能。。。。。

1. HTML骨架:铺排导航、介绍区和联系表单

先新建 index.html,,, ,,,,,放入网站的主体结构。。。。。导航链接别离跳转到带有对应 id 的区块,,, ,,,,,访客还能够在联系表单中填写邮箱。。。。。

<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>简洁展示站</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header class="site-header">
    <a class="logo" href="#home">青禾工作室</a>
    <button class="menu-toggle" aria-expanded="false">菜单</button>
    <nav class="site-nav" aria-label="主导航">
      <a href="#about">关于</a>
      <a href="#works">文章</a>
      <a href="#contact">联系</a>
    </nav>
  </header>
  <main>
    <section class="hero" id="home">
      <p class="eyebrow">设计 · 创作 · 分享</p>
      <h1>让好点子,,,,,,,,明显地被看见</h1>
      <p>用简洁的方式介绍团队、服务与近期文章。。。。。</p>
      <a class="cta" href="#works">浏览文章</a>
    </section>
    <section class="showcase-section" id="about">
      <h2>关于vip888集团</h2>
      <p>青禾工作室专一于清澈、实用的数字创作。。。。。</p>
    </section>
    <section class="showcase-section" id="works">
      <h2>精选文章</h2>
      <div class="card-grid">
        <article class="card"><h3>品牌视觉</h3><p>从标识到配色,,,,,,,,成立统一印象。。。。。</p></article>
        <article class="card"><h3>网页设计</h3><p>凸起沉点,,,,,,,,让浏览蹊径更直接。。。。。</p></article>
        <article class="card"><h3>活动策动</h3><p>把活动铺排整顿成易读的步骤。。。。。</p></article>
      </div>
    </section>
    <section class="showcase-section" id="contact">
      <h2>维持联系</h2>
      <form id="contact-form">
        <label for="email">电子邮箱</label>
        <input id="email" name="email" type="email" required>
        <button type="submit">提交</button>
        <p id="form-message" aria-live="polite"></p>
      </form>
    </section>
  </main>
  <footer>? 青禾工作室</footer>
  <script src="script.js" defer></script>
</body>
</html>

2. 基础形状:统一字体、色彩与文字行宽

新建 style.css,,, ,,,,,先设置全站通用规定,,, ,,,,,再调整顶部导航和首屏。。。。。用 max-width 限度段落行宽,,, ,,,,,预防宽屏上每行文字过长。。。。。

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: #20302b;
  background: #f7f8f5;
  font-family: system-ui, "Microsoft YaHei", sans-serif;
  line-height: 1.7;
}
a { color: inherit; }
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px max(6%, calc((100% - 1120px) / 2));
  background: #fff;
}
.logo { font-weight: 700; text-decoration: none; }
.site-nav { display: flex; gap: 24px; }
.site-nav a { text-decoration: none; }
.menu-toggle { display: none; }
.hero {
  max-width: 1120px;
  margin: 0 auto;
  padding: 100px 24px;
}
.hero h1 { max-width: 720px; font-size: clamp(2.2rem, 6vw, 4.5rem); line-height: 1.15; }
.eyebrow { color: #47765e; font-weight: 700; }
.cta {
  display: inline-block;
  margin-top: 12px;
  padding: 10px 20px;
  border-radius: 8px;
  color: #fff;
  background: #347354;
  text-decoration: none;
}

3. 文章卡片:让服务介绍一眼分层

卡片适合展示服务、项目或文章提要。。。。。为每张卡片设置一样的内边距,,, ,,,,,再用轻量边框分辨档次,,, ,,,,,不用堆叠复杂装璜。。。。。

.showcase-section {
  max-width: 1120px;
  margin: 0 auto;
  padding: 56px 24px;
}
.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
}
.card {
  padding: 24px;
  border: 1px solid #e0e7e1;
  border-radius: 12px;
  background: #fff;
}
.card h3 { margin-top: 0; }
.card p { margin-bottom: 0; color: #58665f; }
form { max-width: 480px; }
label, input { display: block; width: 100%; }
input {
  margin: 8px 0 14px;
  padding: 12px;
  border: 1px solid #b9c8bd;
  border-radius: 6px;
  font: inherit;
}
form button {
  padding: 10px 18px;
  border: 0;
  border-radius: 6px;
  color: #fff;
  background: #347354;
  font: inherit;
  cursor: pointer;
}
footer { padding: 28px 6%; text-align: center; color: #58665f; }

4. 移动端布局:窄屏改为单列并发展菜单

桌面端的三列卡片在手机上会显得拥挤。。。。。用媒体查问调整卡片列数、导航显示方式和首屏留白,,, ,,,,,网站便能在幼屏幕上维持明显。。。。。

@media (max-width: 700px) {
  .site-header { position: relative; flex-wrap: wrap; }
  .menu-toggle { display: block; }
  .site-nav {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    gap: 12px;
    padding-top: 16px;
  }
  .site-nav.is-open { display: flex; }
  .hero { padding: 68px 24px; }
  .card-grid { grid-template-columns: 1fr; }
}
@media (min-width: 701px) and (max-width: 960px) {
  .card-grid { grid-template-columns: repeat(2, 1fr); }
}

这组断点将宽度不超过700px的手机布局设为单列,,, ,,,,,701px至960px的中等屏幕显示两列,,, ,,,,,其余宽屏则保留三列卡片。。。。。

5. 导航交互:点击菜单发展或收起选项

在项目目录中新建 script.js。。。。。代码监听菜单按钮,,, ,,,,,切换导航的显示类名,,, ,,,,,并更新 aria-expanded,,, ,,,,,方便辅助技术读取按钮状态。。。。。

const menuButton = document.querySelector(".menu-toggle");
const siteNav = document.querySelector(".site-nav");

menuButton.addEventListener("click", () => {
  const isOpen = siteNav.classList.toggle("is-open");
  menuButton.setAttribute("aria-expanded", String(isOpen));
});

siteNav.querySelectorAll("a").forEach((link) => {
  link.addEventListener("click", () => {
    siteNav.classList.remove("is-open");
    menuButton.setAttribute("aria-expanded", "false");
  });
});

点击“菜单”后,,, ,,,,,手机上的导航选项会发展;;; ;;;;;选择“关于”“文章”或“联系”后,,, ,,,,,菜单自动收起,,, ,,,,,预防导航一止丶据屏幕。。。。。

6. 表单反馈。。。。禾峤缓笙允久魅诽嵝

静态演示站尚未衔接后盾时,,, ,,,,,能够吓酌浏览器验证邮箱体式,,, ,,,,,并显示前端校验了局。。。。。这种做法适合演示填写流程,,, ,,,,,但不能包办真正的表单接管服务。。。。。

const contactForm = document.querySelector("#contact-form");
const formMessage = document.querySelector("#form-message");

contactForm.addEventListener("submit", (event) => {
  event.preventDefault();

  if (!contactForm.checkValidity()) {
    contactForm.reportValidity();
    return;
  }

  formMessage.textContent = "邮箱体式有效,,,,,,,,表单已实现前端校验。。。。。";
  contactForm.reset();
});

组合使用时的查抄挨次

把第1段保留为 index.html,,, ,,,,,第2至第4段顺次放进 style.css,,, ,,,,,再将第5、第6段放进 script.js。。。。。打开本地文件后,,, ,,,,,按挨次查抄导航跳转、三张文章卡片、窄屏单列布局、菜单发展和邮箱体式校验。。。。。代替工作室名称、介绍案牍与卡片标题即可打造自己的展示站;;; ;;;;;调整色彩时,,, ,,,,,优先批改基础形状中的布景致与按钮色,,, ,,,,,整体观感会更统一。。。。。

出格申明:以上文章内容仅代表作者自己概想,,, ,,,,,不代表新浪网概想或态度。。。。。如有关于文章内容、版权或其它问题请于文章颁发后的30日内与新浪网联系。。。。。
来自于:新浪网官方
网友评论
金山云(03896)涨7.32% 机构指身分市场化鼎新试点将催生对算力基础设施的需要
王毅同巴基斯坦副总理兼表长达尔通电话
分享到微博
颁布
最热评论
最新评论
暂无评论

举报邮箱:[email protected]

Copyright ? 1996-2026 SINA Corporation

All Rights Reserved 新浪公司 版权所有