How center elements in a navbar?

How can I center elements in a navbar using only HTML and CSS?

Asked by: AlgorithmWizard

Answers:

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Centered Navbar</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <nav class="navbar">
    <div class="nav-center">
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Services</a>
      <a href="#">Contact</a>
    </div>
  </nav>
</body>
</html>

CSS (styles.css)

body {
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
}

.navbar {
  background-color: #333;
  color: #fff;
  height: 60px;
}

.nav-center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.nav-center a {
  color: #fff;
  text-decoration: none;
  padding: 10px 20px;
}

.nav-center a:hover {
  background-color: #555;
}
Answered by: PixelMagician

Answer:

Related Pages:

  • Waveshaping Distortion with Tone.js

    In the Tone.js library, how would you create a custom audio effect that applies a dynamic waveshaping distortion to an incoming audio signal, allowing the user to control the amount of distortion and the shape of the waveshaping curve using HTML sliders, while ensuring efficient memory management and minimizing audio latency?

  • Building Spotify Clone with HTML, CSS, and JavaScript

    I'm looking to create a clone website of Spotify using HTML, CSS, and JavaScript. Can anyone guide me on the essential steps and provide some example code snippets? I want to ensure the website is SEO friendly and has an engaging user interface. Any suggestions on design patterns or best practices for this project?

  • Designing Brain-Controlled Web Interfaces

    Imagine a scenario where you have to develop a web application that can be controlled using brain signals. How would you design the interface and implement the interaction using HTML, JavaScript, and any other necessary technologies?

  • Create a numpy clone for java

    Is it possible to create a numpy clone for java using java class? If yes, create one class named np and functions with parameters. Functions name: np.array(), np.zeros()

  • Create a numpy clone for java

    Is it possible to create a numpy clone for java using java class? If yes, create one class named np and functions with parameters. Functions name: np.array(), np.zeros()