Header One tag causing strange behavior in Navbar

Struggling with my HTML and CSS - I'm trying to create a navbar. HTML:

<!DOCTYPE html>
  <html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My Website</title>
    <link rel="stylesheet" href="./index.css">
</head>
<body>
    <div class="nav">
            <a href="#" class="nav-title">Website</a>
            <nav>
                <ul>
                    <li><a href="#" class="nav-link">Home</a></li>
                </ul>
            </nav>
    </div>
<h1>Heading</h1>
</body>
</html>

CSS:

@import url('https://fonts.googleapis.com/css?family=Roboto:400,700');

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


.nav-title {
    float: left;
    color: rgb(114, 114, 114);
    text-decoration: none;

    padding: 1rem 1rem 1rem 1rem;
}

nav {
    float: right;
}

nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

nav li {
    display: inline-block;
    margin-left: 70px;
    position: relative;

    padding: 1rem 1rem 1rem 1rem;
}

nav li a {
    padding: 5px 5px 5px 5px;
    color: rgb(114, 114, 114);
    text-decoration: none;
    background-color: rgb(230, 230, 230);
    border-radius: 3px;
}

.title {
    margin: 0;
    text-align: center;
    color: rgb(114, 114, 114);
    font-size: 3em;
    padding-top: 2vh;
}

But when I add an h1 tag below the div with the nav class, it messes things up.

Screenshot Here

What am I missing?

Answer №1

Check out this resource on floats

div.nav is experiencing layout issues due to containing only floating elements. Here's a CSS fix:

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

/* Clearfix for wrapping floating content */
.clearfix::after {
    content: "";
    clear: both;
    display: table;
}
/* End of the fix */

.nav { background: #eee; }

.nav-title {
    float: left;
    color: rgb(114, 114, 114);
    text-decoration: none;

    padding: 1rem 1rem 1rem 1rem;
}

nav {
    float: right;
}

nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

nav li {
    display: inline-block;
    margin-left: 70px;
    position: relative;

    padding: 1rem 1rem 1rem 1rem;
}

nav li a {
    padding: 5px 5px 5px 5px;
    color: rgb(114, 114, 114);
    text-decoration: none;
    background-color: rgb(230, 230, 230);
    border-radius: 3px;
}

.title {
    margin: 0;
    text-align: center;
    color: rgb(114, 114, 114);
    font-size: 3em;
    padding-top: 2vh;
}
<div class="nav clearfix">
            <a href="#" class="nav-title">Website</a>
            <nav>
                <ul>
                    <li><a href="#" class="nav-link">Home</a></li>
                </ul>
            </nav>
    </div>
            <h1 class="title">My header</h1>

Answer №2

Upon inspection, everything appears to be in order from my end. Feel free to check out my jsfiddle for reference. To ensure the H1 tag is positioned below the navigation bar, it is necessary to define the .nav class in your CSS.

Additionally, give width: 100% and overflow: hidden a try on the .nav element. This should solve the issue.

Visit the jsfiddle here for more details

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

Can you explain the process of implementing @media queries in SASS?

I am having some trouble understanding the syntax for media queries in SASS. I attempted to use this particular line of code, but it resulted in an error: @media screen (max-width: 1550px) #server-name left: 80% ...

Pattern to filter out numeric values from a collection

I want to apply a regex pattern to the <input> form element. The pattern should specify a range of numbers that are not permitted as input. For example, let's say I have a list of numbers like {1,4,10}, and any number other than these should b ...

Using AJAX and PHP to dynamically fill HTML drop-down menus

In order to populate the DropDown controls on my HTML Form dynamically, I have implemented a code that utilizes AJAX to make a call to a .php file. This .php file is responsible for filling the DropDown control with values from a single column. Throughout ...

Preserve the location of selected text in an HTML document with the help of

Is there a way to retrieve the character positions of the start and end of a selected text using window.getSelection() within the HTML code? This data would need to be saved to the server for future reference. ...

Discovering the present width of an Angular element after it has been eliminated

Imagine you have a horizontal navigation bar coded as follows: HTML: <ul> <li ng-repeat="navItem in totalNavItems">{{name}}</li> </ul> CSS: ul, li { display: inline-block; } The data for the navigation items is fetched from thi ...

Script Error: Sorry, that function is not recognized

Currently, I am working on creating a basic bread-and-butter HTML and Javascript in Appscript to develop a modal dialog. The issue arises when I try to call a function upon clicking a button or checking a checkbox. Despite the modal appearing correctly, it ...

Incorporate dynamic JavaScript content within an HTML document

Currently, I am facing an issue with a webpage that I need to load in order to extract some information. Using HttpClient and Jsoup worked well for loading the majority of the content on the page. However, my problem lies with certain elements that are onl ...

Non-encrypted GWT HTML class tag styles

I have inherited an old xsl transformation that generates plain HTML, which is then bound to a widget using a GWT HTML field. The current setup looks like this: HTML html = new HTML html.setHTML(result) In the UiBinder section of the widget, there are so ...

Unable to send email through Ajax/PHP contact form

It's quite amusing that it worked perfectly for one evening. After reaching out to my host, they assured me that there should be no issues with it not working. I even tried testing it in Firebug, but it appeared to be sending successfully. Additionall ...

Struggling to properly close the bootstrap tags

Having an issue where I can't seem to properly close Bootstrap tags in my code. Here's a snippet of the HTML: <html> <head> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/jquery.js ...

jQuery effects failing to run properly in Internet Explorer

<script type="text/javascript" src="css/jquery-1.7.1.js"></script> <script type="text/javascript"> function slidedown(id){ $('#rerooftext').hide(0); $('#inspectiontext').hide(0); $('#remodelingtext').hid ...

When using jQuery and AJAX together, it seems that the POST method is returning

Currently experimenting with Cloud9. The current project involves creating an image gallery. On the initial page: There are a few pictures representing various "categories". Clicking on one of these will take you to the next page showcasing albums fro ...

A dual row navigation layout with the second row featuring equally distributed elements in a neat and organized manner

Looking to create a responsive collapsible navigation with two rows. The first row should contain the logo and language selector, while the second row should have the main links: | | | ...

Customize your website with CSS and Bootstrap: create a transparent navbar-wrapper

Is there a way to make the menu bar background transparent without affecting the transparency of the text on the menu bar? This template is based on Twitter Bootstrap carousel. In this specific line, <div class="navbar navbar-inverse navbar-static-to ...

What is the process of video buffering in HTML5?

Is automatic buffering handled by HTML5? I'm planning to use video tags to display user-uploaded videos in my application. From what I've gathered, HTML5 buffers videos differently depending on the client's browser, with buffering varying ba ...

Scalable and movable images contained within a div

Is there a way to enable resizing and dragging for each image? Below is a snippet of code that allows users to select which image they would like to display. They have the option to choose multiple images if they prefer. I am looking to implement functio ...

The Clash of Bootstrap Spanning: Firefox Takes on Chrome and Safari

I am experiencing some strange issues with spacing in different browsers when using Bootstrap. Firefox is showing the spans correctly, but Chrome and Safari are spanning the items across the full width of the page, which I know is a responsive behavior of ...

Selecting elements in Thymeleaf using jQuery

Is there a way to retrieve the value from the hidden field within the div shown below? <div class="col-lg-6" th:each="devicedit : ${eachdevicedetails}"> <div class="courses-container"> <div class="cou ...

Guide to making a Material Design Radial effect animation

I am looking to create a unique toolbar effect by following the material design radial reaction choreography guideline. https://i.stack.imgur.com/6oB8r.gif I want to achieve this using an angular 2 transition, but I need some guidance on how to implement ...

Transform a flexbox child into a complete row that is wider than the container

I'm currently in the process of developing a portfolio section using Bootstrap 4 and delving into flexbox knowledge. Each item in the portfolio starts with a thumbnail that can be expanded or collapsed to reveal more details when clicked. When the vi ...