What is the best way to set a div's height to a percentage of the window's width

My idea is to create a website background that consists of a grid of perfectly square squares. The challenge here is that I need these squares to adjust in size proportionally to fit the screen without any cut-off squares on one side. Essentially, they need to be responsive and fill the entire page seamlessly. If this explanation isn't clear, please let me know!

I've explored two possible solutions for this issue. The first involves using simple CSS. While there's VH (vertical height) unit available for setting the height, I haven't been able to find an equivalent for width. If such a unit exists, I could set the squares' dimensions to 1W x 1W. This might solve the problem if it does exist.

If the CSS approach doesn't work, another alternative would be utilizing jQuery. With this method, I would calculate the screen width in pixels and assign a variable 'X' to be equal to the width divided by 100. By then setting the square height to 10*X and the width to 10%, we might achieve the desired result. However, this solution comes with the downside of having to execute the function every time the screen is resized, which could potentially cause performance issues on slower internet connections due to the number of squares used.

Perhaps I'm overcomplicating things. Does anyone have a simpler or more efficient solution to this dilemma?

Answer №1

Utilizing the vw unit (which is dependent on the viewport/window width) is a valuable technique. 1vw represents 1% of the window width.

Answer №2

When calculating padding, it is based on the width of an element. This means you can set the height of your elements to 0 and just adjust the padding.

Here is the HTML:

<div id="wrapper">
    <div class="tenth">
        1
    </div>
    <div class="tenth">
        2
    </div>
    ... (more similar divs) ...
</div>

And the CSS:

#wrapper{
    font-size:0;
}
.tenth {
    font-size:1rem;
    height: 0;
    width: 10%;
    padding-bottom: 10%;
    box-sizing:border-box;
    display:inline-block;
    border:1px solid black;
    margin:0;
}

Check out the fiddle here: http://jsfiddle.net/trex005/xc7Lgzr9/

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

Tips for creating unique title descriptions for identical files to prevent repeating content

Essentially, Google considers www.example/cz and example.cz/index.php as duplicate content because they share the same meta descriptions. It's important that example.com/cz and example.com/cz/index have unique meta descriptions to avoid this issue. I ...

Utilizing jQuery to dynamically load and display multiple files within a single div, replacing any previously loaded content

Forgive my lack of experience, as I am just starting out with this and it may be a simple issue. My goal is to have an empty div (popBox) load a file when a link is clicked, but currently I am struggling to overwrite the content in the div when clicking o ...

Is there a way to identify when a radio button's value has changed without having to submit the form again?

Within this form, I have 2 radio buttons: <form action='' method='post' onsubmit='return checkForm(this, event)'> <input type = 'radio' name='allow' value='allow' checked>Allow ...

What is the best method to activate or deactivate a link with jQuery?

Can you provide guidance on how to toggle the activation of an anchor element using jQuery? ...

What is the best way to reference the className within nested SCSS when working with Next.js and React.js?

I am working on customizing a navbar that includes a hamburger button. My goal is to change the style, specifically the bar color and the appearance of the hamburger button, upon tapping. I have already attempted using &:active and .activeBar in the SCSS f ...

Is the issue of Padding Overlap with Sidebar Item List getting in the way?

I am currently new to coding in HTML and CSS, so please bear with me if my code seems outdated or unconventional. I am working on creating a basic test website with a sidebar, but I'm encountering an issue where the items in my list are overlapping du ...

Could someone assist me in resolving the issue of receiving empty emails from my online form submission?

Every day, I receive 6 blank emails from my contact form at the same time. Even though the form on the website is filled out correctly and all the information goes through, I still get these mysterious blank emails. I have implemented validation checks in ...

Tips for concealing broken images in jQuery/JavaScript when generating dynamic content

Is there a way to hide the broken images in this dynamically generated HTML code using JavaScript? Unfortunately, I don't have access to the source code itself. I'm new to JQuery and could really use some assistance. Below is the snippet of the ...

Trigger animation once you've scrolled past a designated point in the document

I created a count-up counter animation using JavaScript, but the issue is that the counter starts animating as soon as I refresh the page regardless of where I am on the page or if the counter is even visible. I would like the counter to only start workin ...

Extract JSON data from a web address

Currently in the process of creating a website, and I am utilizing a URL that gives back a JSON response structured like so: {name:mark; status:ok} My goal is to extract just the name using JavaScript or jQuery exclusively within my HTML page. Could any ...

Is it feasible to manage the HTML content surrounding my information within the Google Maps InfoWindow in ReactJS?

In my Google Maps application, I have an InfoWindow that displays some of my HTML content created in React. return ( <div className="map-tooltip-wrapper"> <div className="map-tooltip-image><img src={image || '& ...

Text overflow happening in Bootstrap 4 fixed-top navbar

Hey there, I have a question that might sound newbie. I want to create a fixed side navigation bar using bootstrap 4. When I add the 'fixed-top' class, it does fix the position but the text in my navbar overflows and looks messy: https://i.sstat ...

Is it feasible to implement the Bootstrap GRID System?

Hello there! I am currently exploring a potential scenario. When viewing on a desktop or laptop, the layout should look like this: <div class="col-md-3"> sidebar </div> <div class="col-md-9"> article </div> On a smaller screen, ...

JavaScript not functioning properly with 'required' validation

pushData = []; //+ button when clicked function myFunction() { var custOfficeId = document.getElementById('customOfficeId').value; var custOfficeName = $("#customOfficeId option:selected").text(); var fromDate = document.getElementByI ...

What is the best way to retrieve the value of a textbox using jQuery and ajax?

I'm facing an issue where I am trying to retrieve a value from a textbox and display it in a table using jQuery. Strangely, when I try to access the "data" inside the success:function (data) of my AJAX call, it returns empty, but if I log it outside t ...

Deliver HTML emails with PHP including files attached

Recently, I encountered an issue: In the past, I sent HTML emails using PHP with a header that contained Content-type: text/html; However, I have now implemented functionality to include attachments. This required me to change the line to Content-Type: ...

Extracting data using Ajax: Retrieving plain text content using getElementByTagName

I'm trying to scrape a specific piece of information from a URL using Ajax/jQuery. However, I'm still new to this and unsure of the correct syntax. Here is my current ajax call: $.ajax({ url: URL, dataType: 'text', success ...

Has anybody dabbled with Indexed DB before?

After finding out that WebSQL is deprecated, I started exploring IndexedDB as an option for browser offline storage. However, I discovered that IndexedDB has undergone significant design changes since the examples provided in HTML5Rocks (http://www.html5 ...

Confirm proper functioning of Dropdown without the need for a form tag

We offer users the ability to choose a "Brand & Model" and then click on the "See cases" button. Once the selection is made, it will redirect to the appropriate URL. If someone clicks directly on "see cases" without selecting a Brand & Model, it redir ...

How can I transmit data to a jQuery web-service request?

I have been following various online tutorials but haven't had any luck so far. My goal is to create a basic example that successfully passes a value to a web service call. What am I missing here? I can achieve this easily with HttpHandlers...why is ...