What is the best way to align my navigation bar in the middle of two elements with different

I've been struggling with my code for the past couple of days, but nothing seems to be working. I need the navigation bar to be perfectly centered between two elements (<img> and <form>). Right now, it's almost centered, but it starts messing up when the window is resized to around 1600px. However, I only really need it to work up to 1500px because I plan on changing the navigation bar location and style after that.

Check out the Codepen here: http://codepen.io/anon/pen/KpECd

Here's the HTML:


<header>
    <img src="../Images/Logo.svg" class="logo">   
    <nav>
        <a href="#" class="one">SHARING</a>
        <a href="#" class="two">HOSTING</a>
        <a href="#" class="three">PRICING</a>
        <a href="#" class="four">ACCOUNT</a>
    </nav>
    <form method="post" action="index.php">
        <button value="SIGNUP" name="signup">SIGNUP</button>            
        <button value="LOGIN" name="login">LOGIN</button>
    </form>    
</header>

And here's the CSS:


@charset "utf-8";

header {
    background-color:#464646;
    height:100px;
    width:100%;
}

header .logo {
    max-height:44px;
    max-width:535px;
    margin:28px 28px 28px 28px;
}

header form {
    display:inline;
    float:right;
    margin:28px 28px 28px 28px;
}

header form button {
    height:44px;
    width:125px;
    border:2px solid white;
    border-radius:10px;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    background-color:transparent;
    font-family:"Lato Black";
    color:white;
    font-size:22px;
}

header form button:hover {
    background-color:rgba(255, 255, 255, .2);
    cursor:pointer;
}

header form button:active {
    background-color:rgba(255, 255, 255, .8);
}

nav {
    width:47.5%;
    display:inline;;
    position:absolute;
    text-align:center;
}

nav a {
    display:inline-block;
    height:100px;
    width:20%;
    color:white;
    font-family:"Lato Black";
    text-decoration:none;
    font-size:16px;
    line-height:100px;
}

nav a:hover {
    background-color:#00a651;
    cursor:pointer;
}

Answer №1

If you have 3 elements that you want to position one on the left, one on the right, and the last one centered between them, you can achieve this using a table structure similar to the following:

<table cellpadding="0" cellspacing="0" border="0" style="width:100%;">
 <tr>
  <td align="left" valign="middle">
      <img src="myimage.jpg" />
  </td>
  <td align="center" valign="middle" style="width:100%;">
  <!-- Setting width to 100% distributes remaining space -->
    <a href="#" class="one">one</a>
    <a href="#" class="two">two</a>
    <a href="#" class="three">three</a>
    <a href="#" class="four">four</a>
  </td>
  <td align="right" valign="middle">
    <form method="post" action="index.php" style="width:260px;">
        <button value="SIGNUP" name="signup" style="width:120px;">SIGNUP</button>
        <button value="LOGIN" name="login" style="width:120px;">LOGIN</button>
    </form>
  </td>
 </tr>
</table>

Check out the DEMO here

If you prefer not using tables, an alternative approach is to utilize divs with floats along with jQuery for dynamic resizing:

winResize = function(){

    var HeaderWidth = $("#header").outerWidth();
    var div1Width = $("#div1").outerWidth();
    var div3Width = $("#div3").outerWidth();

    var div2Width = HeaderWidth - div1Width - div3Width;
    $("#div2").css("width", div2Width+"px");
}

winResize();

$(window).resize(function(){

    winResize();
});

View the JQUERY DEMO here

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

The ng-repeat function is unable to fetch data from a JSON file

Within my AngularJS framework template, I have the following snippet of html code: <ul class="sb-parentlist"> <h1 class={{headerClass}}> Popular</h1><div data-ng-repeat="data in data track by $index"> <li> ...

Difficulty in showcasing error on the website with JavaScript

I have recently developed a webpage that includes several input boxes and a submit button within a form as shown below: <form action="" method="post" name="password"> Upon clicking the submit button, a JavaScript function is triggered to validate i ...

Can you explain the concept of fallback color in CSS?

Can you explain to me what a fallback color means? I searched online and found out that it is used to display a color when the specified format is not supported by browsers, like Internet Explorer. Is there anything else I should know about fallback colors ...

Including an embedded iframe in an asp.net web form

I need to integrate an iframe into a web form by retrieving the URL from an API call to a third-party payment service. I am working with ASP.NET 4.5 and C# 6.0. The code for my web form includes implementing an iframe similar to that in an ASP.NET MVC cod ...

Organize Radio Selectors

My intention was to align the radio buttons as shown in the image below: https://i.stack.imgur.com/oPTjD.jpg However, the final result looked like this https://i.stack.imgur.com/Mixga.jpg Below is the code for your reference HTML <div class="form ...

What is the process for retrieving an object from a node.js server using JSON.stringify() in a JavaScript file?

Looking to organize my code, I want to separate the JavaScript from my page and link it through a file instead of having it embedded within script tags. The issue arises when I receive a "SyntaxError: expected expression, got '<' " in Firefox ...

Tips for resizing a chartjs within bootstrap columns to accommodate various web browsers

I am using chartjs charts within bootstrap rows and columns. The number of columns per row can be dynamically changed. For example, I could rebuild my rows with the following markup: const twoColumn = spacerColumn + "<div class=&ap ...

Facing issues with converting SVG to PDF in Rails 5.0

Below you will find two images: the first one is displayed in a browser view, while the second one shows a PDF view. The issue lies with the SVG to PDF conversion. I have dynamically set the stroke-dashoffset value, which works correctly in the browser bu ...

Changing Tailwind CSS variables for customization

Using Variables in index.css : :root { --property: 1; } A Different Approach to Changing it with CSS on Hover: img{ transform: scale(var(--property));; } .another-element:has(:hover, :focus) { --property: 1.1; } There's a way to inclu ...

Transfer responsibilities of events to the canvas and then fetch the Element in the handler

Currently, I am utilizing the Raphaël library to create a network graph, where nodes are depicted as circles. Users have the ability to dynamically add nodes by clicking on the canvas. When a new node is added, an Element object is pushed into both a Set ...

Footer overlapping occurs on a single page

My webpage is having an issue where the footer is overlapping. It seems to be fine on all other pages except for this one. I would prefer not to make any changes to the footer since it's working correctly on other pages. Is there a way to adjust the C ...

Which particular files should be included such as CSS and JavaScript?

After downloading bootstrap4, I'm unsure which css, js, or other files are necessary to include in order to make my form Bootstrap ready. My current task involves creating a form in CakePHP 3. Click here for more information on how to create a form ...

Wrapping is disabled for all elements within the container div that contains three additional div elements

I am trying to prevent one of the three divs inside a fixed top navbar container from sliding underneath another, causing the navbar to become taller. The code below is using Bootstrap and the issue is with the navbar-header div sliding underneath the he ...

Achieving Vertical Center Alignment in CSS

I'm having trouble understanding what's happening here: My goal is to vertically center an iframe and another div, both positioned alongside each other horizontally. What I've attempted is placing the iframe and div inside a new div called ...

Smooth carousel navigating through dots

I am currently using the slick carousel from http://kenwheeler.github.io/slick, and I am looking for a way to limit the number of dots to 8 even when there are more than 8 slides. The navigation dots should include arrows on the left and right sides to in ...

What is the best way to synchronize the image dimensions and source when dynamically loading images?

I am facing an issue with a function that updates images by altering the src and css (width/height) of an IMG tag within the document. Below is a simplified example to demonstrate the problem: updateImage = function(src, width, height) { $("#changeMe"). ...

Rules of HTML tables extend to the border size

Check out this HTML code snippet: div { border: 2px solid red; height: 50vh; width: 50vw; } table { border: 1px solid; table-layout: fixed; } <div> <table rules="cols"> <tr> <th>Name</th> < ...

Endless Loop Seamless Vertical JavaScript Experience

I've been working with an HTML table structure of data and was successful in setting up a vertical list loop using some JavaScript. However, I'm facing challenges in achieving a smooth constant vertical scroll. Currently, it goes row by row when ...

Arranging items in a flex container

My goal is to achieve the following layout with flexbox: #box { display: flex; flex-wrap: wrap; flex-direction: row; justify-content: center; justify-items: center; align-items: center; } #spotLight img { width: 15%; height: 15%; } # ...

Clicking on React Js reverses an array that had previously been unreversed

My issue involves an array pulled from a database that I have reversed so that the newest entry is displayed at the top when a button is clicked, opening a modal-style window. However, when the array is displayed in the modal window, it appears to be flipp ...