【问题标题】:Images won't align in a line图像不会排成一行
【发布时间】:2013-07-11 02:51:23
【问题描述】:

我有 3 张具有悬停属性的图像,这将充当指向我网站上另一个页面的超链接。但是,我不知道如何将 3 个图像定位到同一行。这是带有代码的a jsFiddle page

HTML:

<div class="view view-sixth" style="margin-bottom:30px">
     <img src="champions/mode/ashe.jpg">
     <div class="mask">
     <h2>1</h2>
     <p>1</p>
         <a href="http://www.google.com" class="info" target="_blank">Google</a>
     </div>
</div>

<div class="view view-sixth" style="margin-bottom:30px">
     <img src="champions/mode/garen.jpg">
     <div class="mask">
     <h2>2</h2>
     <p>2</p>
         <a href="http://www.google.com" class="info" target="_blank">Google</a>
     </div>
</div>


<div class="view view-sixth" style="margin-bottom:30px">
     <img src="champions/mode/ryze.jpg">
     <div class="mask">
     <h2>3</h2>
     <p>3</p>
          <a href="http://www.google.com" class="info" target="_blank">Google</a>
     </div>
</div>

CSS:

.view-sixth img {
   -webkit-transition: all 0.4s ease-in-out 0.5s;
   -moz-transition: all 0.4s ease-in-out 0.5s;
   -o-transition: all 0.4s ease-in-out 0.5s;
   -ms-transition: all 0.4s ease-in-out 0.5s;
   transition: all 0.4s ease-in-out 0.5s;

}
.view-sixth .mask {
   background: rgba(146,96,91,0.5);
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=0)";
   filter: alpha(opacity=0);
   opacity: 0;
   -webkit-transition: all 0.2s ease-in 0.2s;
   -moz-transition: all 0.2s ease-in 0.2s;
   -o-transition: all 0.2s ease-in 0.2s;
   -ms-transition: all 0.2s ease-in 0.2s;
   transition: all 0.2s ease-in 0.2s;

}
.view-sixth h2 {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=0)";
   filter: alpha(opacity=0);
   opacity: 0;
   background: transparent;
   margin: 20px 40px 0px 40px;
   -webkit-transform: scale(10);
   -moz-transform: scale(10);
   -o-transform: scale(10);
   -ms-transform: scale(10);
   transform: scale(10);
   -webkit-transition: all 0.3s ease-in-out 0.1s;
   -moz-transition: all 0.3s ease-in-out 0.1s;
   -o-transition: all 0.3s ease-in-out 0.1s;
   -ms-transition: all 0.3s ease-in-out 0.1s;
   transition: all 0.3s ease-in-out 0.1s;

}
.view-sixth p {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=0)";
   filter: alpha(opacity=0);
   opacity: 0;
   -webkit-transform: scale(10);
   -moz-transform: scale(10);
   -o-transform: scale(10);
   -ms-transform: scale(10);
   transform: scale(10);
   -webkit-transition: all 0.3s ease-in-out 0.2s;
   -moz-transition: all 0.3s ease-in-out 0.2s;
   -o-transition: all 0.3s ease-in-out 0.2s;
   -ms-transition: all 0.3s ease-in-out 0.2s;
   transition: all 0.3s ease-in-out 0.2s;

}
.view-sixth a.info {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=0)";
   filter: alpha(opacity=0);
   opacity: 0;
   -webkit-transform: translateY(100px);
   -moz-transform: translateY(100px);
   -o-transform: translateY(100px);
   -ms-transform: translateY(100px);
   transform: translateY(100px);
   -webkit-transition: all 0.3s ease-in-out 0.1s;
   -moz-transition: all 0.3s ease-in-out 0.1s;
   -o-transition: all 0.3s ease-in-out 0.1s;
   -ms-transition: all 0.3s ease-in-out 0.1s;
   transition: all 0.3s ease-in-out 0.1s;

}
.view-sixth:hover .mask {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=100)";
   filter: alpha(opacity=100);
   opacity: 1;
   -webkit-transition-delay: 0s;
   -moz-transition-delay: 0s;
   -o-transition-delay: 0s;
   -ms-transition-delay: 0s;
   transition-delay: 0s;


}
.view-sixth:hover img {
   -webkit-transition-delay: 0s;
   -moz-transition-delay: 0s;
   -o-transition-delay: 0s;
   -ms-transition-delay: 0s;
   transition-delay: 0s;


}
.view-sixth:hover h2 {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=100)";
   filter: alpha(opacity=100);
   opacity: 1;
   -webkit-transform: scale(1);
   -moz-transform: scale(1);
   -o-transform: scale(1);
   -ms-transform: scale(1);
   transform: scale(1);
   -webkit-transition-delay: 0.1s;
   -moz-transition-delay: 0.1s;
   -o-transition-delay: 0.1s;
   -ms-transition-delay: 0.1s;
   transition-delay: 0.1s;

}
.view-sixth:hover p {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=100)";
   filter: alpha(opacity=100);
   opacity: 1;
   -webkit-transform: scale(1);
   -moz-transform: scale(1);
   -o-transform: scale(1);
   -ms-transform: scale(1);
   transform: scale(1);
   -webkit-transition-delay: 0.2s;
   -moz-transition-delay: 0.2s;
   -o-transition-delay: 0.2s;
   -ms-transition-delay: 0.2s;
   transition-delay: 0.2s;

}
.view-sixth:hover a.info {
   -ms-filter: "progid: DXImageTransform.Microsoft.Alpha(Opacity=100)";
   filter: alpha(opacity=100);
   opacity: 1;
   -webkit-transform: translateY(0px);
   -moz-transform: translateY(0px);
   -o-transform: translateY(0px);
   -ms-transform: translateY(0px);
   transform: translateY(0px);
   -webkit-transition-delay: 0.3s;
   -moz-transition-delay: 0.3s;
   -o-transition-delay: 0.3s;
   -ms-transition-delay: 0.3s;
   transition-delay: 0.3s;

}

.view {
   width: 260px;
   height: 390px;
   border:  solid purple;
   position: relative;
   text-align: center;
   box-shadow: 1px 1px 2px purple;
   cursor: default;


}
.view .mask,.view .content {
   width: 260px;
   height: 390px;
   position: absolute;
      background: rgba(0, 0, 0, 0.7);
   overflow: hidden;
   top: 0;
   left: 0;

}
.view img {
   display: block;
   position: relative;

}
.view h2 {
   text-transform: uppercase;
   color: white;
   text-align: center;
   position: relative;
   font-family: Candara;
   font-size: 20px;
   padding: 30px;
}
.view p {
   font-family: Candara, serif;
   font-size: 20px;
   position: relative;
   color: white;
   padding: 10px 20px 20px;
   text-align: center;
}
.view a.info {
   display: inline-block;
   text-decoration: none;
   padding: 7px 14px;
   background: white;
   color: black;
   text-transform: uppercase;
   -webkit-box-shadow: 0 0 1px #000;
   -moz-box-shadow: 0 0 1px #000;
   box-shadow: 0 0 1px #000;
   border-radius: 5px;
}
.view a.info:hover {
   -webkit-box-shadow: 0 0 5px #000;
   -moz-box-shadow: 0 0 5px #000;
   box-shadow: 0 0 5px #000;
}

谁能给我一些指点?


编辑:感谢大家的回复。我的问题现在已经解决了。 :D

【问题讨论】:

    标签: html css image alignment


    【解决方案1】:

    float: left; 添加到.view 规则集:

    .view {
       width: 260px;
       height: 390px;
       border: solid #800080;
       position: relative;
       text-align: center;
       box-shadow: 1px 1px 2px #800080;
       cursor: default;
       float: left; /* ← add this */
    }
    

    A fixed jsFiddle

    【讨论】:

    • 抱歉,还有一个问题。我应该使用什么代码将 3 张图片居中?
    • 如果您使用填充选项按自己的方式工作,它不起作用吗?我看不到完整的图像,甚至看不到要处理它的小提琴。
    • 我正在考虑使用边距和自动值来保持图像相对于浏览器大小居中,但我不确定如何或在何处放置代码。这是jsfiddle链接。 jsfiddle.net/sugarcraving/y9GXs/4
    • 要使页面中的 3 个图像居中,您需要创建一个容器 div 来包装 3 个 .view div,并为其提供 3 个 div 组合的宽度并添加边距:0 auto;因此,您将拥有一个宽度为 800 像素(或多或少)且边距为 0 的容器 div; jsfiddle.net/mb2R5/embedded/result
    【解决方案2】:

    通过添加:

    .view-sixth {
        display:inline-block;
    width:33%;    
    }
    

    它为我解决了问题:D

    【讨论】:

    • That solution 在 Firefox 22 上对我不起作用。只有两个框在一行上,并且框与黑色悬停覆盖层未对齐。
    • 这个:-moz-box-sizing:border-box; -webkit-box-sizing:边框框; box-sizing:边框框;应该修复它。
    • 或者如果不需要响应,您可以手动修复witdh。
    【解决方案3】:

    在您的视图类规则中,添加float:left;

    jsFiddle example

    【讨论】:

      【解决方案4】:

      您只需要float: left 上的.view div

      看看 - http://jsfiddle.net/8P7rB/

      .view {
         width: 260px;
         height: 390px;
         border:  solid purple;
         position: relative;
         text-align: center;
         box-shadow: 1px 1px 2px purple;
         cursor: default;
          float: left;
          margin-right: 10px;
      }
      

      【讨论】:

        【解决方案5】:

        将包含图像的divs 的CSS display 属性设置为inline-block

        .view {
            display: inline-block;
        }
        

        jsFiddle

        如果有足够的空间,图像应该对齐。如果您希望即使空间不足也始终对齐图像,请将它们包含在容器中并设置容器的min-width

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-29
          • 1970-01-01
          • 2018-09-17
          • 1970-01-01
          • 2018-09-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多