如何在div中对齐文本

时间:2014-02-25 20:56:33

标签: html css

我试图显示4张带有标题,说明和删除链接的图片。我无法在我的框中找到float right的链接。

这是html

<div id="container">
  <div class="image-wrapper">
    <div id="image-container">
     <img src="gallery_traditional_1.jpg" width="200" height="100"><br>
     <span class="img-info">Title:</span>
     <span class="img-info">Description:</span>
      <span class="img-info alignright"><a href="#">Delete</a></span>
 </div>
    </div>
  </div><!--end container --> 

这是css

 #container {
   width: 50%;
 }

 .image-wrapper {
   display: inline-block;
   width:200px;
   padding: 10px;
  }

  #image-container {
    color: #898989;
background:#F9F9F9;
border: solid 1px #ddd; 
border-radius:10px;
-moz-border-radius: 10px;
    -webkit-border-radius: 10px;
box-shadow: 0px 0px 10px #888;
-moz-box-shadow: 0px 0px 10px #888;
-webkit-box-shadow: 0px 0px 10px #888;
margin:5px auto;
padding:5px;
width:200px;
   }

   .img-info {
display: block; 
   }

   .alignleft {
float:left;
   }

   .alignright {
float:right;
   }

图片框的html重复了3次。如何将第三个跨度链接浮动到左侧?任何帮助都会很棒。

4 个答案:

答案 0 :(得分:2)

添加margin-top:-29px;应该做到这一点。

DEMO http://jsfiddle.net/4RZJx/1

HTML:

<div id="container">
        <div class="image-wrapper">
            <div id="image-container">
                <img src="gallery_traditional_1.jpg" width="200" height="100"><br>
                <span class="img-info">Title:</span>
                <span class="img-info">Description:</span>
                <span class="img-info"><a href="#">Delete</a></span>
            </div>
            <span class="img-info alignright"><a href="#">Delete</a></span>
        </div>

        <div class="image-wrapper">
            <div id="image-container">
                <img src="gallery_traditional_1.jpg" width="200" height="100"><br>
                <span class="img-info">Title:</span>
                <span class="img-info">Description:</span>
                <span class="img-info">Description:</span>
            </div>
            <span class="img-info alignright"><a href="#">Delete</a></span>
        </div> 

        <div class="image-wrapper">
            <div id="image-container">
                <img src="gallery_traditional_1.jpg" width="200" height="100"><br>
                <span class="img-info">Title:</span>
                <span class="img-info">Description:</span>
            </div>
            <span class="img-info alignright"><a href="#">Delete</a></span>
        </div>

        <div class="image-wrapper">
            <div id="image-container">
                <img src="gallery_traditional_1.jpg" width="200" height="100"><br>
                <span class="img-info">Title:</span>
                <span class="img-info">Description:</span>

            </div>
            <span class="img-info alignright"><a href="#">Delete</a></span>
        </div>

    </div><!--end container -->

CSS:

#container {
            width: 50%;
        }
        .image-wrapper {
            display: inline-block;
            width:200px;
            padding: 10px;
        }

        #image-container {
            color: #898989;
            background:#F9F9F9;
            border: solid 1px #ddd; 
            border-radius:10px;
            -moz-border-radius: 10px;
            -webkit-border-radius: 10px;
            box-shadow: 0px 0px 10px #888;
            -moz-box-shadow: 0px 0px 10px #888;
            -webkit-box-shadow: 0px 0px 10px #888;
            margin:5px auto;
            padding:5px;
            width:200px;
        }

        .img-info {
        display: block; 
        }

        .alignleft {
            clear: both;
            float:left;
        }

        .alignright {
            float:right;
            margin-top: -29px;
        }

答案 1 :(得分:0)

使用text-align:

.alignleft {
    text-align: left;
}

.alignright {
    text-align: right;
}

float最适合用于希望图片向右移动,并希望文本在其周围流动等等。因为它还会导致其他布局问题,您也可以尝试text-align

http://jsfiddle.net/4RZJx/3/

答案 2 :(得分:0)

尝试这种快速修复,但是,您可能需要修改宽度等:

DEMO:http://jsfiddle.net/4RZJx/

<div id="container">
  <div class="image-wrapper">
    <div id="image-container">
     <img src="gallery_traditional_1.jpg" width="200" height="100"><br>
         <div class="info-container">
         <div class="alignleft">
     <span class="img-info">Title:</span>
     <span class="img-info">Description:</span>
             </div>
      <span class="img-info alignright"><a href="#">Delete</a>
             </span>
 </div>
         </div>
    </div>
  </div><!--end container --> 

CSS:

 #container {
   width: 50%;
 }

 .image-wrapper {
   display: inline-block;
   width:200px;
   padding: 10px;
  }

  #image-container {
    color: #898989;
background:#F9F9F9;
border: solid 1px #ddd; 
border-radius:10px;
-moz-border-radius: 10px;
    -webkit-border-radius: 10px;
box-shadow: 0px 0px 10px #888;
-moz-box-shadow: 0px 0px 10px #888;
-webkit-box-shadow: 0px 0px 10px #888;
margin:5px auto;
padding:5px;
width:200px;
   }
.info-container{
    overflow: hidden;
}
   .img-info {
display: block; 
   }

   .alignleft {
float:left;
   }

   .alignright {
float:right;
   }

答案 3 :(得分:0)

首先,您在提供的代码中使用.alignleft在哪里?

其次,.alignright范围有两个类:img-infoalignright。对于img-info,您有display-block。这是多余的,因为float属性将生成元素display-block

您需要做的就是将overflow: hidden添加到主div(图像容器)。应用于链接的浮动使其表现得好像它不在同一“层”中。添加overflow-hidden将解决此问题。 Jsfiddle - http://jsfiddle.net/piedoom/6jTyD/

enter image description here