Home > database >  Can't center button within a HTML form using flexbox - code inside
Can't center button within a HTML form using flexbox - code inside

Time:03-10

Hi guys I can't seem to center my button within the flexbox container . Any ideas why this is?

the button is below the email form but is left aligned to it (start in the same position).

I am trying to center the button like the example on this link:https://codepen.io/freeCodeCamp/full/RKRbwL. My CSS code also has a CSS reset but I haven't pasted that in. Thanks!

* {
  box-sizing: border-box;
}

body {
  font-family: "Roboto", sans-serif;
  min-height: 100vh;
  background-color: #eee;
  color: black;
}

nav {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  font-weight: 900;
  font-size: 1em;
  padding: 20px 10px;
  flex-wrap: wrap;
  border-bottom: 1px solid grey;
  position: sticky;
}

ul {
  flex-grow: 1;
  max-width: 30%;
  display: flex;
  justify-content: space-around;
  text-decoration: none;
}

img {
  display: flex;
  width: 40vw;
}

ul li {
  display: inline;
  text-decoration: none;
  display: flex;
}

.nav-links a {
  text-decoration: none;
  color: black;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

h1 {
  font-size: 2em;
  font-weight: 700;
  padding: 20px;
}

input[type="email"] {
  padding: 5px 10px;
  margin: 10px 0px;
  border: solid 1px black;
  width: 350px;
}

input[type="submit"] {}

@media (max-width: 700px) {
  header {
    font-size: 1em;
  }
  nav {
    display: flex;
    flex-direction: column;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
  }
}
<html>

<body>
  <header>
    <nav >
      <img src="https://cdn.freecodecamp.org/testable-projects-fcc/images/product-landing-page-logo.png" alt="original trombones logo" >
      <ul >
        <li><a href="#" >Features</a></li>
        <li><a href="#" >How It Works</a></li>
        <li><a href="#" >Pricing</a></li>
      </ul>
    </nav>
  </header>
  <section >
    <h1>Handcrafted, home-made masterpieces</h1>
    <form >
      <div >
        <label for="email"></label>
        <input type="email" name="email" id="email" placeholder="Enter your email address" required>
      </div>
      <div >
        <input  type="submit" value="GET STARTED">
      </div>
    </form>
  </section>
</body>

</html>

CodePudding user response:

Just add

.button-center { text-align: center}

Your container is centered, but is full width, so the button goes to the left, adding the code above, will center the button.

CodePudding user response:

For a flex solution, you have some options. It truly just depends on the extent of your design. Here are a few:

.button-center {
  display: flex;
  justify-content: center;
  /* text-align: center; ~ non-flex solution */
}

form.email-form {
  display: flex;
  flex-direction: column;
  align-items: center;
} 

See it working here:

* {
  box-sizing: border-box;
}

body {
  font-family: "Roboto", sans-serif;
  min-height: 100vh;
  background-color: #eee;
  color: black;
}

.button-center {
  display: flex;
  justify-content: center;
  /*text-align: center;*/
}

form.email-form {
  display: flex;
  flex-direction: column;
  align-items: center;
} 

nav {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  font-weight: 900;
  font-size: 1em;
  padding: 20px 10px;
  flex-wrap: wrap;
  border-bottom: 1px solid grey;
  position: sticky;
}

ul {
  flex-grow: 1;
  max-width: 30%;
  display: flex;
  justify-content: space-around;
  text-decoration: none;
}

img {
  display: flex;
  width: 40vw;
}

ul li {
  display: inline;
  text-decoration: none;
  display: flex;
}

.nav-links a {
  text-decoration: none;
  color: black;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

h1 {
  font-size: 2em;
  font-weight: 700;
  padding: 20px;
}

input[type="email"] {
  padding: 5px 10px;
  margin: 10px 0px;
  border: solid 1px black;
  width: 350px;
}

input[type="submit"] {}

@media (max-width: 700px) {
  header {
    font-size: 1em;
  }
  nav {
    display: flex;
    flex-direction: column;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
  }
}
<html>

<body>
  <header>
    <nav >
      <img src="https://cdn.freecodecamp.org/testable-projects-fcc/images/product-landing-page-logo.png" alt="original trombones logo" >
      <ul >
        <li><a href="#" >Features</a></li>
        <li><a href="#" >How It Works</a></li>
        <li><a href="#" >Pricing</a></li>
      </ul>
    </nav>
  </header>
  <section >
    <h1>Handcrafted, home-made masterpieces</h1>
    <form >
      <div >
        <label for="email"></label>
        <input type="email" name="email" id="email" placeholder="Enter your email address" required>
      </div>
      <div >
        <input  type="submit" value="GET STARTED">
      </div>
    </form>
  </section>
</body>

</html>

  • Related