Home > Enterprise >  The div is not aligning in the center after I added the nav bar to the top
The div is not aligning in the center after I added the nav bar to the top

Time:09-11

I'm new to coding but I need a website soon, So I started working on it but once I added the nav bar the box with contents is not center anymore. I've tried to troubleshoot it and i have tried every single css line and none do the work of aligning the box to the center. Codepen

    .panel {
    background-color: #ffffff;
    border-radius: 10px;
    padding: 15px;
    width: 90%;
    max-width: 1000px;
    display: flex;
    align-items: center;
    flex-direction: column;
    text-align: center;
    text-transform: uppercase;
    border: 2px solid gray;
    background-position: center;
    margin-top: 20px;
    margin-bottom: 20px;
}

CodePudding user response:

I've changed the body to display:flex and align-items:center. Seems to have done the trick. I've made a few other tweaks and commented them for your convenience

*,
*::after,
*::before {
  box-sizing: border-box;
  /* added this to remove some oddball positioning problems */
}

body {
  margin: 0;
  /* added this to remove the margin around the edge */
  display: flex;
  /* this makes the children divs type flexbox. */
  flex-direction: column;
  /* Stack divs on top of each other */
  align-items: center;
  /* Center items in the body */
}

#navbar {
  background-color: #787a7a;
  display: flex;
  justify-content: flex-end;
  /* height: 10vh; <-- won't scale properly at small screen heights*/
  /* position: cover; <--- invalid property*/
  width: 100%;
}

nav a {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 1.5rem;
  font-size: 1rem;
  text-align: center;
  text-decoration: none;
  color: #ffffff;
}

nav a:hover {
  background-color: #121317;
  text-decoration: none;
  color: #ffffff;
}

body {
  line-height: 1;
  background-image: url(https://images.unsplash.com/photo-1447933601403-0c6688de566e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1061&q=80);
  background-repeat: no-repeat;
  background-size: cover;
}

ol,
ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

html {
  box-sizing: border-box;
  font-family: 'Open Sans', sans-serif;
}

.panel {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 15px;
  width: 90%;
  max-width: 1000px;
  display: flex;
  align-items: center;
  flex-direction: column;
  text-align: center;
  text-transform: uppercase;
  border: 2px solid gray;
  background-position: center;
  margin-top: 20px;
  margin-bottom: 20px;
}

button {
  height: 70px;
  width: 200px;
  background-color: transparent;
  border: transparent;
}

button img {
  height: 70px;
  width: 190px;
}

.pricing-plan {
  border-bottom: 1px solid #e1f1ff;
}

.pricing-plan:last-child {
  border-bottom: none;
}

.pricing-img {
  margin-bottom: 30px;
  max-width: 100%;
  width: 200px;
  height: 250px;
  border: 2px solid rgb(188, 185, 185);
}

.pricing-header {
  color: rgb(50, 48, 48);
  font-weight: 600;
  letter-spacing: 1px;
}

.pricing-features {
  margin: 50px 0 25px;
  color: #06366d;
}

.pricing-features-item {
  font-weight: 600;
  letter-spacing: 1px;
  font-size: 12px;
  line-height: 1.5;
  padding: 15px 0;
  border-top: 1px solid #e1f1ff;
}

.pricing-features-item:last-child {
  border-bottom: 1px solid #e1f1ff;
}

.pricing-price {
  color: #0b0f86;
  display: block;
  font-size: 32px;
  font-weight: 700;
}

.pricing-button {
  /*border: 1px solid #9dd1ff;
    border-radius: 10px;
    color: #348efe;*/
  display: inline-block;
  padding: 15px 35px;
  text-decoration: none;
  margin: 25px 0;
  transition: background-color 200ms ease-in-out;
}

.pricing-button:hover,
.pricing-button:focus {
  background-color: #e1f1ff;
}

.pricing-button.is-featured {
  background-color: #48aaff;
  color: white;
}

.pricing-button.is-featured:hover,
.pricing-button.is-featured:focus {
  background-color: #269aff;
  color: white;
}

.buy {
  padding-top: 20px;
}

.buy:hover {
  cursor: pointer;
}

@media (min-width: 900px) {
  .panel {
    flex-direction: row;
  }
  .pricing-plan {
    border-bottom: none;
    border-right: 1px solid #e1f1ff;
    padding: 25px 50px;
  }
  .pricing-plan:last-child {
    border-right: none;
  }
}
<nav id="navbar">
  <a href="index.html#welcome-section"> HOME </a>
  <a href="index.html#projects">ESPRESSO MACHINES</a>
  <a href="index.html#blog"> INSPIRATION </a>
  <a href="index.html#podcast">LISTEN TO A PODCAST</a>
  <a href="index.html#contact">CONTACT</a>
</nav>

<div >

  <div >
    <img src="https://s3.amazonaws.com/www.duckstalk.com/Breville.jpg" alt="" >
    <h2 >Breville - BES870XL</h2>
    <ul >
      <li >Built-In-Grinder</li>
      <li >Material: Steel</li>
      <li >12.5 x 12.6 x 13.1 inches</li>
      <li></li>
    </ul>
    <span >$749.95</span>
    <button  onclick="window.open('https://www.amazon.com/Breville-BES870XL-Barista-Express-Espresso/dp/B00CH9QWOU/ref=sr_1_2?crid=PKJBPC5Z8DB9&keywords=breville&qid=1662162245&sprefix=breville,aps,155&sr=8-2&th=1','_blank')">
        <img src="https://s3.amazonaws.com/www.duckstalk.com/buy it now.png"> </button>
  </div>

  <div >
    <img src="https://s3.amazonaws.com/www.duckstalk.com/philips.jpg" alt="" >
    <h2 >Philips - 3200 Series </h2>
    <ul >
      <li >Press of a button</li>
      <li >Material: Plastic</li>
      <li >10 x 17 x 15 inches</li>
    </ul>
    <span >$599.00</span>
    <button  onclick="window.open('https://www.amazon.com/Philips-Automatic-Espresso-Machine-Frother/dp/B07VDVDHZ9/ref=sr_1_2_sspa?crid=1AZT19ZK50DZ&keywords=Philips 3200-Series&qid=1662418275&s=home-garden&sprefix=philips 3200-series,garden,138&sr=1-2-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUFJTUhJT01PWTdSUFMmZW5jcnlwdGVkSWQ9QTAyNDgxMTgzRVVHNEdUSUcxR01GJmVuY3J5cHRlZEFkSWQ9QTAzNzc1ODgzTjVQSUZWNzZERTVUJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==','_blank')">
        <img src="https://s3.amazonaws.com/www.duckstalk.com/buy it now.png"> </button>
  </div>

  <div >
    <img src="https://s3.amazonaws.com/www.duckstalk.com/delonhi.jpg" alt="" >
    <h2 >De'Longhi - COM532M </h2>
    <ul >
      <li >Programmable timer</li>
      <li >ABS Plastic/Stainless Steel</li>
      <li > 14.52 x 11.02 x 12.79 inches </li>
    </ul>
    <span >$289.95</span>
    <button  onclick="window.open('https://www.amazon.com/Combination-Adjustable-Cappuccino/dp/B08HRM697D/ref=sr_1_28?keywords=delonghi espresso&qid=1662418321&s=home-garden&sprefix=delongin espre,garden,120&sr=1-28','_blank')">
        <img src="https://s3.amazonaws.com/www.duckstalk.com/buy it now.png"> </button>
  </div>
</div>

CodePudding user response:

One simple solution is to use auto margins for margin-left and margin-right of your panel.

your panel class would be like this:

.panel {
    background-color: #ffffff;
    border-radius: 10px;
    padding: 15px;
    width: 90%;
    max-width: 1000px;
    display: flex;
    align-items: center;
    flex-direction: column;
    text-align: center;
    text-transform: uppercase;
    border: 2px solid gray;
    background-position: center;
    margin: 20px auto;
}
  • Related