2017-08-23 42 views
0

所以我花了很多时间试图解决这个问题,但没有任何工作。我有divs,我想每行都有3个,但是居中。我试过选择第四个div并将其放到新的行中,但这不起作用。如何让我的柔性电网有2行3列,但仍然居中?

我试图阅读指南,但不太明白。如果有人能帮我解释它,所以我知道我做错了什么,这将是非常感激。

下面的图片是我想要的全屏设备;我想每行总是有3列。

What I want to have on full screen devices (desktops e.g)

/* general styles */ 
 

 
body { 
 
    margin: 0; 
 
} 
 

 
h2 { 
 
    text-align: center; 
 
    text-decoration: overline underline; 
 
    text-decoration-color: #fff; 
 
} 
 

 
.col li { 
 
    list-style-type: none; 
 
} 
 

 
.group:before, 
 
.group:after { 
 
    content: ""; 
 
    display: table; 
 
} 
 

 
.group:after { 
 
    clear: both; 
 
    /* these styles are called a clearfix (look it up) */ 
 
} 
 

 

 
/* grid layout with flexbox */ 
 

 
.portfolio-col { 
 
    display: flex; 
 
    justify-content: center; 
 
    flex-wrap: wrap; 
 
} 
 

 
.col { 
 
    margin: 1% 1.3%; 
 
    /* equally space our boxes, instead of different left and right margins; specific numbers you can adjust */ 
 
    flex-shrink: 0; 
 
    box-sizing: border-box; 
 
    justify-content: center; 
 
} 
 

 
.col ul { 
 
    padding-left: 0; 
 
    text-align: center; 
 
    width: 100%; 
 
    max-width: 250px; 
 
    /* same width and max-width as our images */ 
 
} 
 

 
.col img { 
 
    border-style: solid; 
 
    border-color: blue; 
 
    width: 100%; 
 
    height: 100%; 
 
    max-width: 250px; 
 
    max-height: 250px; 
 
} 
 

 
.col:nth-child(4) { 
 
    background-color: aqua; 
 
    margin-left: 30%; 
 
    flex-direction: column; 
 
    flex-wrap: wrap; 
 
    margin: auto; 
 
    justify-content: center; 
 
}
<section class="port-folio" id="portfolio"> 
 
    <div class="container"> 
 
    <h2>MY PROJECTS</h2> 
 
    <div class="portfolio-col"> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Player</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Java</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <br> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</section>

回答

1

你只需要改变2个属性,并添加2,使之工作。

如果您在.portfolio-col规则中更改为justify-content: space-around;,并添加margin: 0 auto; max-width: 850px;,您将连续获得3列,始终以大屏幕为中心。

.portfolio-col { 
    display: flex; 
    justify-content: space-around;  /* changed */ 
    flex-wrap: wrap; 

    margin: 0 auto;      /* added */ 
    max-width: 850px;      /* added, 3 * 250px + margin/gutter */ 
} 

由于不建议在组合使用的补白/利润率百分之与Flexbox的(错误行为),我在.col改变顶部/底部margin1%1vhViewport-percentage units),当我们使用space-around.portfolio-col,不需要左/右页边距,所以我将它设置为5px,使他们的边缘前突破到新行窄屏幕来关闭

.col { 
    margin: 1vh 5px;      /* changed */ 
    flex-shrink: 0; 
    box-sizing: border-box; 
} 

注,我也重新移动<br>你在标记有和其中包括的.col:nth-child(4)规则

栈片断

/* general styles */ 
 

 
body { 
 
    margin: 0; 
 
} 
 

 
h2 { 
 
    text-align: center; 
 
    text-decoration: overline underline; 
 
    text-decoration-color: #fff; 
 
} 
 

 
.col li { 
 
    list-style-type: none; 
 
} 
 

 
.group:before, 
 
.group:after { 
 
    content: ""; 
 
    display: table; 
 
} 
 

 
.group:after { 
 
    clear: both; 
 
    /* these styles are called a clearfix (look it up) */ 
 
} 
 

 

 
/* grid layout with flexbox */ 
 

 
.portfolio-col { 
 
    display: flex; 
 
    justify-content: space-around; 
 
    flex-wrap: wrap; 
 
    
 
    margin: 0 auto; 
 
    max-width: 850px; 
 
} 
 

 
.col { 
 
    margin: 1vh 5px; 
 
    /* equally space our boxes, instead of different left and right margins; specific numbers you can adjust */ 
 
    flex-shrink: 0; 
 
    box-sizing: border-box; 
 
} 
 

 
.col ul { 
 
    padding-left: 0; 
 
    text-align: center; 
 
    width: 100%; 
 
    max-width: 250px; 
 
    /* same width and max-width as our images */ 
 
} 
 

 
.col img { 
 
    border-style: solid; 
 
    border-color: blue; 
 
    width: 100%; 
 
    height: 100%; 
 
    max-width: 250px; 
 
    max-height: 250px; 
 
} 
 

 
.col:nth-child(4) { 
 
    background-color: aqua; 
 
}
<section class="port-folio" id="portfolio"> 
 
    <div class="container"> 
 
    <h2>MY PROJECTS</h2> 
 
    <div class="portfolio-col"> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Player</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Java</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="http://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</section>

+0

好吧只是让我更好地了解,让你改变了证明内容的某些属性到空间,因为它给你左右两边的空间,对吗?和/ *增加了,3 * 750px + margin/gutter */margin/gutter是什么?你在做3(列数*宽度250是750)还是增加100?我只是想知道,如果我想要说连2,4或8列或其他什么。 – Cmi

+0

@Cmi是的,这是正确的。而我错误地写了3 * 750像素,应该是3 * 250像素(更新我的答案)。额外的100是为物品留出空间,这也包括弥补'.col'上设置的左右5px的空白。这意味着额外的100不能小于(3 * 250px)+(3 *(5px + 5px)),否则将不适合,在这种情况下,连续3个项目 – LGSon

+0

好的,谢谢你的所有帮助 – Cmi

0

这是我的工作jsFiddle :)

*{ box-sizing: border-box; } 
 

 
.port-folio h2{ text-align: center; } 
 
.portfolio-col { 
 
    text-align: center; 
 
    overflow: hidden; 
 
    padding: 5px; 
 
    clear: both; 
 
} 
 
.port-folio ul{ list-style: none; padding: 0; margin: 25px 0 0; } 
 
.port-folio ul li{ display: block; } 
 
.col{ 
 
    width: 31.33%; 
 
    border: 1px solid black; 
 
    float: left; 
 
    padding: 5px; 
 
    margin: 15px 1%; 
 
}
<section class="port-folio" id="portfolio"> 
 
    <div class="container"> 
 
    <h2>MY PROJECTS</h2> 
 
    <div class="portfolio-col"> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Player</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Java</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <br> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
     <div class="col span_1_of_3"> 
 
     <img src="https://swords.cc/lotzine/250x250.gif" alt="Smiley face"> 
 
     <ul class="project-info"> 
 
      <li>Name: Game</li> 
 
      <li>Created By: Doe</li> 
 
      <li>Date: August 2017</li> 
 
      <li>Language: Game Maker Language (GML)</li> 
 
      <li><a href="">More Info</a></li> 
 
     </ul> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</section>

希望有所帮助。 :)

0
h2 { 
     text-align: center; 

    } 

    .col li { 
     list-style-type: none; 
    } 

    .portfolio-col { 
     display: flex; 
     flex-wrap: wrap; 
    } 

    .col { 
     align-items: center; 
     box-sizing: border-box; 
     display: flex; 
     flex: 1 0 28%; 
     flex-direction: column; 
     margin: 1% 1.3%; 

    } 

    .col ul { 
     padding-left: 0; 
     text-align: center; 
     width: 100%; 
     max-width: 250px; 
     } 
    .col img { 
     border-style: solid; 
     border-color: blue; 
     width: 100%; 
     height: 100%; 
     max-width: 250px; 
     max-height: 250px; 
    }