A form field leaves the container element, positioned to the far right using the `float: right;` CSS property

This is the content within the <body> tag.

<div id="border">
    <p><h1 style="text-align: center;">Welcome to XYZ Airline!</h1></p>
        <table caption="Select your preferred seat"
            style="margin-bottom: 1.5em; margin-left: auto; margin-right: auto;">
            <tr>
                <td><p id="dd">Window Seat<input type="radio" name="side"/></p></td>
                <td><p id="dd">Front of plane<input type="radio" name="frontorback"/></p></td>
            </tr>
            <tr>
                <td><p id="dd">Aisle Seat<input type="radio" name="side"/></p></td>
                <td><p id="dd">Middle of plane<input type="radio" name="frontorback"/></p></td>
            </tr>
            <tr>
                <td><p id="dd">Center Seat<input type="radio" name="side"/></p></td>
                <td><p id="dd">Back of plane<input type="radio" name="frontorback"/></p></td>
            </tr>
        </table>
    <p><input type="submit" value="continue" style="float: right;"></p>
</div>

Here are some defined styles:

#border{
    border: 2px solid black;
    border-radius: 25px;
    width: 50%;
    margin: auto;
    background-color: #ffc;
}
#dd{
    margin: 0.2em 0.2em 0.2em 0.2em;
}

When tested on Chrome in macOS, the output looks like this.

https://i.sstatic.net/urV3q.png

The button is aligned to the right as intended but extends beyond the div block. When style="float: right;" is removed, it aligns within the border instead of being on the right side.

https://i.sstatic.net/CfndU.png

To keep the button inside the border, what steps should be taken?

Answer №1

Applying overflow: hidden will resolve the issue caused by clearfix. Here is the recommended fix:

#border {
  overflow: hidden;
  padding-bottom: 10px;
}

An additional bottom padding has been added to ensure the entire button is displayed without being cut off. You can also consider adding a margin-right to move the button outside of the border.

Snippet

#border {
  border: 2px solid black;
  border-radius: 25px;
  width: 50%;
  margin: auto;
  background-color: #ffc;
  overflow: hidden;
  padding-bottom: 10px;
}
#dd {
  margin: 0.2em 0.2em 0.2em 0.2em;
}
<div id="border">
  <p>
    <h1 style="text-align: center;">Welcome to ABC Airline!</h1>
  </p>
  <table caption="Choose your seat preference" style="margin-bottom: 1.5em; margin-left: auto; margin-right: auto;">
    <tr>
      <td>
        <p id="dd">Window Side
          <input type="radio" name="side" />
        </p>
      </td>
      <td>
        <p id="dd">Front of plane
          <input type="radio" name="frontorback" />
        </p>
      </td>
    </tr>
    <tr>
      <td>
        <p id="dd">Aisle Side
          <input type="radio" name="side" />
        </p>
      </td>
      <td>
        <p id="dd">Middle of plane
          <input type="radio" name="frontorback" />
        </p>
      </td>
    </tr>
    <tr>
      <td>
        <p id="dd">Center Seat
          <input type="radio" name="side" />
        </p>
      </td>
      <td>
        <p id="dd">Back of plane
          <input type="radio" name="frontorback" />
        </p>
      </td>
    </tr>
  </table>
  <p>
    <input type="submit" value="next" style="float: right;">
  </p>
</div>

Preview

https://i.sstatic.net/nLFUz.png

Answer №2

To resolve this issue, you can utilize the CSS property overflow auto to establish a Block Formatting Context (BFC), as demonstrated below:

overflow: auto;

You can find more information on this topic at: Why does overflow: hidden have the unexpected side-effect of growing in height to contain floated elements?

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

"How can I open a DOCX file in a new window using JavaScript and then close the window

When attempting to open a doc/docx file in Word from an HTML link, my goal is to prevent the opening of a new browser window. First attempt: mywin=window.open("filename.docx","viewer"); The first attempt works fine, but it results in opening a new "view ...

Ways to retrieve the child number using JavaScript or PHP

Is there a way to retrieve the child number upon clicking? View screenshot For example, when I click on the X button, I want to remove that specific element. However, this action should only apply to item n2. In order to achieve this, I need to determine ...

iOS devices experiencing issues with fixed positioning

Currently, I am in the process of implementing a few instances of , but I am encountering some issues with the CSS. When viewing the website on an iPad or iPhone, the calendar is positioned correctly as the container covers the window like a fixed position ...

Having issues with floats in Bootstrap?

I'm facing an issue trying to float two elements within a Bootstrap navigation bar, but for some reason it's not working. .cls1 { float: left !important; } .cls2 { float: right !important; } <link href="https://maxcdn.bootstra ...

Modify the transparency of a form's backdrop without affecting the input fields

I'm attempting to achieve a similar effect as shown in this example on my website form. I've tried using opacity to make the form field background only 20% visible, but it ends up affecting the text inside and when typing. Is there a way to apply ...

Positioning of the navigation menu

After customizing my Bootstrap navbar, I noticed that when the responsive size changes to mobile, the burger menu opens on the left side. Is there a way to move it to the right? Any help would be appreciated. Here is the HTML code: <!DOCTYPE html> ...

Ways to automatically trigger the opening of a Bootstrap accordion without manual

On my website, I have an "About Us" page featuring 3 bootstrap accordions - "Stories", and "Testimonials". My goal is to make it so that when a user clicks on the "FAQ" link in the navigation menu, they are directed to the "About Us" page with the "Testim ...

What is the best way to ensure that `col-sm-4` takes up the full space previously occupied by `col-sm-5` when it is hidden

In tablet mode, I am concealing my col-sm-5 using the class hidden-sm. <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-5"> </div> <div class="col-sm-3"> </div> </div& ...

What is the functionality behind a free hosting website?

Is anyone familiar with websites like Hostinghood, where users can create a subdomain and upload HTML, CSS, etc.? I'm curious about how they operate and how I can create a similar site. This is my first question here, so please respond instead of disl ...

The image fails to load once the AI-generated image is complete and the response process has concluded

After a long hiatus from development, I am diving back in and acknowledging the rustiness of my skills. Any syntax or formatting errors encountered are preemptively apologized for. Currently, I am immersed in a small web app project that involves taking a ...

Display an image in HTML, followed by a brief pause, and then showcase a second image

I am attempting to create a functionality where when a user checks off a radio box, an image displays first, followed by an swf file after 10 seconds. I have successfully implemented the image display. However, my attempt at using PHP sleep function within ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...

Unable to adjust font weight as intended

I'm encountering an issue with getting the lighter font weights to display correctly. Despite having all the necessary font weights installed on my computer, it seems that many fonts do not support the lighter options. What could be causing this incon ...

When a PHP if statement is triggered by reading a text file, it activates a checked

I am working on a PHP project that involves an on-off button and a txt file. The goal is to have the button disabled (with 'unchecked' echoed) when the value in the txt file is true, and enabled (with 'checked' echoed) when the value is ...

After refreshing in FireFox, the "disabled" attribute of the button fails to function

On my webpage, there are 2 buttons - one enabled by default and the other disabled. When I click on an enabled button, their states switch - the enabled button becomes disabled and vice versa. This functionality works perfectly in Chrome, Edge, and IE11, b ...

Tips for altering value by selecting radio buttons

Currently, I am in the process of developing a website using Adobe Dreamweaver with php. The site includes three buttons that serve as payment method options and act as continue buttons. My goal is to have the selected radio button (I agree button) add a s ...

Removing a outside div element based on a dropdown change in ASP.NET jQuery

this is the structure of my unique div <div class="row-fluid"> <div class="span12"> <div style="overflow: auto; width: 90%;" class="goleft"> <div style="display: inline-block; width: 200px;"> ...

Utilizing ng-click within ng-repeat along with $index

I'm experimenting with using ng-click on a div that is part of an ng-repeat loop, utilizing $index as seen in the code below : HTML: <div class="elementContainer" ng-repeat="element in elements" ng-click="elementUrl($index)"> <h2>{{ ...

Adjusting canvas size to match content dimensions

I posed a similar inquiry and it was categorized as a duplicate even though the suggested duplicate did not provide an answer to my question. Edit: The purported duplicate was [stackoverflow.com/questions/17211920/make-canvas-height-auto][1] I am utilizi ...

Utilizing loops for data selection and presentation within a div using MySQL and PHP

I currently have a large table in my MySql database containing numerous rows of records (referred to as the existingbankproducts table): https://i.sstatic.net/jQzd3.png Below is the code I am using to select data from the database: $stmt2 = $DB_con-> ...