zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    spark编译
    使用MapReduce实现两个文件的Join操作
    响应式 Web 设计
    响应式 Web 设计
    900W+数据只用300ms搞定!SQL查询优化这样做最快耗时347ms
    响应式 Web 设计
    CSS3 多媒体查询实例
    CSS3 多媒体查询:查找设备的类型,CSS3 根据设置自适应显示
    CSS3 弹性盒子(Flex Box):确保元素拥有恰当的行为的布局方式
    CSS3 框大小:padding(内边距) 和 border(边框)
  • 原文地址:https://www.cnblogs.com/zxyun/p/7048552.html
Copyright © 2011-2022 走看看