What is causing the content to overflow outside of the navbar on smaller screens or mobile devices?

/* *{
  font-family: sans-serif;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
} */

.main {
  min-height: calc(100vh - 10rem);
  display: grid;
  place-items: center;
}

p {
  font-size: 4rem;
  line-height: 1.6;
}

nav {
  z-index: 1;
}


/* .main1 {
  display: flex;
  justify-content: center;
  align-items: center;
} */

.navbar-nav.navbar-center {
  position: absolute;
  left: 50%;
  transform: translatex(-50%);
}

.nav-link {
  position: relative;
  text-align: center;
}

.nav-link>a {
  letter-spacing: 1px;
  color: grey !important;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: 0.5s;
}

@keyframes animation {
  from {
    opacity: 0;
    transform: translateY(15px);
  }
  to {
    opacity: 1;
    transform: translateY(0px);
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <title>Navigation</title>

  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="style.css">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>

<body>
  <nav class="navbar navbar-expand-sm navbar-default">
    <div class="navbar-header">
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#myNavbar" aria-controls="myNavbar" aria-expanded="false" aria-label="Toggle navigation">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav navbar-center text-center">
        <li class="nav-link"><a href="#">Home</a></li>
        <li class="nav-link"><a href="#">Upcoming Event</a></li>
        <li class="nav-link"><a href="#">Our Team</a></li>
        <li class="nav-link"><a href="#">Results</a></li>
        <li class="nav-link"><a href="#">Contact Us</a></li>
      </ul>
    </div>
  </nav>
  <div class="container main">
    <p>My query relates to a design issue during responsiveness testing of my navigation bar. As I resize the window, the content tends to overflow the navigation bar and even the toggle button doesn't provide a solution as it still causes overflow when clicked. Any advice on how to fix this would be greatly appreciated.
Thanks.</p>
  </div>
</body>

</html>

Answer №1

Opt for using the flex property instead of manually positioning the navbar.

This is recommended especially for flex items displayed as column in small devices view (<=768px).

@media only screen and (max-width:768px){
  .navbar-nav.navbar-center { 
    display: flex;
    flex-direction:column;
  }
}

Rather than using this code:

.navbar-nav.navbar-center { 
  position: absolute; 
  left: 50%; 
  transform: translatex(-50%); 
} 

Add the following code to center align navbar-center on desktop view:

.navbar-nav.navbar-center { 
    float:none;
    display: flex;
    align-items: center;
    justify-content: center;
 }

https://codepen.io/Rayeesac/pen/LYNdNra

.main {
  min-height: calc(100vh - 10rem);
  display: grid;
  place-items: center;
}

p {
  font-size: 4rem;
  line-height: 1.6;
}

nav {
  z-index: 1;
}
.navbar-nav.navbar-center { 
    float:none;
    display: flex;
    align-items: center;
    justify-content: center;
 }
@media only screen and (max-width:768px){
  .navbar-nav.navbar-center { 
    display: flex;
    flex-direction:column;
  }
} 

.nav-link {
  position: relative;
  text-align: center;
}

.nav-link > a {
  letter-spacing: 1px;
  color: grey !important;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: 0.5s;
}

@keyframes animation{
  from{
      opacity: 0;
      transform: translateY(15px);
  }
  to{
      opacity: 1;
      transform: translateY(0px);
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1>
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <title>Navigation</title>

  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="style.css">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
    integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
    integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
    crossorigin="anonymous"></script>

</head>

<body>
  <nav class="navbar navbar-expand-sm navbar-default">
    <div class="navbar-header">
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#myNavbar"
        aria-controls="myNavbar" aria-expanded="false" aria-label="Toggle navigation">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav navbar-center text-center">>
        <li class="nav-link"><a href="#">Home</a></li>
        <li class="nav-link"><a href="#">Upcoming Event</a></li>
        <li class="nav-link"><a href="#">Our Team</a></li>
        <li class="nav-link"><a href="#">Results</a></li>
        <li class="nav-link"><a href="#">Contact Us</a></li>
      </ul>
    </div>
  </nav>
  <div class="container main">
    <p>Testing out Navigation</p>
  </div>
</body>

</html>

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

Using jquery to locate an element based on a specific id pattern

In my search, I am attempting to locate a span element with an id that follows a specific pattern. This is primarily used for locating particular elements generated by an asp.net (aspx) page that is based on a master page. ...

JavaScript Functions for Beginners

I'm currently facing some challenges with transferring the scripts and HTML content from the calendar on refdesk.com. My task involves moving the JavaScript to a separate stylesheet and utilizing those functions to replicate the calendar on an HTML pa ...

Adjusting Table Width with Bootstrap Styling

https://i.sstatic.net/MIkw0.png Could someone please advise on how to adjust the width of the Bootstrap HTML table above? It appears to be spreading out across the entire browser screen. Thank you. # in this section, I inserted bootstrap into my html tab ...

Implementing dynamic width with ng-style in AngularJS

I am trying to dynamically resize a div when an event is fired from S3. In the code below, I pass the progress variable to the updateProgress function as a parameter. This function resides in the top scope of my Angular controller. s3.upload(params).on(& ...

Chosen link fails to update color

I've successfully implemented a navigation bar that changes the content in the body when each link is selected. Utilizing AJAX for dynamic content changing, I can change the color of menu items on hover but am facing issues with changing the color upo ...

jquery is showing up in the browserify bundle.js file, however, it is not functioning properly

Currently, I am trying to follow a brief tutorial on how to use Browserify. Despite following the instructions precisely, jQuery seems to not be working properly when bundled. Specifically, the button element in my app.js code is not appended to the body. ...

Transmitting several images via the HTML INPUT tag through AJAX to a Laravel Controller, in addition to sending other data not contained within the

$('#update-post').on('click', function(event){ $.ajax({ method: 'POST', url: editPostUrl, data: { content: $('#post-content').val(), deleteImages: deleteImages, postId: postID, ...

Instructions on inserting a new row beneath a selected row using jQuery

https://i.stack.imgur.com/AlwHm.png When the second column is clicked, the method below is called: function getListOfList(primaryId, isFlat, col) { alert(primaryId) $.ajax({ url : ("/" + serverURL + "/dataGrid/getChilds"), ...

Pass the disabled select option value to a form using a hidden input field

My Django form has a section that is set up like this: <select {% if 'ba1' in widget.name or 'bs1' in widget.name or 'pm2' in widget.name %} disabled {% endif %} id="{{ widget.attrs.id }}" ...

The continuous resizing of the window is triggering a loop in flexslider when the resize function is called

I am currently working on a website that utilizes the flexslider plugin. My goal is to detect when the browser window is resized, and then reinitialize the slider so that it can adjust its size and other parameters accordingly. Initially, I was able to a ...

How do I implement a dynamic input field in Angular 9 to retrieve data from a list or array?

I'm looking to extract all the strings from the Assignes array, which is a property of the Atm object, and populate dynamic input fields with each string. Users should be able to update or delete these strings individually. What approach can I take us ...

.toggleClass malfunctioning inexplicably

I've been struggling to make a div box expand when clicked and revert to its original size when clicked again. It seems like a simple task, but no matter what I try, I can't seem to get it right. I'm not sure where I'm going wrong, as t ...

A unique technique for creating a stunning visual effect with images using

Can anyone help me with this issue: Check out this animated GIF The images in my project are overlapping when scrolling! How can I achieve a similar effect for my images? Is there a tutorial or plugin available for this? Here is the current code sn ...

Implementing a smooth transition effect on an ajax tab

I have a tab code that uses ajax data attributes to load content. The tabs are loaded with ajax from PHP code based on the data attribute. How can I add a fade in/out effect to the tabs' content? I've attempted it but haven't been successful ...

Is it possible to update the content page without having to refresh the master page?

Our website features a Master page with main menu options and corresponding sub menus. When a user clicks on a main menu item, the relevant sub menus should be displayed. After clicking on a sub menu, the content page should update without having to refr ...

Tips for modifying the "width" of a style within an HTML template implemented in a NodeJs express application

Currently, I am facing a challenge in dynamically adjusting the width value for a <div> element serving as a coloured bar within an HTML template used for PDF generation. While I can successfully set other values using something like {{my_value}}, ap ...

Why do my padding and font size fail to match the height of my container?

When setting the height of my boxes to match the height of my <nav>, I encountered overflow issues. Despite using a 10rem height for the nav and a 2.25rem font, calculating the padding as 10-2.25/2 didn't result in the desired outcome. Can someo ...

Having trouble accessing NSE Bhavcopy zip file in R due to a HTTP status of '403 Forbidden'

My current project involves automating the process of downloading a zip file in R. On the specific webpage I am working with, I can easily download the zip file by clicking on it or by right-clicking and saving the file. However, my challenge lies in down ...

Recommendation for a jQuery slider extension for setting ranges

Looking for a smaller range slider plugin similar to http://jqueryui.com/demos/slider/#range. I don't want to include the whole UI pack just for the slider feature, as it's too large at 150 KB. ...

Bootstrap 4 custom dropdown menu

Looking to revamp the design of my dropdown menu, but struggling to achieve the desired outcome. My goal is to have the drop-down menu match the size of its container. (refer to screenshot below) I've attempted various modifications to the dropdown- ...