Designing a Flexbox Square Dependent on Parent DOM's Smallest Dimension Using Bootstrap and React

Is there an efficient method to create a square card or div using Bootstrap flexboxes with minimal custom CSS and/or utilizing built-in Bootstrap properties if feasible?

My aim is to achieve a responsive square positioned in the center of this card, based on the minimum width or height of its parent element with some margin.

Essentially, the square would have dimensions NxN where

N = Math.min(parentWidth, parentHeight) - (marginSize * 2);

https://i.sstatic.net/o7dDY.png

This is my current setup

<div class="d-flex flex-column min-vh-100">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a href="#home" class="navbar-brand">url.com</a>
    </nav>
    <div class="card m-5 h-100 flex-grow-1 rounded-0">
        <div class="card small-cards m-5 h-100 flex-grow-1 rounded-0 border-0">I WANT THIS TO BE SQUARE :)</div>
    </div>
</div>

Answer №1

Here is the solution I crafted to meet my specific needs and preferences, just in case this question gains visibility in search results:

https://i.sstatic.net/a7PBJ.png

<html>
  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/darkly/bootstrap.min.css" integrity="sha384-nNK9n28pDUDDgIiIqZ/MiyO3F4/9vsMtReZK39klb/MtkZI3/LtjSjlmyVPS3KdN" crossorigin="anonymous">
    <title>url.com</title>
  </head>

  <body>
    <div class="d-flex flex-column min-vh-100">
      <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a href="#home" class="navbar-brand">url.com</a>
      </nav>
      <div class="d-flex flex-grow-1 justify-content-center">
        <div class="card small-card h-100 flex-grow-1 rounded-0 align-self-center" style="max-width: 85vmin; min-width: 85vmin; min-height: 85vmin; max-height: 85vmin;" />
      </div>
    </div>
  </body>
</html>

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

If I include the Next.js Image component within a React hook, it may trigger the error message "Attempting to update state on an unmounted component in React."

My UI layout needs to change when the window width changes. However, when I add an Image (Nextjs component) in my hook, I encounter an error message. I am not sure why adding Image (Nextjs component) is causing this problem. The error message is display ...

How do I utilize the Material-UI slider's activated (CSS API) feature to conceal the shadows?

Can someone please explain to me how I can use the activated class to change the style of the thumb and hide its shadows? According to the official website, the activated class is applied to the track and thumb elements to trigger JSS nested styles when a ...

Exploring the process of customizing native classes with Vue.js

Within vue-awesome-swiper, there's a standard class called this: .swiper-button-next { position: absolute; right: 46% } The task at hand is to adjust this CSS code only for right-to-left layouts, like so: .swiper-button-next { position: abso ...

ECharts - Version 3.1.6 Event Plugin

I am looking for advice regarding the management of two charts with reference to echars from Baidu (version 3.1.6). Is there a way to control both charts by engaging in a click event on one of them? In simpler terms, how can I capture and respond to the c ...

Simply click and drag a document from your file explorer directly into the desired text field to instantly generate a clickable

I am interested in dragging a file from our Windows server and dropping it onto a text area on a webpage. The link that would be generated will look something like this: <a href="\\fileserver\folder\pizza_2.pdf">filename.pdf< ...

React @material-ui/core - version 4.11.0 - "a console error indicating that the createMuiTheme function has been renamed to createTheme"

When my React project is at the production level, I am seeing a console error that doesn't show up when running locally. The specific error message mentions changing `createMuiTheme` to `createTheme` in Material-UI. Material-UI: the createMuiTheme fun ...

Can you explain the purpose of CSS classes such as my-2, my-lg-0, and mr-sm-2 in Bootstrap 4?

I have searched through various questions and the Bootstrap documentation but haven't been able to determine the purpose of these classes on elements. Any assistance would be greatly appreciated! My goal is to adjust my search bar's size to 1/4th ...

Modify the height of an image within a container using the display property set to table-row

I'm working on a layout that includes a footer always at the bottom with a variable height. I have placed an image in the center div and I want it to adjust if the screen size is too small. Here is an example: CSS html, body { width:100%; heigh ...

See HTML, HTM, and other similar file formats as simple text

Is there a method to redirect to a page such as domain.com/index.htm and display it as plain text or code instead of the browser rendering the page? If this can be achieved using PHP scripts, that would be fantastic. (It's important to note that the ...

Is there a way to retrieve all div elements within a specific div using webdriver and selenium?

As a newcomer to the world of web scraping, I am currently working on scraping a website in order to extract all elements with the data-feature-id=homepage/story. My ultimate goal is to access a subelement within each of the divs contained in the parent el ...

Installing npm on a Create React App applicationI just installed npm on my brand

After successfully creating an app with Create React App, I decided to try running npm install in a new folder, but encountered issues. My goal is to test this app so that I can share its source code without including the bulky node_modules folder. Unfort ...

NEXT.JS - LocalStorage unexpectedly resets data to its initial state instead of persisting changes after the page is refreshed

Upon initial component run, "1" is displayed. Clicking the button appends it by 3 successfully. The value inside local storage also updates accordingly. However, upon reloading the page, the local storage reverts back to 1. What could be the missing piec ...

Adjust the height of the card body to match the horizontal bootstrap 5 card image

Is there a way to use Bootstrap 5.1.3 to make the image fill the height of the card's body and footer, while also ensuring the card-footer is placed at the bottom of the card? The desired outcome: <link rel="stylesheet" href="https://cdn.jsdeli ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...

Retrieve the difference in time from the current moment using moment.js

I am trying to implement a functionality where I can track when my data was last updated. - After hitting the 'Update' button, it should display 'Update now' - If it has been n minutes since the update, it should show 'n mins ago& ...

Steps to creating a popup within an HTML page from another HTML page

I recently started learning JavaScript. I have a link on my homepage that directs to another HTML page, but I would like it to show as a popup on the homepage with responsiveness and some stylish design elements such as a folded corner. Is there a way to a ...

The browser does not support the display of Spanish special characters

I am working on a website and need to incorporate support for the Spanish language. I have prepared an XML file with both English and Spanish text. The XML file is being read based on the user's selection of language from a dropdown menu. Everything s ...

Attempting to apply custom styles to jQuery components using CSS

Looking to restyle the black bar on my test page located at The black bar with 3 tabs about halfway down the page needs a new color scheme. By inspecting with FireBug, I found these elements: <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ...

Adjust the translateX value and element size based on either the parent element's size or the window's dimensions

Is this question specific enough to relate to others' problems? My issue involves two elements, a child and a parent, with the child element rotating around the parent using CSS animations. <div class="planet"> <div class="moon"></di ...

Bootstrap navbar problem: struggling to adjust padding for image and navigation links

Can anyone help me understand why my navbar isn't displaying padding on the left and right as specified in my CSS? I've been trying to figure it out for a while now, but I'm not sure if I need to remove the div-container fluid or if I'm ...