首页 / 主题开发手册 / 主题基础 / 主样式表(style.css)

主样式表(style.css)

style.css是每个WordPress主题都需要的一个样式表(CSS)文件。它控制着网站页面的展示(视觉设计和布局)。

位置

为了让WordPress将主题模板文件集识别为有效的主题,style.css文件需要位于主题的根目录中,而不是子目录。

有关在主题中如何包含style.css文件的更详细说明,请参阅脚本和样式列队的“样式表”部分。

 

基本结构

WordPress使用style.css的头部注释信息作为主题的信息,显示在 外观>主题 的信息。

示例

以下是style.css的头部信息示例:

/*
Theme Name: Twenty Twenty
Theme URI: https://wordpress.org/themes/twentytwenty/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Our default theme for 2020 is designed to take full advantage of the flexibility of the block editor. Organizations and businesses have the ability to create dynamic landing pages with endless layouts using the group and column blocks. The centered content column and fine-tuned typography also makes it perfect for traditional blogs. Complete editor styles give you a good idea of what your content will look like, even before you publish. You can give your site a personal touch by changing the background colors and the accent color in the Customizer. The colors of all elements on your site are automatically calculated based on the colors you pick, ensuring a high, accessible color contrast for your visitors.
Tags: blog, one-column, custom-background, custom-colors, custom-logo, custom-menu, editor-style, featured-images, footer-widgets, full-width-template, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready, block-styles, wide-blocks, accessibility-ready
Version: 1.3
Requires at least: 5.0
Tested up to: 5.4
Requires PHP: 7.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentytwenty
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
WordPress主题库使用该文件中 "Version"后面的数字来确定该主题是否有新版本可用。

 

说明

带有(*)的项目是WordPress主题库中的主题所必须的。

  • Theme Name (*): 主题的名称
  • Theme URI: 一个公开的URL,用户可以在那里找到关于主题的更多信息。
  • Author (*): 开发该主题的个人或组织的名称。建议使用主题作者的wordpress.org用户名。
  • Author URI: 主题作者的URL
  • Description (*): 对主题的简短描述
  • Version (*): 主题版本,格式 X.X 或 X.X.X
  • Requires at least (*): 主题可以使用的最早的WordPress主版本,以X.X格式编写,主题只需要支持最近的三个版本。
  • Tested up to (*): 主题最后测试到的WordPress主版本,如5.4,只写数字,以X.X格式。
  • Requires PHP (*): 支持的最老的PHP版本,格式为X.X,只有数字
  • License (*): 主题的许可证
  • License URI (*): 主题的许可证URL
  • Text Domain (*): 用于翻译的textdomain的字符串
  • Tags: 允许用户使用标签过滤器查找主题的单词或短语。主题审查手册中有标签的完整列表。
  • Domain Path: 用于使WordPress知道在禁用主题时在哪里可找到翻译,默认/languages.

在必要的头部部分之后,style.css可以包含任何常规CSS文件的内容。

 

子主题的Style.css

如果你的主题是子主题,在style.css头部中需要有Template行。

/*
Theme Name: My Child Theme
Template: twentytwenty
*/

有关创建子主题的详细信息,可以参阅子主题页面。