关于的垂直居中的问题_html/css_WEB-ITnose
居中分为垂直居中和水平居中,这里因为水平居中比较简单就不过多说了,然后垂直居中呢又分为以下几种情况种情况:
1、单行居中
这个也简单直接line-height为容器的高度就行了。
2、多行居中
和上面的单行居中差不多,只是line-height的值发生了变化,用容器高度除以行数,得到的结果就是line-height的值。
3、DIV垂直居中。这个也分为两种情况
3.1、DIV知道高度的情况,这里就需要用到定位,下面是具体代码。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin: 0; padding: 0; } body{ width: 100%; height: 100%; } div{ width: 100%; height: 400px; position: absolute; top: 50%; left: 0; margin-top: -200px; background: red; } .box{ width: 100%; height: 380px; margin-top: -180px; background: black; } </style></head><body> <div> <div class="box"></div> </div></body></html>
但是这么做有一个地方不好,当我们的浏览器的高度缩小到一定的大小的时候,那么DIV就有一份看不见了,而且滚动条也不能滑动上去。这个问题,我还没找到具体的解决方法,如果哪位大神知道怎么解决,请评论告知下,不胜感谢!
3.2、DIV不知道高度的情况下,下面是具体代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> body{ margin: 0; padding: 0; } html,body{ width:100%; height:100%; } .main{ text-align:center; width:100%; height:100%; display:table; } .box{ display:table-cell; vertical-align:middle; } </style> <!--[if lte ie 7]> <style> .main{ position:absolute; top:50%; height:auto; } .box{ position:relative; top:-50%; } </style> <![endif]--></head><body> <div class="main"> <div class="box"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid harum cupiditate mollitia placeat quam repellendus, expedita magni vitae officiis eaque dolor quis architecto illum, iure atque cum. Iusto, enim, animi. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi autem neque optio suscipit minus maiores id magnam porro, harum maxime! Maiores eum tempore quod quas rem, totam, vitae incidunt sed! </div> </div></body></html>
好了,这些差不多就是我遇到过得所有关于垂直居中的问题了,先写到这里,后面遇到了再总结。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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.

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit
