Align the parent div center and have both dynamic and fixed width child divs inside

In the main parent div, there is a combination of my logo and navigation bar.

The logo has a fixed width while the dynamic-width navigation bar is positioned 100px to the right of the logo. However, the current layout places them on the left side of the screen and I am seeking to center both elements without altering any other aspect.

After searching and experimenting with online suggestions, none seem to preserve the desired layout. Any suggestions or solutions would be greatly appreciated. Thank you in advance.

Code Snippets

<div id="allHead">
    <div id="logo"></div>
    <div id="navigation"></div>
</div>

CSS Styles

#allHead {

    position: relative;
}


#logo {

    width : 100px;
    height : 80px;
    background-color: green;
}


#navigation {

    position: absolute;
    max-width: 600px;
    left: 100px;
    top: 10px;
    right: 0px;
    height : 60px;
    background-color: orange;
}

Answer №1

Technique for Centering a Div with Variable Widths

For situations where the widths of div elements vary, a useful centering technique involves utilizing both an outer and inner wrapper.

  1. The outer wrapper div is configured with text-align:center.

  2. The inner wrapper is set to inline-block, allowing it to align centrally within the outer wrapper. It then adjusts this alignment by specifying text-align:left to override the centering effect from the first wrapper.

  3. By floating the logo and menu elements next to each other, they can be positioned side by side.

This method proves effective when dealing with variable-width elements that need to be centered on a page.

In a practical example, the navigation bar could wrap below the logo on smaller screens, offering improved usability in such scenarios.


Explore a sample implementation on JSFiddle through this link: JSFiddle Example

#allHead {
    text-align:center;
}

.center-inner {
    text-align:left; 
    display:inline-block;
}

#logo {
    width : 100px;
    height : 80px;
    background-color: green;
    float:left;   
}

#navigation {
    max-width: 600px;
    background-color: orange;
    float:left;    
}

To maintain the unity of the logo and navigation elements using CSS table displays, a designated layout approach comprising table and table row divisions ensures consistent rendering across different browsers.

Visit the following JSFiddle link for further insights: JSFiddle

#allHead {
    text-align:center;
}

.center-inner {
    text-align:left; 
    display:inline-block;
}

.nav-bar-table {
    display:table;
}

.nav-bar-table-row {
    display:table-row;
}

#logo {
    width : 100px;
    height : 80px;
    background-color: green;
    display:table-cell;
}

#navigation {
    max-width: 600px;
    background-color: orange;
    display:table-cell;
    padding:.5em;    
}

Lastly, here's another experimental variation employing absolute positioning to achieve central alignment. While the top examples offer more reliable outcomes, you can still test this method on JSFiddle: JSFiddle

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

QML: Inside and outside styling attributes

Within my QML Text (RichText) elements, I have incorporated multiple CSS-styled html elements using the inline 'style' attribute within each tag (e.g. <code style=\"background-color:black; text-indent: 10px\"></code&g ...

Improving Java Performance by frequently replacing one String with another

Currently, I am developing an HttpServlet extension (plugin) for a CMS that is responsible for filtering the HTML response. Within my method filterResponse, I retrieve the requested text/html as a String, which is one of three parameters. The main task a ...

Using JavaScript to dynamically load Cascading Style Sheets based on the current

I am trying to dynamically load different CSS files based on the current date (season). I attempted to tweak an image script that I found on Stack Overflow, but it didn't yield the desired result. Could someone please guide me on where I might be ma ...

emphasizing the specific text being searched for within the page

Looking for a solution to search values and highlight matching text words on a page? Let me help you with that. $(function() { var tabLinks = $('.nav > li'), tabsContent = $('.tab-content > div'), ...

Steps for opening a clicked link in a new tab:

I have a link on my page that I would like to open in a new tab when clicked, but it doesn't seem to be working. Can anyone offer some suggestions or help? So far, I've tried the following code: <a target="_BLANK" ng-href="{{news.url ...

What is the best way to constrain the ratio of a full-width image display?

I am in need of a solution for displaying an image at full width inside a frame with a 16:9 aspect ratio. The image needs to be centered within the frame and adjust responsively when resizing the screen. My preference is to achieve this using just CSS. ...

Utilizing HTML templates in Express instead of Jade

Currently in the process of setting up a new MEAN stack project, with Angular as my chosen front-end framework. I am aiming to utilize HTML files for my views in order to incorporate Angular within them. However, I am facing challenges when attempting to s ...

Adjust the size of an image based on the smaller dimension utilizing CSS

Allowing users to upload images that are displayed in a square container in the UI presents challenges. The uploaded images can vary in size, aspect ratio, and orientation, but they should always fill the container and be centered. To address this issue, ...

Mastering the art of using transitions in conjunction with display properties

I'm trying to achieve a fade-in effect with the CSS property display:block. Here's my HTML code: <div>Fade</div> And here's my CSS code: div { display: none; transition: 2s; } div:hover { display: block; } Unfortunately, thi ...

Stack Divs Without Using Absolute Positioning

Attempting to overlay divs without using absolute positioning has been quite a challenge. The issue arose when trying to integrate a Paypal cart through Javascript on the website. By default, the cart was positioned closely to the top margin of the webpage ...

ReactJS - Element not specified

I'm experiencing a specific issue with the component below related to the changeColor() function, which is triggering an error message: TypeError: Cannot set property 'color' of undefined This error seems to be isolated within this compo ...

What is the best way to position a single element in React using the Grid Component without causing any overlap?

I am struggling with positioning 3 components on my react page: PageHeader, SideMenu & FeatureList (which consists of Display Cards). Here is the code for each component: App.js // App.js code here... PageHeader.js // PageHeader.js code here... SideMenu ...

Guide on keeping a footer anchored at the bottom of a webpage

I have gone through numerous tutorials on how to position the footer at the bottom of my webpage, but I'm still struggling to accomplish it for my own site. Some of the resources I've consulted include: Understanding techniques to keep the fo ...

Converting a key-value pair string into an array in PHP: A comprehensive guide

When the checkbox is selected, I will extract the values listed below: attribute_value:samsung, attribute_id:1, spec_group_id:2, prod_id:1 To convert the above string into an array format and obtain a single array outside of the loop. Therefore, if I wa ...

What is causing the mouse to malfunction when interacting with this HTML form?

What is causing the mouse to not work with the form on this specific webpage? The issue here is that the mouse does not seem to be functioning correctly when attempting to interact with the input boxes within the Form. Upon clicking any of the input boxe ...

extract information from the HTML id attribute and assign it to a PHP variable using the GET method

After reviewing numerous examples, I am still unable to retrieve the value from the ID attribute in my input element. This identifier is crucial for deleting records from the database, but my attempts with Get method have been unsuccessful. <?php ...

HTML File Path for C Drive

I am a beginner with HTML and I'm facing an issue regarding displaying an image located at "C:\Users\Jas mine\folder\landscape.jpg" in HTML. I have tried various solutions posted by others but none of them seem to work for me. Can ...

Keep table header stationary while accommodating varying column widths

I have come across various solutions for freezing a table header row, such as creating a separate table containing only the header and hiding the header of the main table. However, these solutions are limited to cases where column widths are predetermine ...

The Vue.js modal is unable to resize below the width of its containing element

My challenge is to implement the Vue.js modal example in a larger size. I adjusted the "modal-container" class to be 500px wide, with 30px padding and a max-width of 80%. However, I'm facing an issue where the "modal-mask" class, containing the contai ...

"Padding has not been recognized as a valid input value

I'm struggling with getting padding to be accepted by the browser when I style a card using Material-UI. const useStyles = makeStyles((theme) => ({ cardGrid: { padding: '20px auto' }, })) Unfortunately, the browser is not recogni ...