What is causing this absolute list item to stray away from its parent container?

Why is the ul element taking up the full width of the wrapper element when the container div is its parent? How can this be adjusted?

Here is the HTML structure:

Wrapper
  | container
       | label
       | input
       | ul
       | botton

Here is the HTML & CSS code:

.wrapper{
  margin: 20px auto;
}
.container {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 200px;
  width: 80%;
  margin: auto;
}
  
.country-input {
  height: 30px;
  margin-bottom: 50px;
}
  
ul {
  height: 200px;
  width: 100%;
  overflow: hidden;
  overflow-y: scroll;
  position: absolute;
  background-color: #F2F2F2;
  top: 20px;
  left: 0px;
}

 
<div class="wrapper">
  <div class="container">
    <label for="country">State</label>
    <input class="input country-input" id="country" type="text" />
    <ul id='country-list'>
      <li id="US">United States</li>
      <li id="AF">Afghanistan</li>
      <li id="AX">Åland Islands</li>
      <li id="AL">Albania</li>
    </ul>
    <button>explore</button>
  </div>
</div>

Click here for the complete code

Preview of how it appears:

here

Observing the off-center position of the element on the left:

here

Answer №1

Could it be that the padding in your <ul> tag is causing the issue? Take a look at the snippet below and let me know if this is the desired outcome:

.wrapper{
    margin: 20px auto;
}
.container {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 200px;
    width: 80%;
    margin: auto
  }
  
  .country-input {
    height: 30px;
    margin-bottom: 50px;
  }
  
  ul {
    height: 200px;
    width: 100%;
    overflow: hidden;
    overflow-y: scroll;
    position: absolute;
    background-color: #F2F2F2;
    top: 20px;
    padding: 0;
  }

  li {
    padding-left: 40px;  

  }
<div class="wrapper">
        <div class="container">
            <label for="country">State</label>
            <input class="input country-input" id="country" type="text" />
            <ul id='country-list'>
                <li id="US">United States</li>
                <li id="AF">Afghanistan</li>
                <li id="AX">Åland Islands</li>
                <li id="AL">Albania</li>
            </ul>
            <button>explore</button>
        </div>
    </div>

Answer №2

Another alternative to consider is using select and option elements instead of an unordered list.

For example:

<select id='country-list'>
  <option id="US">United States</option>
  <option id="AF">Afghanistan</option>
  <option id="AX">Åland Islands</option>
  <option id="AL">Albania</option>
</select>

Answer №3

apply the following styles in the ul tag - width: unset, left: 0, and right: 0

.wrapper{
    margin: 20px auto;
}
.container {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 200px;
    width: 80%;
    margin: auto
  }
  
  .country-input {
    height: 30px;
    margin-bottom: 50px;
  }
  
  ul {
    height: 200px;
    width: unset;
    overflow: hidden;
    overflow-y: scroll;
    position: absolute;
    background-color: #F2F2F2;
    top: 20px;
    left: 0;
    right: 0;
}
<div class="wrapper">
        <div class="container">
            <label for="country">Country</label>
            <input class="input country-input" id="country" type="text" />
            <ul id='country-list'>
                <li id="US">United States</li>
                <li id="AF">Afghanistan</li>
                <li id="AX">Åland Islands</li>
                <li id="AL">Albania</li>
            </ul>
            <button>explore</button>
        </div>
    </div>

Answer №4

Can you adjust the style of the unordered list to have padding set to 0px?

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

Modifying JavaScript using JavaScript

I'm looking for a solution to change the URL of a button in sync with the iframe displayed on the screen using JavaScript. The challenge I'm facing is figuring out how to dynamically update the button's URL based on the iframe content. Here ...

Limit selection choices in select element

Similar Question: Prevent select dropdown from opening in FireFox and Opera In my HTML file, I have a select tag that I want to use to open my own table when clicked. However, the window of the Select tag also opens, which is not desirable. Is there a ...

Stop the color buffer from being automatically cleared in WebGL

Removing gl.clearColor(c[0],c[1],c[2],1.0); gl.clear(gl.COLOR_BUFFER_BIT ); does not stop the screen from getting cleared at the start of the next draw cycle. Is there a method to avoid this situation? I am aiming for an overpaint effect. ...

Our search box is showing suggested results but we are unable to access the specific product sku. What steps can we take to resolve this issue?

/* This is a global settings template for CSS. The code includes various styling rules for elements such as article, nav, form, header, and footer among others. There seems to be an issue with the CSS that may be affecting the functionality of a drop-down ...

Utilize the return function in a separate PHP file

I wanted to create a basic login form as a way to dive into learning php. After setting up the form, I wrote a function that sends a query to a MySQL database to fetch the username and password (stored in md5 encryption) for comparison with user input. T ...

Tips for implementing a method to switch CSS properties of a main container by using a checkbox within its child element in a Svelte component

It took me a while to figure this out, but I still feel like my implementation is not ideal. I'm confused as to why things break when I remove the checkedActivities.has(activity) ? "checked" : "unchecked", because I thought TypeScr ...

Expand the <canvas> element to completely fill the flex item without any scrollbars

I am currently utilizing Bootstrap 5 and experimenting with setting up a nested flex layout that occupies the entire window. One of the flex items should be filled by a "stretchy" canvas element, ensuring there are no scrollbars present. However, when I a ...

What is the best way to combine text from various group radio buttons?

Here is the JavaScript code I have written: $(function(){ $('input[type="radio"]').click(function(){ var txt = $(this).parent().text(); var $radio = $(this); if ($radio.data('waschecked') == true) { ...

Right-align each item when selecting none

Is there a way to change the style of just one of the elements select or option, without affecting the style of both? I attempted to align the select element to the right, while leaving the option elements aligned to the left. However, this did not work a ...

Having trouble seeing the output on the webpage after entering the information

I can't seem to figure out why the result is not displaying on the HTML page, so for now I have it set up as an alert. <h1>Factorial Problem</h1> <form name="frm1"> Enter any number :<input type="text" name="fact1"& ...

Examine the spans and delete the first-child node

Basically, this functionality allows the user to cycle through hidden information by clicking on it. Here is how you can structure your HTML: <div id="facts"> <span>click to cycle</span> <span>fact 1</span> <s ...

Guide on transferring the td id value to a different page

document.getElementById('scdiv').innerHTML=Quantity <td id="scdiv"> </td> document.getElementById('quantitydiv').innerHTML=mrp <td id="quantitydiv"> </td> I am currently facing an issue where the code is being d ...

Learn the position of a div element that appears following the lazy loading of images

I am struggling to make a div sticky below a set of lazy load images. The issue lies in the fact that the offset of the div keeps changing due to the lazy loading images pushing it down. There are 4 images with unknown heights, making it difficult to acc ...

Is there a way to customize the color of a MUI styled component?

I have a customized MUI component that displays a circular badge in green. const StyledGreenBadge = styled(Badge)(({ theme }) => ({ '& .MuiBadge-badge': { backgroundColor: '#44b700', color: '#44b700', ...

Adding a luminous glow effect to the <a> tag upon selection

Currently, I am working on integrating a navigation bar into a test site. I have noticed that some navigation bars highlight the current selected page by adding a bar underneath or creating a glowing effect around the <a> tag. I am unsure of the corr ...

There appears to be a CSS problem cropping up on the right side of my website

Kindly pay a visit to the following URL: dev.dgconcepts.com.pk https://i.stack.imgur.com/sBC4Dm.jpg I am puzzled by the fact that whenever we swipe left or right on mobile responsive, the website shifts slightly on both sides. I am unable to identify ...

Run a PHP script on the current page upon choosing an option from a dropdown list with the help of Ajax or JavaScript

I'm currently working on a MySQL query that needs to be executed when a user selects options from multiple dropdown lists. What I am looking for is the ability to automatically run a query related to the selected dropdown list option using AJAX/JavaS ...

Style the nth child of a particular type in CSS with a unique color scheme

Is there a way to select the nth child of type and apply different colors without assigning IDs because they are dynamically generated in a loop from a template engine? I found some useful information on this topic here: https://www.w3schools.com/cssref/ ...

How can we prevent an unstyled list from causing text to drop to the next row?

As I am not an expert developer, I am in the process of creating a basic menu card using bootstrap. To keep it simple, I decided to use an unstyled list and added a span with float: right to ensure that the price is always aligned to the right. However, I ...

This error is thrown when trying to access the property 'message' of an undefined value

I'm currently working on adding an AJAX contact form to my website. However, I've run into a problem where when I try to submit the form, I encounter the following error in Google Chrome: "Uncaught TypeError: Cannot read property 'message&a ...