search
HomeWeb Front-endHTML Tutorialcss权威指南-基本视觉格式化(水平与垂直)_html/css_WEB-ITnose

1.基本概念

    (1)正常流:是指西方语言文本从左向右,从上向下显示。如果要让一个元素不在正常流中国,唯一的办法

                    就是使之成为浮动或定位元素。

    (2)非替换元素:如果元素的内容包含在文档中,则称之为非替换元素。

    (3)替换元素:指用作为其他内容占位符的一个元素。例子:img中的图像

    (4)块级元素:在正常流中,会在其框之前和之后生成“换行”

                        所以出于正常流中的块级元素会垂直摆放。

    (5)行内元素:不会再之前或之后生成行分隔符,它们是块级元素的后台。

    (6)根元素:位于文档树顶端的元素。在HTML中,就是元素HTML。

 

2.水平格式化

通常我们指的元素的宽度,指的是其内容的宽度,不包括内补,外补,边。

传送门:JavaScript权威设计--CSS(简要学习笔记十六)

水平格式化的七大属性:margin-left,border-left,padding-left,width,padding-right,border-right,margin-left

这七个水平属性的总和要等于父元素的width。

这里面只有width,margin-left,margin-left这三个可以设置为:auto

其他都必须设为特定的值或者默认宽度为0.

 

下面就以这三个auto的组合来展现问题:auto会自动补齐宽度(屏幕总宽度1366)

    设置margin-left为auto:    

<p style="margin-left:auto;width:100px;margin-right: 100px">水平格式化</p>

 

    

    

 

    设置margin-right为auto:

<p style="margin-left:100px;width:100px;margin-right: auto">水平格式化</p>

 

        

 

    设置width为auto:

<p style="margin-left:100px;width:auto;margin-right: 100px">水平格式化</p>

 

        

 

    设置margin-left和margin-right为auto:

<p style="margin-left:auto;width:100px;margin-right: auto">水平格式化</p>

 

        

 

    设置margin-left和margin-right和width都为auto:

<p style="margin-left:auto;width:auto;margin-right: auto">水平格式化</p>

 

 

    设置margin-left和margin-right和width都不为auto:

<p style="margin-left:100px;width:100px;margin-right: 100px">水平格式化</p>

 

 

下面会遇到一种复杂特殊奇怪的现象:那就是外边距可以是负值

<div style="width:400px;border:2px solid red">    <p style="margin-left:10px;width: auto;margin-right: -50px;">zqzqzq</p></div>

 


可以看出来宽度是440(width;auto这里440是实时计算的值,而不是显示的指定的值,

其实这里涉及到一个问题:有些是实时计算的值,有些DOM可以获取实时计算的值。),比父400还要宽!说好的不能比父元素宽呢?

但是这个计算没有错误:

10+0+0+440+0+0-50=400

最终还是等于400。

 

前面说的都是费替换元素的水平格式化,下面来说替换元素的水平格式化。( 典型的替换元素就是img

示例图片:w:200px,h:300px.

当我们改变她的宽度的时候,高度也同比例改变。

200x300

300x450

 

 

3.垂直格式化

一个元素的默认高度由其内容决定。

高度还会受到内容宽度的影响。

段落越窄,相应的就会越高,以便容纳其中所有的内联内容。

对应于水平格式化,它也有7个属性:

margin-top,border-top,padding-top,height,padding-bottom,border-bottom,margin-bottom.

同样这七个值必须等于包含元素块的高。

这七个值中只有三个值可以设为auto:height,margin-top,margin-bottom,其他四个必须设为特定的值或默认为0.

 

当高度小于显示内容的高度:浏览器会处理为有个滚动条(overflow),以容纳下内联元素。

 

垂直格式化的另一个重要的是合并垂直外边距(重叠垂直外边距)

<ul>    <li>大师兄</li>    <li>师傅</li>    <li>被妖怪</li>    <li>抓走啦</li>    <li>俺老孙来也</li></ul>li{ margin-top: 10px; margin-bottom: 15px; 

 

        

相邻列表之间的距离是15px,不是25px。

因为相邻的外边距沿着垂直方向合并了,大边距覆盖小边距。

 

 

 

Statement
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
Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

See all articles

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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.