bootstrap "center-block"

I'm using bootstrap to make a UI like the following:

 ------------------------------------------------ | | | div-outside | | ---------------------------- | | | | | | | | | | | div-inside | | | | | | | | | | | ---------------------------- | | | | | ------------------------------------------------- 

The code is:

 <div> <div> .... </div> </div> 

To make the inner div center at the outer div, I'm using. It works well if the screen is wider than a specific number, such as 1000px, but if the screen width is not big enough, the inner div will be adjacent to the right of outer div:

 ---------------------------------------- | | | div-outside | | ---------------------------- | | | | | | | | | | left | div-inside | | | margin | | | | | | | | ---------------------------- | | | | | ----------------------------------------- 

With Chrome's developer tool, I found that when the inner div is not centered, the left margin will be a fixed number (it seems have a lower bound). But when the screen's width is varying above the boundary value, the left margin number will vary according to the width thus making the inner div always centered. In fact, when the inner div is not horizontally centered, if I manually adjust the left margin number, then the inner div will be centered.

3 Answers

Your example is working. The problem is that the layout does not work if the screen width is smaller than 350px, because you have chosen a fixed width.

Demo

If the <div> should center all the time, use cols and offset:

<div> <div> <div> center with offset </div> </div> </div> 

Example offset

More information about Bootstrap's grid offsetting.

1

Bootstrap's .center-block contains nothing more than

.center-block { display: block; margin-right: auto; margin-left: auto; } 

There should be something else which is adding some other rules to your container. From the Inspector, try to check the CSS (fixed left margin) which you are talking about is getting added from which file.

2

I think this is what you're trying to do.

#app-layout-body { height: auto; margin: 0 auto; padding: 25px; position: relative; border: 5px solid red; text-align: center; } #div-register { border: 5px solid green; padding: 10px; background: red; color: white; }
<link href="" rel="stylesheet" /> <div> <div> <div><strong>I'm a centered Div inside another centered Div..</strong> <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> </div> </div> </div>
6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like