欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

css 如何让文字和图片对齐?问题图片和代码内详 自己亲自试的 有大用

shiping1 的头像

我的方法 
<div class="msfc-more"><img src="{dede:global.cfg_templets_skin/}/images/more1f.png" alt=""> 更多</div>

.msfc-more{
float: right;
font-size: 18px;
height:25px;
line-height:25px;
 
}
.msfc-more img{
vertical-align:middle;
}





<h3><img src="wp-content/themes/HotNewspro/images/bulletin.gif">教务通知</h3>

外部CSS:

#sidebar h3 {        

padding: 0px 0px 0px 5px;

height:27px;

line-height:40px; 

color: #393939;

font-size: 12px;

font-weight: normal;

}

#sidebar h3 img{

vertical-align:middle

}

我想让教务通知和这个小喇叭图片距离底下的灰色边界10px 调整其中的padding中的top参数无法实现效果

请高手帮帮我!

提问者采纳
 
① 首先,我要知道你这个小喇叭图片的尺寸,并且告诉你:

img标签和文字在同一行的时候,默认 图片底部和文字底部对齐,所以这一行里面,img的高度将会影响这一行的高度

② 你设定了 #sidebar h3 {height:27px; line-height:40px}
当你line-height大于height的时候,文字固然就下沉,建议尝试line-height:27px,这样可以令文字居中

来自 http://zhidao.baidu.com/question/1753540964197633988.html



在div+css布局中,如果一行(或一个DIV)内容中有图片和文字的话,图片和文字往往会一个在上一个在下,这是一个新手都会遇到问题,我的解决方法有三:

处理前:                                                                    处理后:

DIV+CSS 让同一行的图片和文字对齐 - 彘丑先生 - 彘丑网志     DIV+CSS 让同一行的图片和文字对齐 - 彘丑先生 - 彘丑网志

 

1.添加CSS属性:vertical-align:middle;

代码: <style> a img{border:none} .testdiv *{ vertical-align:middle; } </style>

<div class="testdiv">  <a href="http://www.zc144.com/"><img src="http://www.zc144.com/download/Template.jpg" alt="这里是图片" /></a>  <span>这里是文字,看看文字对齐了没</span> </div>

[查看效果]

2.div嵌套:将图片和文字分别套上一个div,就可以利用 margin 熟悉任意定位了

代码: <style> a img{border:none} .testIMG{ float:left; display:inline; margin-top:0; margin-left:5px; } .testTXT{ float:left; display:inline; margin-top:20; margin-left:5px; } </style>

<div class="testdiv">  <div class="testIMG"><a href="http://www.zc144.com/"><img src="http://www.zc144.com/download/Template.jpg" alt="这里是图片" /></a></div>  <div class="testTXT"><span>这里是文字,看看文字对齐了没</span></div> </div>

[查看效果]

3.把图片作为背景:如果你的图片只是用来作为小图标放在文字的左侧,那就推荐用这个方法,图片设置成文字的背景,不循环,定位在左侧上下居中,文字向左padding图片的宽度加几个像素。

代码: <style> a img{border:none} .testTXT{ height:60px; line-height:60px; padding-left:65px; background:url(http://www.zc144.com/download/Template.jpg) no-repeat left center } </style>

<div class="testdiv">  <div class="testTXT"><span>这里是文字,看看文字对齐了没</span></div> </div>

[查看效果]

PS.  彘丑先生原创文章,转载请注明出处并保留原文所有链接,谢谢!

来自 http://www.cnblogs.com/yisheng/archive/2012/12/18/2823089.html


普通分类: