Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                 <div class="container">
        <div class="card active"
            style="background-image: url('https://images.unsplash.com/photo-1522069169874-c58ec4b76be5?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=412&q=80')">
            <h3>GoldFish</h3>
        </div>
        <div class="card"
            style="background-image:url('https://images.unsplash.com/photo-1547382442-d17c21625a44?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80')">
            <h3>Dolphen</h3>
        </div>
        <div class="card"
            style="background-image: url('https://media.istockphoto.com/photos/underwater-world-with-corals-and-tropical-fish-picture-id472563478?k=20&m=472563478&s=612x612&w=0&h=nc6rQKFct_FC4WFuC9OLJ-I3jJ9QDDWcwNzK4-ar3lU=')">
            <h3>Sea Animals</h3>
        </div>
        <div class="card"
            style="background-image: url('https://images.unsplash.com/photo-1568660357733-823cbddb0f6a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80')">
            <h3>Sea Turtle</h3>
        </div>
        <div class="card"
            style="background-image: url('https://images.unsplash.com/photo-1535591273668-578e31182c4f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80')">
            <h3>Nemo</h3>
        </div>
    </div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  display: flex; /*讓內容在viewport的中間*/
  justify-content: center;
  align-items: center;
  height: 100vh;
  overflow: hidden;
}

/*裝卡片的容器*/
div.container{
  display: flex; /*讓圖片變成水平排列*/
  width: 90vw; /*會隨著視窗調整寬度*/
}

/* 卡片主體 */
div.card{
 background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 80vh;
  border-radius: 50px;
  cursor: pointer;
  color: #fff;
  font-weight: bold;
  flex: 0.5;
  margin: 10px;
  position: relative;   /*這樣設是因h3要依照絕對位子擺放*/
  transition: flex 0.7s ease-in;
}

/* 未被展開的圖片標題 */
div.card h3 {
  font-size: 24px;
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  opacity: 0; /*透明度設為0讓它看不到*/
}

/* 展開的圖片 */
div.card.active {
  flex: 5; /*flex是<flex-grow> <flex-shrink> <flex-basis>的縮寫,若只有一個值代表是flex-grow*/
}

/* 展開圖片的標題 */
div.card.active h3 {
  opacity: 1;
  transition: opacity 0.3s ease-in 0.5s; /*讓標題比圖片慢0.5秒進入*/
}

/* 小屏幕下只會有三張圖 */
@media screen and (max-width: 576px) {
  .container {
    width: 100vw;  /*原本是90vw,為了在小屏幕下佔用更多空間所以設100vw*/
  }
  .card:nth-of-type(4),
  .card:nth-of-type(5) {
    display: none;
  }
}
              
            
!

JS

              
                let cards = document.querySelectorAll(".card");
console.log(cards); //NodeList(5),五個區塊共五個節點
console.log(cards[0]); //div.card.active這個節點
cards.forEach((card) => {
  //箭頭函式中若只有一個參數,可以省略括號( )不寫
  card.addEventListener("click", () => {
    removeActiveClasses(); //把原本的active先移除
    card.classList.add("active"); //再新增active到點擊的圖片上
  });
});

// 移除active
function removeActiveClasses() {
  cards.forEach((card) => {
    card.classList.remove("active");
  });
}

              
            
!
999px

Console