Using Bootstrap to shift columns away from the center

I am new to using Bootstrap and facing some issues with aligning my content centrally.

My goal is to constrain the content width while allowing the background colors to extend across the full width of the page. I have attempted to use offset columns, but it resulted in uneven spacing on either side of the content.

Here is my current markup:

Visit this link for code

<div class="container-fluid">
    <div class="row">

        <div id="example-col-1" class="col-lg-5">
            <div class="row">
                <div class="col-lg-8 offset-md-4">
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vel aliquet ex. In hac habitasse platea dictumst. Phasellus feugiat tincidunt turpis et sodales.</p>
                </div>            
            </div>
        </div>

        <div id="example-col-2" class="col-lg-7">
            <div class="row">
                <div class="col-lg-8">
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vel aliquet ex. In hac habitasse platea dictumst. Phasellus feugiat tincidunt turpis et sodales.</p>
                </div>
            </div>
        </div>

    </div>
</div>

Answer №1

  1. Utilize ml-auto for the left inner column and mr-auto for the right inner column.

#example-col-1 {
  background-color: beige;
}

#example-col-2 {
  background-color: pink;
  text-align: right;
}
<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>

<body>

  <div class="container-fluid">
    <div class="row">
      <div id="example-col-1" class="col">
        <div class="row">
          <div class="col-lg-8 ml-auto">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis lectus magna. Proin efficitur, mauris vitae pharetra bibendum, orci augue aliquam ante, non commodo purus nisi vel mi.</p>
          </div>
        </div>
      </div>

      <div id="example-col-2" class="col">
        <div class="row">
          <div class="col-lg-8 mr-auto">
            <p>Sed vulputate dapibus tristique. Vivamus gravida tortor nec neque condimentum, ut suscipit nulla lobortis. In hac habitasse platea dictumst. Sed eleifend ultrices odio in egestas.</p>
          </div>
        </div>
      </div>

    </div>
  </div>
</body>

</html>

https://codepen.io/anon/pen/BVpWwo

  1. Add a dummy col-lg-4 column before the first inner column and after the second inner column.

#example-col-1 {
  background-color: beige;
}

#example-col-2 {
  background-color: pink;
  text-align: right;
}
<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>

<body>

  <div class="container-fluid">
    <div class="row">
      <div id="example-col-1" class="col">
        <div class="row">
          <div class="col-lg-4"></div>
          <div class="col-lg-8">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis lectus magna. Proin efficitur, mauris vitae pharetra bibendum, orci augue aliquam ante, non commodo purus nisi vel mi.</p>
          </div>
        </div>
      </div>

      <div id="example-col-2" class="col">
        <div class="row">
          <div class="col-lg-8">
            <p>Sed vulputate dapibus tristique. Vivamus gravida tortor nec neque condimentum, ut suscipit nulla lobortis. In hac habitasse platea dictumst. Sed eleifend ultrices odio in egestas.</p>
          </div>
          <div class="col-lg-4"></div>
        </div>
      </div>

    </div>
  </div>
</body>

</html>

https://codepen.io/anon/pen/jKyBwy


This question may also be helpful.

Troubleshooting issues with column offsetting (Bootstrap v4.0.0-beta)


Update

If you prefer a smaller left column and larger right column layout, consider using the following code.

#example-col-1 {
  background-color: beige;
}

#example-col-2 {
  background-color: pink;
}
<html>
  
  <head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  </head>
  
  <body>

<div class="container-fluid">
  <div class="row">
    <div id="example-col-1" class="col-5">
      <div class="row">
        <div class="col-lg-4"></div>
        <div class="col-lg-8">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis lectus magna. Proin efficitur, mauris vitae pharetra bibendum, orci augue aliquam ante, non commodo purus nisi vel mi. Vestibulum varius pretium ipsum imperdiet condimentum. Curabitur tincidunt maximus auctor. Donec vulputate mollis ligula, a pellentesque tortor tincidunt non.</p>
        </div>;            
      </div>
    </div>;
    
    <div id="example-col-2" class="col-7">
      <div class="row">
        <div class="col-lg-9 text-right">
          <p>Phasellus eu vestibulum quam. In hac habitasse platea dictumst. Vestibulum efficitur dui in ultricies placerat. Etiam nec ipsum ac lectus finibus malesuada. Integer eget lacus nec ex venenatis consequat. Pellentesque interdum lectus metus.</p>
        </div>
        <div class="col-lg-3"></div>
      </div>
    </div>;

  </div>;
</div>;
  </body>;
</html>;

Additionally, it's preferred to use text-right over inline styling for compatibility purposes.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn the method to animate changing the background-color of an element using a click function

Can you create an animation for changing the background color when clicking on an element? Most examples I've found use mouseover/hover functions, but none with a click function. The following line changes the background color without any animation: ...

What is the best way to center this image horizontally within its variable-sized parent container?

I am currently working on a webpage that has a responsive design. The page features a list of products, with the product listing adjusting in size as the page width changes. My goal is to center the product image within its container, ensuring that the i ...

Using Angular to pass parameters to a function

When passing a parameter to the getActiveId function in the ng-click <span class="col ion-ios7-heart-outline center" ng- click="getActiveId({{activeSlide.selected}})"> The value turns to zero in the controller, but it is passed correctly after tes ...

Tips for creating proper spacing between text and adjacent boxes in CSS/HTML

I am currently working on a registration form and facing an issue with the spacing. The space between each credential and its corresponding input box appears irregular, which is not desired. I have tried using the <pre> tag to format the code, but th ...

Sometimes the sidebar (with float: left) doesn't stay on the left side

I created a container to hold sidebars and content, but I noticed that when I have more text in the sidebar compared to the container, the second sidebar floats slightly to the side. Here is the code I am using: HTML: <div class="container"> &l ...

How to modify Django datepicker date format

I've been attempting to modify the datepicker format from mm/dd/yyyy to dd-mm-yyyy. Initially, I added DATE_INPUT_FORMATS in settings.py to ['%d-%m-%Y'], but unfortunately, this did not produce the desired change. I also tried adjusting it u ...

Alter the color of the text labels on the bar graph using the chartsjs-plugin-data-labels

The bar chart below shows the number of students. Here are the custom options for formatting it: public chartOptions = { responsive: true, maintainAspectRatio: false, legend: { display: true, labels: { fontColor: "bla ...

Image Not Displayed on Page

I am facing an issue where I have a div class 'breadcam_bg' with an image url, but the image is not being displayed on the page even though the console detects the div. html <div class="bradcam_area breadcam_bg"> This div! & ...

Ways to prevent a timeout when the score exceeds 1000

Could someone help me with clearing the interval and resetting the score in my code? It seems to be working fine, but when the score goes over 1000 and I hit reset, it doesn’t reset completely. I've tried placing the clearTimeout functions before e ...

What steps are involved in customizing the footer section of a

I'm trying to track down a piece of html that seems to be triggering random black boxes appearing on my website pages. It looks like there might be a duplicated footer section, and <footer></footer> could be the culprit. I'm not sure ...

What are the standard browser values for HTML elements like TABLE, TR, and TD, and how can you reset styles to their default settings?

It's common knowledge that each browser has its own default values for HTML elements. But what happens when we want to override specific styles set by CSS and revert back to the default browser style? For instance, if all TRs have a height value of 5 ...

dividing the expanded and compacted bootstrap 4 navbar options

While constructing a bootstrap 4 navbar menu, I am facing a challenge with styling only the responsive menu after clicking the toggle button. The issue lies in having just one CSS selector for both the uncollapsed and collapsed UL. Working with WordPress, ...

Stylish navigation menu with a subtle scrolling effect in the

Both the left panel and main content should have individual scrolling abilities. You can view a demonstration of this functionality here: http://plnkr.co/edit/Le6hPWwMm0pu5Ukm2cYg?p=preview Below is the CSS code required for this feature: html { ove ...

Ways to verify if all javascript and css files have been successfully loaded

I have been exploring different ways to notify users if certain resources do not load correctly. So far, I have come across the following methods: For CSS, I stumbled upon an example in the source code of Trello: <div id="nocss"> Your browser ...

Select the input by clicking on the <a> text

Is there a way to ensure that clicking on the text within the <a> tag activates the checkbox instead of closing the drop-down menu? Currently, when someone clicks on the text of the checkbox, the drop-down menu closes. Here is the code snippet in que ...

Managing the intersection of div elements

Can someone help me make the inner part of the red circle white, overlapping with the blue circle and transparent for the rest to reveal the green color underneath? If anyone has a solution, I would greatly appreciate it. #bigCircle { display: flex ...

Displaying an element to appear over all client applications when hovering

Currently, I have an application that highlights specific areas when hovered over or clicked. While it functions properly, I would like the hover and click effects to be visible on every client, each with an identical overlay setup. I realize my method may ...

Select from the variety of options in the dropdown menu to make your choice

By clicking on the button labeled Press Me, you can open a drop-down menu. Clicking on it again or anywhere in the window will close the menu. I want to synchronize this function with the dropdown-arrow located next to it as well. I attempted to give the ...

Creating an angular div with rounded corners in the bottom right corner is a simple and stylish way to enhance the look

I've been attempting to design my div to resemble this example, but unfortunately, I haven't had much luck. I experimented with the polygon method to achieve the desired look, but all I managed to create was an angled line that didn't align ...

Exploring the relationship between CSS z-index values and a canvas

http://jsfiddle.net/y2q3yLpj/ In my example, I tried setting the z-index of a canvas to 0 and the z-index of a div to 1. However, despite this configuration, the canvas appeared higher than the div. Surprisingly, when I set the z-index of the canvas to -1 ...