Managing Container Items using Flexbox

I have successfully created a small website using two flex containers, but now I want to know if it is possible to achieve the same layout with just one flexbox container on the parent class.

I've been attempting to do this, but so far my efforts have not been successful. I really don't want to resort to having two flexbox containers simultaneously.

/* Custom.css */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
ul {
    padding: 0;
    margin: 0;
    list-style: none;
}
body {
    font-family: 'Electrolize', sans-serif;
}
.container {
    max-width: 940px;
    margin: 0 auto;
    padding: 0 5%;
}
.gallery {
    display: flex;
}
.gallery li {
    width: 200px;
    background-color: #1c1c1c;
    color: #bdc3c7;
    margin: 0% 0.5% 0% 0.5%;
}
.gallery img {
    width: 100%;
    height: auto;
}
.gallery p {
    margin: 0;
    padding: 6%;
    font-size: 1.25em;
    background-color: #483636;
    color: #bdc3c7;
    text-align: center;
}
.galleryproducts {
    display: flex;
}
.galleryproducts li {
    width: 200px;
    margin: 2%;
}
.galleryproducts img {
    width: 100%;
    height: auto;
    border: 3px solid white;
}
.latest {
    margin-top: 1%;
    background-color: #1c1c1c;
}
.latest h1 {
    color: white;
    font-size: 1.5em;
    font-weight: 300;
    border-bottom: 3px solid white;
    margin-bottom: 5%;
    padding: 2%;
}
a {
    text-decoration: none;
}
<!-- index.html -->
<div class="container">
  <section class="boxes">
    <ul class="gallery">
      <li>
        <a href="img/electrical.png">
          <img src="img/electrical.png" alt="">
          <p>Electrical Installations</p>
        </a>
      </li>
      <li>
        <a href="img/lighting.png">
          <img src="img/lighting.png" alt="">
          <p>Lighting Decorations</p>
        </a>
      </li>
      <li>
        <a href="img/homeappliances1.png">
          <img src="img/homeappliances1.png" alt="">
          <p>Electrical Appliances</p>
        </a>
      </li>
      <li>
        <a href="img/homeappliances2.png">
          <img src="img/homeappliances2.png" alt="">
          <p>Kitchen Appliances</p>
        </a>
      </li>
    </ul>
  </section>
  <section class="latest">
    <h1>Our latest products</h1>
    <ul class="galleryproducts">
      <li>
        <a href="img/1.jpg">
          <img src="img/1.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="img/2.jpg">
          <img src="img/2.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="img/3.jpg">
          <img src="img/3.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="img/4.jpg">
          <img src="img/4.jpg" alt="">
        </a>
      </li>
    </ul>
  </section>
</div>

Answer №1

Sorry, but applying the display: flex property to the container will not cause the children of the gallery or galleryproducts to behave as they currently do. This is because those children are not direct descendants of the container.

If you were to make that change, the boxes or latest elements would become flex items, while the children of the gallery or galleryproducts would simply be regular li items displayed one on top of the other, instead of side by side.

Therefore, your current setup is actually correct in order to achieve the desired result, especially if you intend to utilize the flex property.

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

JavaScript program that continuously reads and retrieves the most recent data from a dynamically updating JSON file at regular intervals of every few seconds

I am a beginner in JavaScript and I'm facing an issue with displaying the most recent values from a .json file on an HTML page. The file is updated every 10 seconds, and I am also reading it every 10 seconds, but I'm not getting the latest data. ...

The Next.js Image component does not implement the maxWidth attribute

<Image src="/assets/blog/image.webp" style={{ maxWidth: "700px" }} width={1400} height={1400} /> Issue Detected The image with src '/assets/blog/image.webp' has the following styles applied, but they are being overwrit ...

Animate the text before it fades away

Could someone help me figure out how to make my text animations work correctly? I want to show and hide specific text with animation when buttons are pressed, but I'm having trouble implementing the hiding animation. Any guidance would be appreciated. ...

Display or conceal a <div> segment based on the drop down selection made

A dropdown menu controls the visibility of certain div elements based on the selection made. While this functionality is working for one dropdown, it's not working for another even though the code is very similar. I've tried various solutions but ...

Tips for perfectly centering text within p tags using CSS

Currently, I am troubleshooting the JSFiddle where my goal is to vertically center text using CSS. Below is the HTML code that I utilized to create the JSFiddle: <div class="poster text-center pt-4"> <div class="item"> <img class=" ...

What could be causing my page to suddenly disappear?

After saving my changes in the IDE and refreshing the browser to test the prompt() function in my index.js file, the entire page goes blank, showing only a white screen. I double-checked the syntax of my function and it seems correct. Everything else on th ...

What are some ways I can ensure my divs are fully responsive on all devices

Why are my three transparent divs in the center of the page not functioning properly when resizing the screen to md, sm, and xs? The one on the left is offset to the left. How can I make them adaptive? They are meant to be centered next to each other, but ...

Ensure that the child element stays within the parent container by using the max-top property for relative

Check out this HTML code snippet: https://jsfiddle.net/1t68bfex/ button { position: relative; top: 100px; left: 150px; max-top: 10px; } .third-party-block { //display: none; } <div style="border:1px solid red;"> <button> ...

"When using Webpack and Sass, the background image specified with background: url() is processed correctly. However, when using webpack-dev-server, the image is

Currently, I am utilizing Webpack 2 in conjunction with webpack-dev-server and Sass loader. Here is my current configuration: { test: /\.scss/, loaders: [ "style", { loader: "css", query: { modules: false, sourceMap: true } }, ...

The process is consuming a significant amount of time to generate the expected result

My code is currently running all the queries inside a while loop and it's taking around 1 minute to complete. I need suggestions on how to optimize it for faster performance. Here is my code: <table style="font-size:13px" width="150%" border="0" c ...

How to Ensure Center Column Maintains Visibility on Mobile Devices with Bootstrap 4

Is there a way to ensure that page content stays centered without looking too narrow when viewed on mobile devices? Specifically, in the context of a single column layout that is centered. Although the current design appears well-balanced and spaced out o ...

What is the reason for Safari using different fonts on iPad compared to Mac?

Whenever I access our website through Safari and need Github authentication, I encounter this issue: Please check the code snippet below: Make sure to configure these settings: .breadcrumb-link { font-family: SFPro-Medium; font-size: 22px; co ...

Breadcrumb floating to the right, yet it still manages to obstruct other content, forcing it into a

Within the Breadcrumbs section, I have the path home/profile The desired layout involves floating the Breadcrumbs to the right while keeping the Other contents unfloated to the left. Ver1 -------------------- home/profile -------------------- Ot ...

The URL is functional in CodePen, but unfortunately does not seem to be working in JSFiddle

This code works perfectly on CodePen: http://codepen.io/ekilja01/pen/pEXLNY However, when tried on jsfiddle, the navigation bar is completely messed up. External resources are added on. What could be wrong? Any help would be appreciated! <link hr ...

On initial loading, Materialize JS seems to experience technical difficulties

Struggling to develop a Vue.js SPA chat application, encountering issues with Materialize CSS and JS. The JS doesn't function properly on initial load and requires a page reload to work. No errors are appearing in the console, making it challenging to ...

Can HTML blocks be coded to have a triangular shape and content inside?

An old colleague who used to work with me recently sent over a website design concept. Here's a section of the page showcasing a list of posts - Upon closer inspection, I noticed that most of the pink polygons are just background elements while some ...

What is the best way to retrieve data from divs that are nested within another element using Javascript?

I am currently working on implementing a modal that will showcase specific information based on the event-card it originates from. Here is an HTML snippet showcasing an example event-card: <div class="event-card upcoming hackathon"> <d ...

The resizing effect in jQuery causes a blinking animation

I would like to create a functionality where, when the width of .tabla_gs_gm surpasses the width of .content, .tabla_gs_gm disappears and another div appears in its place. While this is already working, there seems to be a screen flicker between the two d ...

`Creating a functional list.js filter``

I'm having trouble making the List.js filter function work properly. The documentation for List.js is not very helpful for someone new to development. Below is the code I am using: HTML: <div class="col-sm-12" id="lessons"> <input class= ...

Using jquery for form validation along with datalist functionality

I am facing a similar issue as discussed in this post: Validation with datalist However, the solution provided by FelDev is in JavaScript and I require it in jQuery. Since I am new to jQuery, any assistance would be greatly appreciated. Below is FelDev&a ...