Html text maximum width

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
Читайте также:  How to check null in java

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

Источник

max-width

The max-width CSS property sets the maximum width of an element. It prevents the used value of the width property from becoming larger than the value specified by max-width .

Try it

max-width overrides width , but min-width overrides max-width .

Syntax

/* value */ max-width: 3.5em; /* value */ max-width: 75%; /* Keyword values */ max-width: none; max-width: max-content; max-width: min-content; max-width: fit-content(20em); /* Global values */ max-width: inherit; max-width: initial; max-width: revert; max-width: revert-layer; max-width: unset; 

Values

Defines the max-width as an absolute value.

Defines the max-width as a percentage of the containing block’s width.

No limit on the size of the box.

The intrinsic preferred max-width .

The intrinsic minimum max-width .

Uses the fit-content formula with the available space replaced by the specified argument, i.e. min(max-content, max(min-content, argument)) .

Accessibility concerns

Ensure that elements set with a max-width are not truncated and/or do not obscure other content when the page is zoomed to increase text size.

Formal definition

Initial value none
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 the percentage as specified or the absolute length or none
Animation type a length, percentage or calc();

Formal syntax

max-width =
none |
|
min-content |
max-content |
fit-content( )

=
|

Examples

Setting max width in pixels

In this example, the «child» will be either 150 pixels wide or the width of the «parent,» whichever is smaller.

HTML

div id="parent"> div id="child"> Fusce pulvinar vestibulum eros, sed luctus ex lobortis quis. div> div> 

CSS

#parent  background: lightblue; width: 300px; > #child  background: gold; width: 100%; max-width: 150px; > 

Result

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.

Источник

CSS max-width Property

The max-width property defines the maximum width of an element.

If the content is larger than the maximum width, it will automatically change the height of the element.

If the content is smaller than the maximum width, the max-width property has no effect.

Note: This prevents the value of the width property from becoming larger than max-width . The value of the max-width property overrides the width property.

Default value: none
Inherited: no
Animatable: yes, see individual properties. Read about animatable Try it
Version: CSS2
JavaScript syntax: object.style.maxWidth=»600px» Try it

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

CSS Syntax

Property Values

Value Description Demo
none No maximum width. This is default Play it »
length Defines the maximum width in px, cm, etc. Read about length units Play it »
% Defines the maximum width in percent of the containing block Play it »
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

More Examples

Example

Set the maximum width of a

element to 50% of the container:

Источник

Html text maximum width

Last updated: Apr 28, 2023
Reading time · 4 min

banner

# How to set Max Character length in CSS

To set max character length in CSS:

  1. Set the max-width CSS property on the element to N ch .
  2. Set the overflow CSS property to hidden .
  3. Optionally set text-overflow to ellipsis to display an ellipsis.
  4. Set white-space to nowrap to collapse the whitespace and suppress line breaks.
Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> p max-width: 15ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; > style> head> body> p>bobbyhadz.comp> p>This is a very long sentence.p> body> html>

set max characters length with ellipsis

The example sets the maximum character length of the p element to ~15 characters.

The ch relative unit represents the advance measure of the glyph 0 in the element’s font.

Copied!
style> p max-width: 15ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; > style>

If it is impossible to determine the measure of the 0 glyph, it is assumed to be 0.5em wide by 1em tall.

We also set the overflow property to hidden .

When overflow is set to hidden , the overflowing content is clipped at the element’s padding box and the clipped content is not visible.

The text-overflow property can be set to ellipsis to display an ellipsis . that represents the clipped text.

If you don’t want to display an ellipsis, remove the property.

Copied!
style> p max-width: 15ch; overflow: hidden; white-space: nowrap; > style>

set max character limit without ellipsis

When the white-space CSS property is set to nowrap :

# Setting the max-width property to N pixels

You can also set the max-width of the element to N pixels to truncate the text.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> p max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; > style> head> body> p>bobbyhadz.comp> p>This is a very long sentence.p> body> html>

set max width to n pixels to truncate text

The example sets the max-width CSS property on all p elements to 100px .

Note that the width of the ellipsis is also included in the 100px .

If you want to remove the ellipsis, you can remove the text-overflow property, however, that wouldn’t look good because a letter might be cut off.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> body margin: 100px; > p max-width: 100px; overflow: hidden; white-space: nowrap; > style> head> body> p>bobbyhadz.comp> p>This is a very long sentence.p> body> html>

set max width in pixels without ellipsis

# How to set a Max Character length in CSS using grid

You can also use CSS grid to set a max character length in CSS.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> .container display: grid; grid-template-columns: 1fr 15ch 1fr; > .paragraph grid-column: 2/3; > style> head> body> div class="container"> p class="paragraph">bobby hadz com abc 123 xyz.p> p class="paragraph">This is a very long sentence.p> div> body> html>

using css grid to set max character length

We wrap the p tags in a div and set the element’s display property to grid.

The grid value enables us to align elements into columns and rows.

The grid-template-columns CSS property can be used to define the track sizing of the grid columns.

In this case, we have 3 columns where the middle column has a maximum width of 15 characters.

We then set the grid-column CSS property on the p elements.

The property specifies the grid item’s size and location within a grid column.

A similar solution would be to simply set the max-width CSS property on the p elements to N characters.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> .paragraph max-width: 10ch; > style> head> body> p class="paragraph">bobby hadz com abc 123 xyz.p> p class="paragraph">This is a very long sentence.p> body> html>

set max width to n characters

N characters are displayed and then the text is wrapped to the next line.

You can also set the overflow-wrap property to anywhere to achieve a similar result.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> .paragraph max-width: 100px; word-break: normal; overflow-wrap: anywhere; > style> head> body> p class="paragraph">bobby hadz com abc 123 xyz.p> p class="paragraph">This is a very long sentence.p> body> html>

set overflow wrap property to anywhere

When the word-break CSS property is set to normal , the default line break rule is used.

When overflow-wrap is set to anywhere , unbreakable string of characters like long words or a URL may be broken at any point if there are no acceptable break points in the line.

Notice that the characters are still broken up nicely.

There is also a break-all value of the word-break property.

The break-all value inserts word breaks between any two characters to prevent overflow.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> style> .paragraph max-width: 100px; word-break: break-all; > style> head> body> p class="paragraph">bobby hadz com abc 123 xyz.p> p class="paragraph">This is a very long sentence.p> body> html>

set word break to break all

However, notice that this makes the text a bit difficult to read.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

  • How to adjust a Button’s width to fit the Text in CSS
  • Change Select Option Background-Color on Hover in CSS/HTML
  • Check if Element is Input or Select dropdown in JavaScript
  • Change a Button’s color onClick using JavaScript
  • Change button text on Click using JavaScript
  • How to Change Text color on Mouseover in JavaScript
  • Add a class to the Clicked Element using JavaScript
  • Check if Element was Clicked using JavaScript
  • Hide element when clicked outside using JavaScript
  • Set min-margin, max-margin, min-padding & max-padding in CSS
  • How to Apply a CSS Hover effect to multiple Elements
  • Changing Bold Text into Normal (Unbold Text) in HTML
  • How to bring an element to the Front using CSS
  • Apply multiple inline CSS styles to an HTML element
  • Force the text in a Div to stay in one Line in HTML & CSS
  • CSS text-align: center; not working issue [Solved]
  • Tailwind CSS classes not working in Vanilla or React project
  • Remove whitespace between inline-block elements using CSS

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью