Issue with CSS percentage-based positioning when applied to an element inside a container is not producing

Apologies for the vague title, but describing this issue is a bit tricky. Essentially, I have a wrapper div that is positioned absolutely with right = 100%, causing it to move completely off the screen to the left. Inside the wrapper, there is a div named "answer" that I want to bring back onto the screen by using left = 100%. Although there are other divs within the wrapper labeled "1," "2," and "3" which I reposition to show on the screen through scripting, this isn't the main concern. Below is the HTML code snippet:

<!DOCTYPE html>

<html>
    <head>
        <title>321 2.0</title>
        <link rel="stylesheet" type="text/css" href="main.css">
    </head>
    <body>
        <div id="wrapper">
            <div id="1"></div>
            <div id="2"></div>
            <div id="3"></div>
            <div id="queue"></div>
            <div id="answer">
                <input id="field" type="text" name="answer">
                <button onclick="answer()">></button>
            </div>
            <script type="text/javascript" src="script.js"></script>
        </div>
    </body>
</html>

And here's the CSS code block:

body {
    margin: 0;
}

div {
    margin: 0;
    width: 100%;
    padding-top: 5px;
    padding-bottom: 5px;
    text-align: center;
}

#wrapper {
    position: absolute;
    right: 100%;
    padding: 0;
}

#1 {
    background-color: #FFCCCC;
}

#2 {
    background-color: #DDDDDD;
}

#3 {
    background-color: #CCCCFF;
}

#queue {
    width: 300px;
    height: 300px;
    position: absolute;
    left: 50%;
    top: 50%; 
    margin-left: -150px;
    margin-top: -150px;
    padding: 0;
}

#answer {
    margin: 0;
    padding: 0;
    left: 100%;
    background-color: #FFCCCC;
}

The outcome results in a blank screen, as expected given that the wrapper is meant to shift everything off to the left (except #queue). However, I anticipate #answer to remain visible on the screen due to its left: 100% property. My assumption is that the issue lies in #answer being relatively positioned rather than absolutely, as the container only moved offscreen once it was set to absolute positioning. I hope this isn't the problem since I require all elements inside the wrapper to maintain relative positioning. Any insights into what might be happening?

Answer №1

left is only valid for elements with relative, fixed, or absolute positioning. The element #answer does not have a specified position, so it defaults to static.

To resolve this, add position: absolute; to #answer:

#answer {
    margin: 0;
    padding: 0;
    position: absolute;
    left: 100%;
    background-color: #FFCCCC;
}

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

Switch between multiple unordered lists (ul) so that when one list is clicked, the other lists reset to their initial state as though they were never

When I click on the first item in the ul list, it should slideToggle() to show its corresponding items. Similarly, when I click on the second item in the ul list, its items should slideToggle(), but the first ul list remains visible as well. What I am tryi ...

Identify which anchor tag from the group with the matching class was selected and retrieve its unique identifier

There are multiple anchor tags with the same class in my code. <a href='' id='id1' class='abc'>Link 1</a> <a href='' id='id2' class='abc'>Link 2</a> <a href='&apos ...

What is the process for linking my HTML page to my CSS stylesheet?

This is similar to what I have in the content of my HTML page. <link rel="stylesheet" type="text/css" href="/untitled2.css"> I thought this was right, but it doesn't seem to be functioning. Any ideas on what might be wrong? ...

Can we dynamically adjust font size based on the width of a div using CSS?

My div is set to 70% width and I have a specific goal in mind: To fill that div with text To adjust the font size so that it takes up the entire width of the div https://i.stack.imgur.com/goVuj.png Would it be possible to achieve this using only CSS? B ...

Tips for incorporating decimal numbering into lists

Is it possible to replicate this layout using HTML and CSS? 1. 1.1 1.2 2. 2.1 2.2 Can I achieve this with the <li> and <ul> tags? ...

Can someone help me uncover the previous URL for login using just JavaScript? I've tried using document.referrer but it's not giving me the

Currently, I am utilizing express with pug templates and pure JavaScript. In order to enhance the user experience of my log in system, I would like to save the URL that someone came to the login page with, so that I can redirect them back to it once they h ...

The type of limitations known as Standard Limits

Is there a way to create a custom D struct that combines the minimum and maximum values of a type and initializes them correctly by default? Here is an example: alias Pair(T) = Tuple!(T, T); struct Limits(T) { /* TODO: Fix purity of this by fixi ...

I'm looking to extract information from a webpage using Python by inputting specific criteria

Looking to gather information from a specific website - Link and save it either in a database or another location. The process involves providing input parameters such as entering the vehicle number, submitting it, then verifying if it matches your car. N ...

In Vue, applying CSS styles to D3's SVG elements may not work as expected when using the scoped attribute

Here is the code snippet of my component: <template> <div id="something" class="card"> </div> </template> const height = 200; const width = 200; let graph = d3 .select('#something') .append('svg') ...

Merge jQuery with the jetpack infinite scroll feature

Utilizing the jQuery code below for an accordion effect on all my WordPress posts on the front end. (function($) { function initAccordion() { var $ele = $('.entry-content').hide(); $(".entry-header").unbind('click&apos ...

Experimenting with combining a CSS class alongside the Nth-child selector

Hello, I've been researching various sources including Stackoverflow on how to effectively use an Nth child selector and a Class together but have not had much success so far. In essence, my menu consists of Main categories (class = cat) and subcateg ...

Update password after initial login success

In the process of developing a web application with existing Auth features in Django, I have encountered a need for enhanced security measures. The system involves an admin creating user profiles with usernames and passwords, which are then provided to use ...

Adding text to an existing div element using jQuery and ensuring the div tag automatically adjusts its height according to the text length

I have attempted to implement the following code, but unfortunately, it is not functioning as expected. I am hopeful that someone can assist me. I am seeking a way for the div tag to dynamically increase its height based on the size of the text. Does any ...

Simulating HTML5 Drag and Drop with Selenium Webdriver using JavaScript

Can someone help me convert the Python code provided below into a Javascript (nodejs) version using WebdriverIO? Here is the Python code: from selenium import webdriver with open("drag_and_drop_helper.js") as f: js = f.read() driver.execute_script(js + ...

Class for Bootstrap CSS to adjust card image when resized

I'm stumped, I can't seem to find the right CSS element to trigger the transition from displaying the image on the right to below the text. It's a simple change that I didn't anticipate, but I kind of like it. In horizontal view, I wan ...

Converting HTML to .txt in C: A step-by-step guide

Looking for assistance with converting HTML to .txt format using C programming language. For example, the program needs to identify each of the following tags: 1. <p> 2. <tr> 3. <ul> and so on... The goal is to convert these tags into ...

Is it possible to protect passwords internally via URL and AJAX?

During my time at a previous company, we had an internal website that required a password to be entered at the end of the URL in order to view it. I suspect this was done using AJAX, but I am unsure. Even if AJAX was used, I do not know how to code it myse ...

What is the best way to adjust the square size using CSS on multiple divs with the same class?

Is it possible to adjust the image size of the second div in CSS while keeping only the square shape intact and using the same class for different divs? <div class="square"> <img class="square__img" src="square.png" ...

The PNG image that is stored in the MySQL database is not displaying completely on the web browser

Here are the PHP codes from index.php <?php include("connection.php"); $sql = "SELECT prod_cost, prod_name, prod_image FROM products"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_all($result, MYSQLI_ASSOC ...

Issue with horizontal elements not functioning properly while using cdkDragPreview and cdkDragPlaceholder

My goal is to have my elements laid out horizontally instead of vertically, so I'm using display-inline. However, I've encountered some unusual behavior with the CSS that's causing two specific issues. When I click and drag an element, it ...