Adjust the alignment of items in the NavBar using BootStrap to ensure they

I'm having trouble centering the contents of my Nav Bar, and for some reason, everything seems to be left-aligned instead of centered.

I haven't used float: left anywhere, so I'm not sure why it's not working as expected.

The goal is to center both the image and navigation links in the bar.

Here is the code snippet:

<!DOCTYPE html>

<html>
    <head>
        <title>Soni's Computer Repair</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/styles.css" rel="stylesheet">

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

        <script src="js/bootstrap.js"></script>

    </head>

    <body>
        <div class="navbar navbar-inverse navbar-static-top">

            <div class="container">
                <a class="navbar-brand"><img src="Final.png"/></a>

                <div class="navbar-header">
                    <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                </div>


                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li><a href="#">Home</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">About Us</a></li>
                        <li><a href="#">Contact Us</a></li>
                    </ul>
                </div>
            </div>
        </div>

        <footer class="footer">
            <div class="container">
                <h6 class="text-center">Copyright &copy; Soni Computer Repairs</h6>
                <p class="text-center">www.SoniRepairs.com</p>
            </div>
        </footer>
    </body>
</html>

This is the CSS code that would help align the elements properly:

.nav, .navbar-nav {
    text-align: center;
}

.container img {
    height: 100%;
    display: block;
    margin-right: auto;
    margin-left: auto;
}

.footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 60px;
    background-color: #f5f5f5;
}

@media screen and (max-width: 500px) {

    .navbar-nav, .container img{
        margin-left: auto;
        margin-right: auto;
        display: table;
        table-layout: fixed;
        float: none;
    }

    .navbar-brand {
        position: absolute;
        width: 100%;
        left: 0;
        text-align: center;
        margin: auto;
    }
}

Answer №1

HTML: I made a change by placing the navbar-brand element inside the ul tag

<div class="navbar-collapse collapse" aria-expanded="false" style="height: 1px;">
    <ul class="nav navbar-nav">
        <a class="navbar-brand"><img src="Final.png"></a><li><a href="#">Home</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Contact Us</a></li>
    </ul>
</div>

CSS: I've included these 3 lines of code for styling purposes

@media (min-width: 768px) {
    .navbar-nav { float: none; }
    .navbar-nav>li { float: none; display: inline-block; }
    .navbar>.container .navbar-brand, .navbar>.container-fluid .navbar-brand { float: none; display: inline-block; }
}

This adjustment has been effective in my setup. Hope you find it helpful! =)

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

Deciphering the principles of Bootstrap

I'm looking to understand more about what Bootstrap is. I know it's commonly used for styling web pages, but could you provide me with a clear explanation? Can big companies like Twitter, Facebook, or YouTube utilize Bootstrap for their platforms ...

Initiate an animation in Wordpress once the entire page has finished loading

Recently, I incorporated some "raw html" elements with animations into my WordPress site. However, the issue I'm facing is that these animations kick off as soon as the page loads, without waiting for the preloader to complete and display the actual c ...

Tips for customizing the event target appearance in Angular 2?

After following the steps outlined in this particular blog post (section 3, event binding), I successfully added an event listener to my component class. I can confirm that it responds when the mouse enters and exits. <p class="title" (mouseenter)="unf ...

Enhance your web design skills by customizing Bootstrap5 framework styles using the @layer feature

Here is the HTML code I am working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name=& ...

Html content overlapping div rather than being stacked vertically

My goal is to arrange a group of divs inside another div in a vertical stack. However, I am facing an issue where the text from the last div is overlapping with the second div instead of following a proper vertical alignment where the text in the third div ...

File remains visible after deletion upon refreshing the page, but disappears after using the Ctrl + F5

After deleting a file on the server, I noticed that it still appeared when I refreshed the page. However, when I tried to do a hard refresh by pressing ctrl+F5, it resulted in a 404 error. This behavior is puzzling to me - shouldn't a simple refresh s ...

Slide inside a div to move content

Check out the script I've been experimenting with for bringing in a menu from the left: Demo Fiddle To make this work, the sliding DIV needs to not only appear on the left but also push the 'left panel' and 'right panel' accordin ...

The correct way to write media queries in CSS for the iPhone

After successfully formatting a website for various devices using an online responsive design tester and CSS declarations like @media only screen and (max-width: 480px), I encountered an issue during testing on an actual iPhone. The browser was not computi ...

Is it possible to modify hidden classes within tempus-dominus (datetimepicker) without accessing the source file?

I'm trying to modify the .day class of tempus-dominus (), but even using !important doesn't seem to work when inspecting it (avoid clicking around as the inspection can be strange, and even forcing :hover makes the dropdown disappear). For examp ...

Revamp and Enhance Your Layout Using Bootstrap Cards

I am working with a bootstrap card and trying to control the visibility of the .task__content part using CSS transforms. Specifically, I want to hide it with transform: scaleY(0) and reveal it on hover over the .task element. However, I am encountering a s ...

Div element obstructing the ability to view markers

Issue with a positioned DIV over Google Maps: The DIV is causing accessibility problems for the map, taking up the entire width when it should stop at the green square borders. Interaction with the map is limited to the very bottom after the green square. ...

What could be the reason for my React/HTML select element occasionally failing to display the default selected value?

I am currently working on creating a select element in Reactjs/HTML and I am facing an issue with setting a default value based on a component variable. Essentially, the default value of the select should be the id of a component. This is my current imple ...

Methods to prevent images from spilling over into divs?

I am looking to implement a unique functionality that involves three images sliding out of the page to the right, fading in the process, and not overflowing. Simultaneously, three other images slide in to take their place. If you want to see the Framework ...

Display a fresh <p> tag when the condition in the if-statement is met

If you are looking for a questionnaire, check out this one. Now, I am interested in creating if-statements using HTML/CSS/jQuery to achieve the following scenario: Initially, only Question 1 will be visible. When the input matches a certain value like X, ...

What are the challenges associated with using replaceChild?

function getLatestVideos(url) { var http = new XMLHttpRequest(); http.open("GET", url, false); // false for synchronous request http.send(null); return http.responseText; } var videosText = getLatestVideos("https://www.googleapis.com/youtube/v3/se ...

"Want to extend the current date (Y-m-d) by a few days? Here

On my form, there is a date input that I want to automatically set to the date that is 14 days from today. I know how to set the value to today's date, but how can I add 14 days to it? <p>Term:<br> <input type="date" name="term" i ...

When viewing a webpage on a small browser, the content will automatically expand to fill the

Attempting to utilize responsive CSS media queries to hide the sidebar unless the screen is large or a tablet big enough in landscape mode. It appears to be functioning properly while resizing the browser, but at a certain size, it occupies the entire scre ...

Customizing listview appearance in asp.net with css and enhancing with javascript

When I print, the css class is not being used although it appears when using the program <script type="text/javascript"> function printDiv() { var divToPrint = document.getElementById('DivIdToPrint'); ...

What could be causing my jQuery script to malfunction?

After scouring through numerous Stack Overflow questions and conducting countless Google searches, I am still stumped by the issue at hand. As a beginner in web development, all I want is for this simple page to function properly. Can someone please point ...

When clicking on an HTML link pointing to a cloud, it does not open in the same frame but instead opens in a new window

I am currently working on an HTML5 application that features a screen divided into two parts. The left part of the screen contains a few links, and upon clicking one of these links, the resulting content should open in the right side frame within the same ...