Automatically set the margin for the initial element with flex-grow

In the scenario presented, there is a navigation structure utilizing two flexbox containers. The flex-grow property allows the navigation to stretch and fit the main wrapper, which in this case has a width of 100%.

Here is the HTML code:

<!DOCTYPE html>
<html lang="en">
    <body>
        <div id="main_navigator">
            <a id="main_navigator_logo" src="/"></a>
            <ul id="main_navigator_r1">
                <li>
                    <a class="main_nav_btn">BTN 1</a>
                </li>
                <li>
                    <a class="main_nav_btn">BTN 2</a>
                </li>
                <li>
                    <a class="main_nav_btn">BTN 3</a>
                </li>
                <li>
                    <a class="main_nav_btn">BTN 4</a>
                </li>
                <li>
                    <div id="main_navigator_s1"></div>
                </li>
                <li>
                    <div id="main_navigator_regbox"></div>
                </li>
            </ul>
        </div>
    </body>
</html>

The CSS styling for the above HTML structure is as follows:

body {
    margin: 0;
}
#main_navigator {
    position: fixed;
    display: flex;
    flex-direction: row;
    background-color: black;
    width: 100%;
}
#main_navigator_logo {
    width: 416px;
    height: 120px;
    display: block;
    background-color: white;
}
... (CSS continues)

The container includes both a logo (<a> element) and a navigation section (<ul> with flexbox properties).

The left margin of the first element remains fixed while other margins between navigation elements increase with viewport scaling. A visualization of this can be viewed here.


Is it feasible to have all elements within the navigation automatically adjust their margins equally?

An attempt was made to place the logo inside the <ul> container, but due to resizing issues, it disappeared. Is there a specific way to wrap the logo within the container without losing its size stability?

Thank you for any insights or suggestions!

Answer №1

For the first element to have an automatic margin added, the navigator should not have any preset padding and its inline elements must be centered. To achieve this:

#main_navigator_r1 {
    padding:0;
    text-align:center
}

To maintain equal spacing between list elements, they should all have the same display properties, such as display: inline-block:

#main_navigator_r1 li {
    display: inline-block;
}

The use of the flex-basis property is also recommended for elements with parent containers having flexbox display properties - setting flex-basis to 0 ensures that all elements within the flexbox container will have equal width and spacing between them.

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

PHP Date Formatting: Perfecting the Date Display

I have implemented a DateTimePicker feature in my project. <div class="input-group date form_datetime"> <input type="text" size="16" readonly class="form-control"> <span class="input-group-btn"> <button class="btn btn- ...

Attempting to superimpose a CSS triangle arrow onto a Google Map

I'm currently working on incorporating a CSS arrow on top of a Google map as a design element. The concept involves a horizontal stripe with a downward arrow placed halfway through, and I aim to have this arrow overlay the Google map. Here is a snipp ...

Develop adaptable flex items

I am working with a container element that contains variable child elements whose width I want to scale. To see an example of what I'm trying to achieve, take a look at this simple plunker: https://plnkr.co/edit/ef0AGPsK7FJJyBqgWuMi?p=preview When ...

Having trouble locating an element on a webpage? When inspecting the element, are you noticing that the HTML code differs from the source page?

I'm having trouble locating a specific element on a webpage. When I use the Inspect Element tool, I can see the HTML code with the element id = username, but when I check the page source, all I see is JavaScript code. Does anyone have any suggestions ...

Using CSS to create a triangular background within a <div> element

I am looking to create a CSS triangle shape for a background, but I am unsure of how to achieve this or if it is even possible. Ultimately, I want the menu to resemble the image linked below: While I have come across methods to create the desired shape, ...

Review a roster of websites every half a minute (Revise pages every half an hour??)

Just starting out with HTML coding! Can someone please provide the code that will allow me to save various webpages and automatically cycle through them every 30 seconds? And also ensure that the webpages are updated every 30 minutes. ...

Tips for personalizing tooltips in Bootstrap 4

Currently, I am utilizing Bootstrap '4.0.0-alpha.6' and attempting to customize the tooltip styles. Despite adding a custom CSS class along with using Bootstrap 4, the tooltip is not responding correctly to my specified style when hovering over. ...

Gradient transition effect changing background color from bottom to top

I am facing an issue where I have a block and I want the background color to animate from bottom to top on hover. Can you please help me identify what is causing this problem? Your assistance is greatly appreciated. .right-block--wrapper { backgroun ...

Implement two different styles within an element's style properties

Welcome everyone, I have a question regarding adding marquee effects to a table row along with highlighting it. Is it possible to include moving text from left to right in the same active row? For example, let's say that the current time is 12:45 AM. ...

Develop a radio button filter utilizing the "Date of Creation" attribute, implemented with either jquery or JavaScript

I am currently utilizing a customized Content Query Web Part to load a series of list items. Each of these items has an XSLT attribute known as the Created Date. Shown below is an example of the markup: <div class="item" createddate="2014-01-22 13:02 ...

The jQuery table sorting function seems to be malfunctioning

Having trouble with the sorting function in the JavaScript files. My page contains the following scripts: <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="js/jquery.tablesorter.min. ...

Is there a way to generate a checkerboard dynamically with the help of jQuery?

I've made some progress so far, but I'm facing a challenge where I need to push 8 <td> elements into 8 different <tr> elements without hardcoding anything or modifying the HTML directly. Any help would be appreciated! JavaScript v ...

Having trouble with my AJAX request and can't figure out why. Anyone available to take a look and help out?

I have successfully implemented this AJAX script on various pages in the past without any issues. <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <script type="text/javascript" src="jquery.validate.min.js"></script& ...

The SVG path is not aligned properly with the container's viewbox dimensions

Recently, I came across an icon provided by a plugin I am using called calendar. However, I wanted to enhance my icons set with a new addition - the twitter icon. svg { display: inline-block; height: 16px; width: 16px; margin: 2px 0; ...

Storing HTML table data in a MySQL database will help you

Operating a website focused on financial planning where users can input various values and cell colors into an HTML table. It is crucial to uphold the integrity of these HTML tables. How can I store the complete HTML table (including values and colors) i ...

Can anyone help me identify the issues in my PHP code?

My PHP code doesn't appear to be functioning correctly. Instead of displaying any errors, it simply shows a blank page. Can you identify what I might be doing incorrectly? <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_ ...

Issue with importing CSS file in Vaadin Java

Currently, I am tackling the eighth part of the Vaadin Tutorial series. Watch the video here: https://www.youtube.com/watch?v=ttuBu8dYNn0 For the text version, visit: I am currently facing a challenge with the final step of importing the provided CSS fi ...

Creating a row with 5 columns in Bootstrap 4 dynamically is a handy trick for enhancing the layout of your website

Struggling to find the right layout for displaying products on my store's home page. <div class="row"> @foreach($produts as $product) //this is the syntax of my templating engine <div class="col-md-3"> </div> @endf ...

I am disappointed with the lack of functionality in Angular's HTML type inference

When working inside an Angular component, I want to select a div element by id or class. This method works menuDisplayDiv = document.getElementsByClassName("some_class")[0] as HTMLDivElement; menuDisplayDiv = document.getElementById("some ...

What is the process for registering a click using a class in jQuery and retrieving the ID of the clicked element?

Currently, I am working on developing a webpage where I need to use jQuery to register a click using the items class and then extract the ID of that particular object. For example: HTML: <div class="exampleclass" id="exampleid1"></div> <d ...