使用正确调整大小的图像创建弹性箱网格

时间:2018-04-17 17:10:44

标签: html css flexbox

我正在flexbox中创建一个网格,一面有图像,另一面有文字。我面临的问题是图像在网格中没有正确对齐。

这是我的代码:

/* iPhone */

@media only screen and (max-width: 768px) {
  body {
    overflow-x: hidden;
  }
  .body-img {
    background: url('background-mobile.png') no-repeat fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
    height: 100vh;
    width: 100%;
  }
  .search-img {
    background: url('search-img-mobile.png') no-repeat fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
    height: 100vh;
    padding-bottom: 10%;
  }
  .logo {
    width: 150px;
    margin-top: 15%;
  }
  h1,
  h2 {
    font-family: 'Open Sans', sans-serif;
    color: #ffffff;
    font-weight: 300;
    padding-top: 20%;
  }
  h1 {
    font-size: 1.5em;
  }
  h2 {
    font-size: 1.5em;
    padding-bottom: 15%;
  }
  h3 {
    font-family: 'Open Sans', sans-serif;
    font-size: 1.0em;
    font-weight: 400;
  }
  .searchbar {
    margin-top: 15%;
  }
  .btn {
    background-color: #1075C1;
    color: #ffffff;
    border-radius: 0px;
  }
  @keyframes bouncing {
    0% {
      bottom: 0;
    }
    50% {
      bottom: 20px;
    }
    100% {
      bottom: 0;
    }
  }
  .arrow {
    animation: bouncing 1s infinite ease-in-out;
    bottom: 0;
    display: block;
    height: 26px;
    left: 50%;
    margin-left: -25px;
    position: absolute;
    width: 26px;
  }
  .left {
    background-color: #1075C1;
  }
  .col-md-12 {
    min-height: 0px;
  }
  .container-flex {
    display: flex;
    flex-direction: column-reverse !important;
    justify-content: space-between;
  }
  .container-flex2 {
    display: flex;
    flex-direction: column-reverse !important;
    justify-content: space-between;
  }
  .searchbox {
    background-color: rgba(255, 255, 255, 0.6);
    padding-top: 5%;
    padding-left: 5%;
    padding-right: 5%;
    padding-bottom: 5%;
  }
  .form-check-label {
    font-size: 0.8em;
  }
  .btn-lg {
    padding-left: 25%;
    padding-right: 25%;
    padding-top: 5%;
    padding-bottom: 5%;
  }
  .btn-lg:hover {
    background-color: #003e6c;
  }
}

@media only screen and (min-width: 1024px) {
  body {
    overflow-x: hidden;
  }
  .body-img {
    background: url('nathan-dumlao-609935-unsplash.png') no-repeat fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
    height: 100vh;
    width: 100%;
  }
  .logo {
    width: 250px;
    margin-top: 15%;
  }
  h1,
  h2 {
    font-family: 'Open Sans', sans-serif;
    color: #ffffff;
    font-weight: 300;
    padding-top: 5%;
  }
  .searchbar {
    margin-top: 8%;
  }
  .btn {
    background-color: #1075C1;
    color: #ffffff;
    border-radius: 0px;
  }
  .arrow {
    display: none;
  }
  .container-flex {
    display: flex;
    flex-direction: row !important;
    justify-content: space-between;
    background-color: #1075C1;
  }
  .container-flex2 {
    display: flex;
    flex-direction: row-reverse;
    background-color: #1075C1;
    justify-content: space-between;
  }
  .left {
    background-color: #1075C1;
  }
  .description {
    background-color: #1075C1;
  }
}
<section id="one">
  <div class="container-flex">
    <div class="left">
      <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12 text-center">
          <h2>Sign up for our career guide and receive free insight into available jobs</h2>
        </div>
      </div>
    </div>
    <div class="right">
      <img src="section1-mobile.png" class="img-fluid">
    </div>
  </div>
  <div class="container-flex2">
    <div class="left">
      <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12 text-center">
          <h2>Answer questions as we help you with your job search</h2>
        </div>
      </div>
    </div>
    <div class="right">
      <img src="section2.png" class="img-fluid">
    </div>
  </div>
  <div class="container-flex">
    <div class="left">
      <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12 text-center">
          <h2>Get matched with companies looking for talent like you</h2>
        </div>
      </div>
    </div>
    <div class="right">
      <img src="section3.png" class="img-fluid">
    </div>
  </div>
</section>

我正在寻找的end result

我不确定我在CSS中做错了什么?

3 个答案:

答案 0 :(得分:0)

在这里要指出的一些事情可能会帮助你了解你希望他们看到的东西。

  • 首先,你的标记中的缩进是一个小小的倾斜,所以,乍一看,弄清楚它有点棘手,在你的标记和css中对缩进进行排序将使你更容易,和其他人一起调试。
  • 其次,你的标记中有很多多余的样式/类,看起来像是来自bootstrap。这些可能会影响您自己的自定义样式并导致冲突。
  • 第三,你在标记中嵌套很多,这使得很难应用样式。

我建议先简化你的标记。

我还建议将你的css剥离到最低限度,删除你的供应商前缀(-webkit-, - o-),@ keyframes声明(或者至少将它们注释掉)。你总是可以在之后添加它们或者通过预处理器或者autoprefixer将css放到后面但是为了调试它们会使事情变得困难。

Flexbox有时会有点棘手,所以I put together a pen on codepen showing我将如何处理你的模型(下面的完整代码)。它只是一个快速草案,但也许其他一些社区成员会有一些意见/建议。与此同时,要对价值观进行调查并观察结果。

我也推荐这个关于flexbox的SO question,关于水平和垂直对齐的最佳答案是相当简洁的。我还建议您查看flex box froggy以了解您的弹性盒技能。

标记:

    <section class="section">
      <div class="row">
        <div class="content">
          <span class="icon"></span>
          <span class="text">here is some text</span>
        </div>
        <img class="image" src="http://lorempixel.com/400/200" />
      </div>
      <div class="row">
        <div class="content">
          <span class="icon"></span>
          <span class="text">here is some other text</span>
        </div>
        <img class="image" src="http://lorempixel.com/400/200" />
      </div>
      <div class="row">
        <div class="content">
          <span class="icon"></span>
          <span class="text">here is some more text</span>
        </div>
        <img class="image" src="http://lorempixel.com/400/200" />
      </div>
    </section>

的CSS:

    body {
      width: 100%;
      text-align: center;
      font-family: sans-serif;
    }

    .section, .row {
      width: 75vw;
      background: dodgerblue;
      color: #fff;
      font-size: 1.5em;
    }

    .section {
      display: inline-block;
    }

    .row {
      display: flex;
      justify-content: space-between;
      flex-direction: row;
    }

    .row:nth-child(odd) {
      flex-direction: row-reverse;
    }

    .content {
      width: 50%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    .text {
      width: 50%;
      text-align: center;
    }

    .image {
      width: 50%;
      height: auto;
    }

    .icon {
      font-size: 1.5em;
    }

我希望这会有所帮助。

答案 1 :(得分:0)

我测试了屏幕大小(1024x786)和ipad pro(1024x1366)的两个片段

/* iPhone */

.left {
    width: 50%;
    left: 0px;
}

.right {
    width: 50%;
    right: 0px;
}

h2 {
    font-family: arial !important;
}

@media only screen and (max-width: 768px) {
    body {
        overflow-x: hidden;
    }
    .body-img {
        background: url('background-mobile.png') no-repeat fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        background-size: cover;
        -o-background-size: cover;
        height: 100vh;
        width: 100%;
    }
    .search-img {
        background: url('search-img-mobile.png') no-repeat fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        background-size: cover;
        -o-background-size: cover;
        height: 100vh;
        padding-bottom: 10%;
    }
    .logo {
        width: 150px;
        margin-top: 15%;
    }
    h1, h2 {
        font-family: 'Open Sans', sans-serif;
        color: #ffffff;
        font-weight: 300;
        padding-top: 20%;
    }
    h1 {
        font-size: 1.3em;
    }
    h2 {
        font-size: 1.3em;
        padding-bottom: 15%;
    }
    h3 {
        font-family: 'Open Sans', sans-serif;
        font-size: 1.0em;
        font-weight: 400;
    }
    .searchbar {
        margin-top: 15%;
    }
    .btn {
        background-color: #1075C1;
        color: #ffffff;
        border-radius: 0px;
    }
    .arrow {
        animation: bouncing 1s infinite ease-in-out;
        bottom: 0;
        display: block;
        height: 26px;
        left: 50%;
        margin-left: -25px;
        position: absolute;
        width: 26px;
    }
    .left {
        background-color: #1075C1;
    }
    .col-md-12 {
        min-height: 0px;
    }
    .container-flex {
        display: flex;
        justify-content: space-between;
    }
    .container-flex2 {
        display: flex;
        justify-content: space-between;
    }
    .searchbox {
        background-color: rgba(255, 255, 255, 0.6);
        padding-top: 5%;
        padding-left: 5%;
        padding-right: 5%;
        padding-bottom: 5%;
    }
    .form-check-label {
        font-size: 0.8em;
    }
    .btn-lg {
        padding-left: 25%;
        padding-right: 25%;
        padding-top: 5%;
        padding-bottom: 5%;
    }
    .btn-lg:hover {
        background-color: #003e6c;
    }
    .text-center {
        height: 200px;
        line-height: 200px;
        text-align: center;
        border: 2px dashed #f69c55;
    }
    .text-center h2 {
        display: inline-block;
        vertical-align: middle;
        line-height: normal;
    }
}

@media only screen and (min-width: 1024px) {
    body {
        overflow-x: hidden;
    }
    .body-img {
        background: url('nathan-dumlao-609935-unsplash.png') no-repeat fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        background-size: cover;
        -o-background-size: cover;
        height: 100vh;
        width: 100%;
    }
    .logo {
        width: 250px;
        margin-top: 15%;
    }
    h1, h2 {
        font-family: 'Open Sans', sans-serif;
        color: #ffffff;
        font-weight: 300;
        padding-top: 5%;
    }
    .searchbar {
        margin-top: 8%;
    }
    .btn {
        background-color: #1075C1;
        color: #ffffff;
        border-radius: 0px;
    }
    .arrow {
        display: none;
    }
    .container-flex {
        display: flex;
        justify-content: space-between;
        background-color: #1075C1;
    }
    .container-flex2 {
        display: flex;
        flex-direction: row-reverse;
        background-color: #1075C1;
        justify-content: space-between;
    }
    .left {
        background-color: #1075C1;
    }
    .description {
        background-color: #1075C1;
    }
    .text-center {
        height: 500px;
        line-height: 500px;
        text-align: center;
        border: 2px dashed #f69c55;
    }
    .text-center h2 {
        display: inline-block;
        vertical-align: middle;
        line-height: normal;
    }
}
<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <section id="one">
        <div class="container-flex">
            <div class="left">
                <div class="row">
                    <div class="col-sm-12 col-md-12 col-lg-12 text-center">
                        <h2>Sign up for our career guide and receive free insight into available jobs</h2>
                    </div>
                </div>
            </div>
            <div class="right">
                <img src="https://dummyimage.com/500x500/000/fff" class="img-fluid">
            </div>
        </div>
        <div class="container-flex2">
            <div class="right">
                <div class="row">
                    <div class="col-sm-12 col-md-12 col-lg-12 text-center">
                        <h2>Answer questions as we help you with your job search</h2>
                    </div>
                </div>
            </div>
            <div class="left">
                <img src="https://dummyimage.com/500x500/000/fff" class="img-fluid">
            </div>
        </div>
        <div class="container-flex">
            <div class="left">
                <div class="row">
                    <div class="col-sm-12 col-md-12 col-lg-12 text-center">
                        <h2>Get matched with companies looking for talent like you</h2>
                    </div>
                </div>
            </div>
            <div class="right">
                <img src="https://dummyimage.com/500x500/000/fff" class="img-fluid">
            </div>
        </div>
    </section>
</body>

</html>

我还附加了两种尺寸的输出截图 Screen 1024x768 screen 1024x1366

希望,您找到了解决方案。

答案 2 :(得分:0)

使用<img>代替背景图片,因为您可以使用

更轻松地使用它们
.img-responsive {
  display: block;
  height: auto;
  max-width: 100%;
}

因此,请为图片使用大尺寸,以便始终 填充 空间

使用flex-basis来处理宽度,例如左侧为60%,右侧为40%

制作容器flex-wrap: wrap,以便容器中的列被包装并连续

在容器中设置

.container > div {
  display: flex;
  flex-direction: column;
  justify-content: center; // Center vertically
  align-items: center; // Center horizontally
}

居中 水平垂直内的内容。

实施例

body {
  background: black;
}

.container {
  display: flex;
  flex-wrap: wrap;
  margin: 0 auto;
  max-width: 960px;
  border: 3px solid white;
  background: #0F75C0;
}

.container>div {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  justify-content: center;
  align-items: center;
}


/* Columns on the left side */

.container>div:nth-child(odd) {
  flex-basis: 60%;
}


/* Columns on the right side */

.container>div:nth-child(even) {
  flex-basis: 40%;
}


/* Make images responsive */

.container>div>img {
  display: block;
  height: auto;
  max-width: 100%;
}

.container .info {
  max-width: 70%;
  text-align: center;
  padding: 15px;
  font-size: 22px;
  color: white;
}

@media screen and (max-width: 320px) {
  .container {
    flex-direction: column;
  }
}
<div class="container">
  <div>
    <div class="info">
      <img src="http://via.placeholder.com/48x48?text=icon" alt="">
      <p>Sign up for our career guide</p>
    </div>
  </div>
  <div>
    <img src="http://via.placeholder.com/800x600" alt="">
  </div>
  <div>
    <img src="http://via.placeholder.com/1000x500" alt="">
  </div>
  <div>
    <div class="info">
      <img src="http://via.placeholder.com/48x48?text=icon" alt="">
      <p>Answer a few questions as we aid you in your job search</p>
    </div>
  </div>
  <div>
    <div class="info">
      <img src="http://via.placeholder.com/48x48?text=icon" alt="">
      <p>Get matched with companies looking for talent like you</p>
    </div>
  </div>
  <div>
    <img src="http://via.placeholder.com/800x600" alt="">
  </div>
</div>