Home > Article > Web Front-end > Learn the basic framework construction principles and implementation methods of CSS
With the rapid development of the Internet, the design of web pages has received more and more attention. As one of the important parts of web design, CSS has attracted much attention for its principles and implementation methods of making the basic framework of web pages. This article will explain the principles and implementation methods of using CSS to create the basic framework of web pages through specific code examples.
1. Basic syntax of HTML and CSS
Before understanding the basic framework of CSS for making web pages, we need to first understand the basic syntax of HTML and CSS, which will help to better understand the use of CSS. .
HTML is the basic language of web pages, which is used to define the content and structure of web pages. A basic HTML structure is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>网页标题</title> </head> <body> 网页内容 </body> </html>
Among them, is used to define the document type, and the <code>
tag is used to define The root element of the document, the tag is used to define the header information of the web page, the
<meta>
tag is used to set the metadata of the web page, <title the> tag is used to define the title of the web page, and the <code>
tag is used to define the main content of the web page.
CSS is the language of web page layout and style. It is used to control the display effect of various HTML elements in web pages. A basic CSS structure is as follows:
选择器 { 属性1: 值1; 属性2: 值2; 属性3: 值3; }
Among them, the selector is used to select the HTML element that needs to be styled, and the curly brackets are the specific style settings, including attributes and values.
2. The principle of making basic framework of web pages with CSS
The principle of making basic framework of web pages with CSS is very simple. It is to set the position, size, background, border, spacing and other style attributes of HTML elements to achieve the purpose of layout. The following will introduce in detail the implementation method of CSS to create the basic framework of web pages.
Before we start making the basic framework of the web page, we need to set the basic style of the web page, such as setting the background color, font, and font size of the web page wait.
body { background-color: #f5f5f5; /* 设置网页的背景颜色 */ font-family: Arial, sans-serif; /* 设置字体 */ font-size: 16px; /* 设置字号 */ }
The layout of a web page refers to the position and size of each HTML element in the web page. Before implementing the web page layout, we need to define the containing block and document flow of the web page.
The inclusion block refers to the area where the HTML element is located. Its size and position determine how the HTML element is positioned. You can define the size of the containing block by setting properties such as width
, height
, padding
, border
, margin
, etc. and location.
Document flow refers to the flow direction of HTML elements in a web page, which is divided into block-level elements and inline elements. Block-level elements occupy an exclusive line and occupy the entire width of their parent element; inline elements are arranged in the same line, and the width is determined by the content. You can control the layout of elements by setting the display
attribute.
/* 定义网页的包含块 */ .container { width: 960px; /* 宽度为960px */ margin: 0 auto; /* 居中 */ padding: 20px 0; /* 上下各留20像素的padding */ } /* 定义网页的布局方式 */ .header { display: block; /* 块级元素 */ height: 100px; /* 高度为100px */ background-color: #333333; /* 背景为黑色 */ color: #ffffff; /* 文字为白色 */ } .nav { display: block; /* 块级元素 */ height: 40px; /* 高度为40px */ background-color: #f5f5f5; /* 背景为灰色 */ } .content { display: block; /* 块级元素 */ margin: 20px 0; /* 上下各留20像素的margin */ } .footer { display: block; /* 块级元素 */ height: 80px; /* 高度为80px */ background-color: #333333; /* 背景为黑色 */ color: #ffffff; /* 文字为白色 */ }
After defining the layout of the web page, we need to define the style of each HTML element.
You can set font-size
, color
, font-weight
, line-height
and other attributes to control the size, color, thickness, line height, etc. of the text.
h1 { font-size: 32px; /* 设置标题字号为32px */ color: #333333; /* 设置标题颜色为黑色 */ font-weight: bold; /* 设置标题字体为粗体 */ line-height: 1.5; /* 设置字行距为1.5倍 */ } p { font-size: 16px; /* 设置正文字号为16px */ color: #666666; /* 设置正文颜色为灰色 */ line-height: 1.5; /* 设置字行距为1.5倍 */ }
You can set border
, background-color
, background- attributes such as image
to control the borders and background of HTML elements.
.nav li { display: inline-block; /* 行内块元素 */ border: none; /* 取消边框 */ padding: 0 15px; /* 左右各留15像素的padding */ line-height: 40px; /* 文字与底部对齐 */ background-color: #f5f5f5; /* 背景颜色为灰色 */ } .button { display: inline-block; /* 行内块元素 */ border: 1px solid #cccccc; /* 设置边框 */ padding: 5px 10px; /* 上下各留5像素,左右各留10像素的padding */ background-color: #ffffff; /* 背景颜色为白色 */ color: #333333; /* 文字颜色为黑色 */ }
3. Method of implementing the basic framework of making web pages with CSS
After understanding the principle of making the basic framework of web pages with CSS, we can deepen our understanding through specific code implementation.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS制作网页基本框架</title> <style> body { background-color: #f5f5f5; font-family: Arial, sans-serif; font-size: 16px; } .container { width: 960px; margin: 0 auto; padding: 20px 0; } .header { display: block; height: 100px; background-color: #333333; color: #ffffff; } .nav { display: block; height: 40px; background-color: #f5f5f5; } .nav li { display: inline-block; border: none; padding: 0 15px; line-height: 40px; background-color: #f5f5f5; } .content { display: block; margin: 20px 0; } h1 { font-size: 32px; color: #333333; font-weight: bold; line-height: 1.5; } p { font-size: 16px; color: #666666; line-height: 1.5; } .button { display: inline-block; border: 1px solid #cccccc; padding: 5px 10px; background-color: #ffffff; color: #333333; } .footer { display: block; height: 80px; background-color: #333333; color: #ffffff; } </style> </head> <body> <div class="container"> <div class="header"> <h1>网页标题</h1> </div> <div class="nav"> <ul> <li>导航1</li> <li>导航2</li> <li>导航3</li> <li>导航4</li> </ul> </div> <div class="content"> <h2>文章标题</h2> <p>文章内容</p> <p>文章内容</p> <p><a href="#" class="button">按钮</a></p> </div> <div class="footer"> <p>版权信息</p> </div> </div> </body> </html>
The above code implements a basic web page layout, including the title, navigation, content and footer of the web page. By setting the corresponding CSS properties, the position, size, background, style and other effects of each part are achieved.
4. Summary
This article introduces the principles and implementation methods of CSS to create the basic framework of web pages. Through specific code examples, it explains how CSS styles HTML elements and implements web page layout. Understanding and mastering this knowledge can allow us to better express our creativity and achieve results in web design and development.
The above is the detailed content of Learn the basic framework construction principles and implementation methods of CSS. For more information, please follow other related articles on the PHP Chinese website!