Home Web Front-end HTML Tutorial div+css第三天_html/css_WEB-ITnose

div+css第三天_html/css_WEB-ITnose

Jun 24, 2016 pm 12:31 PM

一、行内元素和块元素

行内元素(inline element),又叫内联元素:内联元素只能容纳文本和其他内联元素。

常见的内联元素有:,,

块元素(block element):块元素一般都从新行开始,可以容纳文本,内联元素和其他块元素,即使内容不能占满一行,块元素也要把整行占满。

常见的块元素有:

,

二、案例

运行效果:

从案例我们可以看出,行内元素只占能显示自己内容的宽度,不会占满整行;而块元素不管内容有多少,都会占满整行。

 

三、行内元素和块元素的区别

1.行内元素只占内容的宽度,块元素不管内容多少要占据全行

2.行内元素只能容纳文本和其他行内元素,块元素可以容纳文本,行内元素和块元素(与浏览器的版本和类型有关)

3.一些css属性对行内元素不生效,比如:margin,left,right,width,height,建议尽可能使用块元素定位(与浏览器的版本和类型有关)

 

四、细节

尽管我给块元素设置了宽度,它们还是占据了整行,看效果:

五、块元素和行内元素相互转换

display:inline;------>块元素转为行内元素

display:block;------>行内元素转为块元素

案例:

大家注意观察前后的区别,span转为了块元素,占据了整行,而div转为行内元素,没有占据整行了。

上面案例最终的代码:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>行内元素和块元素</title> 6 <style type="text/css"> 7 .sp1{ 8     background:red; 9     display:block;/*我转为块元素了*/10 }11 .div1{12     background:gray;13     width:200px;14     display:inline;/*我转为行内元素了*/15 }16 .p1{17     background:#CC0;18     width:200px;19 }20 </style>21 </head>22 <body>23 <span class="sp1">我是行内元素span1</span>24 <span class="sp1">我是行内元素span2</span>25 <input name="username" type="text" value="我是行内元素input"/>26 <div class="div1">我是块元素div1</div>27 <div class="div1">我是块元素div2</div>28 <p class="p1">我是块元素段落1</p>29 <p class="p1">我是块元素段落2</p>30 </body>31 </html>
Copy after login

六、css文件之间的相互引用

语法:@import url('被引用的css文件地址');

七、标准流和非标准流

流:html元素在网页中显示的顺序

标准流:在html文件中,写在前面的元素在前面显示,写在后面的元素在后面显示

非标准流:在html文件中,当某个元素脱离了标准流,那么它就处于非标准流

八、css中的盒子模型

九、盒子模型入门案例

View Code

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <link href="box1.css" rel="stylesheet" type="text/css" /> 6 <title>盒子模型</title> 7 </head> 8 <body> 9 <div class="div1">10     <img  src="/static/imghw/default1.png"  data-src="img1.jpg"  class="lazy" / alt="div+css第三天_html/css_WEB-ITnose" >11 </div>12 </body>13 </html>
Copy after login

View Code

 1 @charset "utf-8"; 2 /* CSS Document */ 3  4 body{ 5     border: 1px solid #F00; 6     width: 500px; 7     height: 500px; 8     /*让body自动居中*/ 9     margin: 0 auto;10 }11 12 /*盒子模型*/13 .div1{14     width: 120px;15     heigth: 120px;16     border: 1px solid blue;17     margin: 5px 0px 0px 5px;18     /*padding-top: 5px;*/19     /*设置padding,div会根据里面的内容自适应*/20 }21 .div1 img{22     width: 50px;23     height: 50px;24     margin: 5px 0px 0px 5px;25 }
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles