How to make a div have a fixed width using CSS?

By IncludeHelp Last updated : November 13, 2023

To make a div having a fixed width using CSS, use width and display properties. Set the width property to the specified width size and display property to inline-block to display the content within the div container.

CSS to make a div have a fixed width

Try this CSS:

div {
  width: 250px;
  display: inline-block;

  background-color: #006969;
  color: #fff;
  padding: 8px;
}

Example

In this example, the given div have a fixed with. Additionally, we have set other properties to make the div stylish.

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
      body{	
      margin: auto;
      max-width: 1024px;
      }
      div{
      width: 250px;
      display: inline-block;	  
      background-color: #006969;
      color: #fff;
      padding: 8px;
      }
    </style>
  </head>
  <body>
    <h1>Example to make a div have a fixed width using CSS</h1>
    <div>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
    </div>
  </body>
</html>

Output

The output of the above example is:

make a div have a fixed width

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.