Creating a gap between two elements without using the space-between property

Currently working on creating a responsive menu,

      <div style={{ display: "flex", flexFlow: "row wrap" }}>
        {/* left side */}
        <div key="1">nav 1</div>
        <div key="2">nav 2</div>
        <div key="3">nav 3</div>
        {/* left side */}

        {/* right side */}
        <div key="4">nav 4</div>
        <div key="5">nav 5</div>
        <div key="6">nav 6</div>
        {/* right side */}
      </div>

My goal is to have equal spacing between the two sides while keeping each element wrapped individually.

Visual representation: https://i.sstatic.net/1SXgi.png

In need of some guidance, appreciate your support!

Answer №1

A clever solution is to introduce a pseudo element within the wrapper and adjust its order property to position it between the 3rd and 4th div elements, while also setting flex-grow: 1:

#wrapper {
  display: flex;
  flex-wrap: wrap;
}
#wrapper > div:nth-child(-n + 3) {
  order: 1;
  background-color: papayawhip;
}
#wrapper::after {
  content: "";
  order: 2;
  flex-grow: 1;
}
#wrapper > div:nth-child(n + 4) {
  order: 3;
  background-color: palegoldenrod;
}
<div id="wrapper">
  <!-- left -->
  <div key="1">nav 1</div>
  <div key="2">nav 2</div>
  <div key="3">nav 3</div>
  <!-- right -->
  <div key="4">nav 4</div>
  <div key="5">nav 5</div>
  <div key="6">nav 6</div>
</div>

Answer №2

Instead of utilizing flex, I decided to use float: left/right for my layout. Feel free to resize your browser window to see the effect. To test it out yourself, you can visit this fiddle: https://jsfiddle.net/x7f2pvn9/

div.wrapper div {
  display: inline;
  background: red;
  padding: 1rem;
  margin: 5px;
  width: 40px;
  height: 50px;
}

div.left {
  float: left;
}

div.right {
  float: right;
}

@media only screen and (max-width: 600px) {
  div.right {
    float: left;
  }

}
<div class="wrapper">

    <div class="left">1</div>
    <div class="left">2</div>
    <div class="left">3</div>


    <div class="right">4</div>
    <div class="right">5</div>
    <div class="right">6</div>
</div>

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

Is it possible to create HTML content directly from a pre-rendered canvas element or input component like a textbox?

As I delve into learning JavaScript and HTML5, a couple of questions have sparked my curiosity: 1) Can we create HTML from a Canvas element(s)? For instance, imagine having a Canvas shape, and upon clicking a button, it generates the HTML5 code that displ ...

Unnecessary scrollbar appears in the Simple Material UI dialog demonstration

My Material UI dialog consists of a grid with a scrollbar that allows scrolling even though the content fits on the screen. <Dialog open={isOpen} onClose={() => changeIsOpen(false)}> <DialogContent> <Grid containe ...

The information retrieved from the database query is failing to appear on my webpage

I am encountering an issue where I am unable to display product details on my webpage in a specific format after submitting an ID number through a form. The query does not show any data when submitted. My objective is to append the latest retrieved data be ...

CSS hover image resizing not functioning as expected

Is there a way to dynamically scale up an image when hovered over? I'm trying to achieve an effect where the image increases from 100x75 pixels to 300x225 pixels. Everything seems to be working fine with the layout, but the width doesn't seem to ...

The use of the picture element, with its ability to support various image formats and sizes, must always include

Recently, I came across a VueJS template that closely resembles HTML but has the following structure: <picture> <source type="image/avif" :scrset="avif" /> <source type="image/webp" :scrset="webp" ...

Header formatting issue when attempting to implement tablesorter plugin

I implemented the widget-scroller and widget column Selector in my table using the table sorter plugin in jQuery. Has anyone encountered a problem like the one shown in this picture? It seems that my table headers do not align with the columns properly an ...

Executing a webservice method in an html page using javascript without the need to refresh the page

Is it possible to call a webservice from an index.html page using JavaScript? My webservice is located at "localhost/ws/service.asmx" and the specific web method I want to call is called HelloWorld. The index.html page contains an HTML submit button whic ...

Dynamic horizontal scrolling

I need help implementing a site using React that scrolls horizontally. I'm unsure how to implement certain aspects, so I am looking for some assistance. Within a wrapper, I have multiple container Divs. <div class="wrapper"> <div class=" ...

To generate a new <div> element by clicking a button and then clicking on it using C# Selenium

Before clicking the button, this is the initial HTML code for the page: <div id="layerContainer"> </div> After clicking the button, the code changes as shown in the picture here I have been attempting to locate the new button, but I keep rec ...

Linking an image to a database-stored value through a hyperlink

My goal is to give users the option to link their social media accounts such as Facebook and Twitter, with each link displaying an image that corresponds to the platform. The intention is for these images to direct them to the links stored in the database. ...

Error when attempting to open and close a div through a click event

Currently, I am in the process of developing a web service that generates arrays. I am facing an issue where all the div elements on my page load open by default, but I want them to remain closed and only open upon clicking. Specifically, I want a nested d ...

Guide to Sending and Scheduling Notifications through HTML 5's Notification Web APIs

Is it possible to utilize Web APIs to Schedule Notifications and send them at specific times? I am working on a CMS application that allows users to schedule and send push notifications for their mobile apps. I am interested in implementing something sim ...

JavaScript Image Swap

I tried implementing this script but it didn't work for me. I'm not sure what to do next, so I'm reaching out for help. The script is at the top of the page, followed by a picture with an id that I'd like to change when a button below i ...

PHP: Showing the content of every div on the page

I am facing an issue with retrieving input values from multiple divs in the HTML code provided below. Specifically, I want to extract the Input values with the name LI_Dept as an array for each individual div when handling data in PHP. Is there a way to f ...

Issue encountered: difficulty with vertical alignment in table cell containing both text input and image

I am facing an issue with aligning two items (an input text and an image) within a cell of a table. Despite trying various methods like using vertical-align:middle, margin top, padding, and setting height to 100%, I haven't been able to achieve the de ...

Bootstrap 4 - DropDown option - Element is positioned above the navigation bar

Currently facing an issue with the DropDown menu. Whenever I add padding to the DropDown menu (class - drop_link), it ends up pushing the entire element over the <nav>. I'm unsure of how to prevent this from occurring. I attempted to disable som ...

The layout of my website appears distorted on mobile devices

When I view my website, http://www.healthot.com, on an iPhone or other mobile device, the page doesn't display correctly. It scales the window, requiring me to zoom out to see the entire page. To better understand the issue, please refer to this photo ...

Inject a list of titles into a pipe and randomize the items

I have a list of recipes on my website, with a "view more" button to redirect users to the full recipes page: <div class="recipes_list"> <ul> <li>Recipe 1</li> <li>Recipe 2</li> <li>Recipe 3</li> ...

having difficulty showing the primary content page

I'm having trouble displaying a header, side navigation bar, and main content on my page with the code below. Instead of showing anything, I keep getting a parse error: unexpected end of file. What could be causing this issue? <!doctype html> ...

The ">" CSS child selector does not seem to be functioning correctly with specific properties

While experimenting with applying CSS properties to direct child elements of a parent element using ">", I noticed that it works smoothly with certain properties like borders, but not so much with font-related properties such as color and font-weight. ...