代码实例如下:

<!DOCTYPE html>
<html>
<head>
<  charset=\"utf-8\">
<  name=\"author\" content=\"http://www.manongjc.com/\" />
< >码农教程</ >
<style type=\"text/css\">
.parent{
  width:400px;
  height:400px;
  border:1px solid red;
}
.children{
  border:1px solid blue;
  height:50px;
}
</style>
</head>
<body>
<div class=\"parent\">
  <div class=\"children\">码农教程欢迎你</div>
</div>
</body>
</html>

以上代码可以看出,默认状态下,并不能够实现我们想要的效果。下面对以上代码进行修改如下:

<!DOCTYPE html>
<html>
<head>
<  charset=\"utf-8\">
<  name=\"author\" content=\"http://www.manongjc.com/\" />
< >码农教程</ >
<style type=\"text/css\">
.parent{
  width:400px;
  height:400px;
  border:1px solid red;
}
.children{
  border:1px solid blue;
  height:50px;
  display:inline-block;
  *display:inline;
  *zoom:1;
}
</style>
</head>
<body>
<div class=\"parent\">
  <div class=\"children\">码农教程欢迎你</div>
</div>
</body>
</html>

以上代码实现我们想要的效果,并且各浏览器兼容性良好,主要是添加如下核心代码:

display:inline-block; 
*display:inline; 
*zoom:1;

大家可以把上面实例代码复制到这里运行一下,查看一下效果。

收藏 打印