A div without any content and styled with a percentage height will appear as invisible

I'm working on creating a responsive background image for a room, where I want the wall to take up 2/3 of the vertical height and the carpet area to cover the remaining 1/3.

Here are snippets of the code I've been using - my issue is that I am trying to control the heights of the divs using percentages instead of fixed pixel values.

<style>

#room {
    height:100%;
    width:100%;
    display:block;
}

#wall_bg_1 {
    background-image:url(./walls/wall_1.png);
    background-repeat:repeat-x;
    height:66%;
    display:block;
}

#carpet_bg_1 {
    background-image:url(.carpets/carpet_1.png);
    background-repeat:repeat-x;
    height:66%;
    display:block;
}
</style>


<body>
        <div id="room">
            <div id="wall_bg_1">&nbsp;</div>
            <div id="carpet_bg_1">&nbsp;</div>         
        </div>

</body>
</html>

Can the heights of the divs be controlled in percentages? Thank you for your assistance!

Answer №1

Include the following code snippet in your CSS file:

body {
    margin: 0;
    padding: 0;
}

Answer №2

html, body {
    width: 100%;
    height: 100%;
}


#room {
    height:100%;
    width:100%;
    display:block;
}

#wall_bg_1 {
    background-image:url(http://www.placehold.it/30x30);
    background-repeat:repeat;
    height:66%;
    display:block;
}

#carpet_bg_1 {
    background-image:url(http://www.placehold.it/10x10);
    background-repeat:repeat;
    height:33%;
    display:block;
}
<div id="room">
            <div id="wall_bg_1">&nbsp;</div>
            <div id="carpet_bg_1">&nbsp;</div>         
        </div>

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

Ways to obtain data in JavaScript function from HTML

Struggling to pass the key from a database query in HTML using PHP to a JavaScript function? You're not alone. The challenge lies in passing the field_id to the updateRecords() JS function through the onClick() event of an HTML button. Previously, se ...

Utilizing Paragraph Styles Within a CSS Stylesheet

I'm attempting to create two unique styles for a pair of paragraphs in an xhtml document by utilizing a CSS style sheet. Despite my efforts and extensive search, I have yet to find a clear solution. ...

Ways to deactivate a text area or mat-form-field

I need assistance with disabling a form field using Angular (4) + Angular Material and Reactive Forms. I have tried searching for syntax options like disabled="true", but haven't found the correct one yet. Can you please provide me with the right synt ...

The HTML code displays the changes in output after resizing the screen

Currently, I am utilizing canvas(graph) within my Angular JS UI5 application. My main goal is to increase the width of the graph. However, whenever I attempt to adjust the size of the Graph, it remains unchanged. Interestingly, when I shrink the screen, th ...

Unexpected behavior observed with Mui theme breakpoints

I have defined breakpoints for my MUI React-based app like so export const lighttheme = createTheme({ palette: palette, typography: typography, breakpoints: { values: { xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536, ...

"Is there a way to adjust the range slider to display currency instead of

I stumbled upon this amazing slider on codepen. Can someone guide me on how to adjust it to display a range from €500 to €6000 while keeping the vibrant red background? I've attempted various solutions like: <input id = "range" type = "range ...

How can I access an InputStream from a local XML file in a PhoneGap application?

Looking for advice on how to fetch an inputstream from a local XML file using JavaScript in my PhoneGap application. I'm new to JavaScript, so any guidance would be appreciated! ...

Tips for executing a Python function from JavaScript, receiving input from an HTML text box

Currently, I am facing an issue with passing input from an HTML text box to a JavaScript variable. Once the input is stored in the JavaScript variable, it needs to be passed to a Python function for execution. Can someone provide assistance with this pro ...

Create copies of Div elements for them to revert back to their original state following

It seems like a simple task, but I'm struggling to figure out how to do it. On a single page, I have multiple divs: <div id="playerid-1"><a href="javascript:loadplayer(1);">[LOAD PLAYER]</a></div> <div id="playerid-2">& ...

What is the best method for adding a line break in the body of an email when we include the recipient, subject, and message?

Do you have a requirement where information needs to be sent to an end user via Gmail (preferred) and they need to click on a button in the email body to respond? The button response is set up with predefined to, subject, and body fields, but there are dif ...

What is the best option: using a Javascript template or exploring another

Just starting out in web development. I want to include the same menu on every page of my new website, but I don't want to manually update it in each file whenever there's a change. Is there an easy template engine for JavaScript or another sol ...

After logging in, I am unable to redirect to another PHP page as the login form simply reloads on the same page

Lately, I've encountered an issue that has persisted for a few days. The login validation is functioning flawlessly. However, the problem arises when attempting to redirect to another page, specifically 'index.php', after logging in. Instead ...

Click to switch divs

I am struggling and in need of help. I have been trying to rotate divs on click using jQuery, which was successful until I had two sets of divs that needed to be rotated independently on the same page. When clicking on the arrows to rotate the content in t ...

Using identical CSS for two separate layouts

Imagine having to work with an html page that cannot be altered in any way. The only tool at your disposal is CSS. The page is loaded and shown through an iframe from a different domain. This particular page serves as a payment gateway, displaying either ...

What steps should be followed to effectively incorporate Google Fonts into a Material UI custom theme for typography in a React/TypeScript project

Hey there, I'm currently working on incorporating Google fonts into a custom Material UI theme as the global font. However, I'm facing an issue where the font-weight setting is not being applied. It seems to only display the normal weight of 400. ...

Activate a button only when a value is inputted into a text box associated with a chosen radio button

I'm facing a challenge with my radio buttons and sub-options. When a user selects an option, the corresponding sub-options should be displayed. Additionally, I want to enable the next button only when text is entered in all sub-option text boxes for t ...

Choosing a value from a dropdown automatically based on the selection of a specific value from another dropdown

Currently, I am working on a project that involves selecting a value from a dropdown menu based on the selection from another dropdown menu. var gender1 = document.querySelector("#gender1"); var gender2 = document.querySelector("#gender2"); gender1.add ...

Accessing the facebox feature within a dropdown menu

Looking for assistance in creating a function to open a facebox when an option from a drop down list is selected. Here is what I have so far: <select><option value="www.google.com/" id="xxx"></option></select> In the header sectio ...

Change the code from a for-loop to use the Array#map method

I have been working on a simple JavaScript function and attempting to convert the code from using a for-loop to Array#map. I'm sharing my code in the following fiddle as I am currently learning about array map: http://jsfiddle.net/newtdms2/. function ...

Ways to capture all characters (including special characters like ) using Visual Studio Regex

What I'm Hoping to Achieve: I am looking to reformat multiple HTML files that have a similar structure in Visual Studio. While the HTML sections may vary in length, they all begin with a <div id="some_id"> tag and do not contain any other <d ...