Dealing with Inline Styling Woes

Trying to style a row of buttons on the left, a center-aligned navigation list, and another row of buttons on the right. The navigation buttons are grouped to account for layout changes. How can I horizontally center the navigation within the parent element?

Both the parent divs and navigation are set to "display: inline".

  • Is absolute positioning necessary for the navigation to be centered? Why?

  • What's the best way to horizontally center the navigation?

Snippet of my HTML:

  // Ensure font smoothing for crisp lines 
    html{
        background-color: rgb(235, 235, 235);
    }
    
    #main_body{
        width: 1200px;
        margin: auto;
        background-color: white;
    }
    
    // Styling for the first row header 
    #header_topnav{
        width: 96%;
        height: fit-content;
    }
    
    // Styling for buttons, navigation, and buttons 
    .topnav_button{
        width: 45px;
        height: 35px;
        margin-top: 18px;
        margin-bottom: 10px;
    }
    
    // Make div inline to align on the same line
    #left_buttons{
        display: inline;
        position: relative;
    }
    
    // Make div inline to align with the buttons
    // Align using left or margin 
    #right_buttons{
        display: inline;
        position: relative;
        margin-left: 87.5%;
        // left: 86.5%;
    }
    
    // Vertical centering 
    ul {
        margin: auto;
    }
    
    // Styling for horizontal nav menu 
    li {
        display: inline;
        float: left;
        margin-left: 10px;
    }
    
    // Create a larger clickable area 
    li a {
        display: block;
      }
    
    // How to horizontally center this? 
    // Is absolute positioning necessary? Can it be centered between button sections without that?
    #top_navmenu {
        position: absolute;    
        display: inline;
        margin-top: 28px;
    }
<div id="main_body">
    <header>
        <section id="header_topnav">
            <div id="left_buttons">
            <button class="topnav_button"></button>
                <button class="topnav_button"></button>
            </div>

            <nav id="top_navmenu">
                <ul>
                    <li><a>F+</a></li>
                    <li><a>POLITIK</a></li>
                </ul>
            </nav>

            <div id="right_buttons">
                <button class="topnav_button"></button>
            </div>
        </section>
    </header>
</div>

Answer №1

You can achieve the layout without using position: absolute by utilizing flexbox.

To do this, simply apply display:flex to the wrapper element (#header_topnav) and flex-grow: 1 to the middle menu. This configuration will ensure that the menu occupies the space between the left and right buttons.

It's important to share valid code when seeking help on platforms like Stack Overflow.

  • The HTML code is missing some closing tags.
  • Remember, // is not a valid CSS comment. Use /* comment */ instead.

Refer to the code snippet below for a visual representation.

html {
  background-color: rgb(235, 235, 235);
}

#main_body {
  width: 100%;
  margin: auto;
  background-color: white;
}

#header_topnav {
  height: fit-content;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}

.topnav_button {
  width: 45px;
  height: 35px;
  margin-top: 18px;
  margin-bottom: 10px;
}

#left_buttons {
  display: inline;
  position: relative;
}

#right_buttons {
  position: relative;
}

ul {
  margin: auto;
  display: flex;
  flex-direction: row;
}

li {
  margin-left: 10px;
}

li a {
  display: block;
}

#top_navmenu {
  margin-top: 28px;
  flex-grow: 1;
  display: flex;
  align-items: center;
}
<div id="main_body">
  <header>
    <section id="header_topnav">
      <div id="left_buttons">
        <button class="topnav_button"></button>
        <button class="topnav_button"></button>
      </div>

      <nav id="top_navmenu">
        <ul>
          <li><a>F+</a></li>
          <li><a>POLITIK</a></li>
        </ul>
      </nav>

      <div id="right_buttons">
        <button class="topnav_button"></button>
      </div>
    </section>
  </header>
</div>

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

Dealing with a divided image in a separate thread: What you need to know

I am facing a challenge with incorporating a LARGE image into a website, which is affecting performance. My solution is to divide the image into smaller pieces and stitch them together using a grid upon loading. The only issue is that it must be in the ba ...

What are some strategies for preventing the $window.alert function from being triggered within an AngularJS controller's scope?

How can I prevent the alert from appearing on my dashboard? Do you have any suggestions? Thank you! I attempted to override the alert empty function in my controller, but I am still encountering the window alert when I navigate to my page. $window.alert ...

Align the central element in the Bootstrap menu between two other elements

My menu layout looks like this : .navbar { box-shadow: 0 5px 15px rgba(0, 0, 0, .15); background: #fff; border: 0; max-height: 73px } .navbar-center>span>a { display: inline-block; position: relative; } #header>.navbar>div:nth-child(2)> ...

Update the div's content once the data has been successfully submitted to MySQL

As a beginner in using ajax, I am looking to submit data through ajax and replace the existing data in the same div with the new data. Below is the jQuery code for sliding tab: $(document).ready(function() { // Vertical ...

Switching to a dropdown navigation option

Sorry for the repetition, but I haven't been able to find a solution to my problem yet, so here I am asking again. I am still getting the hang of html5 and css3, and while I've managed so far, I'm stuck now. I want to turn one of the option ...

The request for data:image/png;base64,{{image}} could not be processed due to an invalid URL

I am facing an issue with converting image data that I receive from the server using Angular.js for use in ionic-framework. Here is the code snippet I have been working with: $http.post(link, { token: token, reservationCode: reservatio ...

Effortlessly Transform HTML into Plain Text Using jQuery

If I wanted to create a feature on my website where users can input text into a text area and then convert it to HTML by clicking a button, how could I achieve that? I'm looking for functionality similar to what Wordpress offers, where users can enter ...

Trouble with Metro UI Library: CSS not loading properly

I am having trouble with the navbar CSS on my website while using the Metro UI CSS library. Check out my HTML code: <!DOCTYPE html> <html lang="en"> <head> <title>TelePrint Blog</title> <link rel="stylesheet" href= ...

SVG is not extending to the entire viewport in mobile view

I need help with adjusting wave SVG's on my website to fill the entire viewport or screen width. Does anyone have any suggestions? To create the waves, I used a Wave SVG generator and placed them within a div element. <nav> <div> //align ...

Accessing a JavaScript variable in another <script> section

I'm facing a challenge where I need to access a JavaScript variable that is declared in one block of an HTML page from another block on the same page. This is crucial for me to be able to halt an AJAX call that is currently ongoing. However, I'm ...

Tips for creating several radio buttons with separate functionality using Bootstrap 5

Is there a way to create separation between these two groups of radio buttons? Whenever I select an option in one group, it automatically deselects the option in the other group. <!DOCTYPE html> <html lang="en"> <head> ...

JavaScript: Understanding the concept of closure variables

I am currently working on an HTML/JavaScript program that involves running two counters. The issue I am facing is with the reset counter functionality. The 'initCounter' method initializes two counters with a given initial value. Pressing the &a ...

One common issue is being unable to target input[type=file] when multiple forms are present on a page using JavaScript

Question: I have multiple dynamic forms on a web page, each with a file input field. How can I specifically target the correct file input using $(this) in JavaScript? Here is an example of one of my forms: <form enctype="multipart/form-data" action="c ...

Google Chrome introduces innovative composite layers to handle stubborn content resistant to compression

I'm trying to understand the meaning behind the message in the Chrome profiler that says "Layer was separately composited because it could not be squashed." Recently, while modifying my HTML, I added a fixed position div inside a relative div and app ...

Easy steps to dynamically add buttons to a div

I need help with a JavaScript problem. I have an array of text that generates buttons and I want to add these generated buttons to a specific div element instead of the body. <script> //let list = ["A","B","C"]; let list = JSON.p ...

The text element does not line up with the icons

As I'm working on creating my first account page header, I've run into an issue with some of the icons not aligning perfectly with the text. While advanced developers may find this task simple, as a beginner, I could really use some advice on how ...

Exploring how CSS affects backgrounds in the Google Chrome browser

Having an issue with the background on my website. In Firefox and other browsers, the background appears much lighter and whiter compared to Chrome. This is the CSS code for my background: body {background:#ffffff url(../../images/background.jpg); direct ...

Include an additional Div tag in the existing row

I've been attempting to include an additional div, but it consistently appears as the second row.. I want all of them to be on the same row and aligned with each other. Here is the HTML code: <div class="content-grids"> <div clas ...

Insert text into the cursor location within Summernote using JQuery

Currently, I am working on a flutter application where I have implemented the summernote editor using JQuery. ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); String txtIsi = data.text .replaceAll("'", '\&bsol ...

Creating a stunning effect with radial-gradient on an image element

I am looking to achieve a fading effect on an element using a radial gradient. While it works when I set an image as a background image, it does not work on the element directly. Is there a way I can accomplish this? My goal is to have the image fade fro ...