How does a web browser determine the appropriate element width when the width attribute is set to "auto"?

1) When the width property of a textbox element (<input type=”text” />) is set to inherit, it does not overflow. However, if the width is set to auto, it will overflow due to the browser's calculation of the width.

a) Why doesn't the browser consider that the textbox is inside another element and adjust the width accordingly?

b) What parameters does the browser use to determine the width of a textbox?

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head> 
    <style type="text/css">
       p
       {
         width:60px;
       }
    </style>
 </head>
 <body>
       <p>
         <input type=”text” />
       </p>
 </body>
</html>

2) Are all HTML elements automatically set to have their width value as auto by default?

Thank you

Answer №1

To determine the width for width:auto, you can refer to various rules found here.


When it comes to using inherit:

Any property set to 'inherit' implies that, for a particular element, the property adopts the same computed value as its parent element's property.

Consequently, setting width:inherit means it inherits the computed width from the parent element.


With regards to applying width:auto on inline elements:

If both 'height' and 'width' are computed as 'auto' and the element possesses an intrinsic width, then that intrinsic width will be utilized as the 'width' value.

If 'height' and 'width' are both set as 'auto' and the element has no intrinsic width but holds an intrinsic height and ratio; or if 'width' is 'auto', 'height' has another computed value, and there is an intrinsic ratio, then the 'width' value would be:

(used height) * (intrinsic ratio)

In situations where 'height' and 'width' are both 'auto', and the element features an intrinsic ratio without intrinsic height or width, and the containing block's width doesn't depend on the replaced element's width, then the 'width' value is determined by the constraint equation applicable to block-level, non-replaced elements in normal flow.

Alternatively, if 'width' is 'auto' and the element possesses an intrinsic width, then that intrinsic width gets assigned as the 'width' value.

In other cases where 'width' is 'auto', but none of the above conditions apply, the 'width' value defaults to 300px. If this width exceeds the device's capacity, browsers should use the width of the largest rectangle with a 2:1 ratio that fits the device instead.

Hence, for inline elements, the element's "intrinsic" width is often used, even if it surpasses the parent element's width.

Please note that distinct rules exist for block-level and floating elements, however, <input> is inherently considered an inline element.


width:auto represents the default value

'width'
Value: <length> | <percentage> | auto | inherit
Initial: auto

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

I am facing an issue where the table in my Laravel Vue component is not displaying the data from

Recently, I've been diligently following an instructional series on VUE applications by a highly recommended YouTuber. Every step was meticulously executed until I hit a roadblock out of nowhere. The data from my database refuses to display on the fro ...

Guide to redirecting on click when utilizing an iframe

I needed to prevent the page from reloading before the action was complete by using an iframe. However, I encountered an issue where I needed the page to refresh after the action took place. I attempted two methods to achieve this - refreshing the page and ...

Trouble with CSS submenus: Are your lists not appearing as they should? Potential style override

I am encountering an issue with my dropdown mega menu. I have two sets of list items that should be displayed vertically side by side with bullet points in the dropdown, but only one list is appearing despite coding for both. Additionally, the list elemen ...

When using the background-blend-mode property, transition-duration is not taken into account

Try hovering over the top left h1 element in this code example You'll notice that it smoothly changes the h1 element to a shade of blue in just 2 seconds. However, the transition doesn't affect the blend mode of the backgrounds. Even though the ...

Issue with enabling overflow-y: scroll within a Bootstrap Modal

I've implemented a modal using the Bootstrap library that contains multiple lists populated through Ajax search requests. These lists have a fixed size and utilize the CSS property overflow-y: scroll to accommodate a large number of elements without a ...

The server is throwing a 403 error when trying to submit an AJAX request

I am currently working on a project similar to jsfiddle and encountering an issue during development. Whenever I attempt to make an ajax request with the JS alert() function in a text box, the server is returning a 403 error. Can anyone provide assistance ...

Can the <p> tags be aligned to ensure they are evenly spaced?

Is there a way to transform this https://i.sstatic.net/944F8.png Into this: https://i.sstatic.net/k4Ch8.png I want the second column to be uniform across all categories. Here is how my current code appears: <div class="prod-card-f ...

Nivo Slider's Interactive Image Mapping

I am encountering difficulties in integrating an image map into my nivo slider. The code I am currently using is shown below, and you can access the site by clicking on this link. Do you have any suggestions on how to resolve this issue? <div class=" ...

I'm encountering a Parse error: syntax error, wherein the file unexpectedly reaches its end

I'm currently in the process of creating a personalized search engine that enables users to search using multiple fields. However, an error message stating "syntax error, unexpected end of file" keeps popping up, specifically on line 201 of my code, r ...

Unusual scroll bar movements when using jQuery validation

When I click the Add Dependent link button, I wanted the scroll bar to automatically scroll to the bottom, and it does just that. However, there is a small issue after the postback where the text "Please fix the following problems:" briefly appears, even t ...

Incorrect column alignment in Tailwind CSS

I am working on a straightforward flexbox layout with a two-column grid setup. <div class="flex relative"> <div class="columns-2 gap-4 space-y-4"> <div class="bg-red-500 p-10">1</di ...

Dynamic website featuring fixed background with content transitioning through clicks or scrolls

Seeking a template or tutorial on how to create a website similar to the following examples: The desired website layout would allow users to scroll through content while keeping the background neutral, with the option to click links that transition to new ...

When text is selected by triple clicking or over-selecting, it will create extra lines when pasted

When using React/Typescript, I have a function that shows user input: function displayMessage({ message }: UserMessage) { return ( <div> {message.split('\n').map((line) => ( <Fragment>{line} ...

Setting the dimensions of an HTML - CSS block

I am trying to style a navigation bar using the following CSS code: #nav {} #nav a { position: relative; display: inline-block; color: #F0F0F0; width: 1em; height: 2em; line-height: 0.9em; } #nav a.ic ...

Modify the content of the input field using Bootstrap

I'm looking to prefill an input/textbox in Bootstrap with a specific string as the default value. This isn't about using the placeholder attribute, but actually changing the text that appears in the input so users can select and edit it. For exa ...

Having trouble with applying a class in Gtk3 css?

MY ATTEMPT AND EXPLANATION: In my application, I have the following layout structure: GtkWindow GtkOverlay GtkBox GtkGrid GtkButton Even after trying to apply this CSS style: GtkButton{ background-color:blue; } T ...

Start fresh with your list styling in Sass/CSS

I'm attempting to revert ul and li elements to their default styles using a specific class, but I'm encountering difficulties in doing so. I've experimented with the inherit and initial values, but they haven't proven effective. This i ...

When using jQuery's remove() or after() functions, be aware that they may inadvertently remove

Here is the html code I am working with: <p>This is some random text in a paragraph with a <span class="blue">blue</span> word.</p> <p>This is some random text in a paragraph with a <span class="blue">blue</span> ...

Is it possible to launch a UIWebView from within another UIWebView?

I'm working on a view controller with a web view at the bottom that has clickable links. My goal is to make it so that when a user clicks on a link, a full-screen web view opens up with the content of the page they clicked on. Alternatively, I'd ...

Using HTML and JavaScript allows for the video URL to seamlessly open in the default video player app on the device

Working on my mediaplayer website, I want to give users the option to choose which app to play their uploaded videos with. So far, I've attempted to implement a button that triggers this action: window.open("video.mkv", '_blank'); Howeve ...