Tips for Aligning Navigation Tabs/Links at Center in Bootstrap 4

I've been trying to center some tabs on a navigation bar in Bootstrap 4 without success. Additionally, I can't seem to shorten the line underneath the tabs either. Any advice or hints would be greatly appreciated. Thank you in advance.

Here is my code:

.nav-tabs>li,
.nav-pills>li {
  float: none;
  display: inline-block;
  *display: inline;
  /* ie7 fix */
  zoom: 1;
  /* hasLayout ie7 trigger */
}

.nav-tabs,
.nav-pills {
  text-align: center;
}
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Registration</title;
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="stylesheets/asd.css">
  <link rel="stylesheet" href="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js">
  </script>
</head>

<body>

  <ul class="nav nav-tabs">
    <li class="nav-item"><a data-toggle="tab" class="nav-link active" href="#student">Student</a></li>
    <li><a data-toggle="tab" class="nav-link" href="#teacher">Teacher</a></li>
  </ul>

</body>

</html>

Any thoughts or suggestions?

Answer №1

  1. Center the content of the navigation list by using justify-content-center.
  2. To center an element, use mx-auto. However, keep in mind that mx-auto only works if the width of the element is smaller than its parent's width. For instance, in this case, the width is defined as 500px.

.w-500px {
  width: 500px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />


<ul class="nav nav-tabs justify-content-center mx-auto w-500px">
  <li class="nav-item"><a data-toggle="tab" class="nav-link active" href="#student">Student</a></li>
  <li><a data-toggle="tab" class="nav-link" href="#teacher">Teacher</a></li>
</ul>

You can also utilize classes like w-25, w-50, or w-75 instead of w-500px.

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

Troubleshooting Bootstrap: Identifying errors in my bootstrap code

Could you help me diagnose the issue with this code? I have tailored it to be responsive on my tablet by using 12 columns for two divs, expecting the third div to be in a new row. However, upon checking my index.html on the tablet, all the divs are display ...

Automate the process of modifying specific data in tables through Javascript

I've been attempting to replicate the appearance of a stock-exchange board, but I'm encountering difficulties in updating text automatically without interrupting another. My attempts so far: var textPositive = ["2.0%", "1.7%" ...

My code is giving me a headache because it just refuses to cooperate

There is an issue with my HTML code for creating 5 sliders. The numbers are changing in the first slider, but not in the other sliders. <body> <div class="box"> <div class="slider"> <input type="range" name="e ...

<head> Javascript codes are failing to run

Currently utilizing JQuery Mobile, my goal is to navigate between pages with minimal script loading each time a page loads. Essentially, I want to import all the general scripts (such as JQuery.js, jquery_mobile.js, main.js, etc.) ONCE for all pages. In m ...

Broadcasting real-time video from a webcam on an HTML website that is accessible locally through a Raspberry Pi device

My goal is to stream live video from a USB camera connected to my Raspberry Pi device onto a simple HTML site that is only visible on localhost. The site will be hosted on the Raspberry Pi itself and will only need to display the video streamed by the Ra ...

I am experiencing issues with how the data from the database is being displayed

I'm trying to fetch all my users from the database. The code works, but I believe there is an issue with my syntax and how it's being echoed. As a result, it generates a separate table for each data entry. Check out the attached image for refer ...

how can you make keyframe animation pause at the last stage?

Here is an example of utilizing CSS animations: .show-left-menu { -webkit-animation-name: leftSidebarAni; -webkit-animation-duration: .25s; -webkit-animation-timing-function: ease-out; -webkit-animation-iteration-count: 1; } @-webkit-keyframes l ...

What is the best way to place tfoot at the top of the table

Below is the structure of my table: https://i.stack.imgur.com/QmlVn.png In the table, there are search input fields at the bottom of each column. I would like to move them to the top, right after the <thead> </thead>. The basic HTML code for ...

I'm unsure of how to eliminate the border outline on this particular text area

The textarea has an unwanted blue outline, and despite trying outline:none; in the CSS, it's still there. See the code below: #floatingTextarea { border: none; outline: none; } <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/ ...

Customize the website's CSS for optimal viewing on an iPad

I have a website with two CSS files: one for viewing the site on a desktop screen and the other for viewing it on an iPad screen. Despite having the same HTML code, is there a way to detect the iPad device and force it to use the appropriate CSS file? Tha ...

Discovering the origin of an unexpected element.style manifestation

I cannot figure out how this strange issue occurred. This is the correct HTML code structure: <nav class="navbar navbar-inverse navbar-fixed-top"> However, Chrome is displaying the following unexpected code: <nav class="navbar navbar-invers ...

Demonstrate a array of values at varying angles within a circle using the functionalities of HTML5 canvas

Looking to utilize HTML5 Canvas and Javascript for a project where I need to showcase various values (depicted by dots possibly) at different angles within a circle. For example, data could include: val 34% @ 0°, val 54% @ 12°, val 23% @ 70°, a ...

Instruments for crafting an application compatible with a wide range of web browsers

Currently tackling various browser challenges. I find myself wondering whether it's achievable to create an application that is compatible with all browsers, or at least the majority. Should it be a web application? Any recommendations on frameworks a ...

Resetting initial values with Velocity.js post-animation

After animating elements into view on a page, I want to defer further animation through CSS classes. However, Velocity keeps all animated properties in the style= tag, hindering CSS transitions. My solution involves resetting the CSS upon completion, but ...

Bootstrap tab toggling with checkbox

I'm attempting to create tabs with the ability to include checkboxes, but when using data-toggle="tabs", the checkbox functionality seems to be disabled. Here is the link to my fiddle. Is there a solution to make the checkbox work within the tab tog ...

On three out of five pages, Mozilla Firefox and Internet Explorer are causing extra spacing in my navigation section, yet not on every page

I've been troubleshooting my website and can't seem to pinpoint the issue. I have 5 pages all connected to the same CSS file and created in a consistent manner. Interestingly, Safari, Chrome, and Opera display everything correctly. However, in Fi ...

Creating a responsive YouTube video embed within a div container with a fixed background image

Hello there, I'm currently working on positioning the YouTube Iframe on the background to make it look like the video is playing on a laptop screen. I also want it to scale down appropriately with the background image on different resolutions. The w ...

I am facing an issue where my simple three.js code is not displaying properly

I'm relatively new to three.js and HTML, but I have experience creating other JS files that import into HTML pages without any issues. However, I am confused as to why my basic box code is not showing up on the page when I import the JS file in the sa ...

Unable to access .php file on new server using JQUERY AJAX .get

After migrating my website to a new server, I encountered an issue where all the $().get("whatever.php") calls were failing. Previously, everything was functioning properly but now none of the requests are even reaching the php script. For instance: inde ...

Include a visible symbol in the search bar of the kendo UI for easier navigation

Incorporating kendo UI Jquery into my website has allowed me to utilize components like the Kendo Multiselect, which features a clear icon on the right side provided by default. <div> <label for="simple-input">Input</label> <input ...