【问题标题】:Bootstrap 4: position <div> in a responsive way next to centered imageBootstrap 4:以响应方式将 <div> 定位在居中图像旁边
【发布时间】:2018-12-12 04:10:51
【问题描述】:

我正在创建一个图片库,我想通过以下方式显示图片:

因此图像应始终居中,如果右侧有足够的空间,则应将包含一些信息的&lt;div&gt; 放置在其右侧。否则,信息框应推到图片下方。

this post 中提出了类似的问题,但在他们的情况下,无论如何,信息框都将位于图像的右侧。这是他们的代码:Link

当屏幕太窄时,如何实现它位于图像下方?我正在使用 Bootstrap 4。

编辑 我尝试了 Zims 方法,但它不适用于直立图片: 我想在图片旁边放一个带有元数据的盒子... 这里是 HTML:

<div class="container-fluid">
<div class="row align-items-md-end">

    <div class="px-1 col-xl-6 offset-xl-3 text-center">
        <div class="photoboxTest">
             <div class="text-center">
                <img class="img-fluid" src="https://res.cloudinary.com/ddofbxz8d/image/upload/v1543814237/nwzv41b8gddr1uf873ki.jpg">
            </div>
            <div class="my-navigation d-flex justify-content-between">
                <a class="icon"><i class="fas fa-info-circle mx-2" style="color: transparent"></i></a>
                <div class="d-flex justify-content-center">
                    <a class="icon" href="#"><i class="fas fa-chevron-left"></i></a>
                    <a class="icon" href="/photos"><i class="fas fa-th mx-3"></i></a>
                    <a class="icon" href="#"><i class="fas fa-chevron-right"></i></a>
                </div>
                <a class="icon" href="#"><i class="fas fa-info-circle mx-2"></i></a>
            </div>
        </div>

    </div>
    <div class="px-1 col-xl-3">
        <div class="metabox ">
            <div class="my-title text-center">
                <h4>Metadata</h4>
            </div>
            <div class="my-metadata">
                <table style="width:100%">
                    <tr>
                        <td>Date</td>
                        <td>10.12.1992</td>
                    </tr>
                    <tr>
                        <td>Time</td>
                        <td>12:14</td>
                    </tr>
                    <tr>
                        <td>Location</td>
                        <td>This 1 nice location</td> 
                    </tr>
                    <tr>
                        <td>Focal length</td>
                        <td>80 mm</td>
                    </tr>
                    <tr>
                        <td>Aperture</td>
                        <td>f/10</td>
                    </tr>
                    <tr>
                        <td>Exposuse time</td>
                        <td>10 s</td>
                    </tr>
                    <tr>
                        <td>ISO</td>
                        <td>200</td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 请发布代码,显示您目前尝试过的内容。
  • 我无法试探你的解决方案,因为我最近几天很忙,我会告诉你我尝试后的结果,如果它有效,我会接受你的帖子作为解决方案;)跨度>

标签: html css twitter-bootstrap bootstrap-4


【解决方案1】:

我可以不用引导程序。我想这就是你想要的。

body {
  padding:10px;
}

.flex {
  display:flex;
  flex-direction:row;
  align-items:flex-end;
  margin:0 auto;
  justify-content:center;
}


.picture {
  margin-right:10px;
  align-content:center;
 
}
  
.text {
  max-width:calc((100% - 300px)/2);
  position:absolute;
  bottom:0;
  right:0;
}

.cont {
  position:relative;
}


@media screen and (max-width:600px) {
  .flex {
  display:flex;
  flex-direction:column;
  align-items:flex-end;
}
  
  .picture {
    margin:0 auto;
  }
  
  .text {
    margin: 10px auto 0;  
    width:300px;
    top:310px;
    right:calc((100% - 300px)/2);
    transform:translateX(calc((100% - 300px)/2));
    max-width:300px;
  }
  
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="cont">
<div class="flex">
<div class="picture">
  <img src="https://picsum.photos/300/300" alt="">
</div>
</div>
<div class="text">
  Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quaerat veniam ipsum, placeat laboriosam dolor harum? Labore, sequi in. Modi aliquam, quae officiis quaerat mollitia magni incidunt nobis ipsa inventore autem.
</div>
</div>

【讨论】:

  • 但是图片可以居中吗?
  • 谢谢,但现在图像和文本都居中了。我只希望图像居中,文本应该出现在居中的图像旁边...
  • 再看看 :) 一定是现在。你可以给文本最大宽度。
【解决方案2】:

由于该问题的标题和标记为 Bootstrap 4,因此解决方案如下。没有理由使用额外的 CSS。

<div class="container">
    <div class="row justify-content-center align-items-center align-items-md-end text-center">
        <div class="col-md-3">
            <h2>Title</h2>
            <img src="//placehold.it/400x480" alt="" class="img-fluid">
        </div>
        <div class="col-md-3">
            Nullam sapien massa, aliquam in cursus ut, ullamcorper in tortor. 
        </div>
    </div>
</div>

演示:https://www.codeply.com/go/ZTLELR7tRk

【讨论】:

  • 但在您的解决方案中,问题是图像不在宽视图中居中...
  • 然后只是偏移网格列,并为图像使用任何你想要的列宽度。我更新了演示:codeply.com/go/ZTLELR7tRk
猜你喜欢
  • 2017-03-10
  • 2018-09-28
  • 2016-03-02
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 2012-07-11
相关资源
最近更新 更多