Home > Back-end >  Problem with CSS; centering of survey form
Problem with CSS; centering of survey form

Time:10-13

I need to center a survey form in my website, but when I try it, it doesn't work. I don't know what's wrong. Can somebody help me?

Here is my Pen. And here's my CSS code:

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    font-family: 'Poppins', sans-serif;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.4;
    color: var(--color-white);
    margin: 0;
}

/* mobile friendly alternative to using background-attachment: fixed */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    z-index: -1;
    background: var(--color-darkblue);
    background-image: linear-gradient(
        115deg,
        rgba(58, 58, 158, 0.8),
        rgba(136, 136, 206, 0.7)
    ),
    url(https://cdn.freecodecamp.org/testable-projects-fcc/images/survey-form-background.jpeg);
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

.container {
    width: 100%;
    margin: 3.125rem auto 0 auto;
}

CodePudding user response:

you can use this on your body

body {
  background-color: lightblue;
  font-family: Helvetica;
  font-size: 16px;
  flex: 0 1 auto;
  align-self: auto;
  width: 100%;
  text-align: center;
  
  
}

CodePudding user response:

It is not recommended to use body directly and style it. But I made a few tweaks on your code to keep letting you use body directly.

Please check this out for reference when you want to center an element. https://css-tricks.com/centering-css-complete-guide/

*,
*::before,
*::after {
  box-sizing: border-box;
}

.wrapper {
  margin: 0 auto; // this centers bloc elements 
  text-align: center; // This centers inline elements
}

header {
  width: 100%; 
}

body {
  width: 100%; // just let your body take 100% of the page
  background-color: lightblue;
  font-family: Helvetica;
  font-size: 16px;
  position: absolute;
  flex: 0 1 auto;
  align-self: auto;
}

#survey-form {
  border: solid 2px;
  border-radius: 1%;
  padding: 20px;
  margin-top: 30px;
  background-color: lightpink;
  position: relative;
}

.form input {
  width: 100%;
  height: 30px;
}

.container {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  //border: solid 2px #fff;
  max-width: 700px;
  margin: auto;
}

h1 {
  color: darkblue;
}

p {
  font-weight: bold;
}

form {
  text-align: left;
}

#dropdown {
  width: 100%;
  height: 30px;
}

.checkbox {
  display: flex;
  flex-direction: column;
}

#submit {
  height: 20px;
  width: 100%;
  background-color: #bbh77f;
  color: #000;
  padding: 0 20px;
  text-align: center;
  vertical-align: middle;
}

.input-textarea {
  min-height: 120px;
  width: 100%;
  margin-bottom: 15px;
  resize: vertical;
}
<!DOCTYPE html>
<html>

<head>
  <meta>
  <title>Formularz</title>
</head>

<body>
  <header>
    <div class='wrapper'>


      <h1 id="title">Formularz freeCodeCamp.org</h1>
      <p id="description">Dziękujemy za poświęcony czas i ulepszanie naszej platformy</p>
    </div>
  </header>
  <div class="container">
    <form id="survey-form">
      <div class="form box">
        <div>
          <label id="name-label" for="name"><p>Imię</p></label>
          <input id="name" type="text" required placeholder="Wpisz swoje imię" />
        </div>
        <div>
          <label id="email-label" for="mail"><p>E-mail</p></label>
          <input id="name" type="email" required placeholder="Wpisz swój e-mail" />
        </div>
        <div>
          <label id="number-label" for="number"><p>Wiek</p></label>
          <input type="number" min="10" max="99" placeholder="10">
        </div>
      </div>

      <div class="form-group">
        <p id="dropdown-label">Która opcja najlepiej Cię opisuje?</p>
        <select id="dropdown">
          <option disabled selected value>Wybierz swoją rolę</option>
          <option value="student">Student</option>
          <option value="full-time-job">Pełen etat</option>
          <option value="full-time-learner">Uczeń</option>
          <option value="prefer-not-to-say">Nie chcę mówić</option>
          <option value="other">Inne</option>
        </select>
      </div>

      <div class="form-radio">
        <p id="radio-label">Czy poleciłbyś freeCodeCamp znajomemu?</p>
        <div>
          <input type="radio" id="tak" name="polecenie" value="tak" checked>
          <label for="tak">Tak</label>
        </div>

        <div>
          <input type="radio" id="nie" name="polecenie" value="nie">
          <label for="nie">Nie</label>
        </div>

        <div>
          <input type="radio" id="nie-wiem" name="polecenie" value="nie-wiem">
          <label for="nie-wiem">Nie wiem</label>
        </div>


      </div>

      <div class="form-group">
        <p id="dropdown-label">Jaka część freeCodeCamp jest Twoją ulubioną?</p>
        <select id="dropdown">
          <option disabled selected value>Wybierz opcję</option>
          <option value="challenges">Challenges</option>
          <option value="projects">Projects</option>
          <option value="community">Community</option>
          <option value="open-source">Open source</option>
        </select>
      </div>

      <div class="form-group checkbox">
        <p>Co chcesz ulepszyć?</p>
        <div>
          <input type="checkbox">
          <label>Front-end Projects</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Back-end Projects</label>
        </div>
        <div>
          <input type="checkbox" checked>
          <label>Data Visualization</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Challenges</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Open Source Community</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Gitter help rooms</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Videos</label></div>
        <div>
          <input type="checkbox">
          <label>City Meetups</label></div>
        <div>
          <input type="checkbox">
          <label>Wiki</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Forum</label>
        </div>
        <div>
          <input type="checkbox">
          <label>Additional Courses</label>
        </div>

      </div>

      <div class="form-group">
        <p>Masz sugestie lub podpowiedzi? </p>
        <div class="textarea container input-textarea">
          <textarea rows="8" placeholder="Wprowadź swoją opinię tutaj"></textarea>
        </div>
        <button id="submit">Wyślij</button>

      </div>
  </div>
  </form>
  </div>
  </div>
</body>

</html>

  • Related