The Enigma of the Empty Gap When Employing "display:table/table-cell"

Struggling to understand why there is a blank space between the image and the menu.

Here is the HTML and CSS related to it:

<header class="header_nav">

    <img src="img/logo.png" class="logo"/>

    <nav class="nav_bar">
        <ul>
            <li>Home</li>
            <li>The Nest</li>
            <li>Our Sparrows</li>
            <li>Volunteer</li>
            <li>Support</li>
            <li>Contact</li>
        </ul>
    </nav>

</header>



body, html
{
width:100%;
min-height:100%;
margin:0;
border:0;
padding:0;
background-color: #F0EEE8;
}

.header_line
{
margin:0;
border:0;
padding:0;
position:absolute;
top:0;
height:20px;
width:100%;
background:#75B0A8;
border-bottom:5px solid white;
}

.header_nav
{
display:table;
margin:0;
border:0;
padding:0;
margin-right:10%;
margin-left:10%;
width:80%;
margin-top:35px;
height:83px;
}

.logo
{
display:table-cell;
margin:0;
border:0;
padding:0;
}

.nav_bar
{
display:table-cell;
margin:0;
border:0;
padding:0;
vertical-align:middle;
}

.nav_bar ul
{
margin:0;
border:0;
padding:0;
list-style:none;
}

.nav_bar ul li
{
margin:0;
border:0;
padding:0;
display:inline;
font-family:Georgia,serif;
font-size:20px;
}

No matter what I do, I can't seem to fix it. It seems like the issue might be due to using display:table and display:table-cell. If this method is not working, I need an alternative way to apply vertical-align:middle to the menu bar.

Any suggestions would be greatly appreciated!

Answer №1

To achieve alignment, opt for display:inline-block instead of display:table-cell for elements with classes .logo and .nav-bar, and include vertical-align:middle on .logo.

Check out this jsFiddle example for reference.

Answer №2

Check out your updated CSS below.

The issues were related to the classes .logo, .header-nav, and nav-bar.

When you set an element to display as a table cell, it behaves like one. However, the complex nature of tables can easily lead to layout errors if not used correctly.

Instead, consider using inline-block for block elements aligned on the same row.

body, html {
    width:100%;
    min-height:100%;
    margin:0;
    border:0;
    padding:0;
    background-color: #F0EEE8;
}

.header_line {
    margin:0;
    border:0;
    padding:0;
    position:absolute;
    top:0;
    height:20px;
    width:100%;
    background:#75B0A8;
    border-bottom:5px solid white;
}

.header_nav {
    display:block;
    margin:0;
    border:0;
    padding:0;
    margin-right:10%;
    margin-left:10%;
    width:80%;
    margin-top:35px;
    height:83px;
}

.logo {
    display:inline-block;
    vertical-align: middle;
    margin:0;
    border:0;
    padding:0;
}

.nav_bar {
    display:inline-block;
    margin:0;
    border:0;
    padding:0;
    vertical-align:middle;
}

.nav_bar ul {
    margin:0;
    border:0;
    padding:0;
    list-style:none;
}

.nav_bar ul li {
    margin:0;
    border:0;
    padding:0;
    display:inline;
    font-family:Georgia,serif;
    font-size:20px;
}

Answer №3

To better position your elements, consider using wrappers to define their dimensions and positioning, allowing you the flexibility to move them around as needed. Avoid using table-cell display in this scenario.

.left-col{width:25%; float:left}
.right-col{width:75%; float:right;}

Take a look at this demo showcasing the updated solution DEMO

Answer №4

There seems to be an issue with the .header_nav{width:80%} property for centering the header. Instead, try using the following properties:

.header_nav{
    width:100%;
    display:block;
    margin:auto;
}

To correct the margin of the navigation bar, you can use:

.nav_bar{
    margin-top:2%;
}

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

Guide on stacking a transformed 3D div on top of a masked layer with higher Z-index without success

I am attempting to stack a green div on top of a blue div using CSS transformations. The blue div acts as a masked layer, while the green div is a dialog that should appear on top. I have tried adjusting the z-index property, but the blue div always ends ...

The res.download() function in Express is failing to deliver the accurate URL to the client

When trying to utilize the res.download() method for downloading specific files from a server, there is an issue where triggering the res.download does not redirect the client to the correct URL. The files intended for download are located in a directory s ...

Pressing a specific button triggers a broadcast signal, prompting a modal window to pop up and display relevant information

Within my website's HTML page named rollup.html, there exists a button that triggers the opening of a modal. <button id="myBtn" ng-click="printDivModal('rollup-tab')">ModalTest</button> In the accompanying JavaScript file, roll ...

An error was encountered involving an unexpected token < at the beginning of a JSON file while using express with firebase

After setting up an authentication system in express using firebase auth, I established an endpoint in my express server: app.post('/users/login', (req, res) => { console.log('logging in...'); firebase.auth().signInWithEmail ...

Create a resizable textarea within a flexbox container that allows for scrolling

Hey there! I'm struggling with a Flexbox Container that has three children. The first child contains a textarea, but it's overflowing beyond its parent element. Additionally, I want to make the content within child 2 and 3 scrollable. I've ...

Fade one element on top of another using Framer Motion

Looking to smoothly transition between fading out one div and fading in another using Framer Motion, but facing issues with immediate rendering causing objects to jump around. Example code snippet: const [short, setShort] = useState(false); return ( ...

How can I align content to the left within a div that is right-aligned?

When I attempt this, the content aligns to the right as expected: <div class="text-right"> ... ... ... </div> I had hoped that this would result in left-aligned content in relation to itself, but right-aligned within the ...

enabling the editing of two input fields by clicking on a button

Looking to make two input fields editable with a button click. Once edited, the data from these fields will be sent to separate columns in a database using ajax. Struggling to find a solution through online search. Any advice is appreciated! ...

Is it possible to display multiple slideshows in separate tabs on a single page simultaneously?

I am trying to create multiple slideshows, each in a different tab on the same page. Although I am not an expert, I am attempting to figure out how to have the slideshow repeat with buttons controlling each one separately. When adding the complete script ...

`The value of an element within an array changes automatically`

In my current setup, I have a traditional array where each element represents an HTML element. The issue arises when I manipulate these elements within the array; any changes made to the HTML element also reflect in the array automatically. However, I pref ...

How can I bypass hotlink protection for RSS images?

According to the definition, RSS feeds are considered public. When images are displayed within the feed, it is essentially "hotlinking," meaning that the content is being read directly from the Internet rather than your local machine. Is there a specific ...

Creating a CSS navigation sub menu

I am having trouble with my CSS code regarding the sub navigation menu. It seems to close as soon as I try to select an option. I suspect that there might be something wrong with the last CSS style. Currently, it only shows when you hover over it but disap ...

V-model prevents checkbox from being checked upon clicking

codesandbox: https://codesandbox.io/s/github/Tapialj/AimJavaUnit6?file=/src/frontend/src/components/AddMovieForm.vue I am currently working on implementing a feature where I need to collect an array of selected actors using checkboxes. I have set up a v-m ...

Creating a menu category for a restaurant using CSS

I'm trying to set up a menu with categories like this: enter image description here However, my result ends up looking like this: enter image description here The description section keeps stacking on top of the photo section; it's strange beca ...

What is the method for calculating the 100% width of a flex child?

Adjusting the width of the .sidebar to 100% seems to make it smaller in size. Interestingly, removing the body's font-size: 1.3rem and toggling the .sidebar's width: 100% results in a slightly larger appearance. It is expected that setting the ...

The background color property of the CSS class "ui-layout-north

Currently working with primefaces 4.3 and incorporating it into my main UI. My goal is to modify the background color of the north unit to red, as currently only the top 80% of the unit is affected. .ui-layout-north .ui-layout-unit-content { backgro ...

What is the best way to use a handlebar ternary operation to dynamically add an HTML element in AngularJS?

Please Note: When I refer to "handlebars," I am not talking about Handlebars.js, but rather the double curly braces. I have a table where I need to display data in one of the columns along with an additional HTML element based on the result of a ternary o ...

An anchor-shaped name tag with a touch of flair

I stumbled upon this unique anchor name design on the website Does anyone here know how to create something similar? What is that style called? ...

Targeting multiple frames in HTML

Have you ever tried clicking on a link and opening three different pages that target three different frames simultaneously? I've managed to open two separate pages targeting two distinct frames using href combined with the onclick function and locatio ...

Encountered a runtime error while trying to insert a list item <li> into a paragraph <p> element

Take a look at this code snippet: <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication18._Default" %> <!DOCTYPE html> <html> <body> <p id="someId"></p& ...