调整浏览器大小时对齐Flexbox部分

时间:2016-02-19 17:17:57

标签: html css css3 flexbox

enter image description here

以上是它在1920宽度浏览器上的外观截图。这基本上是我想要的布局,但是当我调整浏览器大小时,它看起来像这样: enter image description here

我希望这些方框在各个部分的左右两侧都有没有白色间距,并保持它们100%宽度,有/无间距
我意识到间距正在发生,因为代码是如何设置的,其中中间部分有将其推开的边缘,但我不知道如何设置它以实现第一个截图和这个效果。

小提琴:https://jsfiddle.net/jzhang172/r641pmzb/

body,
html {
  margin: 0px;
  font-family: lato-reg;
  font-size: 16px;
  color: #3c3c3c;
  overflow-x: hidden;
}
img {
  width: 100%;
  height: 100%;
}
.section {
  position: relative;
}
/*--------------------------------------Fifth Section--*/

.flex-container {
  margin-top: 10px;
  width: 100%;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-flex-flow: row wrap;
  -ms-flex-flow: row wrap;
  flex-flow: row wrap;
}
.work-indiv {
  width: 540px;
  -webkit-flex: auto;
  -ms-flex: auto;
  flex: auto;
  display: inline-block;
  height: 237px;
  position: relative;
  margin-bottom: 10px;
  cursor: pointer;
  transition: 1s;
}
.work-indiv:hover img {
  opacity: .6;
  transition: .6s;
}
.middle {
  padding-left: 10px;
  padding-right: 10px;
  width: 783px;
}
.work-indiv span {
  position: absolute;
  clear: both;
  bottom: 0;
  left: 0;
  color: White;
  margin-left: 40px;
  text-transform: uppercase;
  margin-bottom: 40px;
  font-size: 2.125em;
  font-family: latobold;
  letter-spacing: .16em;
}
.middle span {
  margin-left: 50px;
}
<div class="section" id="fifth">
  <div class="flex-container">
    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Topworkz">
      <span>Topworkz</span>
    </div>
    <div class="work-indiv middle">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Joint">
      <span>Joint</span>
    </div>
    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Market">
      <span>Market</span>
    </div>

    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Worktop">
      <span>Worktop</span>
    </div>
    <div class="work-indiv middle">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Lable">
      <span>Lable</span>
    </div>
    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="KYIV">
      <span>KYIV</span>
    </div>


  </div>

2 个答案:

答案 0 :(得分:1)

尝试删除媒体查询中padding上的左右.middle,以获得更小的屏幕尺寸。

你的代码中有这个:

.middle {
    padding-left: 10px;
    padding-right: 10px;
    width: 783px;
}

尝试此调整:

@media screen and ( max-width: 1200px) {
    .middle { 
         padding-left: 0;
         padding-right: 0; }
}

现在,当屏幕尺寸小于1200px宽时,将删除填充。

Revised Demo

答案 1 :(得分:0)

将元素的填充更改为始终 5px。

现在,它们之间的分离是10px,这就是你想要的。

要使最左侧元素的填充再次显示为零,请在Flex容器上设置-5px的负边距。这将产生元素完美排列的错觉

body,
html {
  margin: 0px;
  font-family: lato-reg;
  font-size: 16px;
  color: #3c3c3c;
  overflow-x: hidden;
}
img {
  width: 100%;
  height: 100%;
}
.section {
  position: relative;
}
/*--------------------------------------Fifth Section--*/

.flex-container {
  margin-top: 10px;
  width: 100%;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-flex-flow: row wrap;
  -ms-flex-flow: row wrap;
  flex-flow: row wrap;
  margin-left: -5px;
  margin-right: -5px;
}
.work-indiv {
  width: 540px;
  -webkit-flex: auto;
  -ms-flex: auto;
  flex: auto;
  display: inline-block;
  height: 237px;
  position: relative;
  margin-bottom: 10px;
  cursor: pointer;
  transition: 1s;
  padding-left: 5px;
  padding-right: 5px;
}
.work-indiv:hover img {
  opacity: .6;
  transition: .6s;
}
.middle {
  width: 783px;
}
.work-indiv span {
  position: absolute;
  clear: both;
  bottom: 0;
  left: 0;
  color: White;
  margin-left: 40px;
  text-transform: uppercase;
  margin-bottom: 40px;
  font-size: 2.125em;
  font-family: latobold;
  letter-spacing: .16em;
}
.middle span {
  margin-left: 50px;
}
<div class="section" id="fifth">
  <div class="flex-container">
    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Topworkz">
      <span>Topworkz</span>
    </div>
    <div class="work-indiv middle">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Joint">
      <span>Joint</span>
    </div>
    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Market">
      <span>Market</span>
    </div>

    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Worktop">
      <span>Worktop</span>
    </div>
    <div class="work-indiv middle">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="Lable">
      <span>Lable</span>
    </div>
    <div class="work-indiv">
      <img src="https://pixabay.com/static/uploads/photo/2015/12/21/06/41/landscape-1102117_960_720.jpg" alt="KYIV">
      <span>KYIV</span>
    </div>


  </div>