What are the best practices for Bootstrap?


Ashley Brown

Suppose you have this simple markup.

<div class="container">
 <div class="row">
  <div class="col-md-12">
    <h1> title </h1> 
  </div>
 </div>
</div>

If I wanted to add a margin-topmove h1towards the middle of the container down ( which would place itself at the very top by default ), would that be better painted over h1, .rowor .col-md-12?

E.g:

.row {
 margin-top: 100px;
}

UPDATE : This is not a "primarily opinion-based" question, because depending on where I apply the styles, it may break the expected behavior when it comes to the responsive aspect. Dawood Awan's answer is a perfect example of the question's legitimacy and its independence from opinion.

Alex

Bootstrap rules:

Use rows and columns alternately with
negative margins for rows to ensure columns respect the width of the container.

container
|   row
|   |   column
|   |   |   row
|   |   |   |   column
|   |   |   |   column
|   |   |   row
|   |   |   |   column
|   |   |   |   column
|   |   column

Always include col-xs-* on columns
this is to prevent float issues. If your column should be 12 wide, don't ignore col-xs-12

<div class="row">
  <div class="col-xs-12 col-md-6">
      Some stuff
  </div>
</div>

Mobile first
starts with the smallest screen size. Start with xs < sm < md < lg and you should be good to go!

<div class="row">
  <div class="col-xs-12 col-sm-8 col-md-6 col-lg-4">
      Some stuff
  </div>
</div>

The small column acts as the larger column
and the sm column also acts as the md column, if not specified otherwise.

<div class="row">
  <div class="col-xs-12 col-sm-6 col-md-6">
      This is the same
  </div>

  <div class="col-xs-12 col-sm-6">
      as this one
  </div>
</div>

Finally: don't style rows and columns!
Feel free to add classes to modify them, but don't override them in any way!

Bad example:

.row {
  border: 5px solid pink;
  margin: 0 10px;
}

<div class="row">
  <div class="col-xs-12">
    This is a no-go!
  </div>
</div>

good example

.pink-bordered {
  border: 5px solid pink;
  margin: 0 10px;
}

<div class="row pink-bordered">
  <div class="col-xs-12">
    Totally fine
  </div>
</div>

Related


What are AJAX best practices?

Patrick I'm trying to learn javascript best practices and I'm a little confused. I think the best ajax practice is: function doSomething(arg1, arg2) { jQuery.ajax({ var urlink = resourceURL url: urlink, cache: false, data

What are AJAX best practices?

Patrick I'm trying to learn javascript best practices and I'm a little confused. I think the best ajax practice is: function doSomething(arg1, arg2) { jQuery.ajax({ var urlink = resourceURL url: urlink, cache: false, data

What are the best practices for SQLite on Android?

Vidal Westnes What is the best practice when executing a query on a SQLite database in an Android application? Is it safe to run insert, delete and select queries from AsyncTask's doInBackground? Or should I use the UI thread? I guess database queries might be

What are the best practices for background tasks?

Safe Verma I have a use case where whenever a transaction completes or fails, I have to wait 5 minutes in the background (without freezing the UI) and call a piece of code without user intervention. So AFAIK I need to implement a background service for this. I

Checkboxes in Django - what are the best practices?

Jeremy I know there is an explanation on djangoproject.com that you should use NullBooleanField for checkboxes in Django, however, I wonder if it's better to use something like this? models.CharField(null=True, choices=(('Y','YES'),)) Then, null will only eva

What are the best practices for service accounts?

Phil 91 How should service accounts be used? Should we create a service account per container? In order to group service accounts, is there a concept equivalent to "user groups"? Artem Golenyaev Pods use service accounts to access the Kubernetes API or Secrets

What are the best practices for Docker logging?

Jenos I am using docker in my web service. When I deploy with Docker, some log files are lost (nginx access logs, service logs, syslogs, etc.) The reason, the docker deployment system uses down and up container architecture. So I thought of this question. Logg

What are LDAP authentication best practices?

mke mostafa I would like to know the best practice for user authentication using OpenLDAP. I can search using cn. But what if I encounter multiple similar matches cn(under different ancestors of course) and all use the same password? I tried to use, uidsbut th

What are the best practices for SQLite on Android?

Vidal Westnes What is the best practice when executing a query on a SQLite database in an Android application? Is it safe to run insert, delete and select queries from AsyncTask's doInBackground? Or should I use the UI thread? I guess database queries may be "

What are best practices? Properties or entities?

User 9419320 I'm trying to build a people directory database where the person has many entities such as address, phone as well as email and notes. But I would like/try to implement "email" and "notes" as properties of the "person" class instead of separate ent

What are LDAP authentication best practices?

mke mostafa I would like to know the best practice for user authentication using OpenLDAP. I can search using cn. But what if I encounter multiple similar matches cn(under different ancestors of course) and all use the same password? I tried to use, uidsbut th

What are the best practices for background tasks?

Safe Verma I have a use case where whenever a transaction completes or fails, I have to wait 5 minutes in the background (without freezing the UI) and call a piece of code without user intervention. So AFAIK I need to implement a background service for this. I

What are the best practices for application documentation?

Pearl 7721 After working in software development for two years, I started wondering how to communicate effectively with users, including details of UX, feature changes, bug reporting systems, etc. I have worked in two small companies in Korea and found that th

What are the best practices for standardizing CSS?

username I've been scouring the internet for the best way to standardize my website CSS. I have read the following: Conditional style sheets; CSS reset; Normalize.css; Initializr; HTML5 boilerplate; I know the last two have a lot of other features, but they al

Checkboxes in Django - what are the best practices?

Jeremy I know there is an explanation on djangoproject.com that you should use NullBooleanField for checkboxes in Django, however, I wonder if it's better to use something like this? models.CharField(null=True, choices=(('Y','YES'),)) Then, null will only eva

Checkboxes in Django - what are the best practices?

Jeremy I know there is an explanation on djangoproject.com that you should use NullBooleanField for checkboxes in Django, however, I wonder if it's better to use something like this? models.CharField(null=True, choices=(('Y','YES'),)) Then, null will only eva

What are the best practices for database connections

User 3787036 I would like to know what is the better way to establish a database connection using PHPOO. If I set a connection on __construct and close it on __destruct , will I make too many useless connections? For example, my class is called "sqlClass" and

What are the best practices for SQLite on Android?

Vidal Westnes What is the best practice when executing a query on a SQLite database in an Android application? Is it safe to run insert, delete and select queries from AsyncTask's doInBackground? Or should I use the UI thread? I guess database queries may be "

What are LDAP authentication best practices?

mke mostafa I would like to know the best practice for user authentication using OpenLDAP. I can search using cn. But what if I encounter multiple similar matches cn(under different ancestors of course) and all use the same password? I tried to use, uidsbut th

What are the best practices for background tasks?

Safe Verma I have a use case where whenever a transaction completes or fails, I have to wait 5 minutes in the background (without freezing the UI) and call a piece of code without user intervention. So AFAIK I need to implement a background service for this. I

What are the best practices for service accounts?

Phil 91 How should service accounts be used? Should we create a service account per container? In order to group service accounts, is there a concept equivalent to "user groups"? Artem Golenyaev Pods use service accounts to access the Kubernetes API or Secrets

What are the best practices for database connections

User 3787036 I would like to know what is the better way to establish a database connection using PHPOO. If I set a connection on __construct and close it on __destruct , will I make too many useless connections? For example, my class is called "sqlClass" and

What are the best practices for standardizing CSS?

username I've been scouring the internet for the best way to standardize my website CSS. I have read the following: Conditional style sheets; CSS reset; Normalize.css; Initializr; HTML5 boilerplate; I know the last two have a lot of other features, but they al

What are the best best practices for Go workspaces?

heckj: I'm just starting to learn Go and reading through existing code to learn "what other people do". In this case, traversal uses go "workspaces", especially work related to project dependencies. What is the common (best practice) for using single or multip

What are the best best practices for Go workspaces?

heckj: I'm just starting to learn Go and reading through existing code to learn "what other people do". In this case, traversal uses go "workspaces", especially work related to project dependencies. What is the common (best practice) for using single or multip

What are the best best practices for Go workspaces?

heck I'm just starting to learn Go and reading through existing code to learn "what other people do". In this case, the traversal uses go "workspaces", especially those related to project dependencies. What is the best practice (or exists) for using single or

What are the best best practices for Go workspaces?

heck I'm just starting to learn Go and reading through existing code to learn "what other people do". In this case, the traversal uses go "workspaces", especially those related to project dependencies. What is the best practice (or exists) for using single or

What are the best best practices for Go workspaces?

heckj: I'm just starting to learn Go and reading through existing code to learn "what other people do". In this case, traversal uses go "workspaces", especially work related to project dependencies. What is the common (best practice) for using single or multip

What are the best best practices for Go workspaces?

heckj: I'm just starting to learn Go and reading through existing code to learn "what other people do". In this case, traversal uses go "workspaces", especially work related to project dependencies. What is the common (best practice) for using single or multip