帮助了 AmazingCounters.com 位小伙伴

DIV+CSS 让同一行的图片和文字对齐【转藏】

DIV+CSS 让同一行的图片和文字对齐

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

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.  彘丑先生原创文章,转载请注明出处并保留原文所有链接,谢谢!

 

 

 

 css:
           .title{
             position: relative;
             width: 100%;
           }
           .tx-center{
              width: 160px;
              margin: 0 auto;
              text-align: center;
              background-color: #fff;
              font-size: 24px;
           }
           html:
           <div class="title">
              <div class="tx-center">热门点评任务</div>
          </div>

 

posted on 2017-06-19 14:30  云的旋律  阅读(6006)  评论(0编辑  收藏  举报

导航

前端攻城狮分享群