Position a div directly beneath the header

I have recently added a widget area below the header on my website, and I am facing an issue with centering it just under the logo.

I've attempted to use display: block and margin:0 auto, as well as text-align: center on the #header-sidebar in the CSS, but unfortunately, these approaches haven't worked. I also tried adjusting the HTML code structure instead of solely relying on CSS tweaks, but the #header-sidebar still remains stacked on the left side instead of being centered.

Below is the snippet of my code:

HTML code:

<header id="header">

    <div class="header-container">

        <a href="javascript:;" class="show-on-phone show-on-tablet" id="phone-toggle-menu"></a>

        <a href="#" class="logo"><img style="vertical-align: middle;" src="#"></a>

        <nav> //here is the navigation, hidden in mobile view </nav>

        <div id="header-sidebar" class="header-widget-area">
            <div class="textwidget">
                //some widget here
            </div>

            <div class="mlp_language_box">
                // some other widget here
            </div>
        </div>
    </div>

</header>

CSS code:

header#header {
    position: relative;
    z-index: 300;
    height: 70px;
    line-height: 70px;
    display: block;
}

header#header #phone-toggle-menu {
    left: 20px;
    margin-left: 0;
    position: absolute;
}

header#header .header-container {
    max-width: 1200px;
    margin: 0 auto;
}

header#header .logo {
    width: 100%;
    float: none;
    box-sizing: border-box;
    text-align: center;
    position: absolute;
}

Answer №1

By utilizing the properties <code>display:table
and margin:auto for both the logo and widget area, you can achieve the desired effect. This allows the toggle button to remain in an absolute position.

header#header {
    position: relative;
    z-index: 300;
    height: 70px;
    line-height: 70px;
    display: block;
}

header#header #phone-toggle-menu {
    left: 20px;
    margin-left: 0;
    position: absolute;
}

header#header .header-container {
    max-width: 1200px;
    margin: 0 auto;
}

header#header .logo {
    box-sizing: border-box;
    display: table;
    margin: auto;
}

header #header-sidebar{
    display: table;
    margin: auto;
}

Answer №2

Make sure to update your css styles with the code provided below:

nav#main-navigation {
    position: relative;
    z-index: 500;
    height: 80px;
    line-height: 80px;
    display: block;
}

nav#main-navigation #menu-toggle-btn {
    left: 30px;
    margin-left: 0;
    position: absolute;
}

nav#main-navigation .navigation-container {
    max-width: 1400px;
    margin: 0 auto;
    text-align: center; 
}

nav#main-navigation .logo {        
    box-sizing: border-box;
    text-align: center;
    position: relative;
}

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

Achieving a Banner-Like Background Image Using CSS

I've been studying various website source codes, but I'm struggling to achieve a specific effect. I want to have an image as the background that sits behind everything on the page. However, I don't want the entire background to be covered by ...

The URL in the dropdown menu on an Android device is unresponsive

I am facing an issue with the navigation menu on my website. It works perfectly fine on desktop and Apple devices, but on Android devices using Chrome, there is a problem. The submenu opens correctly, but when trying to click on a link within the submenu ...

`What is the best way to extract text and URLs from user input within a JSP page?`

I am having trouble with my programming skills while working on a web application using HTML and JSP pages. The goal of the application is to allow users to post content, but I am struggling to separate text from links within the textarea. Here is how the ...

The footer refuses to stay positioned at the bottom of the page

Can you help me locate the CSS style for the footer? #footer { background-color: #000; bottom: 0px; color: #fff; clear: both; text-align: center; padding: 5px; } What is the best way to ensure the footer is positioned at the bottom of ...

CSS Selector for table cell containing a checked input in the cell before it

Is it possible to select a table cell based on the state of a previous cell using CSS? I have a feeling that this may not be achievable with CSS alone. Can someone confirm my suspicion? Let's consider this example: <table> <tr> ...

The issue of a malfunctioning Customized Bootstrap Carousel when used with flex and gap is causing

I have created a unique bootstrap carousel where instead of displaying one div, I showcase three (one in the middle and half of each on the sides). The issue arises when I add borders to my divs and attempt to create spacing between them using display: fle ...

Generate a custom map by utilizing JavaScript and HTML with data sourced from a database

Looking for guidance on creating a process map similar to this one using javascript, html, or java with data from a database. Any tips or suggestions would be appreciated. https://i.sstatic.net/tYbjJ.jpg Thank you in advance. ...

Adjust for browser zoom within Three.js CSSRenderer

I'm currently developing a project in three.js using the CSSRenderer and the challenge I'm facing is ensuring that it displays correctly even when the browser zoom is not set at 100%. So far, it appears that: There isn't a way to forcibly ...

What is the best way to ensure a flexbox image item keeps its aspect ratio?

After exploring numerous questions and answers, it seems that most discussions on flexbox aspect ratio involve using <div> elements instead of <ul><li>. However, I am struggling to find a solution to my specific issue. When utilizing < ...

Generate random positions for several divs using jQuery

Here is my code snippet: http://jsfiddle.net/BREvn/2/ (it's functional). However, I want each div to have a unique position coordinate. Currently, the script moves others to the place of the first one. How can I resolve this issue? I attempted using a ...

Looking to learn how to replicate data effortlessly with either javascript or jquery?

I am currently trying to figure out how close I am to successfully cloning this div. Honestly, I am a bit lost at this point and could really use some assistance. Should I consider using jQuery for this task? <div id="question20">20. Details of Chi ...

Having trouble compiling your Vue project and running into the "No mixed spaces and tabs" error?

Below are the details with an error: Error Details: Failed to compile. ./src/components/Header.vue Module Error (from ./node_modules/eslint-loader/index.js): H:\project\VueProjects\stock-trader\src\components\Header.vue 27: ...

Issue with opening image modal when clicking on images, JavaScript seems to be malfunctioning

I created a gallery of modal images that I want to open when clicked, but for some reason it's not working as expected. What I'm trying to achieve is passing the image ID of each image to JavaScript when they are clicked so that the script can th ...

CSS: The sub-class functionality is not functioning as intended

Having trouble with this HTML element : <span class="item-menu-notif non-lue" onclick="dosomething(2150)"> TEXT </span> These are the CSS classes : .item-menu-notif{ display: block; cursor: pointer; padding: 0 0.4em 0 0.4em; } s ...

Optimal methods for refreshing website lists

I've designed an ASP.NET MVC application with a list that displays all the current items from a database. There is also a button that triggers a popup-dialog to create a new entry. The goal is to automatically add the new item to the list once it is c ...

Problem with loading images from class in IE7

Encountering issues with IE7 and images in my CSS. .dot-preview { background: url("../images/bola02.png") no-repeat scroll 0 0 transparent; display: block; height: 12px; margin: 3px; width: 13px; } .navi-active .dot-preview { back ...

Save your canvas image as a file and attempt to force a download, only to be met with a failure to

Is there a way to save a canvas as an image file and allow users to choose the format (png or jpg) for download without saving the file on the server? Here's what I have tried: JavaScript Code: $.ajax( { type : "POST", url : "../php/downloa ...

Inspect every div and perform an action if the criteria are met

I need assistance with adding text inside a specific div based on certain conditions. Currently, all the div elements are being populated with the added text. Please review my code for more clarity on what I am trying to achieve. Can you suggest the most e ...

Creating a new webpage that automatically populates with multiple images from a server using PHP

I'm inquiring about generating a webpage automatically from an HTML template, How can I create a new webpage from an HTML template and display multiple images from the server on the generated page? Please review my code below, Thank You. The follo ...

What is the best way to adjust the decimal values in my API calls?

Is there a way to adjust the numerical data returned by the API to display only two decimal places instead of three? For example, if the number is 140.444, I want it to show as 140.44 What changes do I need to make? function fetchData() { fetch(" ...