'How can I center content on a page but also have a navigation bar floating to the left?

Currently I have them both in a Flexbox with space-between and a 25% margin to the right of the content. It's a bit too hacky and doesn't work well with different sized windows

here

I also want the navbar to be sticky.



Solution 1:[1]

Please try this method and give response. its better to use css grid for page responsiveness.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-size: 62.5%;
}

html,
body {
  background-color: lightgray;
  height: 100%;
  width: 100vw;
}

.container {
  min-height: 100%;
  width: 100%;
  background-color: inherit;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: repeat(5, max-content);
  row-gap: 0.5rem;
}

.header {
  grid-column: 1/-1;
  background-color: #61988e;
}

.main {
  grid-column: 1/-1;
  grid-row: 2/3;
  background-color: #a0b2a6;
}

.main2 {
  grid-column: 1/-1;
  grid-row: 3/4;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 0.5rem;
}

.feature {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
  grid-gap: 0.5rem;
  grid-column: 1/-1;
}

.item {
  padding: 6rem;
  color: white;
  font-size: 2.5rem;
  font-family: sans-serif;
  text-transform: uppercase;
  border-radius: 0.2rem;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 1rem 4rem rgba(0, 0, 0, 0.25);
  transition: all 0.2s;
}

.item--1 {
  background-color: #a0b2a6;
}
.item--2 {
  background-color: #360568;
}
.item--3 {
  background-color: #586ba4;
}
.item--4 {
  background-color: #87a878;
}
.item--5 {
  background-color: teal;
}
.item--6 {
  background-color: #8789c0;
}
.item:hover {
  background-color: lightgray;
  color: gray;
  box-shadow: 0 1rem 4rem rgba(0, 0, 0, 0.5);
}

.footer {
  grid-column: 1/-1;
  background-color: #493843;
}

/* Navigation */
@media only screen and (max-width: 41.875em) {
  .navMenu a {
    min-width: 100%;
    text-align: center;
  }
}
.navMenu a {
  color: #f6f4e6;
  text-decoration: none;
  font-size: 1.2em;
  text-transform: uppercase;
  font-weight: 500;
  display: inline-block;
  width: 80px;
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}

.navMenu a:hover {
  color: #fddb3a;
}

.navMenu .dot {
  width: 6px;
  height: 6px;
  background: #fddb3a;
  border-radius: 50%;
  opacity: 0;
  -webkit-transform: translateX(30px);
  transform: translateX(30px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}

.navMenu a:nth-child(1):hover ~ .dot {
  -webkit-transform: translateX(30px);
  transform: translateX(30px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}

.navMenu a:nth-child(2):hover ~ .dot {
  -webkit-transform: translateX(110px);
  transform: translateX(110px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}

.navMenu a:nth-child(3):hover ~ .dot {
  -webkit-transform: translateX(200px);
  transform: translateX(200px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}

.navMenu a:nth-child(4):hover ~ .dot {
  -webkit-transform: translateX(285px);
  transform: translateX(285px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="header">
        <div class="item item--header">
          <nav class="navMenu">
            <a href="#">Home</a>
            <a href="#">Blog</a>
            <a href="#">Work</a>
            <a href="#">About</a>
            <div class="dot"></div>
          </nav>
        </div>
      </div>

      <div class="feature">
        <div class="item item--1">box1</div>
        <div class="item item--2">box2</div>
        <div class="item item--3">box3</div>
        <div class="item item--4">box4</div>
        <div class="item item--5">box5</div>
        <div class="item item--6">box6</div>
      </div>

      <div class="footer">
        <div class="item item--footer">footer</div>
      </div>
    </div>
  </body>
</html>

Solution 2:[2]

Please try this method and give response. its better to use css grid for page responsiveness. Please also check the page on various screen sizes.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-size: 62.5%;
}

html,
body {
  background-color: lightgray;
  height: 100%;
  width: 100vw;
}

.container {
  min-height: 100%;
  width: 100%;
  background-color: inherit;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: repeat(5, max-content);
  row-gap: 0.5rem;
}

.header {
  grid-column: 1/-1;
  background-color: #61988e;
}

.main {
  grid-column: 1/-1;
  grid-row: 2/3;
  background-color: #a0b2a6;
}

.main2 {
  grid-column: 1/-1;
  grid-row: 3/4;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 0.5rem;
}

.feature {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
  grid-gap: 0.5rem;
  grid-column: 1/-1;
}

.item {
  padding: 6rem;
  color: white;
  font-size: 2.5rem;
  font-family: sans-serif;
  text-transform: uppercase;
  border-radius: 0.2rem;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 1rem 4rem rgba(0, 0, 0, 0.25);
  transition: all 0.2s;
}

.item--1 {
  background-color: #a0b2a6;
}
.item--2 {
  background-color: #360568;
}
.item--3 {
  background-color: #586ba4;
}
.item--4 {
  background-color: #87a878;
}
.item--5 {
  background-color: teal;
}
.item--6 {
  background-color: #8789c0;
}
.item:hover {
  background-color: lightgray;
  color: gray;
  box-shadow: 0 1rem 4rem rgba(0, 0, 0, 0.5);
}

.footer {
  grid-column: 1/-1;
  background-color: #493843;
}

/* Navigation */
@media only screen and (max-width: 41.875em) {
  .navMenu a {
    min-width: 100%;
    text-align: center;
  }
}
.navMenu a {
  color: #f6f4e6;
  text-decoration: none;
  font-size: 1.2em;
  text-transform: uppercase;
  font-weight: 500;
  display: inline-block;
  width: 80px;
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}

.navMenu a:hover {
  color: #fddb3a;
}

.navMenu .dot {
  width: 6px;
  height: 6px;
  background: #fddb3a;
  border-radius: 50%;
  opacity: 0;
  -webkit-transform: translateX(30px);
  transform: translateX(30px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}

.navMenu a:nth-child(1):hover ~ .dot {
  -webkit-transform: translateX(30px);
  transform: translateX(30px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}

.navMenu a:nth-child(2):hover ~ .dot {
  -webkit-transform: translateX(110px);
  transform: translateX(110px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}

.navMenu a:nth-child(3):hover ~ .dot {
  -webkit-transform: translateX(200px);
  transform: translateX(200px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}

.navMenu a:nth-child(4):hover ~ .dot {
  -webkit-transform: translateX(285px);
  transform: translateX(285px);
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
  opacity: 1;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="header">
        <div class="item item--header">
          <nav class="navMenu">
            <a href="#">Home</a>
            <a href="#">Blog</a>
            <a href="#">Work</a>
            <a href="#">About</a>
            <div class="dot"></div>
          </nav>
        </div>
      </div>

      <div class="feature">
        <div class="item item--1">box1</div>
        <div class="item item--2">box2</div>
        <div class="item item--3">box3</div>
        <div class="item item--4">box4</div>
        <div class="item item--5">box5</div>
        <div class="item item--6">box6</div>
      </div>

      <div class="footer">
        <div class="item item--footer">footer</div>
      </div>
    </div>
  </body>
</html>

Solution 3:[3]

Use this code for aligned to center:

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

ul {
  float: left;
  border: solid;
}

div {
box-sizing: border-box;
  width: 40%;
  height: 80%;
  background-color: green;
  padding: 20px 0;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: solid;
}
h1,p{
  margin: 0;
}
button {
  height: 15%;
  width: 80%;
  display: block;
  margin: 0 auto;
}
<ul>
  <li>item1</li>
  <li>item2</li>
  <li>item3</li>
  <li>item4</li>
</ul>
<div>
  <h1>Title</h1>
  <button>button1</button>
  <button>button2</button>
  <button>button3</button>
  <button>button4</button>
  <p>Some text. Some text. Some text. Some text.</p>
</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhilash Asokan
Solution 2 Abhilash Asokan
Solution 3 Arman Ebrahimi