Tips for creating a div that is consistently 80% of the page height and perfectly square

Sorry if this question seems a bit confusing, but I just can't seem to find the right words to explain it! What I'm trying to achieve is having a div element always be 80% of the height of the page, while also being perfectly square in shape. In other words, I want the width of the div to match that of 80% of the page's height, creating a square shape. Despite my efforts in researching, I haven't been able to find a solution yet. Ideally, I would prefer using CSS only for this task, but I am open to using JavaScript as well if needed. Here is an example of how I envision my layout at different page heights and aspect ratios: MY PAGE LAYOUT

The blue div should always be 80% of the page height and should remain square in shape. I require this functionality because I aim to have a page without any scrollbars, which means the div needs to adjust based on the page height while maintaining a perfect square shape.

Thank you!

Answer №1

To determine the size of an element dynamically based on the viewport's initial containing block, you can utilize vh where 1vh is equivalent to 1% of the height of the viewport.

Helpful link for understanding CSS length

For implementation, your CSS class would resemble:

.customClass {
  height: 80vh;
  width: 80vh;
}

Answer №2

You have the option to specify both the width and height using vh units, which corresponds to 1% of the viewport height. In this scenario, it would look like this:

div{
  width:80vh;
  height:80vh;
}

However, this method is not recommended as it can lead to issues if the viewport height exceeds the width, resulting in horizontal scrollbars or hidden content overflowing.

A more effective approach for such situations would be to utilize vmin, which represents 1% of the smaller viewport dimension.

div{
  width:80vMin;
  height:80vMin;
}

Alternatively, you can employ media queries to determine whether the viewport is in landscape (wide) or portrait (tall) mode.

@media screen and (orientation: landscape) {
  div{
    width:80vh;
    height:80vh;
  }
}

@media screen and (orientation: portrait) {
  div{
    width:80vw;
    height:80vw;
   /*or any other value*/
  }
}

Answer №3

.equalSize {
  width: 80vh;
  height: 80vh;
  background-color: red;
}
<div class="equalSize"><div>

You have the option to utilize CSS built-in units like vh (viewport height). Refer here for more information on CSS units

Alternatively, you can achieve equal sizing by using aspect ratio techniques as explained in this guide: Aspect Ratio

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

Several pictures are not displaying in the viewpage

I have a controller method set up to fetch files from a specific folder. public JsonResult filesinfolder(ProductEdit model) { string productid = "01"; string salesFTPPath = "C:/Users/user/Documents/Visual Studio 2015/Projects/root ...

Would it be beneficial to create classes for frequently used CSS styles and apply them to HTML elements?

Check out my code snippet: style.css .bg-cover{background-size:cover; background-position:center;} .opacity-1{opacity:0.1;} .border-3-solid{border-width:3px; border-color: solid;} .black-border{border-color:#000;} .full-width{width:100%;} index.html ...

What is the proper way to safely escape a JSON string for an object that contains user-generated content?

How can I correctly use the variable s for the value-field of an option-element while escaping user input used for key and value? var key='highway'; var value='track'; var s = JSON.stringfy({ [key]:value }, undefined,''); s ...

Issues with displaying search form div using jQuery AJAX

Currently, I am in the process of developing a search form that includes a dropdown menu. The functionality involves selecting an item from the dropdown menu triggering an AJAX call to another PHP file. This call returns tags which resize both the dropdown ...

Retrieve the value of a variable from a JSON decoded response

My json response has been decoded into an array $data: stdClass Object ( [outboundSMSMessageRequest] => stdClass Object ( [deliveryInfoList] => stdClass Object ( [deliveryInfo] => stdClass Object ( [address] => 8606142527 [deliveryStatus] => ...

Is it possible to have the website show up at a default size of 90% on the display

My preference is for my website to appear at 90% instead of 100%. This seems to be a common occurrence for me. Is there a way to have it default to this appearance? ...

Creating a webpage with a left panel and draggable elements using JavaScript/jQuery

As someone new to Javascript/jQuery, I have been given the task of creating a web page with elements in a "pane" on the left side that can be dragged into a "droppable" div area on the right side. The entire right side will act as a droppable zone. I am u ...

Is it possible to modify the default behavior of a sensitive region within a button?

I created a calculator application in React and overall, it's working fine, however... I've noticed that when I hold a click longer, it only registers as a click if the mouse was pressed down and released on the button itself. Although I unders ...

Using jQuery to retrieve the domain extension from a URL

Seeking assistance with extracting domain extensions from various URLs using jQuery. Uncertain how to account for all possible scenarios. Here are the parts of the URL that need to be extracted: https://www.amazon.**com**/dp/067144901X https://www.amazon. ...

The concept of nested labels: defining conditions for displaying specific sections of a label

Let's say I have a label that contains the following text: <label>This is a text. This is another text.</label> Is there a way to indicate that the second sentence should only be visible when a boolean value is true? For example: <la ...

What are some effective replacements for SoundManager2 worth considering?

While Soundmanager2 is a useful app, many find the code to be overly complex and difficult to navigate. It almost seems as if it was purposely written in a way to confuse rather than clarify. Are there any recommended alternatives to Soundmanager2 that are ...

Tips for aligning the text of a button and placeholder vertically

I am facing an issue with centering placeholder/input text and button text vertically on my website. I used Bootstrap to create the site and utilized its input and button classes, but I couldn't figure out how to achieve vertical alignment. I tried ad ...

How can you iterate over the input elements that are currently visible within a form using Javascript?

Is there a way to clear the values of all visible input fields in a form using JavaScript? I'm currently struggling with setting text inputs to empty as they come out as undefined. Any suggestions on how to achieve this? ...

Incorporating an SVG Element into a design while ensuring its responsive functionality

I tried adding an SVG wave type picture to my background and making it responsive, but encountered some issues. I wanted to enhance the look of my existing background image by incorporating a wave design. However, when I zoomed in on the website, the new a ...

How to use bootstrap to resize and center a div element

I'm struggling with formatting a form that is enclosed in a bordered fieldset. Currently, all the form fields are filling the entire width of the fieldset. However, I want certain textfields to be shorter and centered within the fieldset. I have attem ...

Using ngModel to bind data within an Angular dialog box

I'm facing an issue with my project where changes made in the edit dialog are immediately reflected in the UI, even before saving. This causes a problem as any changes made and then canceled are still saved. I want the changes to take effect only afte ...

Achieve vertical alignment and responsiveness of elements in primefaces using css techniques

In order to vertically align 2 elements inside an outputpanel, I initially used a margin-top of 3em. However, this method proved to be non-responsive, as shown in the following images: https://i.sstatic.net/yMRXc.png In mobile view: https://i.sstatic.net ...

Is there a way to extract all the aria-label attributes from elements that share a common class?

I am currently working on a program that tallies the number of reactions from Facebook posts, with each reaction stored in an aria label containing detailed information. I am encountering difficulty retrieving the values of these unique aria labels due to ...

Tips for aligning text to match the starting point of other text

Currently, the text appears as follows: Lorem ipsum dolor sit amet consectetur adipiscing elit vitae orci elementum dictum I want it to look like this: Lorem ipsum dolor sit amet consectetur adipiscing elit vitae orci elementum dictum I ...

Download the PDF instead of just viewing or downloading it through a link

I need help configuring a link so that when the user clicks "download here," a PDF file is automatically downloaded to their desktop. Can someone provide guidance on how to achieve this? ...