2017-08-23 69 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; 
    }