html

css

<div class="register-wrapper">
<div class="register">
<h1><span class="icons icons-home"></span>XXX网站首页!</h1>
<div class="register-links-wrapper">
<ul class="register-links">
<li class="icons icons-sina"></li>
<li class="icons icons-qq"></li>
<li class="login"><a href="#">登录</a></li>
<li class="regis"><a href="#">注册</a></li>
</ul>
</div>
</div>
</div>

新浪和qq的背景图[1]是单独用一个标签包起来的。登陆和注册[2]也是,设置[1]高度的时候,[2]的高度也跟着变化了。

/* register 头部最顶层*/
.register-wrapper{
width: %;
background-color: #dcdcdc;
margin-bottom: 30px;
}
.register{
width: 1000px;
height: 30px;
margin: auto;
}
.register-wrapper{
line-height: 30px;
}
.register h1{
display: inline-block;
font-size: 12px;
color: #;
}
.register span,
.collection span{
display: inline-block;
}
/* S 导入小图标 */
.icons{
display: inline-block;
background: url(../images/icons.png) no-repeat center center;
}
.icons-home{
width: 14px;
height: 12px;
background-position: ;
margin-right: 5px;
}
.icons-sina{
width: 25px;
height: 22px;
background-position: -30px ;
}
.icons-qq{
width: 25px;
height: 22px;
background-position: -60px ;
}
.icons-coll{
width: 10px;
height: 22px;
background-position: -90px ;
} /* E 导入小图标 */ /* 头部顶层的链接 */
.register-links-wrapper{
display: inline-block;
float: right;
}
.register-links li{
display: inline-block;
margin-right: 5px;
}
.register-links li a{
display: inline-block;
font-size: 12px;
color: #34bf82;
margin-left: 5px;
}
.register-links li:last-child:before{
content :"|";
color: #34bf82;
}
<div class="register">
<h1><span class="icons icons-home"></span>XXX网站首页!</h1>
<div class="register-links-wrapper">
<ul class="register-links">
<li class="icons icons-sina"></li>
<li class="icons icons-qq"></li>
<li class="login"><a href="#">登录</a></li>
<li class="regis"><a href="#">注册</a></li>
</ul>
</div>
</div>

导入背景图,怎么让它相对父容器垂直居中显示?? vertical-align: middle;

行内块级元素垂直居中:vertical-align: middle;

vertical-align的值为:baseline middle top bottom......  (没有center这个值!!!)

/* register 头部最顶层*/
.register-wrapper{
width: 100%;
background-color: #dcdcdc;
margin-bottom: 30px;
}
.register{
width: 1000px;
height: 30px;
margin: 0 auto;
}
.register-wrapper{
line-height: 30px;
}
.register h1{
display: inline-block;
font-size: 12px;
color: #898989;
}
.register span,
.register a,
.collection span,
.collection a{
display: inline-block;
vertical-align: middle;
}
/* S 导入小图标 */
.icons{
display: inline-block;
background: url(../images/icons.png) no-repeat center center;
}
.icons-home{
width: 14px;
height: 16px;
background-position: 0 0;
margin-right: 5px;
}
.icons-sina{
width: 25px;
height: 22px;
background-position: -30px 0;
}
.icons-qq{
width: 25px;
height: 22px;
background-position: -60px 0;
}
.icons-coll{
width: 25px;
height: 22px;
background-position: -90px 0;
}
.icons-time{
position: relative;
top: -25px;
left: 240px;
width: 20px;
height: 20px;
background-position: -120px 0;
}
/* E 导入小图标 */ /* 头部顶层的链接 */
.register-links-wrapper{
display: inline-block;
float: right;
}
.register-links li{
display: inline-block;
margin-right: 5px;
vertical-align: middle;
}
.register-links li a{
display: inline-block;
font-size: 12px;
color: #34bf82;
margin-left: 5px;
}
.register-links li:last-child:before{
content :"|";
color: #34bf82;
}

(以下 vertical-align  和 line-height 之间的区别 和 vertical-align的解释来自百度知道)

1. vertical-align 适用于一些特定的显示样式,如表格单元。

2,lin-height 适用于所有的块级元素。

区别,vertical-align 相对于 line-height 的一个好处是,如果有多行文字,他也是垂直居中的。line-height是行高。测试例子如下:(例子来自百度知道)

html:

<div>
<span>测试测试,即便是多行,我也还是垂直居中对齐的。</span>
</div>

css:

div{
width:200px;
height:115px;
border:4px solid #ebcbbe;
display:table-cell;
vertical-align: middle;
}

效果如下:

(以下摘自百度知道http://zhidao.baidu.com/link?url=m85_HAZOtvrPh8_Jg8OvFJaD7LxGofYRTuwlwNWfgsJTFLGDzJfaRPoTQYIUwMGT1PE9FmzfM47Xe_OdE-Q4bK  中  learnone 的回答)

vertical-align是什么意思?先举个例子!

这句html元素中的文本为什么不能垂直居中。

<style>

span{ height:60px;vertical-align: middle; #1c93b7;

}

</style>
<span >
dddddddda</span>
我用浏览器试了一下,它的展示图如下:

vertical-align声明在很多中文文档中解释是“决定元素在垂直位置的显示”,它有下面几个参数baseline | sub | super | top | text-top | middle | bottom | text-bottom | 
baseline:与元素的基线对齐。
middle:与元素中部对齐。
sub:字下沉。
super:字上升。
text-top:文本顶部对齐。
text-bottom:文本底部对齐。
top:和本行位置最高元素对齐。
bottom:和本行位置最低元素对齐。

如果按照字面意思理解vertical-align:middle就应该是文字在元素的垂直位置的最中央,把入前面那个标签中,它的位置应该在一个height为60px的box的中央啊。但是事实上不是这样的。
如果这个vertical-align:middle用来定义一个单元格td,那么它和你想像的一样起作用,看看下面的例子
<tr>
<td style="height:80px;vertical-align:middle" onmouseover="this.style.verticalAlign='bottom'" 
onmouseout="this.style.verticalAlign='middle'">
text to align</td> 
</tr>
</table>

W3C官方对vertical-align做了下面的解释:

This property affects the vertical positioning inside a line box of the boxes generated by an inline-level element.

实 际上,一个Box中由很多行很多元素组成,vertical-align只作用于在同一行内的元素,它的垂直并不是相对于整个Box而言的。前面那个 span定义了一个60px的高度,但是这个span的Box中存在很多行,那段文本并不能对齐到span的中央。因此希望那段文本对齐span的中行, 需要给它定义一个line-height的属性,让line-height为60px,作用于一行的vertical-align就按你的想法工作了。

table的单元格,因为是一行内的元素,因此vertical-align按照我们的想法来工作,但是在span中并不是这样的。

关于背景图相对父容器垂直居中问题 —— vertical-align 和 line-height 之间的区别的更多相关文章

  1. JS编写背景图切换

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. 登录界面,body上有背景图,点击输入框时,弹出的手机键盘会把背景图顶变形,而且会把footer顶上去

    js: //防止背景图被手机键盘压缩变形 $(document).ready(function () { $('body').css({'height':$(window).height()}) }) ...

  3. echarts的图表根据父容器大小的改变而改变(弹窗easy-ui的window窗口)

    1.echarts的图表只绘制一次,所以要想大小随着父容器变化就得调方法重新绘制.所以把绘制图表的方法提出来. <div class="echart"> <div ...

  4. css — 定位、背景图、水平垂直居中

    目录 1. 定位 2. 背景图 3. 水平垂直居中 1. 定位 position:static | relative | absolute | fixed; static 静态定位 relative ...

  5. 6-Qt给父widget加上styleSheet(添加背景图)而不改变子widget的styleSheet的方法

    Qt给父widget加上styleSheet(添加背景图)而不改变子widget的styleSheet的方法 比如用stylesheet给widget加背景图,可以用qt designer修改ui文件 ...

  6. CSS常用字体属性(多出的文本隐藏,或者以省略号的形式显示)和背景样式以及背景图的2个不常用属性:background-origin和background-clip

    (一)常用的字体属性: font-weight: 属性值100-900  400等于正常 700等于bold ,数值越大,越粗 font-size:字体大小,单位可以为px或者% font-famil ...

  7. img标签实现和背景图一样的显示效果——object-fit和object-positon

    不知大家在做前端页面的时候,有没有遇到类似这样的问题:有一个不是正方形的图片,可能是宽度大于高度的,也可能是高度大于宽度的,而你又并不想用背景图的方式来做,要实现用img标签来让此图片显示出一个正方形 ...

  8. Day042---浮动 背景图设置 相对定位绝对定位

    1.练习浮动 2.文本属性和字体属性 文本对齐 ​ text-align left 左对齐 right 右对齐 center 中心对齐 justify 两边对齐 只适应于英文 text-indent ...

  9. 20190328-CSS样式一:字体样式font-、文本样式text-、背景图样式background-

    目录 CSS参考手册:http://css.doyoe.com/ 1.字体简写:font:font-style || font-variant || font-weight || font-size ...

随机推荐

  1. YourSQLDba将数据库置于紧急模式的原因浅析

    从SQL SERVER 2000 上迁移了一个数据库到SQL SERVER 2008 R2上,暂且用DataBaseName代替迁移的真实的数据库名(后面的资料也会将数据库真实的名字用DataBase ...

  2. db2start启动失败

    db2start启动失败 [db2inst1@localhost ~]$ db2start db2start: error while loading shared libraries: libaio ...

  3. Oracle索引梳理系列(八)- 索引扫描类型及分析(高效索引必备知识)

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...

  4. 最新的ADT Plugin24.0.2

    下载地址:http://pan.baidu.com/s/1o7OIhWQ 密码:z2it

  5. AD 域账号登录

    域服务数据读写,有俩种模式 1.轻量级的数据读取 _domain是服务器的域名 获取连接PrincipalContext pc = new PrincipalContext(ContextType.D ...

  6. JS中的“!!”

    var o={flag:true};  var test=!!o.flag;//等效于var test=o.flag||false;  alert(test); 由于对null与undefined用! ...

  7. Jquery 操作CheckBox ,RadioButtonList,DropDownList

    Jquery版本2.1.4 CheckBox 1.获取值: $("#chb").prop("checked"); RadioButtonList 1.获取值: ...

  8. ubuntu12.04中shell脚本无法使用source的原因及解决方法

    现象: shell脚本中source aaa.sh时提示 source: not found 原因: ls -l `which sh` 提示/bin/sh -> dash 这说明是用dash来进 ...

  9. [转载]五种常见的电子商务模式对比:B2B、B2C、C2B、C2C、O2O

    转载自http://blog.sina.com.cn/s/blog_64e090b001016843.html 转载自http://blog.sina.com.cn/s/blog_64e090b001 ...

  10. 基于thrift的微服务框架

    前一阵开源过一个基于spring-boot的rest微服务框架,今天再来一篇基于thrift的微服务加框,thrift是啥就不多了,大家自行百度或参考我之前介绍thrift的文章, thrift不仅支 ...