Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

Defination:

The max-width property in CSS is used to set the maximum width of an element. It prevents the element from becoming wider than the specified value, even if the content inside the element could make it larger.

Syntax:

selector {
max-width: value;
}

Example:

Let’s say you want to create a container with a maximum width to ensure that your content doesn’t stretch too wide on larger screens. Here’s an example:

<!DOCTYPE html>
<html lang=”en”>
<head>
  <meta charset=”UTF-8″>
  <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
  <style>
    .container {
      max-width: 800px; /* Set the maximum width of the container to 800 pixels */
      margin: 0 auto; /* Center the container by setting left and right margins to ‘auto’ */
      padding: 20px; /* Add some padding for better visual appeal */
      background-color: #f4f4f4; /* Background color for better visibility */
    }
    p {
      line-height: 1.5; /* Set line height for better readability */
    }
  </style>
  <title>CSS max-width Property Example</title>
</head>
<body>
  <div class=”container”>
    <h2>Max-Width Example</h2>
    <p>
      This is a container with a maximum width of 800 pixels.
      The content inside won’t stretch beyond this width, ensuring a better reading experience on larger screens.
    </p>
  </div>
</body>
</html>

In this example:

  • The .container class has a max-width property set to 800px.
  • Content inside the container won’t stretch beyond this specified width.
  • margin: 0 auto; is used to center the container horizontally.

Adjust the max-width value according to your design preferences and the requirements of your layout. The max-width property is useful for creating responsive designs that adapt to different screen sizes while ensuring a reasonable maximum width for readability and aesthetics.

Output:

In large Screen:

In Small Screen: