Setting a Div's Height Relative to Page Height

I have been following a tutorial on adjusting the height of my div container based on page or screen resolution. Even though it seems simple, I am having trouble getting it to work on my own page.

When I hover over the container div in the page inspect tool, it shows dimensions as 1066.39 x 18.

Here is the source code of my page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .container {
            width:80%;
            height:50%;
            background-color:green;
        }
        html,body {
            height:100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            adsf
        </div>
    </form>
</body>
</html>

I would appreciate any help with this issue.

-------- THE SOURCE CODE OF MY ACTUAL PAGE THAT IS NOT WORKING AS EXPECTED ---

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="CostParameter2.aspx.cs" Inherits="JobCosting.CostParameter2" %>

<%@ Register Src="~/footerControl.ascx" TagPrefix="uc1" TagName="footerControl" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <style type="text/css">
        html,
        body,
        form {
          height: 100%;
        }
        .containermain {
          width: 100%;
          height: 100%;
          background-color: green;
        }
    </style>

    <!-- page content -->
    <div class="containermain">
    <div class="right_col" role="main">
        <div class="row">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                [...complex grid and form elements continue...]
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>

        <div class="clearfix"></div>
        <div class="right_col myright_col_footer" role="main">
            <uc1:footerControl runat="server" ID="footerControl" />
        </div>


    </div>
    <!-- /page content -->
    </div>


</asp:Content>

Answer №1

To successfully implement this, you need to utilize the selector #form1:

html,body {
  height:100%;
}

#form1
{
  width:80%;
  height:80%;
  background-color:green;
}

Explore the code on JSFiddle

Answer №2

It is important for all parent elements to have a specified height. Without a set height for your form element, the browser cannot accurately calculate 80% of an unknown height. Be sure to define a height for the form:

html,
body,
form {
  height: 100%;
}
.container {
  width: 80%;
  height: 80%;
  background-color: green;
}

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

A guide on updating a boolean field in the database using ajax

Here is a piece of my html code: <form action="" method="post"> {% csrf_token %} {% if donate.is_taken == False %} <br> <button type="submit" class="btn" name="taken_or_not" ...

Customizing the date-select widths: A step-by-step guide

I've been struggling with adjusting the width of different instances of this line: <%= f.date_select :deadline, :order => [:month, :day, :year], class: 'date-pick', id: 'goal' %> Despite creating unique ids for select, d ...

Error: JSON.Net has detected a self-referencing loop in the data

I have encountered an issue with sending a List of Route objects to my View in ASP.Net MVC. The code snippet I am using for this purpose is as follows: public ActionResult getRouteFromPart(int partId) { List<Route> routes = _routeService.GetRout ...

Navigate the page by scrolling the absolute positioned div

Is it possible to make the fancybox modal scroll with the page using fancybox 2? I want it to move along with the content rather than being fixed in the center with a max-height restriction. How can I achieve this? $('.fancybox-open').fancybox({ ...

What is the best way to create a stylish outward curve effect for an active sidebar item using just CSS

After spending a week learning frontend designs, I attempted to replicate a design from Dribble. However, I've been struggling with recreating the active style on the sidebar due to the outward curve. If anyone could provide guidance on achieving thi ...

What is the best way to determine if a radio button has been chosen, and proceed to the next radio button to display varied information?

The goal is to display the <div class="resp"> below each radio button when it is selected, with different content in each <div class="resp">. The previously selected <div class="resp"> should be hidden when a new radio button is chosen. O ...

Sending information from the Controller to a JavaScript function

Within my controller, the following code is present: public async Task<IActionResult> Index() { HttpResponseMessage responseMessage = await _httpClient.GetAsync(_getDataControlerApiUrl + "/getUserData"); string stringDat ...

Development of multiple stores

At my current company, we are utilizing ZNode, a third-party software for managing multiple store fronts (portals). Our web team has set up different headers, footers, and navigation for each portal. The URLs for each store are distinct in our development ...

How to dynamically style NavLink using a variable in react-router-dom with Emotion?

Is there a way to extract styles to a variable when using react-router-dom's NavLink activeStyle prop? I want to avoid repeating the same styles without using a css file. Ideally, I would prefer to achieve this using Emotion, but if that's not f ...

Attempting to increase the size of the menu text upon hover action

It seems like a basic mistake on my part, but I just can't seem to make it work. What I'm trying to achieve is that when you hover over a specific item in the menu, the text within that item should increase in size. However, currently it only en ...

Controlling the HTML5 dialog element within a React application

Struggling to implement a basic configuration dialog with a close icon in the top-right corner using React. In other frameworks, it's easy to show/hide a dialog with .showModal()/close(), but manipulating the DOM directly in React is not recommended. ...

How can I extract the Customer's company name using the Reporting Service Expression?

I am a beginner in Reporting Services, and I'm working with the following class: class Customer { public Company Company { get; set; } } The Company class includes a Name property. My report's datasource is a List. How can I d ...

Using jQuery .each() within a PHP foreach loop: A guide

In my code, I have a foreach loop that iterates through an array of images, along with some JavaScript logic to add different classes based on whether the width is greater than or less than the height. The issue I'm facing is that the if function onl ...

Attempting to disrupt the PHP script responsible for handling the form data sent via the $_POST method

I have been searching for specific information on this issue. My HTML5 form... outputs to an external PHP script saves the form data as $_SESSION variables in PHP transfers the variables to another page for display I haven't yet escaped any of the ...

Retrieving text content from an HTML tag using C#

I'm working with a variable that contains the following tag. My goal is to extract the values of type and id into separate variables using C#. What is the most effective approach to accomplish this task? <a href="gana:$type=FlexiPage;id=c828c4ea- ...

Develop an XML document that includes CSS and DTD seamlessly incorporated

Could someone please provide me with a short code example of an XML file that includes both a DTD and CSS styles all in one file? Just need one element as an example. I'm new to XML and can't seem to find any examples with both XML and CSS comb ...

Tips for invoking a function using ng-model together with the ng-model value

Within a div element, I have a text field where I am using ng-model to capture the value. I need to trigger a function for validation when a date is entered in the text field. How can I achieve this while still utilizing ng-model? Any suggestions on how to ...

Is there a way to showcase CSS styling for elements that are "siblings" when hovering over an item without affecting the item being hovered on?

When hovering over one of the 5 text bits, I want to apply CSS to all bits except the one being hovered over. Can you achieve this using jQuery and the .siblings() attribute? Here is the HTML: <table width="1138" height="38" border="0" class="Home111 ...

Activate a specific component of hover effect within multiple components generated by the `v-for` loop

Hey there! I'm currently facing a CSS challenge related to Vue. Let's say I want to display multiple components using v-for, and I want to add a hover effect to each component individually. The goal is to have the hover effect only apply to the c ...

Strategies for restricting user input within a datalist

I have a project in which I am creating a webpage that displays a list of flights that can be filtered by destination, origin, price, and more. To achieve this, I have categorized each flight within a div element using its specific properties as classes. F ...