如何使html中的din居中,CSS 实现完美垂直居中

<?xml version="1.0" encoding="utf-8"?>ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">http://www.w3.org/1999/xhtml" xml:lang="en">

Demo:: vertical align: middle

Demo of middled vertical align

Author: Spenser Lee, Liberty Studio
originally posted on http://bbs.blueidea.com/" title="BlueIdea Forum">BlueIdea Forum

Table of centents:

  1. Single line countainer with/without a fixed height
  2. Align multi-line container which does not have a fixed height
  3. Simulating table layout in container with a fixed height
  4. IE's solution
  5. A perfect compounded sample

Case One: Single line countainer with/without a fixed height

If you only want to display a container which only holds a single line of text, you can set line-height property to height property, then set overflow to hidden.

Sample:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Core code:

.middle-demo-1{  height: 4em;    line-height: 4em;       overflow: hidden;}
Notes:
  1. I strongly recommend you use relative size in height and line-height property. Why? You can simply set the font size larger if your browser support it. When it gets large enough, you will see the countainer is stretched and the height is no longer equal to line-height property, thus, the layout is messed up. Using relative size as em, ex or % will let your countainer stretch with the content.
  2. overflow: hidden is a must. Why? Same as above. Just ensure height and line-height are always equal.
Pros:
  1. Fits in both block elements and inline elements.
  2. Capable of all 5th-gen browsers.
Cons:
  1. Text length is limited. Max with only one line.
  2. Does not work well on none text contents such as images and objects.

Back

Case Two: Align multi-line container which does not have a fixed height

In this case, we should simply set a pair of fixed equivalences to padding-top and padding-bottom attribute. It works on both IE and non-IE browsers.

Sample:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

Core code:

.middle-demo-2{       padding-top: 24px;      padding-bottom: 24px;}
Pros:
  1. Fits in both block elements and inline elements.
  2. Works on none text contents as fine as text contents.
  3. Capable of all 5th-gen browsers. Might need a little tune-up for the box model bug of IE5 though.
Cons:
  1. You can not assign height in this solution.

Back

Case Three: Simulating table layout in container with a fixed height

CSS offers a set of very convenient display atrribute values called display: table, display: table-row, display: table-cell and other display values begin with table-. It offers a way to simulate table layout in all elements. As a result, any element with display: table-cell, vertical-align: middle and a fixed height will display exactly like a table cell.

Sample: (You may not be able to view the effect on IE)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

Core code:

.middle-demo-3{ display: table-cell;    height: 300px;  vertical-align: middle;}
Notes:
  1. display: table-cell must work with other elements with display: table value sets in order to form a literal table. or it might cause unexceptable bugs.
  2. Sadly IE series (including the latest IE 7 beta) does not support any of display: table values, so it won't work in IE.
Pros:
  1. It has the most perfect render for vertical-align: middle align.
Cons:
  1. It only works in latest versions of non-IE browsers, such as Mozilla, Firefox, Netscape 8, Opera 8, and Safari.

Back

Case Four: IE's solution

Since IE is a lame browser when it comes to ANYTHING, so you can't use the solution above simply because IE does not recognize it at all. However, there has been nothing yet you can not code with CSS if you have already coded it with table. It even offers you possibility to layout your page that tables can not do!

So what's the solution for IE? Like what we always do: Hit IE's back with the BUGS it offers!

Sample: (You are able to view the correct result only on IE)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

.middle-demo-4{  height: 300px;  position: relative;}.middle-demo-4 div{ position: absolute;     top: 50%;       left: 0;}.middle-demo-4 div div{        position: relative;     top: -50%;      left: 0;}

See? Don't ask me why it worked. This hack is based on years of study in IE's own stupid layout model and it works very well, even in IE5 given the box width hack. I won't tell you the theory behind all these. It's my top secret, with which I solved a lot of other cross browser layout problems, and I'm not going to share it with you. But you are free to use this IE hack It's kinda handful in most occassions.

Pros:
  1. The only perfect vertical align method in IE. It works even better then add automatic paddings.
Cons:
  1. After all it's a CSS hack. We can avoid it if not for IE.

Back

Case Five: A perfect compounded sample

Here's the perfect compounded solution on vertical centering that works on almost all latest browsers.

Sample: (This works on almost all browsers)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

I'm not going to give you the full code of this one. But it's not difficult to write it yourself, with the solution of case 3 and 4, and a little knowledge in IE/non-IE CSS hackers. And actually the hacking style is not limited, so I don't want to force you to use my hacking style neither. Stop being lazy, and write the code yourself!

Browser capability:
  1. Works on: IE6+, Mozilla 1.5+, Netscape Navigator 7+, Opera 7+, Firefox 1.0+ and Safari 1.0+
  2. On IE5, you might need to apply the box model hacker to make the height correct.
  3. Untested: WebOmini, Konqueror.
  4. Fails on: Other browsers not reffered in the list above.

Back

[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

《宾馆客房管理系统》是一个基于C#与MySQL的项目,旨在帮助学习者掌握数据库管理和系统开发知识。该项目通过完整代码实现,将编程技术应用于宾馆客房管理的实际业务场景。 C#是微软开发的面向对象编程语言,广泛用于Windows应用程序开发。在本项目中,C#用于构建用户界面、处理业务逻辑以及与数据库交互。它拥有丰富的类库,便于开发复杂图形用户界面(GUI),并通过ADO.NET组件实现与MySQL数据库的连接。MySQL是一种流行的开源关系型数据库管理系统(RDBMS),常用于Web应用程序,用于存储客房、预订、客户等核心数据。通过SQL语句,开发者可对数据进行增、删、改、查操作。系统中可能涉及“客房表”“预订表”“客户表”等,包含客房编号、类型、价格、预订日期等字段。 数据库连接是系统的关键部分。C#通过ADO.NET的SqlConnection类连接MySQL数据库,连接字符串包含服务器地址、数据库名称、用户名和密码。用户下载项目后,需根据本地环境修改连接字符串中的用户名和密码。系统主要功能模块包括:客房管理,可展示、添加、修改、删除客房信息;预订管理,处理预订的查看、新增、修改和取消;客户管理,存储和管理客户个人信息;查询功能,支持按客房类型、价格范围、预订日期等条件查询;报表和统计功能,生成入住率、收入统计等报表辅助决策。开发者需编写C#方法对应数据库操作,同时设计直观易用的界面,方便用户完成预订流程。项目中的MySQL文件可能是数据库脚本或配置文件,包含建表、数据填充及权限设置等内容,用户需在本地测试前运行脚本设置数据库环境。 总之,该系统结合C#和MySQL,为学习者提供了一个涵盖数据库设计、业务逻辑处理和界面开发的综合实践案例,有助于提升开发者在数据库应用和系统集成方面的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值