Stake官网

网站代码盘点:8类基础建站常用代码�?楹霞�

网站代码盘点:8类基础建站常用代码�?楹霞�

想搭起一个能展示工作室、介绍文章、适配手机并带有单一交互的网站,能够从下面这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>关于Stake官网</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。打开本地文件后,按挨次查抄导航跳转、三张文章卡片、窄屏单列布局、菜单发展和邮箱体式校验。代替工作室名称、介绍案牍与卡片标题即可打造自己的展示站;调整色彩时,优先批改基础形状中的布景致与按钮色,整体观感会更统一。

[责任编纂:李幼萌]

为您推荐

热点文章

杰出视频

凤凰资讯官方微信
凤凰资讯官方微信
关注更多资讯
【网站地图】