让 CSS 的 "vertical-align: middle" 属性支持 IE 6/7

Here are the examples how to place the elements in the middle of the parent element. The first example works in all new browsers except Internet Explorer of version 7 and less. The second one looks more difficult, but it has some tricky fixes which make it workable in IE7 and IE6.

译者信息本篇文章展现一些能够将目标元素放在父元素的中间例子。第一个例子可以在除IE7以下的所有浏览器中实现,第二个例子虽然看起来有些复杂,但它的一些技巧可以在IE7和IE6中实现。

First example

In this example is used the tag "div" with the class "valign". But, pay your special attention on that the CSS property "height" must be defined for the parent element, in our case it is the tag "div" with the class "valign".

Here is code:

<html>
  <head>
    <style type="text/css">
      .valign
      {
         display: table-cell;
         vertical-align: middle;
         height: 200px;
      }
    </style>
  </head>
  <body>
    <div class="valign">
      <p>text text text text text text</p>
      <p>content content content content content</p>
    </div>
  </body>
</html>
译者信息

在这个例子中,类名为valign的div作为父类元素,需要特别注意的是,valign的css属性中的height必须明确设定高度值才能实现内部元素居中。

下面是代码:

<html>
  <head>
    <style type="text/css">
      .valign
      {
         display: table-cell;
         vertical-align: middle;
         height: 200px;
      }
    </style>
  </head>
  <body>
    <div class="valign">
      <p>text text text text text text</p>
      <p>content content content content content</p>
    </div>
  </body>
</html>
Second example

In this example we use more difficult structure of HTML and CSS which is shown below.

Here is HTML:

<div class="wrap">
  <div class="valign-center">
    <p>text text text text text text</p>
    <p>content content content content content</p>
  </div>
  <div class="valigh-fix"></div>
</div>

CSS for current example:

<style type="text/css">
  .wrap
  {
    display: table-cell;
    vertical-align: middle;
    width: 100%;
    height: 200px;
  }
  .valigh-fix
  {
    display: none;
    width: 1px;
    margin-left: -1px;
  }
</style>
译者信息

第二个例子,在这个例子中,我们运用了更复杂的html和css结构来实现以上目的!

下面是html:

<div class="wrap">
  <div class="valign-center">
    <p>text text text text text text</p>
    <p>content content content content content</p>
  </div>
  <div class="valigh-fix"></div>
</div>

上面html中的css:

<style type="text/css">
  .wrap
  {
    display: table-cell;
    vertical-align: middle;
    width: 100%;
    height: 200px;
  }
  .valigh-fix
  {
    display: none;
    width: 1px;
    margin-left: -1px;
  }
</style>
But, now we need to add CSS styles for Internet Explorer of version 7 and less. These styles have to be separated because they must affect only on Internet Explorer. To make it we need to put styles into the special conditional comments which work only in Internet Explorer. And in the current case this conditional comment tells to Internet Explorer that the content of this conditional comment will work only in versions 7 and less. 译者信息但是,现在我们需要对IE7和更低版本的添加样式,这些样式必须独立出来因为他们只对IE起作用。为了达到这个效果我们使用了特殊的注释让样式只能在IE下起作用。目前情况下,这个注释告诉IE浏览器当前样式定义只能在IE7或者更低版本下起作用。

Here is the example of this:

<!--[if lte IE 7]>
  <style type="text/css">
    .valigh-fix,
    .valign-center
    {
      display: inline-block;
      vertical-align: middle;
    }
    .valign-center
    {
      width: 100%;
    }
    .valigh-fix
    {
      height: 100%;
    }
    .valigh-fix,
    .valign-center
    {
      display: inline;
    }
  </style>
<![endif]-->

Here is HTML file with two examples:

在线演示: http://runjs.cn/detail/txhwhdqq

译者信息

这里有个例子:

<!--[if lte IE 7]>
  <style type="text/css">
    .valigh-fix,
    .valign-center
    {
      display: inline-block;
      vertical-align: middle;
    }
    .valign-center
    {
      width: 100%;
    }
    .valigh-fix
    {
      height: 100%;
    }
    .valigh-fix,
    .valign-center
    {
      display: inline;
    }
  </style>
<![endif]-->

演示地址:

在线演示: http://runjs.cn/detail/txhwhdqq

时间: 2024-08-29 10:47:03

让 CSS 的 "vertical-align: middle" 属性支持 IE 6/7的相关文章

[ css 补充 vertical-align ] css中补充的vertical-align属性讲解

一.关于今天,本文,及其他 今天是个特殊的日子,因为今天是汶川地震两周年的日子,我很悲鸣:今天又是国际护士节,看到微博上护士照横流,我很欣慰. 一段放松的YY后,进入正题.上个月21号,有位同行留言想让我讲讲vertical-align属性,我其实对vertical-align属性也是略知皮毛,要说岂敢谈“讲解”,就说说我对vertical-align属性的一些理解吧,纯属个人见解,若有不准确之处还望见谅.还有,vertical-align属性牵扯到的知识实在是太多了,不是一篇文章就可以讲清楚的

CSS(2)---css字体、文本样式属性

css字体.文本样式属性 这篇主要讲CSS文本属性中的:字体样式属性 和 文本样式属性. 一.字体样式属性 CSS 字体属性主要包括:字体设置(font-family).字号大小(font-size).字体粗细(font-weight).字体风格(font-style).字体颜色(color). 1.字体设置(font-family) 网页中常用的字体有 宋体.微软雅黑.黑体 等,例如将网页中 所有p标签的字体设置为微软雅黑,可以使用如下CSS样式代码: p { font-family:"微软雅

CSS和JavaScript标签style属性对照表

CSS和JavaScript标签style属性对照表一般情况是把"-"去掉,第二个字母用大写. CSS语法 (不区分大小写) JavaScript语法 (区分大小写) border borderborder-bottom borderBottomborder-bottom-color borderBottomColorborder-bottom-style borderBottomStyleborder-bottom-width borderBottomWidthborder-colo

css超出一行添加省略号属性:text-overflow和white-space

css超出一行添加省略号属性:text-overflow和white-space A-A+ 前端博客?前端开发?CSS?14994View1 文章目录 1.text-overflow: ellipsis; 2.white-space属性 3.word-wrap 通过使用text-overflow和white-space属性来使文本在一行内显示,超出则加省略号,添加如下HTML代码: <p>前端开发博客专注前端开发和技术分享,如果描述超过100像素,则会隐藏,添加省略号</p> CS

css字体样式(Font Style),属性

css字体样式(Font Style),属性 css字体样式(Font Style)是网页中不可或缺的样式属性之一,有了字体样式,我们的网页才能变得更加美观,因此字体样式属性也就成为了每一位设计者必需了解的知识.以下是我精心整理的css字体样式属性知识,供大家学习参考: css文本样式 序号 中文说明 标记语法 1 字体样式 {font:font-style font-variant font-weight font-size font-family} 2 字体类型 {font-family:"

[转]图解CSS的padding,margin,border属性(详细介绍及举例说明)

图解CSS的padding,margin,border属性 W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层.盒模型主要定义四个区域:内容(content).边框距(padding).边界(border)和边距(margin). 对于初学者,经常会搞不清楚margin,background-color,background-image,padding,content,border之间的层次.关系和相互影响

CSS多列、用户界面属性

CSS多列 常用属性: column-count 分几列 column-gap 列间距 column-rule 列分割线的样式(写法和border一样) 例如: 一个div分三列,列之间间距为10px,分割线为(可以不设置)5像素红色实线这样写 <style> div{column-count:3; column-gap:10px; column-rule:5px solid red; } </style> 写此样式要加前缀 column-rule 宽度:可选值有thin(细边框)

使用jquery获取css的top和left属性

使用jquery获取css的top和left属性 因为left和top也都是普通的css属性所以可以使用如下代码来获取 var left = $('#test').css('left'); var top = $('#test').css('top'); 当然这样获取的是一个字符串,如果们想直接取得对象的left和top的像素值,可以通过position方法来获取 var left = $('#test').position().left; var top = $('#test').positi

Css3之基础-3 Css 尺寸单位、尺寸属性与边框属性

一.CSS 单位 尺寸单位 - %  : 百分比 - in : 英寸 - cm : 厘米 - mm : 毫米 - pt : 磅(1pt等于1/72英寸) - px : 像素(计算机屏幕上的一个点) - em : 1em等于当前的字体尺寸,2em等于当前字体尺寸的两倍 颜色单位 - rgb(x,x,x)   : RGB 值,如 rgb(255,0,0) - rgb(x%,x%,x%): RGB 百分比值,如 rgb(100%,0%,0%) - #rrggbb:十六进制数,如 #ff0000 - #