What is the best way to scale a div proportionally within a bootstrap grid?

Despite spending hours reading numerous online sources, I am still struggling to fully grasp the CSS/HTML box model. Specifically, I am attempting to create sections with a ratio of 3:2, where 3 represents the width and 2 represents the height. https://i.sstatic.net/lUGnR.png

I have attempted to use SCSS variables to no avail:

.boxProject{
  width: 90%;
  height: calc((width/3) * 2);
  background: purple;
  box-shadow: 0 4px 30px rgba(0,0,0,.5);
  border-radius: 6px;
}

Furthermore, the Bootstrap grid system is perplexing to me. When I apply the h-100 class, the grid overflows its div container. I have yet to find a solution to make the div 'resizable', particularly on smaller screens where the content stacks vertically, resulting in a narrow width but a long height.

.projectSection{
  width: 100%;
  height: 100%;
}

#section4 h1 {
  margin: auto;
  background-image: linear-gradient(to right, #62daf5, #59c8f4, #52aef9, #4586f7);
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  font-family: 'Rubik', sans-serif;
  font-weight: bold;
}

.boxProject{
  width: 90%;
  height: calc((width/3) * 2);
  background: purple;
  box-shadow: 0 4px 30px rgba(0,0,0,.5);
  border-radius: 6px;
}

#section4{
  background-color: black;
  width: 100%;
  height: 100%;
}
<div class="container-fluid" id="section4">

        <div class="row mt-5">
            <div class="col-12">
                <h1> My Projects. </h1>
            </div>
        </div>

        <div class="row h-75 w-100 mt-4">
            <div class="col-6 col-md-6 col-lg-4 col-xl-4">
                <div class="boxProject"> </div>
            </div>

            <div class="col-6 col-md-6 col-lg-4 col-xl-4">
                <div class="boxProject"> </div>
            </div>

            <div class="col-6 col-md-6 col-lg-4 col-xl-4">
                <div class="boxProject"> </div>
            </div>

            <div class="col-6 col-md-6 col-lg-4 col-xl-4">
                <div class="boxProject"> </div>
            </div>

            <div class="col-6 col-md-6 col-lg-4 col-xl-4">
                <div class="boxProject"> </div>
            </div>

            <div class="col-6 col-md-6 col-lg-4 col-xl-4">
                <div class="boxProject"> </div>
            </div>

        </div>

</div>

Answer №1

If you want to use the width in a calculation in SCSS, you need to declare it as a variable first. For example, instead of using width directly in the calc function, you can define it as calc((90vw/3) * 2), where 90vw represents 90% of the viewport width. To declare it as a variable and use it in your styles:

// Declare the variable
$width: '90vw';

// Style block 
.boxProject {
    width: 90%;
    height: calc((#{$width} / 3) * 2); // Note that the variable is declared inside #${}
    background: purple;
    box-shadow: 0 4px 30px rgba(0,0,0,.5);
    border-radius: 6px;
}

Answer №2

When using Bootstrap, the grid system is designed to work with breakpoints. This means that when the display size is smaller than a certain point, such as 1200px, the grid is automatically adjusted for large screens. Resizing the display has no impact on Bootstrap's functionality. However, if you need to resize a div, you can use the following CSS snippet:

.resizable {
     resize: vertical;
     overflow: auto; /* Or hidden */
}

This method can also be applied to col elements within the grid.

For a live example, you can view this codepen: https://codepen.io/Throvn/pen/OJJGExy

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

Display an image tag using the site_url() function within PHP brackets

I have a loop in my view that is fetching all the data from the database: <?php foreach($content as $contentRow): ?> <?php echo $contentRow->value; ?> <?php endforeach; ?> This loop works well for HTML strings ...

Sorting Angular data using database queries

I'm currently setting up a blog for my website. Everything is running smoothly with the database, but I've run into an issue with the order of my posts - they are displayed in the opposite order that I want. The oldest post is showing at the top, ...

Troubleshooting: Images not appearing on HTML pages

I've recently started learning HTML and can't figure out why my code isn't working. I am trying to add an image but it only appears as a blue box with a question mark in it. Here is my code: <head> <title>Welcome</title> ...

Unable to apply 100% height to flex child element

When it comes to creating a layout for my website, I have a simple idea in mind. I want the body content to take up at least 100% of the height of the page. This setup works perfectly on desktop screens, but when I switch to a mobile view (which changes th ...

The CSS3 Animation remains stationary

I've been attempting to animate my block element moving to the left using CSS animation shorthand property, but unfortunately, it's not working as expected. Here is my HTML: <div id="game"> <div id="block">&l ...

Troubleshooting CSS3 pulse animation failure in Internet Explorer and Firefox

My animated background is looping infinitely and working perfectly in Chrome and Safari. However, I'm having trouble getting it to work in IE and FF. I've tried looking at other people's questions on this topic but still can't figure it ...

After closing, the position of the qtip2 is being altered

I've successfully integrated the qtip2 with fullcalendar jQuery plugin, which are both amazing tools. However, I'm encountering an issue with the positioning of the qtip. Here's the code snippet I'm using: $(window).load(function() { ...

Spread out the items within an inline block

Is there a way to create space between icons that are in an inline block without them touching each other? One solution could be to add a container around each icon, center the icons within the containers, and then place the containers in the block. You c ...

Would it be possible for me to adjust the CSS to center the sections?

I'm facing an issue with the layout of my website at . At the bottom, there is a navigation menu and three boxes with images. However, they are currently left-aligned instead of centered. Can anyone suggest CSS code that I can use to center them? Her ...

Is there a way to transform an HTML table from a horizontal layout to a vertical

I have reorganized the bus seating layout horizontally using a table. However, I now need to transform it vertically for mobile phones without disrupting my existing code. My attempt at using transform(rotate90deg) achieved the desired result, but the tab ...

What is the best method for retrieving values from selected radio buttons?

I am trying to retrieve the values of the selected radio buttons with the name is_external example.html <div class="exter_inter"> External <input type="radio" name="is_external" value="1" <?php if(isset($_GET['status']) &&am ...

The Bootstrap Grid System Column is Experiencing Issues

Check out my code snippet: I've attempted changing the container class to container-fluid and resizing the page, but no luck. <div class="container"> <div class="row"> <!--Product: banana split--> ...

What is the best way to make a gradient fill and rounded border with CSS?

I am interested in creating a gradient and rounded border similar to the top bar on Instagram. Although I know how to create a rounded and solid border, this one utilizes a gradient. It appears that Instagram uses a canvas, but I am wondering if it can b ...

What is the best way to prevent right floated DIVs from interfering with each other's positioning, specifically within a specified space?

Despite feeling like this question may have been asked numerous times before, I've searched high and low for an answer to no avail - both here and on Google. My HTML page has a maximum width that limits the size of the content. This content includes ...

Is there a way to ensure that the text stays positioned behind the initial image even as the window size is adjusted

<html> <head> <title> Example </title> <style> img:hover {opacity : 0.3;} img {z-index : 1; height : 33%; width : 33%; } .first { position : absolute; top: 0%; left: 0%;} .second {position : absolute; top: 0%; left: 33%; ...

Mastering the art of utilizing particles.js

Particles.js doesn't seem to be functioning properly for me—I'm struggling to pinpoint the issue. Any guidance or suggestions would be greatly welcomed, as I'm unsure whether it's related to an external dependency... HTML: <div ...

Ways to block past dates on bootstrap date picker starting from the current date and how to prevent dates beyond 90 days in the future from being selected on the

I am currently facing an issue with disabling previous dates from the current date in my code. While the functionality works well for the "From Date" date picker, I am having trouble implementing the same restriction for the "To Date" date picker after 90 ...

What is causing the CSS3 tabbed area to be restricted?

Experimenting with CSS tabs found at http://css-tricks.com/examples/CSSTabs/: Sample six .w3c { min-height: 250px; position: relative; width: 100%; } .w3c > div { display: inline; } .w3c > div > a { margin-left: -1px; position: relative; left: 1 ...

Persist the checkbox value in the database and update the status of the label accordingly

I need a checkbox for user preference selection that is stored in a MySQL database for constant availability. The label should change to "Enabled" when the checkbox is selected and vice versa. Upon saving (submitting) the page, an alert message should disp ...

Utilize a specific component to enclose particular words within a contenteditable div in an Angular project

I have a task at hand where I need to manipulate text input from a contenteditable division, use regex to identify specific words, and then wrap those words within an angular component. Although I have managed to successfully replace the text with the com ...