flexbox with equal width and height

I have two questions regarding flexboxes but I am unable to find the answers.

  1. How can I create 4 boxes with equal width and height, without using the viewport? For instance, if I create a box with 50% width, I want the height to be equal to the width. All other boxes should have the same properties.

  2. If I have these boxes, how do I insert divs inside one of them that takes 20% height and the full width of the box without extending it?

Here is my code so far:

.flex-container {
  float: none;
  width: 50%;
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
  display: flex;  
   -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
}

.flex-item { 
  width: 50%;
  color: white;
}

.flex1 {
  background-color: green;
   float: left;
}

.flex2 {
  background-color: red;
  float: right;
}

.flex3 {
  background-color: purple;
   float: left;
}

.flex4 {
  background-color: orange;
  float: right;
}

.inlineBox {
  width: 100%;
  height: 20%;
  background-color: yellow;
}
<ul class="flex-container">
  <li class="flex-item flex1">these boxes need to have the same height and width, and they must be responsive without using a viewpoint</li>
  <li class="flex-item flex2">2</li>
  <li class="flex-item flex3">3</li>
  <li class="flex-item flex4">4</li>
  
  
</ul>

<br>
<br>
<br>


The yellow boxes need to occupy 100% width and 20% height inside the flex box<br>


<ul class="flex-container">
  <li class="flex-item flex1">
    <div class="inlineBox"> </div>
    <div class="inlineBox"> </div>
    <div class="inlineBox"> </div>
  
  </li>
  <li class="flex-item flex2">2</li>
  <li class="flex-item flex3">3</li>
  <li class="flex-item flex4">4</li>
  
  
</ul>

Answer №1

Sorry, the answer is negative. The explanation can be found in the flexbox specification: https://www.w3.org/TR/css-flexbox-1/#flex-lines

You can achieve the desired outcome using display: grid:

https://codepen.io/pgurav/pen/eYpeOEp

Although not exactly flexbox, the grid mimics its functionality as a container, allowing flexibility for items inside.

The grid system is particularly useful for creating responsive layouts. For instance, altering the number of columns per row is as simple as adjusting grid-template-columns:

grid-template-columns: repeat(1, 1fr); // 1 column
grid-template-columns: repeat(2, 1fr); // 2 columns
grid-template-columns: repeat(3, 1fr); // 3 columns

This approach can be combined with media queries to adapt based on page size.

Unfortunately, container queries or element queries are not widely supported by browsers, hindering seamless adjustment of column numbers based on container size (particularly useful for reusable web components).

For more insights on the grid layout:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

Cross-browser compatibility of Grid Layout:

https://caniuse.com/#feat=css-grid

Answer №2

Here is the HTML code:

<ul class="flex-container">
    <li class="flex-item flex1">
        <span>These boxes that I created here all need to have the same height and width. They need to be responsive and
            don't use a viewport</span>
    </li>
    <li class="flex-item flex2"></li>
    <li class="flex-item flex3"></li>
    <li class="flex-item flex4"></li>
</ul>

<br>
<br>
<br>
These yellow boxes need to take a width of 100% and a height of 20% inside the flex box.<br>

<ul class="flex-container">
    <li class="flex-item flex1">
        <div class="item-wrap">
            <div class="inlineBox"> </div>
            <div class="inlineBox"> </div>
            <div class="inlineBox"> </div>
        </div>
    </li>
    <li class="flex-item flex2"></li>
    <li class="flex-item flex3"></li>
    <li class="flex-item flex4"></li>
</ul>

And here is the CSS code:

.flex-container {
    width: 50%;
    padding: 0;
    margin: 0;
    list-style: none;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -moz-flex;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
}

.flex-item {
    position: relative;
    width: 50%;
    padding-top: 50%;
    color: white;
}

.flex-item>span {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
}

.flex1 {
    background-color: green;
    float: left;
}

.flex2 {
    background-color: red;
    float: right;
}

.flex3 {
    background-color: purple;
    float: left;
}

.flex4 {
    background-color: orange;
    float: right;
}

.inlineBox {
    width: 100%;
    padding-top: 20%;
    background-color: yellow;
}

.item-wrap {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
}

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

Avoiding conflicts between banners, text, and images for HTML/CSS design

I'm having an issue with the banner I created for my project. It seems to be overlapping with text and images, hiding behind them. How can I fix this? Unfortunately, I can't post the link to my project here due to other files present. The specif ...

Could anyone assist me in positioning my personalized Context Menu beside my cursor?

If possible, I would appreciate some assistance with my custom context menu. It may require some minor tweaks or a fresh idea on how to address the issue. Ideally, I would like to keep the HTML and CSS somewhat unchanged. [I'm not sure what to write ...

What sets apart Firefox and Chrome when it comes to interpreting the widths of flex items?

Looking to create a column layout using flexbox with three divs that span the full width of their parent container while maintaining a single line height. If the content in the center div overflows, I want it to display an ellipsis at the end. Check out t ...

Issue with automatic compiling in Sublime 2 for less2css

Whenever I try to save my style.less file, the automatic compilation doesn't happen. Is there a mistake in how I've set it up? Here's what is currently in my ox.sublime-project file: { "folders": [ { ...

Could someone please review my CSS code for the dropdown menu? I'm having trouble centering the navigation menu

As a newcomer to coding in CSS, I have tried the suggested solutions for my issue without success. Any help would be greatly appreciated. My goal is to center my menu on the page while keeping the container background covering the full width available... ...

Is there a more efficient method for showcasing model objects in Thymeleaf rather than utilizing a form?

I've been exploring Spring Boot, Thymeleaf, and frontend development in general. Currently, I have a frontend table that displays a list of objects from the backend, along with a form to allow users to view properties of each item in the list. Howeve ...

Out of the blue, the CSS functionality in my React app completely ceased to

I've been developing this website using React and Material UI, and I chose to implement standard CSS for styling. However, after closing my code editor and reopening it, some parts of the CSS do not seem to be loading properly. I'm completely puz ...

React Issue: Footer not staying attached at the bottom of the page

I'm currently developing a website with react, but I'm struggling to keep the footer at the bottom. When there isn't enough content, the footer ends up right after the content instead of staying at the bottom. I've tried various solutio ...

When the specified width is reached, jQuery will reveal hidden circular text upon page load instead of keeping it hidden

My current issue involves utilizing jQuery to hide circular text when the window width is below 760px. Although the functionality works as intended, there is a minor problem when the page loads under 760px width - the text briefly shows up before hiding ag ...

What are some ways to utilize symbol fonts such as marvosym within a web browser?

Are you looking to use special LaTeX fonts like \Pluto from marvosym in your web development projects? Wondering how to display this symbol with HTML / JavaScript / CSS without relying on an image? You can download the font from This symbol corresp ...

Text spilling out of a floated division

I'm currently working on a fluid layout design, but I've encountered an issue with the left menu text overflowing. I can't seem to get overflow:hidden to work properly. You'll notice that in the blue area, I have highlighted the text t ...

Notifying Users of JavaFX Applet Web Page Refresh and Closure

Is there a way to detect when a user closes or refreshes a web page with a JavaFX applet embedded in it? I need to perform some clean-up tasks in my code when these events occur. In the past, Java Applet had callback methods for this purpose. How can I ac ...

What is the most effective method for incorporating keyframes using JavaScript in a dynamic way?

Is there a way to animate an HTML element by using a variable with keyframes, but without directly manipulating the DOM? Below is the HTML code: <div class="pawn" id="pawn1"></div> Here is the CSS keyframes code: @keyframe ...

Place the element at the bottom of the row above

I'm utilizing Angular to retrieve a list of items from the server and exhibit them on the page using: <div class="col-sm-6 col-md-4" ng-repeat="activity in activities"> <img ng-src="[[ act.icon ]]" height="100" alt=""> <h3>[ ...

An unexpected issue occurred: Unable to invoke method on NPObject

I am new to JSON and having trouble accessing object data. Here is the code snippet: <!doctype html> <html> <head> <meta charset="utf-8"> <title>ajax </title> </head> <body> <p id="p"></p& ...

"Enhance readability by adjusting the font size on mobile devices and iPhones for an

I'm currently working on a website that needs to maintain a consistent look across all types of devices, including laptops, PCs, iPads, iPhones, and other smartphones. To achieve this, I've implemented fittext.js, a pure JavaScript solution that ...

FitText.js malfunctioning

I'm currently experimenting with using FitText.js to dynamically adjust the size of headlines to fit within the limits of the browser width. Interestingly, while this script successfully resizes the text in multiple sections of my website, it seems t ...

Tips for customizing ngTable with expandable detail panels

I am currently working on styling a layout using ng-table, which includes a table within each row. The expanding functionality in Angular is implemented as follows: <tr ng-if="org.expanded" ng-repeat-end> <td></td> <td></td& ...

Tips for creating a more condensed materialized table with form elements

Currently, I am working on a timesheet table using Materialize CSS with form elements in cells. However, I find the height of the table rows to be too large for my liking. Is there any way to make the table more compact? If this is a limitation of Materi ...

Tips for including data labels in a scatter plot using d3.js

I am currently studying d3.js for data visualization and I have chosen to work with the titanic dataset My objective is to incorporate passenger names into my visualization so that when a cursor hovers over a point, the person's name is displayed. H ...