• 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;
                  100%;
               }
               .tx-center{
                   160px;
                  margin: 0 auto;
                  text-align: center;
                  background-color: #fff;
                  font-size: 24px;
               }
               html:
               <div class="title">
                  <div class="tx-center">热门点评任务</div>
              </div>
  • 相关阅读:
    POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
    POJ:2236-Wireless Network
    POJ:2010-Moo University
    POJ :3614-Sunscreen
    POJ:2395-Out of Hay
    Aizu:2224-Save your cats
    POJ:2377-Bad Cowtractors
    CSS属性大全
    博客园背景设置CSS代码
    W3C标准以及规范
  • 原文地址:https://www.cnblogs.com/zxyun/p/7048552.html
Copyright © 2020-2023  润新知