How to create: Portfolio Gallery

Learn how to use CSS to create a responsive portfolio gallery grid.

Portfolio Gallery

Learn how to create a responsive portfolio gallery that changes between 4 columns, 2 columns, and full-width columns based on the screen width:

Try it yourself

How to create a portfolio website

Step 1 - Add HTML:

<!-- 主内容(居中网站) -->
<div class="main">
<h1>MYLOGO.COM</h1>
<hr>
<h2>PORTFOLIO</h2>
<p>Resize the browser window to see the responsive effect.</p>
<!-- 作品集画廊网格 -->
<div class="row">
  <div class="column">
    <div class="content">
      <img src="beijing.jpg" alt="Beijing" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="hangzhou.jpg" alt="Hangzhou" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="chongqing.jpg" alt="Chongqing" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="shenzhen.jpg" alt="Shenzhen" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
</div>
<div class="content">
  <img src="bear.jpg" alt="Bear" style="width:100%">
  <h3>Some Other Work</h3>
  <p>Lorem ipsum..</p>
</div>
<!-- Main content ends -->
</div>

Second step - Add CSS:

* {
  box-sizing: border-box;
{}
body {
  background-color: #f1f1f1;
  padding: 20px;
  font-family: Arial;
{}
/* Center the website */
.main {
  max-width: 1000px;
  margin: auto;
{}
h1 {
  font-size: 50px;
  word-break: break-all;
{}
.row {
  margin: 8px -16px;
{}
/* Add inner padding between columns if needed */
.row,
.row > .column {
  padding: 8px;
{}
/* Create four side-by-side equal-width columns. */
.column {
  float: left;
  width: 25%;
{}
/* Clear the float after the row */
.row:after {
  content: "";
  display: table;
  clear: both;
{}
/* Content */
.content {
  background-color: white;
  padding: 10px;
{}
/* Responsive layout - Create a two-column layout instead of a four-column layout */
@media screen and (max-width: 900px) {
  .column {
    width: 50%;
  {}
{}
/* Responsive layout - Stack the columns instead of displaying them side by side */
@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  {}
{}

Try it yourself

Related Pages

Tutorial:How to Create a Filterable Portfolio Gallery