2

I'm trying to create a site and started with the navbar. I'm trying to center the logo and give the subs/links (? don't know what they're called, the a-tags) a little more space.

I tried following some video tutorials and Stackoverflow answers but they caused the left 3 links to be more downwards and the 3 right links to be above everything else, and caused the overall header size to almost double. I'd hugely appreciate it if someone could help me out! :) There might be some trash code that's not needed or is a bad practice overall, but incase it wasn't clear I'm a total beginner when it comes to web design :p

body { 
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
    display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

.header-right {
  float: right;
}

.header-left {
  float: left;
}

@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: block;
    text-align: left;
  }
  
  .header-right {
    float: none;
  }
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="assets/style.css">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet"></head>
<body>

<div class="header">
  <div class="header-right">
    <a href="#"><b>1</b></a>
    <a href="#"><b>2</b></a>
    <a href="#"><b>3</b></a>
  </div>
  <a href="#" class="logo">LOGO</a>
  <div class="header-left">
    <a href="#"><b>4</b></a>
    <a href="#"><b>5</b></a>
    <a href="#"><b>6</b></a>
  </div>
</div>

</body>
</html>
Always Helping
  • 14,316
  • 4
  • 13
  • 29
kekerino
  • 39
  • 4

4 Answers4

3

You can use display:flex property along with align-items: center on your header class.

Using flex properties is a rec-emended way these days since its very responsive on modern browsers

Add this code in for your header class

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
}

Live Working Example:

body {
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px;
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

.header-right {
  float: right;
}

.header-left {
  float: left;
}


@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: block;
    text-align: left;
  }
  .header-right {
    float: none;
  }
}
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="assets/style.css">
  <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">
</head>

<body>

  <div class="header">
    <div class="header-right">
      <a href="#"><b>1</b></a>
      <a href="#"><b>2</b></a>
      <a href="#"><b>3</b></a>
    </div>
    <a href="#" class="logo">LOGO</a>
    <div class="header-left">
      <a href="#"><b>4</b></a>
      <a href="#"><b>5</b></a>
      <a href="#"><b>6</b></a>
    </div>
  </div>

</body>

</html>
Always Helping
  • 14,316
  • 4
  • 13
  • 29
  • 1
    I think you mucked it up as the left header is on the right and the right header is on the left. – The Grand J Sep 11 '20 at 02:04
  • 1
    No? As in it's working as intended? I don't think this is the result that the OP wanted based on the example given. – The Grand J Sep 11 '20 at 02:06
  • 1
    @TheGrandJ How about now ? – Always Helping Sep 11 '20 at 02:06
  • 1
    There you go :) now it matches the example. Hopefully one of our answers is the one they needed. Have a vote up on your excellent answer with all the detail you put in – The Grand J Sep 11 '20 at 02:07
  • Can't believe this got answered in like 5 minutes, I tried the code and it worked exactly like I wanted it... I've been struggling with this for hours now. I greatly appreciate the help, and linking to the css-tricks site so I can learn more! Thank you! Also, thanks to Ayush and The Grand J for answering too. I can't upvote unfortunatly at the moment since I'm a new account but I'll remember this thread and come back. It's a shame there's no donation button for contributors, would really like to send you guys some for a coffee :) – kekerino Sep 11 '20 at 02:11
  • 2
    @kekerino You are welcome - Happy to help. You can upvote now others answer as useful if it helped you. I have upvoted your question so that you can upvote other answers. Thanks – Always Helping Sep 11 '20 at 02:12
  • 1
    Upvoted you guys! Thanks again and sorry for the spam, hope y'all stay safe :) – kekerino Sep 11 '20 at 02:15
0

To solve your problem simply change the CSS where display: block to display: inline-block.

The inline-block tells it to render the block on the same line rather than by itself.

body { 
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
    display: inline-block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

.header-right {
  float: right;
}

.header-left {
  float: left;
}

@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: inline-block;
    text-align: left;
  }
  
  .header-right {
    float: none;
  }
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="assets/style.css">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet"></head>
<body>

<div class="header">
  <div class="header-right">
    <a href="#"><b>1</b></a>
    <a href="#"><b>2</b></a>
    <a href="#"><b>3</b></a>
  </div>
  <a href="#" class="logo">LOGO</a>
  <div class="header-left">
    <a href="#"><b>4</b></a>
    <a href="#"><b>5</b></a>
    <a href="#"><b>6</b></a>
  </div>
</div>

</body>
</html>
The Grand J
  • 348
  • 2
  • 5
  • 14
0

Use the <centre></centre> tag.

0

Here is a pen where I got everything working.

I was able to get everything how you wanted by simply using CSS Flexbox.

I added display:flex; and flex-direction: row; so that the navbar contents flex along the row axis. Additionally, I added justify-content: space-between; so that on the main (row) axis, the content is justified with space in between.

Next, I simply added display: flex; properties to the 3 items in the navbar along with respective properties so that they are aligned left (aka flex-start), center, and right (aka flex-end).

I highly recommend trying to do all of you responsive layout work with Flexbox or Grid. To really learn Flexbox well, I recommend going through all steps of Flexbox Froggy

body { 
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

/* added display flex, specified row and space-between */
.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  padding-top: 0;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

/* added display flex properties */
.header-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

/* added display flex properties */
.header-left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: block;
    text-align: left;
  }
  
  .header-right {
    float: none;
  }
}
Markohan
  • 122
  • 1
  • 11