【问题标题】:How to position an image on top of another image within the bottom image's dimensions?如何在底部图像的尺寸内将图像放置在另一个图像的顶部?
【发布时间】:2014-10-04 04:23:55
【问题描述】:

我正在尝试为项目组合创建画廊风格的布局。这一切都通过 Bootstrap 3 完成。

在桌面屏幕尺寸(Bootstrap 术语中的 md 和 lg)上,我使用 4 列。在平板电脑屏幕尺寸 (sm) 上,我使用两列,而在移动屏幕尺寸上,我使用 1 列。

画廊中的每个项目都有一个标题、一个简短的摘录和一个图像缩略图。对于每个项目,我将所有这些东西都放在一个小部件区域内。

主要问题在于缩略图。设计师的想法是将每个项目的缩略图放置在 iMac 显示器图像中。为了让这更复杂一点,iMac 显示器的黑色边框伸出了小部件区域。

因为一张图片说明的不仅仅是文字,这里有一个小例子:

我在 iMac 图像中定位实际缩略图时遇到了问题,因此即使我调整屏幕大小以及到达 Bootstrap 网格的断点时,它仍停留在 iMac 图像的灰色区域内。

到目前为止,我对我一直在尝试的东西做了一个孤立的小提琴:http://jsfiddle.net/9ek3eqmv/

图库中每个项目元素的 html 如下所示:

<div class="col-sm-6 col-md-3 project-widget-container">

        <div class="project-widget-wrap" style="border: 1px solid green">

            <div class="project-widget">

                <div class="project-title">
                    <h4>Project 2</h4>
                </div>

                <div class="project-excerpt">
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                </div>

                <!--<div class="project-thumbnail-placeholder"></div>-->

            </div>

            <div class="project-image-container">
                <img class="imac img-responsive" src="http://s29.postimg.org/b5kegwt53/small_mac.png">
                <img class="featured-image img-responsive" src="http://s29.postimg.org/ryb2fc5tz/test_2.jpg">
            </div>

        </div>

    </div>

我做了一个类 .project-widget-wrap 来获得 imac 的突出效果。这只是一个带有 iMac 图像黑色边框填充的包装器:

.project-widget-wrap {
        width: 100%;
        padding: 0 10px;
        margin-bottom: 10px;
        position: relative;
    }

在该元素中,我有 .project-widget 类 div 来获取白色小部件背景并包含其他元素。

我为 .project-widget 提供了底部填充,以便为 iMac 图像和 iMac 顶部的另一个图像创建空间:

.project-widget {
        background-color: white;
        padding-top: 30px;
        padding-bottom: 177px;
        border: 1px solid #eee;
        box-shadow: -5px -5px 5px -5px #eee, 5px -5px 5px -5px #eee;
    }

使用媒体查询,我根据需要更改填充。

我还将包装器 .project-widget-wrap 相对定位,因此我可以将图像容器定位在小部件的底部,在 project-image-container div 上使用 position: absolute:

.project-widget-wrap {
        width: 100%;
        padding: 0 10px;
        margin-bottom: 10px;
        position: relative;
    }

.project-image-container {
        position: absolute;
        bottom: 0;
        width: 100%;
        height: 177px;
    }

.project-image-container img {
        position: absolute;
        bottom: 0;
    }

知道如何设置它的样式,以便特色图像保留在 iMac 屏幕图像内并使其缩放到不同的屏幕尺寸?

【问题讨论】:

    标签: css twitter-bootstrap responsive-design twitter-bootstrap-3


    【解决方案1】:

    我更改了很多 CSS。您确实需要一个 .container 围绕一行,否则填充将关闭。要做的事情是找到比率。我不知道图像是正方形还是什么,但这就是你所拥有的。否则,我建议您只制作复合材料。或者只是在它周围使用简单的线条(绘制带有边框和形状的“监视器”),它会看起来更好,体积更小。

    演示:http://jsbin.com/lodej/1/

    http://jsbin.com/lodej/1/edit

    另一个有背景的版本:http://jsbin.com/lodej/2

    HTML

      <div class="container">
         <div class="row">
           
           
        <div class="col-sm-6 col-md-3 project-widget-container">
           <section>
              <div class="project-title">
                 <h4>Project 1</h4>
              </div>
              <div class="project-excerpt">
                 Lorem <a href="#">Test</a> dolor sit amet, consectetur adipiscing elit.
              </div>
              <div class="project-image-container">
                 <div class="thumb"><img class="img-responsive" src="http://bombdiggitydesign.com/jsbin/xffdfd.png"></div>
                 <img class="imac img-responsive" src="http://s29.postimg.org/b5kegwt53/small_mac.png"> 
              </div>
           </section>
        </div>
        <!-- col -->
       
       
        <div class="col-sm-6 col-md-3 project-widget-container">
           <section>
              <div class="project-title">
                 <h4>Project 1</h4>
              </div>
              <div class="project-excerpt">
                 Lorem <a href="#">Test</a> dolor sit amet, consectetur adipiscing elit.
              </div>
              <div class="project-image-container">
                 <div class="thumb"><img class="img-responsive" src="http://bombdiggitydesign.com/jsbin/xffdfd.png"></div>
                 <img class="imac img-responsive" src="http://s29.postimg.org/b5kegwt53/small_mac.png"> 
              </div>
           </section>
        </div>
        <!-- col -->   
       
       <div class="clearfix visible-sm"></div>
    
        <div class="col-sm-6 col-md-3 project-widget-container">
           <section>
              <div class="project-title">
                 <h4>Project 1</h4>
              </div>
              <div class="project-excerpt">
                 Lorem <a href="#">Test</a> dolor sit amet, consectetur adipiscing elit.
              </div>
              <div class="project-image-container">
                 <div class="thumb"><img class="img-responsive" src="http://bombdiggitydesign.com/jsbin/xffdfd.png"></div>
                 <img class="imac img-responsive" src="http://s29.postimg.org/b5kegwt53/small_mac.png"> 
              </div>
           </section>
        </div>
        <!-- col -->           
    
        <div class="col-sm-6 col-md-3 project-widget-container">
           <section>
              <div class="project-title">
                 <h4>Project 1</h4>
              </div>
              <div class="project-excerpt">
                 Lorem <a href="#">Test</a> dolor sit amet, consectetur adipiscing elit.
              </div>
              <div class="project-image-container">
                 <div class="thumb"><img class="img-responsive" src="http://bombdiggitydesign.com/jsbin/xffdfd.png"></div>
                 <img class="imac img-responsive" src="http://s29.postimg.org/b5kegwt53/small_mac.png"> 
              </div>
           </section>
        </div>
        <!-- col -->           
       
       <div class="clearfix visible-sm"></div>
       
     </div>
     <!-- row -->
    

    CSS

    .project-widget-container {
        position: relative;
        margin-bottom: 30px;
    }
    .project-widget-container section {
        position: relative;
        padding-top: 20px;
        margin-left: 10px;
    }
    .project-widget-container section:before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        right: 20px;
        bottom: 0;
        border: 1px solid #eee;
        z-index: -1;
    }
    .project-widget-container section:after {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        right: 20px;
        bottom: 0;
        box-shadow: -5px -5px 5px -5px #eee, 5px -5px 5px -5px #eee;
        z-index: -1;
    }
    .project-title {
        border-left: 2px solid #660061;
        padding-left: 20px;
    }
    .project-title h4 {
        color: #660061;
        font-weight: bold;
        font-size: 1.4em;
        line-height: 50px;
    }
    .project-excerpt {
        color: #666;
        font-size: 1.1em;
        padding: 20px 20px 0 20px;
        line-height: 1.2em;
        height: 60px;
    }
    img.imac {
        width: 100%;
        position: absolute;
        left: 0;
        bottom: 0;
        right: 0;
    }
    .project-image-container {
        position: relative;
        left: -10px;
        padding-top: 85%;
    }
    .thumb img {
        width: 50%;
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -25%;
    }
    .thumb {
        position: absolute;
        z-index: 1;
        bottom: 18%;
        left: 0;
        right: 0;
    }
    

    【讨论】:

    • 哇,非常感谢!我只是将它放入实际页面并让它工作。实际上,我确实在行周围有一个容器元素,但我只是为了小提琴而剥离了所有东西。这些图像不是正方形,它们有不同的尺寸,所以我仍然需要想办法解决这个问题,但我可能只会渲染限制宽度和高度的拇指。再次感谢,非常感谢您的帮助!
    【解决方案2】:

    您可以创建一个纯 CSS 的 iMac,然后将图像放入其中...

    像这样:http://jsfiddle.net/9ek3eqmv/10/

    .project-widget {
        width:30%;   
        margin:auto;
    }
    .imac {
        display:block;
        position:relative;
        padding-bottom:9%;
        margin-bottom:16%;
        border-radius:5px;
        background: #a0a0a0;/* Old browsers */
        background: -moz-linear-gradient(left, #a0a0a0 0%, #e0e1e3 100%);/* FF3.6+ */
        background: -webkit-gradient(linear, left top, right top, color-stop(0%, #a0a0a0), color-    stop(100%, #e0e1e3));/* Chrome,Safari4+ */
        background: -webkit-linear-gradient(left, #a0a0a0 0%, #e0e1e3 100%);/* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(left, #a0a0a0 0%, #e0e1e3 100%);/* Opera 11.10+ */
        background: -ms-linear-gradient(left, #a0a0a0 0%, #e0e1e3 100%);/* IE10+ */
        background: linear-gradient(to right, #a0a0a0 0%, #e0e1e3 100%);/* W3C */
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#a0a0a0', endColorstr='#e0e1e3', GradientType=1);/* IE6-9 */
    }
    .imac .bezel {
        display:block;
        position:relative;
        background-color:black;
        border-radius:5px 5px 0px 0px;
        padding:4.25%;
        overflow:hidden;
    }
    .imac .bezel:after {
        content:"";
        display:block;
        position:absolute;
        background-color:rgba(255, 255, 255, 0.2);
        background: -moz-linear-gradient(top,  rgba(255,255,255,0.25) 0%, rgba(255,252,252,0) 100%);/* FF3.6+ */
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0.25)), color-stop(100%,rgba(255,252,252,0))); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(top,  rgba(255,255,255,0.25) 0%,rgba(255,252,252,0) 100%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(top,  rgba(255,255,255,0.25) 0%,rgba(255,252,252,0) 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top,  rgba(255,255,255,0.25) 0%,rgba(255,252,252,0) 100%); /* IE10+ */
        background: linear-gradient(to bottom,  rgba(255,255,255,0.25) 0%,rgba(255,252,252,0) 100%); /* W3C */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#40ffffff', endColorstr='#00fffcfc',GradientType=0 ); /* IE6-9 */
        top:0%;
        bottom:-20%;
        right:0;
        left:60%;
        -webkit-transform: rotate(-25deg);
        -webkit-transform-origin: 0% 0% -o-transform: rotate(-25deg);
        -o-transform-origin: 0% 0% transform: rotate(-25deg);
        transform-origin: 0% 0%;
    }
    .imac .screen {
        display:block;
        position:relative;
        overflow:hidden;
        width:100%;
        height:100%;
        background:#444 no-repeat center center;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
    .imac .screen:before {
        content:"";
        display: block;
        padding-top: 56.25%; /* imac has aspect ratio of 16:9*/
    }
    .imac .stand {
        display:block;
        position:absolute;
        top:100%;
        left:0;
        right:0;
        bottom:-22%;
        width:20%;
        margin:auto;
        background:#ddd;
        background: rgb(141, 142, 146); /* Old browsers */
        background: -moz-linear-gradient(top, rgba(141, 142, 146, 1) 3%, rgba(226, 227, 230, 1) 38%, rgba(244, 244, 244, 1) 51%, rgba(128, 129, 132, 1) 64%, rgba(193, 193, 193, 1) 77%, rgba(224, 224, 224, 1) 95%); /* FF3.6+ */
        background: -webkit-gradient(linear, left top, left bottom, color-stop(3%, rgba(141, 142, 146, 1)), color-stop(38%, rgba(226, 227, 230, 1)), color-stop(51%, rgba(244, 244, 244, 1)), color-stop(64%, rgba(128, 129, 132, 1)), color-stop(77%, rgba(193, 193, 193, 1)), color-stop(95%, rgba(224, 224, 224, 1))); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(top, rgba(141, 142, 146, 1) 3%, rgba(226, 227, 230, 1) 38%, rgba(244, 244, 244, 1) 51%, rgba(128, 129, 132, 1) 64%, rgba(193, 193, 193, 1) 77%, rgba(224, 224, 224, 1) 95%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(top, rgba(141, 142, 146, 1) 3%, rgba(226, 227, 230, 1) 38%, rgba(244, 244, 244, 1) 51%, rgba(128, 129, 132, 1) 64%, rgba(193, 193, 193, 1) 77%, rgba(224, 224, 224, 1) 95%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top, rgba(141, 142, 146, 1) 3%, rgba(226, 227, 230, 1) 38%, rgba(244, 244, 244, 1) 51%, rgba(128, 129, 132, 1) 64%, rgba(193, 193, 193, 1) 77%, rgba(224, 224, 224, 1) 95%); /* IE10+ */
        background: linear-gradient(to bottom, rgba(141, 142, 146, 1) 3%, rgba(226, 227, 230, 1) 38%, rgba(244, 244, 244, 1) 51%, rgba(128, 129, 132, 1) 64%, rgba(193, 193, 193, 1) 77%, rgba(224, 224, 224, 1) 95%); /* W3C */
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8d8e92', endColorstr='#e0e0e0', GradientType=0); /* IE6-9 */
    }
    .imac .stand:before, .imac .stand:after {
        content:"";
        display:block;
        position:absolute;
        bottom:0;
        width:2px;
        height:2px;
        border:10px solid grey;
        border-color:transparent #e0e0e0;
    }
    .imac .stand:before {
        right:100%;
        border-width:9px 12px 0px 0px;
    }
    .imac .stand:after {
        left:100%;
        border-width:9px 0px 0px 12px;
    }
    <div class="project-widget">    
    
    <div class="imac">
        <div class="bezel">
            <div class="screen" style="background-image:url(http://lorempixel.com/400/250/abstract)" >
            </div>
        </div>
        <div class="stand"></div>
    </div>
      
    </div>

    【讨论】:

      【解决方案3】:

      试试http://jsfiddle.net/webspilka/oa74zbb6/

      也许它需要一个 java 脚本来设置 .project-image-container.height = .imac.height 的高度 和

      img.featured-image {
          max-height: 150px;
          position:absolute;
          top: 0;
       }
      
      $('.project-image-container').outerHeight('setHeightChild.imac')
      

      【讨论】:

        【解决方案4】:

        如果您只是将“imac”图像设置为背景图像,是否会使您的工作更轻松,这样您总是可以使用@media(无论您的调整是什么)CSS 在任何尺寸的屏幕上获得效果。这也将帮助您消除所有填充问题。这里有一些标记可以帮助您入门。希望对您有所帮助!

        <!--Your HTML Mark-Up-->
        

        <div class="project-widget">
        
          <div class="project-title">
            <h4>Project 2</h4>
          </div>
        
          <div class="project-excerpt">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </div>
        
          <!--<div class="project-thumbnail-placeholder"></div>-->
        
        </div>
        
        <div id="project-image-container">
          <!--I removed the "iMac" and replaced it in your background"-->
          <img class="featured-image" src="http://s29.postimg.org/ryb2fc5tz/test_2.jpg">
        </div>
        
        </div>
        
        </div>
        
        
        /* Your CCS */
        
        #project-image-container {
        min-width: 255px;
        min-height: 177px;
        background-image: url("http://s29.postimg.org/b5kegwt53/small_mac.png");
        /*Just Place Mac Images as background*/
        background-repeat: no-repeat;
        }
        .featured-image {
        width: 242px;
        height: 134px;
        padding: 6px;
        }
        
        /* You can now adjust this for all your media sized with @media(....)*/
        

        Fiddle

        【讨论】:

          猜你喜欢
          • 2018-11-06
          • 1970-01-01
          • 2012-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-29
          • 1970-01-01
          • 2015-09-16
          相关资源
          最近更新 更多