It's proving difficult for me to align my header and navbar on the same line

Recently, I've delved into the world of HTML/CSS and encountered a challenge. My goal is to align my header (containing an h1 tag) and navbar on the same line. Ideally, the header should float left with the navbar positioned closely to its left. However, all my attempts thus far have not produced the desired outcome.

I experimented with floating both the header (identified by mainHeader) and the navbar (with navBar ID) to the left, but that didn't work. I also tried floating the header left and the navbar right, only to face another setback.

<header class="mainHeader"><h1>Website</h1></header>
    <div class="container">
        <nav id="navBar">
            <ul>
                <a href="#">
                    <li>Home</li>
                </a>
                <a href="#">
                    <li>About</li>
                </a>
                <a href="#">
                    <li>Books</li>
                </a>
                <a href="#">
                    <li>Contact Us</li>
                </a>
            </ul>   
        </nav>
        </div>
        <div class="clr"></div>
        <div class="container">



#mainHeader{
    float: left;
    width:30%;
    box-sizing: border-box;
}
#navBar{
    ;
    margin-top: 0px;
    float:left;
    padding: 0;
    margin: 0;
    float:right;
    width:70%;
    box-sizing: border-box;
}
#navBar ul{
    list-style: none;
    text-align: center;

}
#navBar li{
    display: inline;

}
#navBar a{
    text-decoration: none;
}

Answer №1

Utilizing flex is a straightforward way to achieve this layout.

.menuwrappper{
  display: flex;
  justify-content: space-between;
  align-items: center;
}

ul{
  display: flex;
}

ul li{
  list-style: none;
  margin-left: 30px;
}
<div class="menuwrappper">
  <header class="mainHeader"><h1>Website</h1>
</header>
    <div class="container">
        <nav id="navBar">
            <ul>
               <li><a href="#">Home</a></li>
                             <li><a href="#">Home</a></li>

                             <li><a href="#">Home</a></li>

                             <li><a href="#">Home</a></li>

                             <li><a href="#">Home</a></li>

            </ul>   
        </nav>
        </div>
 

</div>

Additionally, make sure that li is a direct child of ul or ol.

I suggest learning more about flex at https://css-tricks.com/snippets/css/a-guide-to-flexbox/. It will greatly benefit your webpage's layout.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for fading out two elements after completing a drag and drop action

Visit this Codepen for more examples - Codepen I have been developing a drag and drop feature in my application. Once the item is dropped, it transitions from red to green and fades out smoothly. The droppable element behind the draggable should also fad ...

Show a picture upon hovering the button

Welcome to my website. My goal: Show an image when a user hovers over the links. I must be making some silly error, but I can't pinpoint it. This is what I've attempted so far: HTML <ul class="nm"> <li><a href="#">Cork& ...

Center the middle item in a flexbox column arrangement

I'm working on a layout where I have three items stacked vertically within a column. My goal is to center the middle item (green) in the column and have the other items positioned around it. Here's my current setup: .flex-row { display: fl ...

What is the best way to extract a table from a website that has stored the table data separately from the HTML?

Attempting to extract table data from the following URL: In a previous scraping attempt, the Python packages utilized were: from bs4 import BeautifulSoup import requests import mysql.connector import pandas as pd from sqlalchemy import create_engine Howe ...

The responsiveness issue with the Bootstrap button menu in my Django project is causing functionality problems

I am currently working on a Django project and have set up a static folder in my "accueil" application. However, when I resize the browser window, the button to show the menu does not function as expected. The template I downloaded for free is not working ...

AngularJS fetches the 'compiled HTML'

If I have this angularjs DOM structure <div ng-init="isRed = true" ng-class="{'red': isRed == true, 'black': isRed == false}"> ... content </div> How can I obtain the 'compiled' version of this on a cl ...

How can jQuery be used to display the size and type of linked files in title attributes?

For instance: Prior to <a target="_blank" href="http://www.adobe.com/devnet/acrobat/pdfs/reader_overview.pdf"> Adobe Reader JavaScript specification </a> As the file is in PDF format, the title should read title="PDF, 93KB, opens in a new ...

Create collapsible horizontal submenus in Bootstrap for mobile devices

I created a modal specifically for mobile users that displays when they access the site. It features various categories and nested menus for easy navigation. <!-- Modal --> <div class="modal fade" id="exampleModalCenter" t ...

Adjusting the link color within a div when hovering over it

Is it possible to change the color of all links in a div when hovering over it, rather than just the individual link itself? I have a div with multiple links and would like them all to transition to a different color when the div is hovered over. I would ...

Images with full-width will scroll horizontally

Is there a way to fix the horizontal scroll issue? <!DOCTYPE html> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https:// ...

Placing an item from a "stack" onto a separate line on smaller screens

My goal in Bootstrap is to achieve the design shown in this image: https://i.stack.imgur.com/9Eoen.png The first layout can be achieved by placing B and C within the same div container. The second layout can be accomplished by defining C on a new row ind ...

A guide to adjusting the width without affecting the td element

Currently, I am facing a challenge while coding in HTML. I am trying to create a table with a header (time range) that fits on a single line without affecting the width of the td elements. Below is the code snippet: <table style="border:1px solid #8c ...

Is there a way to ensure that when displaying information boxes with Bootstrap, the inputs do not get misplaced or shuffled to the wrong location

I have been working on a form that needs to display an alert: https://i.sstatic.net/uGKtG.png When clicking the button next to the input control, an alert should appear. Although the alert is showing correctly, the input controls are shifting to the wron ...

The appearance of Bootstrap-Navbar is altered when viewed on a mobile device

After creating a website that was compatible with both web and mobile devices, I encountered an issue where the navbar would not collapse on my smartphone. However, when I resized the browser window to match the size of the smartphone screen, it worked per ...

The form within the dynamically loaded AJAX content is malfunctioning

My webpage is set up to load content from a separate file (content.php) into a div and refresh it every 5 seconds. In the content.php file, I have a form (basic HTML without javascript) that works fine when accessed directly at (example.com/content.php). ...

jQuery toggle functioning in one scenario, but failing in another

My experience with JS/Jquery is limited which might explain why I'm struggling with this. I am attempting to hide some text on a page and then make it visible again by clicking on a toggle link. The JavaScript code below is what I have been using. The ...

2-panel accordion interface

Hello there, I've encountered a problem that seems to be related to CSS. Here is the project I'm currently working on: My main focus is on the News accordion menu. The goal is to display a small image (50x50 with padding) next to a large headlin ...

Why is only jQuery 3.2.1 or jQuery Mobile 1.4.5 being loaded?

When I load my html file, only jquery or jquery mobile is loading from the head. Changing the order in which they appear in the head causes a switch in which one is loaded. Is this possibly due to a compatibility issue? <head> <title& ...

Reduce the count of products when the button is clicked

Hey there! I'm running a special promotion on my website with limited spots available at a reduced price. I'd love to have a real-time counter that shows the number of remaining spots, decreasing by one each time someone clicks the 'sign up& ...

What is the best way to set the cell width for two HTML tables with varying numbers of columns?

If I were to create 2 different sets of tables like this: <table id="Table1"> <tr> <td></td><td></td><td></td> </tr> </table> <table id="Table2"> <tr> < ...