Bring the navbar text to the center of the screen in CSS

In summary, the conversation is about building a website and creating a navbar that is centered on the page. The speaker tried using the CSS property "display:flex justify-content: center" but it did not work. They provide a solution by increasing the value of "margin-left" or changing the "navbar-nav" class to "mx-auto". They ask for help in identifying which CSS rules are changed in this process.
  • #1
shivajikobardan
674
54
TL;DR Summary
Bring navbar at center CSS
https://demo.w3layouts.com/demos_ne...a-liberty-demo_Free/2002651968/web/index.html

This is the website that I am trying to build.

This is my current navbar.
fJLX3O3apC1PU0tIIH_EzdnGbwUqeLO7jmg5P_T2pLgmUUVb1Q.png


This is what I want to build.
wN-ub_JJv8XmNqoUbNsD4eCY6JQKuQ-EB2rZ-PJy94XDB9yulA.png

My focus is on "Home" to "Contact". I want to put it at the center of the page like in the second one. My guess was to do display:flex justify-content: center. But it didn't work.
Please guide me how to make it work?

Here's the codepen.
 
Technology news on Phys.org
  • #2
Increase the value here:
CSS:
.collapse {
  margin-left: 25%;
}

Or, better, remove the previous value and modify the following:
HTML:
<ul class="navbar-nav me-auto  ">
to:
HTML:
<ul class="navbar-nav mx-auto  ">

(That is how it is done in your link)

Can you identify what CSS rules change in this process?
 

1. How do I center the navbar text in CSS?

To center the navbar text in CSS, you can use the "text-align" property and set it to "center". This will align all the text within the navbar to the center of the screen.

2. Can I center only specific elements within the navbar?

Yes, you can use the "display" property and set it to "flex", then use the "justify-content" property and set it to "center". This will center specific elements within the navbar, while keeping others aligned to the left or right.

3. What if my navbar is not centered after using the "text-align" property?

If your navbar is not centered after using the "text-align" property, it is possible that there are other styles or properties that are affecting the alignment. You can try using the "margin" property and setting it to "auto" to center the navbar.

4. Can I center the navbar text on mobile devices?

Yes, you can use media queries to target specific screen sizes and adjust the alignment of the navbar text accordingly. For example, you can use the "text-align" property and set it to "center" only for screens with a maximum width of 768px, which is commonly used for mobile devices.

5. Is there a way to center the navbar text without using CSS?

Yes, you can use the "align" attribute and set it to "center" in the HTML tag that contains the navbar text. However, it is recommended to use CSS for styling purposes as it provides more flexibility and control over the appearance of the navbar.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Computing and Technology
Replies
3
Views
2K
Replies
2
Views
881
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
25
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
275
  • Art, Music, History, and Linguistics
Replies
1
Views
1K
  • Science and Math Textbooks
Replies
11
Views
14K
  • STEM Educators and Teaching
Replies
20
Views
13K
Replies
4
Views
3K
Back
Top