Flexible layout of perfectly square elements

I am currently working on creating a responsive grid layout consisting of squares that are positioned absolutely within their parent element. The goal is to ensure that when the page is resized, the squares scale proportionally with their parent container without shifting between lines.

For example, let's say I have a 3x3 grid of squares:

----------------
| A  | B  | C  |
----------------
| D  | E  | F  |
----------------
| G  | H  | I  |
----------------

The challenge is to make the entire grid resize fluidly while maintaining the dimensions of each square inside it.

I've attempted a few solutions, but I've encountered difficulties especially with adjusting the heights.

If anyone has any suggestions or ideas on how to achieve this effectively, I would greatly appreciate your input!

Thank you, John

Answer №1

Perhaps this solution will work for you:

Here is a fiddle link you can use to reference: http://jsfiddle.net/Preben/dkeccbwo/

If you plan on including text or images as content, the sample provided may need further customization to suit your preferences and needs:

<div class="myrow">
  <div class="box">A</div>
  <div class="box">B</div>
  <div class="box">C</div>
</div>

<div class="myrow">
  <div class="box">D</div>
  <div class="box">E</div>
  <div class="box">F</div>
</div>

<div class="myrow">
  <div class="box">G</div>
  <div class="box">H</div>
  <div class="box">I</div>
</div>

CSS:

.myrow {width:100%;border:1px dashed grey; margin-bottom:10px;display:table;}
.box {width:29%;min-height:80px;float:left;margin:2%;background-color:#cecece;}

In each div.box element, you have the option to insert square images that will automatically scale down while maintaining their aspect ratio.

Alternatively, you might consider using tables (in certain cases, tables can still be useful). For example, if you are already using Bootstrap CSS, you can explore Bootstrap tables: http://getbootstrap.com/css/#tables

Answer №2

function adjustSize(){
  var width = $('.box:first').width();
  $('.box').css('height', width);
}
$(function(){
  adjustSize();
  $(window).resize(adjustSize);
});
.container{
  max-width: 800px;
  font-size: 0;
}
.box{
  display: inline-block;
  font-size: 16px;
  width: 25%;
  text-align: center;
  border: 1px solid #333;
  box-sizing: border-box;
}
.box::after{
  content: "";
  width: 1px;
  height: 100%;
  position: absolute;
  display: inline-block;
  vertical-align: middle;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="box">a</div>
  <div class="box">b</div>
  <div class="box">c</div>
  <div class="box">d</div>
  <div class="box">e</div>
  <div class="box">f</div>
  <div class="box">g</div>
  <div class="box">h</div>
  <div class="box">i</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

Choosing a CSS selector for a class that does not have a specific ID

Looking for a way to select all elements with the class .tab-pane except for those with the id #home. I attempted using .tab-pane:not#home{...}, but it proved unsuccessful. Any ideas on how to achieve this? Sample HTML <div id="home" class="tab-pan ...

Is it more efficient to declare a variable or directly return the variable from the Element's getText() promise in Protractor?

I am facing a scenario where I need to retrieve the text of a web element using a promise and then extract a specific part of the string for further processing. Which example, the one at the top or the bottom, should I use? var id = element(by.binding( ...

Utilize the class names to assign a distinctive color using mixins in SCSS

I am diving into the world of Sass and I have a peculiar challenge. I want to create a class that allows me to set a color using percentages for red, green, and blue values. For example, the class name color-50-10-60 would represent 50% red, 10% green, a ...

Attempting to modify information in vue.js

I have been facing an issue with overriding data in the App.vue component. It seems that every time I try to update the data in my main.js file, the component still takes the default data. I am working with webpack as well. App.vue <template> & ...

Twig template delivers HTML entities

I am dealing with an issue related to the twig template. Instead of displaying HTML markup, it is showing raw HTML characters. <?php /* HomepageController.php */ namespace App\Controller; use Sensio\Bundle\FrameworkExtraBundle\Co ...

Is there a way to reset the canvas with just a click of a button?

How do I reset the canvas upon button click? I attempted: cx.fillRect() However, the above method did not work as expected. I simply want to refresh the canvas without reloading the entire page. Below is my current code snippet: var canvas = docum ...

Dynamic routes in NextJS automatically append a .txt extension to the end of the URL

Issue: When using NextJS, the link <Link href="/link">link</Link> redirects to /link.txt For a simple link like this, HTML <a href="/link">link</a> can be used instead The real problem arises when using NextJS ...

Attempting to program a bot to navigate in a random pattern across a two-dimensional xz plane using Threejs

My journey began with the code below, which initiates a block moving in a straight line: const bot_geometry = new THREE.BoxGeometry(1,1,1); const bot_material = new THREE.MeshBasicMaterial( {color: 0x7777ff, wireframe: false} ); const bot = new THREE.Me ...

Animation of CSS height when image/icon is hovered over

Having an issue with my icon when hovering. I am trying to change the img src on hover. Here is the code I currently have: #aks { width: 0px; max-height: 0; transition: max-height 0.15s ease-out; overflow: hidden; background: url("https://img. ...

What is the best method for designing a filtering menu with a "Narrow By" option?

Looking to create a sidebar menu similar to the functionality on mcmaster.com. This specific feature allows users to efficiently filter products and toggle through different options dynamically. Upon selecting the "metric" option, the entire page adjusts t ...

Steering clear of ng-include while still maintaining versatility in displaying sub-templates

In my project, I have a component that serves as a "blog post", containing various data. To accommodate different content sections, I've developed multiple templates that can be dynamically rendered within the main "blog" template using ng-include. H ...

Encountering issues with basic login functionality using Node.js and MongoDB - receiving a "Cannot

I'm experiencing some difficulties trying to set up a login for my website. I have a user registered in my database with the email: [email protected] and password 123. The issue arises when I attempt to use the POST method, as it returns the fo ...

Need help with JQuery mouseOver fading in twice when you only want it to fade in

The version can be displayed here. I'm looking to have the current text fade away and the new text fade in. Strangely, it seems to fade in twice for some reason. $(window).load(function(){ var originalTitle = $('.Pinctitle').text(); $(&apo ...

Guide on creating line breaks within a list in a Python email

I'm having trouble querying a list from my Flask database and then sending it out as an HTML email. The issue is that I can't seem to break the list items into different lines. For example, instead of: a b c I currently get 'abc' i ...

Styling the right button of the split button in jQuery Mobile version 1.4.0 using CSS

I was attempting to create a listview with list items that have a button on the right side, much like a jQuery Mobile split button listview. However, I only want the right button to be clickable. The left part of each item should contain a label along with ...

Enhancing your grasp on CSS grid columns - mastering columns through float usage

Struggling to grasp the concepts of using http://semantic.gs alongside LESS.js. The documentation provided isn't quite clear, and I haven't delved deep into understanding LESS.js. However, I have gone through the Semantic Grid System website&apos ...

What strategies can be implemented to improve the total blocking time in Vue for optimal performance

I'm facing a challenge that I can't seem to resolve. My page has a high total blocking time (2+ sec). Despite trying to load every vue component asynchronously, the issue persists with 2+ sec TBT. I'm puzzled by what could be causing such a ...

Learn the trick to making specific words appear bold in Select2 by changing the font style

I'm currently working with select2 version 4.0.1, and I have a requirement to modify the font for specific words. In particular, I want to display codes AK and HI in a different/bolder font style, similar to the example below: <select id="select2" ...

Trouble With OnClick and On/Off Class Script Functionality in Chrome and Internet Explorer

I am working on a page with two iframes and a basic navigation bar. The navigation bar links are set up to target one of the iframes and replace its content when clicked. However, I am having trouble highlighting the currently selected link in the iframe - ...

Is it advisable to utilize media queries and transform: translateY(-%) to position content above the mobile keyboard?

I've been struggling for nearly a whole day and could really use some assistance. I'm having trouble understanding how chat web apps manage to work around this particular issue. There have been numerous forum posts discussing the problem: I am tr ...