skip to Main Content

On the right side of the box, theres no margin.I get that its suppose to cover the entire horizontal width of the screen, but then why can we apply the margin on the left side of container?

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>

  <div class="container-fluid" style="background-color:pink;padding:15%; margin:15%;">
    <h1>My First Bootstrap Page</h1>
    <p>This part is inside a .container-fluid class.</p>
    <p>The .container-fluid class provides a full width container, spanning the entire width of the viewport.</p>
  </div>

</body>

</html>

Also I know there is class called container, but it jumps from a specific width to another width when i reduce the viewport, i was wondering if there a way to make sure it goes smoothly like container-fluid but with the added margins.

2

Answers


  1. use the right syntax with row and col

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      
    <div class="container-fluid" >
    <div class="row" style="background-color:pink;padding:15%; margin:15%;">
    <div class="col">
    <h1>My First Bootstrap Page</h1>
      <p>This part is inside a .container-fluid class.</p>
      <p>The .container-fluid class provides a full width container, spanning the entire width of the viewport.</p>           
    </div>
    </div>
      
    </div>
    
    </body>
    </html>

    Check this for spacing in bootstrap:

    Spacing, How it works

    Login or Signup to reply.
  2. You can do it without bootstrap pure css

    Create a wrapper div

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Without Bootstrap</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    <div class="wrapper" style="background-color:pink;padding:15%; margin:15%;">
      <h1>My First Bootstrap Page</h1>
      <p>This part is inside a .container-fluid class.</p>
      <p>The .container-fluid class provides a full width container, spanning the entire width of the viewport.</p>           
    </div>
    </body>
    </html>

    Flex without container

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      
    <div class="d-flex p-5 m-5" style="background:pink;">
    
    <h1>My First Bootstrap Page</h1>
      <p>This part is inside a .container-fluid class.</p>
      <p>The .container-fluid class provides a full width container, spanning the entire width of the viewport.</p>           
    
      
    </div>
    
    </body>
    </html>

    Container with flex, center vertically and horizontally and a div

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      
    <div class="container d-flex align-items-center justify-content-center">
    
    <div class="TextOutput" style="background:pink; margin:15%;padding:15%;">
    <h1>My First Bootstrap Page</h1>
      <p>This part is inside a .container-fluid class.</p>
      <p>The .container-fluid class provides a full width container, spanning the entire width of the viewport.</p>           
    </div>
      
    </div>
    
    </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search