Table of Contents
前端技术之CSS实现图片垂直居中技术
Home Web Front-end HTML Tutorial 前端技术之CSS实现图片垂直居中_html/css_WEB-ITnose

前端技术之CSS实现图片垂直居中_html/css_WEB-ITnose

Jun 21, 2016 am 09:13 AM

前端技术之CSS实现图片垂直居中技术

maybe yes 发表于2015-03-14 18:52

让 图片实现水平居中比较简单,只需要在父级容器中添加属性 text-align 为 center 即可。使用纯 CSS 实现图片的垂直居中,可以将图片包裹容器的 display 属性设置为 table-cell,然后加上 vertical-align: middle 就好了 。table-cell 的显示效果同 table 是一样的,兼容性还不错,所以直接使用 table 来包裹图片也是同样的效果,个人认为使用 table 会更好一些,如果 DIV 显示成 table-cell 是很难控制边距的。

除了完成了上面的事情外,图片本身也是需要做一些处理的,一般要居中显示的图片的大小和比例都是不一致的。这样的情况下,我们需要去除图片标签本身自带的 width 和 height 属性,然后设置 max-width 和 max-height 属性即可。

除了使用 CSS 实现图片的居中显示,也可以根据图片的高度和宽度属性以及显示区域的大小,然后计算出图片的 margin-top 属性的值得到正常的偏移位置。这样的方法比较麻烦,毕竟要对每个图片进行计算,并且很难保证图片宽高属性的正确性,可能会导致图片变形。

当一个网站的价值越高时,需求也就越多,仅仅通过居中图片不一定给人好的视觉体验。比如 QQ 空间就专门对图片进行了显示优化处理,如果图片中有人脸的话,在缩略图中会突出人脸部分,这种图像识别技术需要服务端的支持。

下面附上实现图片居中的部分代码:

<style>.imgTab{    border-collapse: collapse;    border-spacing: 0;    text-align: center;}.imgTab td{    padding: 5px;}.imgWrap {    width: 200px;}.imgWrap img{    max-height: 200px;    max-width: 200px;}</style><table class="imgTab">    <tr>        <td>            <div class="imgWrap">                <img  src="" / alt="前端技术之CSS实现图片垂直居中_html/css_WEB-ITnose" >            </div>        </td>    </tr></table>
Copy after login

阅(253)评(0)查看评论


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)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
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.

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.

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 an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles