search
HomeWeb Front-endHTML Tutorial如何用css实现元素水平与垂直居中_html/css_WEB-ITnose

如果文章对你有帮助,请点喜欢并关注,这将是我最大的动力,谢谢

1.水平居中

1.1.文字,图片以及内联元素:

如果希望居中的是内联元素或者是文字图片text-align:center;

1.2.块状元素

1.2.1.对于已设置宽度的块状元素:

margin:0 auto;----------------------让margin的左右自适应,通常是居中

1.2.2.对于未设置宽度的块状元素:

1.使用table标签html:

<table>        <tr>            <td>                <div id="container"></div>            </td>        </tr>    </table>

css:

table{     margin:0 auto;}

这个不常用,因为添加了更多无意义的标签2.通过将子块状元素变成行内元素html:

<div>    <p>居中示例</p></div>

css:

p{     display:inline;}div{     text-align:center;}

假设p在div内部,要让p居中先给外层元素设置text-align:center,这个意思是让div里面的内联元素居中,然后将p变成内联元素,即可。3.通过positionhtml:

<div>    <p>居中示例</p></div>

css:

div{     float:left;       position:relative;      left:50%;}p{     position:relative;       right:50%;}

float:left,目的是让父元素靠左并且宽度与子元素等宽;然后相对于原先的位置,向右移动50%,此时该元素的left为50%+(50%父元素自身宽度)然后将子元素相对于原先的位置,向左移动50%(这50%是基于父元素的宽度),此时居中。

2.垂直居中

2.1.对于已设置高度的单行文本

只需将父元素的height值与line-height的值设置相同即可

2.2.对于已设置高度的多行文本

1.使用table标签html:

    <table>        <tr>            <td>                <p>aaaaa</p>                <p>aaaaa</p>                   <p>aaaaa</p>            </td>        </tr>    </table>

css:

table{            height: 500px;            background-color: #aaa;        }

因为table标签中的td拥有标签隐式的样式:vertical-align:middle;2.通过table-cellhtml:

   <p>aaaaa</p>   <p>aaaaa</p>      <p>aaaaa</p>

css:

p{     display:table-cell;     vertical-align:middle;}

table-cell的意思是让元素以表格单元的形似表现,但是只支持IE8以上浏览器​

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
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

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

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 Tools

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools