广州总部电话:020-85564311
广州总部电话:020-85564311
20年
互联网应用服务商
请输入搜索关键词
知识库 知识库

优网知识库

探索行业前沿,共享知识宝库

Web | 知识实践 | 基于CSS实现的静态网页排版

发布日期:2025-08-09 17:10:57 浏览次数: 816 来源:PuddingLaRose
推荐语
掌握CSS静态网页排版的核心技巧,从基础选择器到布局元素全面解析。

核心内容:
1. CSS基础样式设置与选择器使用
2. 外部CSS文件链接与响应式布局配置
3. DIV布局与类/ID选择器实战应用
小优 网站建设顾问
专业来源于二十年的积累,用心让我们做到更好!

Step 1 样式(略)

Step 2 类型选择器

<style>
  h1{
  text-align:center;
  }
</style>

可以通过在 style 元素中指定元素并为其设置属性来向元素添加样式

风格
text_align
字体位置
backround-color
背景颜色
宽度
宽度(可以用px或者百分比
左边距
左边距
右边距
右边距(类)
左边距
外沿距离(页面)
边境
元素的外沿
背景图片
背景图片,可以是本地的也可以是url(例如.background-image:url(https://cdn.freecodecamp.org/curriculum/css-cafe/beans.jpg)
)
字体系列
字体

如果状态相似,可以将它们替换为一个选择器列表

Step 3 css文件

由于会有更多的样式,因此最好将所有样式放在一个单独的文件中并链接到它

在css文件中,不需要加style标签

h1, h2, p {
      text-align: center;
}

在源文件中添加一个链接链接到css文件

<link rel="stylesheet" href="styles.css">

同时为了普适性,可以在mata属性中制作屏幕长宽比适中

<meta name="viewport" content="width=device-width, initial-scale=1.0"/>

第四步 DIV

div 元素主要用于设计布局目的,这与迄今为止使用的其他内容元素不同。

div元素可以设置id以方便应用后面的id选择器

它处于 body 内

<div id="menu">
      <main>
        <h1>CAMPER CAFE</h1>
        <p>Est. 2020</p>
        <section>
          <h2>Coffee</h2>
        </section>
      </main>
</div>

步骤 5 id选择器

id 选择器来选取具有 id 属性的特定元素

将 # 符号直接放在元素的 id 值前面,就形成了 id 选择器。

Step 6 css注释

css内注释格式:/* xxx */

Step 7 类选择器

那类选择器属性就是class=xxx(对比id选择器在html中选择id=xxx

.menu {
  width: 80%;
  background-color: burlywood;
  margin-left: auto;
  margin-right: auto;
}

第 8 步文章

article 元素通常包含具有相关信息的多个元素。

<article class="item">
            <p class="flavor">French Vanilla</p>
            <p class="price">3.00</p>
          </article>
          <article>
            <p>Caramel Macchiato</p>
            <p>3.75</p>
</article>

p 元素嵌套在具有 item 属性的 article 元素中。

可以使用名为 item 的类嵌套在元素中任意位置的所有 p 元素的样式,如下所示:

.item p{
  display:inline-block;
}

最后一步成品(x)

styles.css

body {
  background-image: url(https://cdn.freecodecamp.org/curriculum/css-cafe/beans.jpg);
  font-family: sans-serif;
  padding: 20px;
}

h1 {
  font-size: 40px;
  margin-top: 0;
  margin-bottom: 15px;
}

h2 {
  font-size: 30px;
}

.established {
  font-style: italic;
}

h1, h2, p {
  text-align: center;
}

.menu {
  width: 80%;
  background-color: burlywood;
  margin-left: auto;
  margin-right: auto;
  padding: 20px;
  max-width: 500px;
}

img {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

hr {
  height: 2px;
  background-color: brown;
  border-color: brown;
}

.bottom-line {
  margin-top: 25px;
}

h1, h2 {
  font-family: Impact, serif;
}

.item p {
  display: inline-block;
  margin-top: 5px;
  margin-bottom: 5px;
  font-size: 18px;
}

.flavor, .dessert {
  text-align: left;
  width: 75%;
}

.price {
  text-align: right;
  width: 25%;
}

/* FOOTER */

footer {
  font-size: 14px;
}

.address {
  margin-bottom: 5px;
}

a {
  color: black;
}

a:visited {
  color: black;
}

a:hover {
  color: brown;
}

a:active {
  color: brown;
}
index.html

<h2>Desserts</h2>
<img src="https://cdn.freecodecamp.org/curriculum/css-cafe/pie.jpg" alt="pie icon">
<article class="item">
  <p class="dessert">Donut</p><p class="price">1.50</p>
</article>
<article class="item">
  <p class="dessert">Cherry Pie</p><p class="price">2.75</p>
</article>
<article class="item">
  <p class="dessert">Cheesecake</p><p class="price">3.00</p>
</article>
<article class="item">
  <p class="dessert">Cinnamon Roll</p><p class="price">2.50</p>
</article>
</section>
</main>
<hr class="bottom-line">
<footer>
<p>
<a href="https://www.freecodecamp.org" target="_blank">Visit our website</a>
</p>
<p class="address">123 Free Code Camp Drive</p>
</footer>
</div>
</body>
</html>


优网科技,优秀企业首选的互联网供应服务商

优网科技秉承"专业团队、品质服务" 的经营理念,诚信务实的服务了近万家客户,成为众多世界500强、集团和上市公司的长期合作伙伴!

优网科技成立于2001年,擅长网站建设、网站与各类业务系统深度整合,致力于提供完善的企业互联网解决方案。优网科技提供PC端网站建设(品牌展示型、官方门户型、营销商务型、电子商务型、信息门户型、微信小程序定制开发、移动端应用(手机站APP开发)、微信定制开发(微信官网、微信商城、企业微信)等一系列互联网应用服务。


我要投稿

姓名

文章链接

提交即表示你已阅读并同意《个人信息保护声明》

专属顾问 专属顾问
扫码咨询您的优网专属顾问!
专属顾问
马上咨询
联系专属顾问
联系专属顾问
联系专属顾问
扫一扫马上咨询
扫一扫马上咨询

扫一扫马上咨询

和我们在线交谈!