- width
- Try it
- Syntax
- Values
- Accessibility concerns
- Formal definition
- Formal syntax
- Examples
- Default width
- Example using pixels and ems
- Example with percentage
- Example using «max-content»
- Example using «min-content»
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Code Sample for Modifying HTML Window Width
- How to set html elements width to resize upon browser window resize?
- Min width in window resizing
- Use @media in CSS
- To change panel size according to the screen size
- CSS Change Content Based on Screen Size
- Window and viewport width in CSS and Javascript
- Window != Viewport
- So what does that mean for our Javascript?
- Recap
- tl;dr
- Contact me
- CSS Height, Width and Max-width
- CSS Setting height and width
- CSS height and width Values
- CSS height and width Examples
- Example
- Example
- Setting max-width
- Example
- Try it Yourself - Examples
- All CSS Dimension Properties
width
The width CSS property sets an element’s width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.
Try it
The specified value of width applies to the content area so long as its value remains within the values defined by min-width and max-width .
- If the value for width is less than the value for min-width , then min-width overrides width .
- If the value for width is greater than the value for max-width , then max-width overrides width .
Syntax
/* values */ width: 300px; width: 25em; /* value */ width: 75%; /* Keyword values */ width: max-content; width: min-content; width: fit-content(20em); width: auto; /* Global values */ width: inherit; width: initial; width: revert; width: revert-layer; width: unset;
Values
Defines the width as a distance value.
Defines the width as a percentage of the containing block’s width.
The browser will calculate and select a width for the specified element.
The intrinsic preferred width.
The intrinsic minimum width.
Uses the fit-content formula with the available space replaced by the specified argument, i.e. min(max-content, max(min-content, )) .
Accessibility concerns
Ensure that elements set with a width aren’t truncated and/or don’t obscure other content when the page is zoomed to increase text size.
Formal definition
Initial value | auto |
---|---|
Applies to | all elements but non-replaced inline elements, table rows, and row groups |
Inherited | no |
Percentages | refer to the width of the containing block |
Computed value | a percentage or auto or the absolute length |
Animation type | a length, percentage or calc(); |
Formal syntax
width =
auto |
|
min-content |
max-content |
fit-content( )
=
|
Examples
Default width
p class="goldie">The Mozilla community produces a lot of great software.p>
Example using pixels and ems
.px_length width: 200px; background-color: red; color: white; border: 1px solid black; > .em_length width: 20em; background-color: white; color: red; border: 1px solid black; >
div class="px_length">Width measured in pxdiv> div class="em_length">Width measured in emdiv>
Example with percentage
.percent width: 20%; background-color: silver; border: 1px solid red; >
div class="percent">Width in percentagediv>
Example using «max-content»
p.maxgreen background: lightgreen; width: intrinsic; /* Safari/WebKit uses a non-standard name */ width: -moz-max-content; /* Firefox/Gecko */ width: -webkit-max-content; /* Chrome */ width: max-content; >
p class="maxgreen">The Mozilla community produces a lot of great software.p>
Example using «min-content»
p.minblue background: lightblue; width: -moz-min-content; /* Firefox */ width: -webkit-min-content; /* Chrome */ width: min-content; >
p class="minblue">The Mozilla community produces a lot of great software.p>
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Code Sample for Modifying HTML Window Width
To meet the minimum width of 330px and minimum height of 400px, the CSS code can be written using position fixed along with the top, right, and bottom properties. An example of this can be seen in the bootstrap Getting Started guide. Another solution is to set the min-width property for the body tag in the CSS file. Additionally, the class can be changed to achieve the desired result.
How to set html elements width to resize upon browser window resize?
Utilize Bootstrap’s grid classes for your project, as it may be beneficial. Consider the following code snippet: col-md-2 col-sm-6 col-xs-12 .
Take a glance at the link provided: https://getbootstrap.com/docs/3.3/css/#grid
Grid system
Bootstrap features a fluid grid system that is responsive and prioritizes mobile devices. The grid automatically scales up to 12 columns as the size of the viewport or device increases. It offers predefined classes for convenient layout options and comes with robust mixins that can generate more semantic layouts.
| Extra small devices Phones (<768px)| Small devices Tablets (≥768px) |Medium devices Desktops (≥992px) |Large devices Desktops (≥1200px) __________________________________________________________________________________________________________________________________________________________ Class prefix| .col-xs- | .col-sm- | .col-md- | .col-lg- __________________________________________________________________________________________________________________________________________________________
As evidenced by my example, I eliminated all the width and height CSS references present in your code, resulting in its successful execution.
Bootstrap Getting Started
Additionally, I modified the category to container in order to help you accomplish your objective.
Set size of HTML page and browser window, var container= document.getElementById("container"); container.style.height=(window.innerHeight); container.style.width=window.innerWidth; However, the browser window size is larger than the viewable size and horizontal and vertical scroll bars appear. How do I make it so that my HTML page fits into the browser window without a need for scroll Code sampleFeedback!DOCTYPE>
Min width in window resizing
To establish a minimum width for the body tag using CSS, it is possible to set the min-width property. However, considering that this specific property is not compatible with IE6, it may be necessary to use an alternative approach such as:
body < min-width:1000px; // Suppose you want minimum width of 1000px _width: expression( document.body.clientWidth >1000 ? "1000px" : "auto" ); /* sets max-width for IE6 */ >
Your answer lies in giving the containing element a min-width in your CSS. To support IE6, you can utilize the min-width trick.
This will provide a minimum width of 800px in both IE6 and modern browsers.
Use @media in CSS
Examine the CSS at-rule identified as @media .
Utilize @media screen and (max-width: . px) and @media screen and (max-height: . px) to specify the width of your HTML component, ensuring that your page maintains a consistent appearance. If desired, grant users access to the concealed content on your page by implementing overflow:auto .
Although you cannot stop the user from resizing their browser below certain values, rest assured that your web page will always remain above the selected values.
To illustrate, if a minimum width of 330px and a minimum height of 400px are desired, the CSS code may appear as follows:
body < width: 100%; height: 100%; >@media screen and (max-width: 330px) < html< width: 330px; overflow: auto; >> @media screen and (max-height: 400px) < html< height: 400px; overflow: auto; >>
Scaling HTML layout to the screen size, If you mean the actual browser window, Javascript has screen.width and screen.height which return the current resolution; see examples of use. On a particular webpage, you could set a script to run on a timer to check whether these values have changed, and resize the window if they have.
To change panel size according to the screen size
Utilize the properties of position fixed along with top, right, and bottom to achieve the desired outcome.
Css - How do I control the size of html window?, I would like to control the size (width and height) of an html window page. I would like the page to be small like a popup window. I would like the properties in the same html document to control the size of the browser without having to make a javascript link in another page..
CSS Change Content Based on Screen Size
Place the CSS code labeled @media below the CSS code labeled .notice .
.notice < display: none; visibility: hidden; >@media screen and (max-width:900px), screen and (max-height:500px) < .wrapper < display: none !important; >.notice < display: block; visibility: visible; >>
Assuming your HTML looks like this:
viewable screen size is too small You can add the following css.
// Show content for bigger viewpoints @media screen and (min-width:900px), screen and (min-height:500px) < .wrapper.content .wrapper.notice > // Show notice for smaller viewpoints @media screen and (max-width:900px), screen and (max-height:500px) < .wrapper.content .wrapper.notice >
I hope this either proves useful or provides you with some valuable perspective.
Window and viewport width in CSS and Javascript
If you write a CSS media query like (min-width: 769px) you don’t think long about which width is actually meant. It just works. The browser automatically does the right thing. If the viewport is 769px wide or wider the media query returns true and the CSS will be applied.
If you need the same behaviour in Javascript because you might want to initiate a script at a certain breakpoint it gets a little bit trickier if you don’t know what width you are looking for.
Window != Viewport
CSS media queries react to the width of the browser window including the scrollbars. In other words: The viewport, not the window or website (the width of the html element).
On Mac OSX Yosemite in Chrome for example, (min-width: 769px) is true if the html element is 754px wide. That’s because the scrollbar has a width of 15px . Thus the viewport is 769px wide.
So what does that mean for our Javascript?
jQuery’s .width() as well as .outerWidth() will return 754px for the $(window) width. That’s not the width our CSS media query reacts to.
If you want a Javascript to fire at the same time a CSS media query evaluates to true, you should reach for window.innerWidth
This will return 769px . It returns the width of the window including scrollbars.
There is also window.outerWidth which returns the width of the window including scrollbars and other window elements like the window border or the DevTools panel if you have it open and pinned to the side.
Recap
CSS Media Query:
Width of the window including scrollbars
jQuery $(window).width and $(window).outerWidth:
Width of the window without scrollbars
window.outerWidth
Width of the window including scrollbars and other elements like the window border or DevTools
window.innerWidth
Width of the window including scrollbars
tl;dr
CSS Media Query width == window.innerWidth
Hi, I’m Martin Wolf, a Freelance Frontend Web Developer from Germany.
I believe in hard work and sharing what I know.
Contact me
Interested in working with me? Fantastic! The best way to get in touch is by email (hello@martinwolf.org). I’m looking forward to hearing from you.
CSS Height, Width and Max-width
The CSS height and width properties are used to set the height and width of an element.
The CSS max-width property is used to set the maximum width of an element.
CSS Setting height and width
The height and width properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.
CSS height and width Values
The height and width properties may have the following values:
- auto - This is default. The browser calculates the height and width
- length - Defines the height/width in px, cm, etc.
- % - Defines the height/width in percent of the containing block
- initial - Sets the height/width to its default value
- inherit - The height/width will be inherited from its parent value
CSS height and width Examples
Example
Set the height and width of a element:
Example
Set the height and width of another element:
Note: Remember that the height and width properties do not include padding, borders, or margins! They set the height/width of the area inside the padding, border, and margin of the element!
Setting max-width
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).
The problem with the above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!
Note: If you for some reason use both the width property and the max-width property on the same element, and the value of the width property is larger than the max-width property; the max-width property will be used (and the width property will be ignored).
Example
This element has a height of 100 pixels and a max-width of 500 pixels:
Try it Yourself - Examples
Set the height and width of elements
This example demonstrates how to set the height and width of different elements.
Set the height and width of an image using percent
This example demonstrates how to set the height and width of an image using a percent value.
Set min-width and max-width of an element
This example demonstrates how to set a minimum width and a maximum width of an element using a pixel value.
Set min-height and max-height of an element
This example demonstrates how to set a minimum height and a maximum height of an element using a pixel value.
All CSS Dimension Properties
Property Description height Sets the height of an element max-height Sets the maximum height of an element max-width Sets the maximum width of an element min-height Sets the minimum height of an element min-width Sets the minimum width of an element width Sets the width of an element