cssdisplay

Display flex but it always rows display, not column


test my code

hai, i just jump to studying front-end recently. in this test, i wrote this code for landing page. unfortunally, my code doesn't seems work properly. i expect to my code run with column flex display but it display flex rows. i can't find where is my mistake in my code. i wrote display: flex; and flex-direction: column but nothing happen.

happily if someone help to let me know where is my mistake.

@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Poppins", sans-serif;
}

body{
    background-color: #f7f9fd;
}

.container {
    width: 90%;
    max-width: 992px;
    margin: auto;
}

.add-cart-con {
    display: flex;
    justify-content: center;
    padding: 20px 0 30px;
}

button {
    border: none;
    outline: none;
    cursor: pointer;
    color: #fff;
    transition: all 0.3s ease;
}

.add-cart-con button,
.modal button{
    font-size: 15px;
    background-color: #4caf50;
    padding: 10px 20px;
    font-weight: 500;
    border-radius: 10px;
}

.card-list {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    gap: 20px;
}

.card {
    background-color: #fff;
    padding: 16px 20px 10px;
    width: 31%;
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.que-div{
    margin-bottom: 10px;
    font-weight: 500;
    color: #2d3140;
    word-wrap: break-word;
}

.ans-div{
    font-weight: 400;
    color: #2d3140;
    word-wrap: break-word;
    margin: 10px 0 14px;
    line-height: 22px;
    font-size: 14px;
}

.show-hide-btn{
    background-color: #4caf50;
    width: 100%;
    padding: 10px;
    border-radius: 10px;
    font-size: 15px;
}

.btn-con{
    text-align: right;
}

.edit,
.delete{
    background-color: transparent;
    margin: 10px 4px;
    font-size: 20px;
}

.edit{
    color: #4caf50;
}

.delete{
    color: #f34444;
}

.modal{
    width: 90%;
    max-width: 540px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background-color: #fff;
    position: absolute;
    transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
    padding: 35px 30px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.modal h2{
    font-size: 36px;
    text-align: center;
    color: #2d3140;
    margin-bottom: 50px;
}

.wrapper{
    display: flex;
    justify-content: space-between;
    margin-bottom: 14px;
}

#error{
    color: #f34444;
}

#close-btn{
    font-size: 22px;
    background-color: #4caf50;
    color: #fff;
    padding: 10px 14px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease;
}

label{
    color: #2d3140;
    font-weight: 600;
    margin-bottom: 8px;
}

textarea{
    width: 100%;
    padding: 10px;
    border: 1px solid #ccc;
    outline: none;
    resize: none;
    border-radius: 10px;
    transition: all 0.3s ease;
}

textarea:not(:last-child){
    margin-bottom: 10px;
}

textarea:focus{
    border-color: #2d3140;
}

.hide{
    display: none;
}

.add-cart-con button:hover,
.modal button:hover,
#close-btn:hover{
    background-color: #368339 !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <title>Flash Card App</title>
</head>
<body>
    
<div class="container">
    <div class="add-cart-con">
        <button id="add-cart">Add New Cart</button>
    </div>
    <div class="cards-list">
        <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
                <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
                <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
        </div>
        <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
                <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
                <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
        </div>
        <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
                <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
                <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
        </div>
        <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
                <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
                <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
        </div>
        <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
                <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
                <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
        </div>
    </div>

    <div class="modal hide" id="add-card-modal">
        <h2>Add Flashcard</h2> 
        <div class="wrapper">
            <div class="error-con">
                <span class="hide" id="error">
                    Input fields cannot be empty!
                </span>
            </div>
            <i class="fa-solid fa-xmark" id="close-btn"></i>
        </div>

        <label for="question">Question</label>
        <textarea id="question" class="input" placeholder="Type your question here..." rows="3"></textarea>
        <label for="answer">Answer</label>
        <textarea id="answer" class="input" placeholder="Type the answer here..." rows="4"></textarea>
    </div>
</div>

</body>
</html>

I tried to made a column display style with flex-direction column but nothing happen. stay in rows.


Solution

  • A couple of problems.

    One is a typo which would normally just get the question closed. In the HTML you have cards-list but in the CSS card-list.

    The second is a misuse of column in flex-direction.

    You want it to flex along the row [until it has to wrap]. This is the default direction but this snippet specifically sets flex-direction: row to make it clear.

    @import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      font-family: "Poppins", sans-serif;
    }
    
    body {
      background-color: #f7f9fd;
    }
    
    .container {
      width: 90%;
      max-width: 992px;
      margin: auto;
    }
    
    .add-cart-con {
      display: flex;
      justify-content: center;
      padding: 20px 0 30px;
    }
    
    button {
      border: none;
      outline: none;
      cursor: pointer;
      color: #fff;
      transition: all 0.3s ease;
    }
    
    .add-cart-con button,
    .modal button {
      font-size: 15px;
      background-color: #4caf50;
      padding: 10px 20px;
      font-weight: 500;
      border-radius: 10px;
    }
    
    .cards-list {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      gap: 20px;
    }
    
    .card {
      background-color: #fff;
      padding: 16px 20px 10px;
      width: 31%;
      border-radius: 10px;
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    }
    
    .que-div {
      margin-bottom: 10px;
      font-weight: 500;
      color: #2d3140;
      word-wrap: break-word;
    }
    
    .ans-div {
      font-weight: 400;
      color: #2d3140;
      word-wrap: break-word;
      margin: 10px 0 14px;
      line-height: 22px;
      font-size: 14px;
    }
    
    .show-hide-btn {
      background-color: #4caf50;
      width: 100%;
      padding: 10px;
      border-radius: 10px;
      font-size: 15px;
    }
    
    .btn-con {
      text-align: right;
    }
    
    .edit,
    .delete {
      background-color: transparent;
      margin: 10px 4px;
      font-size: 20px;
    }
    
    .edit {
      color: #4caf50;
    }
    
    .delete {
      color: #f34444;
    }
    
    .modal {
      width: 90%;
      max-width: 540px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      background-color: #fff;
      position: absolute;
      transform: translate(-50%, -50%);
      top: 50%;
      left: 50%;
      padding: 35px 30px;
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    }
    
    .modal h2 {
      font-size: 36px;
      text-align: center;
      color: #2d3140;
      margin-bottom: 50px;
    }
    
    .wrapper {
      display: flex;
      justify-content: space-between;
      margin-bottom: 14px;
    }
    
    #error {
      color: #f34444;
    }
    
    #close-btn {
      font-size: 22px;
      background-color: #4caf50;
      color: #fff;
      padding: 10px 14px;
      border-radius: 50%;
      cursor: pointer;
      transition: all 0.3s ease;
    }
    
    label {
      color: #2d3140;
      font-weight: 600;
      margin-bottom: 8px;
    }
    
    textarea {
      width: 100%;
      padding: 10px;
      border: 1px solid #ccc;
      outline: none;
      resize: none;
      border-radius: 10px;
      transition: all 0.3s ease;
    }
    
    textarea:not(:last-child) {
      margin-bottom: 10px;
    }
    
    textarea:focus {
      border-color: #2d3140;
    }
    
    .hide {
      display: none;
    }
    
    .add-cart-con button:hover,
    .modal button:hover,
    #close-btn:hover {
      background-color: #368339 !important;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" href="style.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
      <title>Flash Card App</title>
    </head>
    
    <body>
    
      <div class="container">
        <div class="add-cart-con">
          <button id="add-cart">Add New Cart</button>
        </div>
        <div class="cards-list">
          <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
              <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
              <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
          </div>
          <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
              <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
              <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
          </div>
          <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
              <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
              <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
          </div>
          <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
              <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
              <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
          </div>
          <div class="card">
            <p class="que-div">Question</p>
            <p class="ans-div">Answer</p>
            <button class="show-hide-btn">Show/Hide</button>
            <div class="btn-con">
              <button class="edit"><i class="fa-solid fa-pen-to-square"></i></button>
              <button class="delete"><i class="fa-solid fa-trash"></i></button>
            </div>
          </div>
        </div>
    
        <div class="modal hide" id="add-card-modal">
          <h2>Add Flashcard</h2>
          <div class="wrapper">
            <div class="error-con">
              <span class="hide" id="error">
                        Input fields cannot be empty!
                    </span>
            </div>
            <i class="fa-solid fa-xmark" id="close-btn"></i>
          </div>
    
          <label for="question">Question</label>
          <textarea id="question" class="input" placeholder="Type your question here..." rows="3"></textarea>
          <label for="answer">Answer</label>
          <textarea id="answer" class="input" placeholder="Type the answer here..." rows="4"></textarea>
        </div>
      </div>
    
    </body>
    
    </html>