Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

HTML & CSS Gap between Nav Bar and <h1> text

Hi there, I was making my website when I noticed that there was a gap that gets filled up by other elements between the nav bar and the Header of the Page when I do not want that to happen. Do you think you could help?
CSS:
h1 {
    float:left;
    padding-top: 15px;
    padding-right: 25px;
    padding-left: 25px;
    padding-bottom: 25px;
}

a {
    color: #66ff00;
    text-decoration: none;
}

.topnav a {
    float: right;
    padding-top: 25px;
    text-align: center;
    color: #66ff00;
    padding-bottom: 25px;
    padding-left: 10px;
    padding-right: 10px;
}
HTML:
<header>
            <div class="topnav">
                <a href="Contact.html">Contact Us</a>
                <a href="Join-us.html">Join Us</a>
                <a href="Projects.html">Our Projects</a>
                <a href="About.html">About Us</a>
                <a href="#home" class="active">Home</a>
            </div>
            <div class="title">
                <h1>Joshua Miles Foundation</h1>
            </div>
    </header>
 
HTML:
<!DOCTYPE html>
<html lang="en-GB">
    <head>
        <title>The Joshua Miles Foundation | Home</title>

        <link rel="stylesheet" type="text/css" href="./style.css">
        <link rel="icon" type="image/x-icon" href="Images/joshpog.png">
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">

        <meta charset="UTF-8">
        <meta name="author" content="The Joshua Miles Foundation">
        <meta name="description" content="Homepage of the Joshua Miles Foundation website">
        <meta name="keywords" content="Joshua Miles, Foundation">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="theme-color" content="#ff0000">
        <meta property="og:type" content="website">
        <meta property="og:url" content="https://the-joshua-miles-foundation.github.io">
        <meta property="og:title" content="The Joshua Miles Foundation | Home">
        <meta property="og:description" content="Welcome to the Joshua Miles Foundation">
        <meta property="og:image" content="">
        <meta property="og:image:height" content="256px">
        <meta property="og:image:width" content="256px">
        <meta property="og:sitename" content="JM Foundation">

        <script src="./global/scripts.js" defer></script>
    </head>
    <header>
            <div class="topnav">
                <a href="contact.html">Contact Us</a>
                <a href="join-us.html">Join Us</a>
                <a href="projects.html">Our Projects</a>
                <a href="about.html">About Us</a>
                <a href="#home" class="active">Home</a>
            </div>
            <div class="title">
                <h1>Joshua Miles Foundation</h1>
            </div>
    </header>
    <body>
        <hr>
    </body>
</html>
CSS:
* {
    font-family: "Roboto";
    margin: 0px;
}

body {
    background-color: #151621;
    color: #66ff00;
}

h1 {
    float:left;
    padding-top: 15px;
    padding-right: 25px;
    padding-left: 25px;
    padding-bottom: 25px;
}

a {
    color: #66ff00;
    text-decoration: none;
}

.topnav a {
    float: right;
    padding-top: 25px;
    text-align: center;
    color: #66ff00;
    padding-bottom: 25px;
    padding-left: 10px;
    padding-right: 10px;
}

.topnav a.active {
    color: white;
}

.topnav a:hover {
    color: white;
    transition: 1s;
}
 
There may be several reasons for the gap you are seeing, you can fix it with below ways,
Instead of floating <h1>, use Flexbox

Code:
.header {
  display: flex; /* Enable Flexbox for the header */
  align-items: center; /* Align contents vertically within the header */
  justify-content: space-between; /* Distribute elements horizontally */
}

.title {
  padding: 15px 25px; /* Adjust padding if needed */
}

.topnav a {
  padding: 10px; /* Adjust padding if needed */
}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom