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

When Components in Vue are called in a Single File, certain elements may not be displaying as expected

I have just finished creating my components using Vue 2, Vuetify, and Vue cli - 4.5.15. I attempted to combine them into a Single Vue file but encountered issues with the components not displaying <v-icons>, <v-textfield>, and some other elemen ...

Check domains using Jquery, AJAX, and PHP

I'm currently developing a tool to check domain availability. Here is the PHP code I have so far: <?php $domain = $_GET["domname"]; function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); ...

The <mat-radio-button> component does not have a value accessor specified

When working with HTML and Angular, I encountered the following issue: <mat-radio-group> <mat-radio-button [(ngModel)]="searchType"> And (Narrower search) </mat-radio-button> <mat-radio-button [(ngModel)]="searchType"&g ...

Is there a way to delete a text link without deleting the entire class?

I am looking to specifically remove the link that contains the text "Skin" within the h3 tag. The challenge I am facing is that when I use the code $("h3:contains('Skin')").remove(); it removes the entire h3 element, whereas I only want to remove ...

Wordpress easily adjusts post alignments for a seamless display

Can anyone help me figure out how to properly align posts in Wordpress to match the example image attached? I've tried using inline-block or float with CSS/HTML, but the posts are not aligning correctly. I'm unsure of what this method is called s ...

Best practice for passing a variable argument in a JavaScript callback?

After searching the internet without success, I couldn't find the answer to my problem. The issue revolves around a function that I have: function ParentFunction (DataBase, Parameters) { for (k = 0; k < DataBase.length; k++){ var ...

The interval continues even when the mouse is no longer hovering

I'm having an issue with the interval "myInterval" continuing even after I move the mouse out of the triggering element. Why is it not stopping as expected? $(".postimagepic").mouseenter(function () { var link = $("#blurpost").attr("src").split(" ...

Generating several copies of an identical form using jQuery and HTML

While employing ASP.NET MVC, Partial Views, and Dialogs, I am making an ajax request to the server which returns a partial view. By using $('#elementTag').html(returnData) to refill the bounding divs, I encounter a situation where the returned pa ...

Design elements using CSS within the <th> tags

When working with HTML tables, the use of a <th> element allows for the implementation of a scope attribute. This attribute serves to indicate whether the header cell is associated with its subsequent column, row, or group. Is it feasible to leverage ...

Encountering a syntax error while utilizing a JavaScript variable in a jQuery selector for a lightbox feature

I'm currently working on creating a lightbox feature and have reached the stage where I am implementing next and previous buttons to navigate through images. I am utilizing console.log to check if the correct href is being retrieved when the next butt ...

Variations in the implementation of responsive web design on laptops versus mobile devices

Looking at the css code below: @media only screen and (min-width: 0px) and (max-width: 768px) { ... } I'm curious about why, on my laptop, when I resize the browser window to exactly 768px, the css properties inside this media query are applied. ...

The tag (p) is not located in a valid position

I am currently working with the following code snippet: <p class="pHelp"> xxxxx <a href="#components">Form components</a> yyyyy </p> This particular line is located within a long chain of nested HTML tags inside html/body/a/a/a/a/ ...

Getting JSON in Codeigniter's Controller

Injecting JSON data into the controller: $.post('xyz_controller/my_function', {url:"dummy data"}, function(data) { alert("Request successful!"); }, 'json'); Upon reaching my controller's function (my_function), I ...

Retrieving ID of an element to be animated with jQuery

I have a sprite image that changes background position when hovered over, and while it's currently working, I'm looking for a more efficient way to achieve this. I need to apply this effect to several images and am exploring ways to avoid duplica ...

Learn how to set up webpack or Next.js to generate a CSS file from custom Material styles and automatically inject it into the header

One of the things I've done is define some custom material styles, like this: import {createStyles, makeStyles, Theme} from '@material-ui/core/styles'; export const useStyles = makeStyles((theme: Theme) => createStyles({ fab ...

Attempting to incorporate the jquery-mousewheel plugin into the jquery cycle2 library

I've been working on integrating the jquery-mousewheel plugin (https://github.com/jquery/jquery-mousewheel) with the jquery cycle2 plugin. Initially, everything was running smoothly until I encountered an issue where mouse scrolling was generating ex ...

Is requesting transclusion in an Angular directive necessary?

An issue has cropped up below and I'm struggling to figure out the reason behind it. Any suggestions? html, <button ng-click="loadForm()">Load Directive Form</button> <div data-my-form></div> angular, app.directive(&apos ...

What is the best way to incorporate a floating label into an input field?

My goal is to create a form where the label of the input field moves up when the user starts typing, and stays up if they enter any text. However, if the user leaves the input area blank, the label should revert back to its original position. I have tried ...

Regular expressions can be used to extract specific attributes and inner content from a div element within a contentEditable container

Context I am currently developing a tagging system called @Name for my website. Successfully, I have managed to detect names upon keystroke and replace the content with the corresponding div class='tag' data-id='User-id'>Name</di ...

The distinction between a keypress event and a click event

Due to my eyesight challenges, I am focusing on keyboard events for this question. When I set up a click event handler for a button like this: $("#button").on("click", function() { alert("clicked"); }); Since using the mouse is not an option for me, ...