Achieving Vertical Alignment of Two Divs with CSS

I've come across several solutions to my issue, but none of them seem to work in my current situation. I have a banner at the top of my site with two floated columns, and suspect that the navigation menu in the right column may be causing the problem.

Here is the jfiddle I've set up: https://jsfiddle.net/Lhd0soL5/1/

.container {
  max-width: 940px;
  margin: 0 auto;
  margin-top: 0px;
  padding-top: 0px;
  box-sizing: border-box;
  position: relative;
}



.high {
  background-color: #FFF;
}

.high .left-col {
  width: 33%;
  float: left;
  height: auto;
  padding: 20px 0px;
}

.high .left-col p {
  font-size: 140%;
}

.high .right-col {
  width: 67%;
  float: right;
  height: auto;
  top: 50%;
  bottom: 50%;
}


.site-nav {
  float: right;
}

.site-nav li {
  list-style: none;
  float: left;
  border-right: 1.5px solid;
}

.site-nav li:last-child {
  border: none;
}

.site-nav ul {
  -webkit-padding-start: 0px;
}

.site-nav a {
  display: block;
  text-decoration: none;
  padding: 5px 20px;
}

/* This is the clear fix method */
.group:before,
.group:after {
  content: "";
  display: table;
}

.group:after {
  clear: both;
}

.group {
  zoom: 1;
}
<div class="high group">
  <div class="container">
    <div class="left-col">
      <p>Company Name</p>
    </div>
    <aside class="right-col">
      <nav class="site-nav">
        <ul class="group">
          <li><a href="draft1-1.html">Home</a></li>
          <li><a href="contact.html" class="last">Contact Us</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#methodology">Methodology</a></li>
          <li><a href="#">Products</a></li>
        </ul>
      </nav>
    </aside>
  </div>
</div>

Appreciate any help! :)

Answer №1

If you're able to utilize flexbox, simply set the container to display:flex with justify-content:space-between (items evenly distributed in the line; first item at the start, last item at the end) and align-items:center (items centered in the cross-axis). Check out the simplified HTML demo below.

jsFiddle

.container {
  max-width: 940px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border: 1px solid;
}
.site-logo {
  font-size: 150%;
}
.site-nav ul {
  padding: 0;
  margin: 0;
}
.site-nav li {
  list-style: none;
  display: inline-block;
  border-right: 1.5px solid;
  margin: 0 0 0 10px;
  padding: 0 14px 0 0;
}
.site-nav li:last-child {
  border: none;
  padding-right: 0;
}
<div class="container">
  <div class="site-logo">Company Name</div>
  <nav class="site-nav">
    <ul>
      <li><a href="draft1-1.html">Home</a></li>
      <li><a href="contact.html" class="last">Contact Us</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#methodology">Methodology</a></li>
      <li><a href="#">Products</a></li>
    </ul>
  </nav>
</div>

Answer №2

To achieve this layout, you can utilize the CSS property display: table on the parent container and then apply display: table-cell along with vertical-align: middle to each of the 2 child elements.

It's worth noting that in this setup, the float property is not used.

fiddle

Here's a snippet showcasing this approach:

/* line 45, ../scss/screen1-2.scss */
.container {
  max-width: 940px;
  margin: 0 auto;
  margin-top: 0px;
  padding-top: 0px;
  box-sizing: border-box;
  position: relative;
  display: table;
}

/* line 90, ../scss/screen1-2.scss */
.high {
  background-color: #FFF;
}
/* line 92, ../scss/screen1-2.scss */
.high .left-col {
  width: 30%;
  display: table-cell;
  vertical-align: middle;
  padding: 20px 0px;
}
/* line 95, ../scss/screen1-2.scss */
.high .left-col p {
  font-size: 140%;
}
/* line 99, ../scss/screen1-2.scss */
.high .right-col {
  width: 70%;
  display: table-cell;
  vertical-align: middle;
  top: 50%;
  bottom: 50%;
}

/* Additional styling for site navigation */
.site-nav {
  float: right;
}
/* Remaining styles for site navigation removed for brevity */

<div class="high group">
  <div class="container">
    <div class="left-col">
      <p>Company Name</p>
    </div>
    <aside class="right-col">
      <nav class="site-nav">
        <ul class="group">
          <li><a href="draft1-1.html">Home</a></li>
          <li><a href="contact.html" class="last">Contact Us</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#methodology">Methodology</a></li>
          <li><a href="#">Products</a></li>
        </ul>
      </nav>
    </aside>
  </div>
</div>

Please note that the widths of the individual sections have been adjusted to 30% and 70% respectively instead of the initial ratios due to layout constraints.

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 pasting Arabic text into an input box, the words in HTML appear to be jumbled and shuffled around

When I replicate this text يف عام and input it into a box, the output is shown as follows عام يف ...

Troubleshooting the issue of CSS animations activating twice and causing a flickering effect specifically in the Firefox browser

I am facing an issue with CSS animations in Firefox. When I try to slide in some radio buttons upon clicking a button, the animation seems to be firing twice in Firefox while it works fine in Chrome. I have attempted several solutions but haven't been ...

Is there an issue with hover not working for MUI styled component when focused?

Having trouble getting both focus and hover effects to work on the same MUI styled component in React. Check out this code sandbox for a demonstration. When clicking on the Select element, the background and border colors do not change: https://i.sstatic. ...

Persistent change issue with JQuery CSS function

Looking for some help with a button-bar div that I want to display only after the user clicks on a "settings" button. Currently, I have set the button-bar div to have a display:none property in my css file. However, when the settings button is clicked, t ...

Footer div is being covered by the page

I am currently working on a website built with "WordPress", and I have implemented a mobile footer plugin that is designed to only appear on mobile devices. However, I am encountering an issue where the page content overlaps the footer on phones. I attemp ...

Implementing an Ajax function for handling multiple button clicks

So, here's the situation - I've inherited a form with two buttons that inexplicably have the same name and id (seriously, don't ask me why, I didn't design this form haha). I'm attempting to utilize the Ajax .click function to trig ...

How can I display the Bootstrap 5.3.0 Collapsible button on this basic website?

I've been struggling to implement a Bootstrap Collapsible on my web page using Bootstrap version 5.3.0. I've tried different approaches but I can't seem to get it to work. All I need is a Collapsible that contains a few links, which should b ...

Adding rows to a database can be done by utilizing a dynamic form that consists of both repeatable and unique fields

Here is my follow-up post addressing the issue I previously encountered. Initially, I erroneously used mysql instead of mysqli, but I have now made the necessary updates based on recommendations. I have a form that contains variable sets of fields (rangin ...

Locate a specific element by utilizing xpath

Recently, I encountered a coding issue that has been puzzling me. Here's the html snippet in question: <div class="Main"> <div> <div class="display">First:</div> <div class="price">$0.01</div> ...

Eliminate the .html extension from the URL and initiate a redirect

I need assistance removing the .html extension from URLs that are displayed in the address bar. To achieve this, I used the following code: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f This solution ...

`Custom transitions between sections using fullpage.js`

Has anyone used the fullpage.js extension to achieve an animation similar to this one, where sections are destroyed on scroll? ...

Tips for arranging images of varying heights on separate lines so they appear cohesive as a group

I've been working with masonry.js but I'm still not achieving the effect I want. Javascript:` var container = document.querySelector('#container'); var msnry = new Masonry( container, {columnWidth: 200, itemSelector: '.item' ...

Using jQuery, you can activate an onclick function based on specific keywords entered by the user

Within this element, there is a text input field that allows users to search for specific items. Next to the input field is an icon image that can be clicked on to trigger a jQuery onclick event. This event sends an AJAX request to a PHP file, retrieves da ...

Is there a way to pass a variable from a Django view to an HTML page using Ajax when the user presses a key

I am developing an application that delivers real-time data to users in HTML, and I aim to dynamically update the paragraph tag every time a user releases a key. Here is a snippet of my HTML: <form method="POST"> {% csrf_token %} <p id="amount_w ...

Activating Vue-Bootstrap components through an image click event in VueJS 2

Seeking to achieve: VueJS integration with Bootstrap for clickable cards I am currently working on a VueJS project where I want the cards to be clickable and reveal collapsible elements upon click. To accomplish this, I have implemented a button with the ...

Using Jquery to set values for css properties

I've recently started using jquery and I have a task related to a drop-down menu that I'm struggling with: if (scrnwidth <= 761) { if (display was block) { //Defaultly testi has display:none property. testi = m ...

Customize the size of innerWidth and innerHeight in your THREEjs project

Is there a way to customize the size of the window where this function launches instead of it automatically calculating the height and width? I've attempted to modify this section of the code, but haven't had any success so far: renderer.setSiz ...

What is the best way to position the section and footer to the left side of the sidenav?

I am relatively new to the world of html and css, trying my hand at constructing html layouts. I have encountered a bit of trouble in aligning the footer and section to the left of the nav bar. No matter what percentage widths I use, they always end up ove ...

Is it possible for the ".filter" function to verify if the target contains multiple attributes?

I'm currently working on a checkbox filter setup and using Jquery's .filter to sort through some divs. Below is the snippet of Jquery code that I am using: $(document).ready(function(){ var checkboxes = $('div.filter-groups').find(&ap ...

Can you display the menu and then conceal it by clicking elsewhere?

I am facing a unique situation where the body tag does not visually wrap all content. In this scenario, I have 3 TR's that each contain 2 TD's - one for information and one for a yellow div. By clicking on the Info TD, a div opens (positioned a ...