【问题标题】:Image container not aligning bottom to other grid container图像容器未将底部与其他网格容器对齐
【发布时间】:2017-10-24 21:01:15
【问题描述】:

你好,

从上图可以看出,我正在尝试实现一个网格系统。第一个网格是 3 个图像,第二个网格是一列,第三个网格是浮动到第二个网格右侧的大图像。你可以在我的作品集网站上看到这张照片:http://www.irwinlitvak.com

我在第一个网格中有三个图像,宽度为 31.33%,第一个和第二个 img 的 margin-right 为 3.005%,以填满容器宽度。

在下一个网格 (grid-2) 中,我的两个图像在一列中向左浮动, (grid-2-of-3) 向右浮动,宽度为 65.556%。

我希望大图的顶部和底部占据网格的整个高度,因此大图的底部与自毁框对齐。

这里是 HTML 和 CSS:

.projects-grid {
  margin: 100px auto 0;
  width: 90%;
}

.projects-grid .title {
  margin-bottom: 20px;
  text-align: center;
}

.projects-grid h1 {
  display: inline-block;
  font-family: "Montserrat";
}

.grid-1 {
  margin-bottom: 4%;
}

.grid-1-of-3 {
  position: relative;
  width: 31.33%;
  float: left;
  overflow: hidden;
}

.grid-2-of-3 {
  position: relative;
  width: 65.556%;
  float: right;
}

.grid-1-of-3:first-child,
.grid-1-of-3:nth-child(2) {
  margin-right: 3.005%;
}

.grid-3 {
  position: relative;
  display: inline-block;
  width: 33%;
  margin-bottom: 60px;
  vertical-align: bottom;
}

.grid-5 {
  position: relative;
  display: inline-block;
  width: 20%;
  margin-bottom: 60px;
  vertical-align: bottom;
}

.box-1 {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.grid-1-of-3:first-child {
  margin-left: 0;
}

.grid-1-of-3:last-child {
  margin-right: 0;
}

.big-box {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.grid-2 {
  width: 31.33%;
  float: left;
}

.grid-2 .box-cont {
  width: 100%;
}

.grid-2 .box-cont:first-child {
  margin-bottom: 4%;
}

.grid-2 .box-cont {
  position: relative;
}
<section class="projects-grid clearfix">

  <div class="row title">
    <h2>Projects</h2>
  </div>

  <div class="grid-1 clearfix">
    <div class="grid-1-of-3">

      <div class="box-1">
        <a href="">
          <img class="bdgt-app" src="assets/budget-app-x-ps.jpg" alt="budget-app pic">
        </a>
        <a href="https://budget-app-x.herokuapp.com/" target="_blank">
          <div class="box-overlay">
            <div class="text-overlay">
              <h3>Budget-App-X</h3>
              <p>Manage your incomes and expenses in a fun and easy app.</p>
            </div>
          </div>
        </a>
      </div>

      <h3>
        <a href="https://budget-app-x.herokuapp.com/" target="_blank">Budget-App-X</a>
      </h3>
    </div>

    <div class="grid-1-of-3">
      <div class="box-1">
        <a href="">
          <img src="/assets/dice-game-x-ps.jpg" alt="dice-game">
        </a>
        <a href="https://dice-game-x.herokuapp.com/" target="_blank">
          <div class="box-overlay">
            <div class="text-overlay">
              <h3>Dice-Game</h3>
              <p>Roll the dice. Test your luck and see who racks the most points.</p>
            </div>
          </div>
        </a>
      </div>
      <h3>
        <a href="https://dice-game-x.herokuapp.com/" target="_blank">Dice-Game</a>
      </h3>

    </div>

    <div class="grid-1-of-3">
      <div class="box-1">
        <a href="">
          <img src="/assets/pomodoro-timer-x-ps.jpg" alt="pomodoro-app-timer">
        </a>
        <a href="http://pomodoro-app-timer.herokuapp.com" target="_blank">
          <div class="box-overlay">
            <div class="text-overlay">
              <h3>Pomodoro-Timer</h3>
              <p>A quick and easy solution to being productive. Set the time and get things done.</p>
            </div>
          </div>
        </a>
      </div>

      <h3>
        <a href="http://pomodoro-app-timer.herokuapp.com" target="_blank">Pomodoro-Timer</a>
      </h3>
    </div>
  </div>

  <div class="grid-2 clearfix">

    <div class="box-cont">
      <div class="box-1">
        <a href="">
          <img src="/assets/cucumberme-x-ps.jpg" alt="cucumber me">
        </a>
        <a href="http://www.cucumberme.com" target="_blank">
          <div class="box-overlay">
            <div class="text-overlay">
              <h3>CucumberMe</h3>
              <p>CucumberMe is your way of anonymously sending cucumbers to a friend, ex or anyone you want.<br><br> Go and send one today! </p>
            </div>
          </div>
        </a>
      </div>

      <h3>
        <a href="http://www.cucumberme.com" target="_blank">CucumberMe</a>
      </h3>

    </div>

    <div class="box-cont">
      <div class="box-1">
        <a href="#">
          <img src="/assets/self-destruct-x-ps.jpg" alt="to do list">
        </a>
        <a href="http://todos-irwin.herokuapp.com/" target="_blank">
          <div class="box-overlay">
            <div class="text-overlay">
              <h3>Self Destructing To-Do-List</h3>
              <p>A to-do-list that will delete itself within 10 seconds. How many chores can you list within that time? </p>
            </div>
          </div>
        </a>
      </div>
      <h3>
        <a href="http://todos-irwin.herokuapp.com/" target="_blank"> 
                                Self Destructing To-Do-List
                            </a>
      </h3>
    </div>
  </div>

  <div class="grid-2-of-3 clearfix">
    <div class="box-cont">
      <div class="big-box">
        <a href="#">
          <img src="/assets/omnifood-x-ps.jpg" alt="omnifood">
        </a>
        <a href="http://con.staging.thegateny.net/con/Omnifood/v1/" target="_blank">
          <div class="box-overlay big-overlay">
            <div class="text-overlay">
              <h3>Omnifood</h3>
              <p>My version of the food app Blue Apron. Take a look! </p>
            </div>
          </div>
        </a>
      </div>

      <h3>
        <a href="http://con.staging.thegateny.net/con/Omnifood/v1/" target="_blank">
                                Omnifood
                            </a>
      </h3>
    </div>
  </div>
</section>

【问题讨论】:

  • 如果你想让它“填满”高度,它会不会拉伸图像?
  • 这会拉伸图像,我正在努力实现这个人在他的网站上所做的事情 - wootten.ca。如果你稍微滚动一下,你会看到他有一个大图像和两个较小的浮动图像高度相同
  • 除非您知道它们的确切尺寸,否则固定宽度/高度肯定不会有帮助。我可能会尝试一下 flexboxes

标签: html css


【解决方案1】:

您可以做的就像我在下面的 sn-p 中显示的那样。你有一个容器,可以容纳你想要的所有 div,高度相同,你给它一个设定的高度。然后你给左边的项目一个容器(height: 100%)并为右边的项目创建一个 div(height: 100%)。
这样一来,左侧的项目就有一个容器,所以你可以让它们,比如说,每个 50%,并且你有一个与左侧容器高度相同的右侧项目。

为了使图像适合 div,请使用 background-size: cover 或类似的东西。 height:100%; width: auto 也适用于响应式图片。

希望对你有帮助。

.outer {
background: blue;
width: 600px;
height: 200px;
}

.leftwrap {
width: 30%;
float: left;
height: 100%;
}

.left1 {
background: purple;
width: 100%;
height: 50%;
}

.left2 {
background: orange;
width: 100%;
height: 50%;
}

.right {
background: teal;
height: 100%;
width: 70%;
float: left;
}

/** New code **/
.image {
  height: 80%;
  width: auto;
  border: 1px solid black;
}

.imagetext {
  color: white;
  text-align: center;
  border: 1px solid black;
}

.left {
  box-sizing: border-box;
  padding-bottom: 30px;
}
<div class='outer'>
  <div class='leftwrap'>
    <div class='left left1'>
      <div class="image">My image here</div>
      <div class="imagetext">Some text</div>
    </div>
    <div class='left left2'>
      <div class="image">My image here</div>
      <div class="imagetext">Some text</div>
    </div>
  </div>
  <div class='right'>3</div>
</div>

【讨论】:

  • 嘿 Friso,在我发布的设计中,我希望在第一个小框的底部留出一个边距,这样下面的文本(项目标题)就可以有空间。如果你看看我的网站 (irwinlitvak.com) 你就会明白。另外,我正在尝试模仿 wootten.ca 的设计。
  • 我更新了答案。基本上,您希望将 left1 和 left2 元素视为图像和文本的容器。使用box-sizing: border-box,您可以在不增大 div 的情况下添加填充。内边距现在是 30px 来说明示例而不是看起来不错。
猜你喜欢
  • 2013-08-31
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多