-
Notifications
You must be signed in to change notification settings - Fork 341
Responsive Design with Bootstrap
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
Bootstrap will figure out how wide your screen is and respond by resizing your HTML elements - hence the name Responsive Design.
With responsive design, there is no need to design a mobile version of your website. It will look good on devices with screens of any width.
You can add Bootstrap to any app just by including it with <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
at the top of your HTML.
When using Bootstrap, if you want an image to be responsive, all you have to do is add the class img-responsive
to it. <img class="img-responsive" src="http://bit.ly/fcc-running-cats">
Now that we're using Bootstrap, we can center our heading elements to make them look better. All we need to do is add the class text-center to our h1 and h2 elements.
Remember that you can add several classes to the same element by separating each of them with a space, like this: <h2 class="text-red text-center">your text</h2>
.
Bootstrap has its own styles for button elements, which look much better than the plain HTML ones.
<button type="submit" class="btn">Like</button>
Normally, your button elements are only as wide as the text that they contain. By making them block elements, your button will stretch to fill your page's entire horizontal space. Note that these buttons still need the btn
class.
<button class="btn btn-block">Like</button>
The btn-primary
class is the main color you'll use in your app. It is useful for highlighting actions you want your user to take. Note that this button will still need the btn
and btn-block
classes.
<button class="btn btn-block btn-primary">Like</button>
Bootstrap comes with several pre-defined colors for buttons. The btn-info
class is used to call attention to optional actions that the user can take. Note that these buttons still need the btn
andbtn-block
classes.
<button class= "btn btn-block btn-info">Info</button>
The btn-danger
class is the button color you'll use to notify users that the button performs a destructive action, such as deleting a cat photo. Note that these buttons still need the btn
and btn-block
classes.
<button class="btn btn-block btn-danger">Delete</button>
Bootstrap uses a responsive grid system that makes it easier to put elements into rows and tell each element's relative width.
Note that in this illustration, the col-md-_
class is being used. Here, md means medium, and _ is a number specifying how many columns wide the element should be. In this case, the column width of an element on a medium-sized screen, such as a laptop, is being specified.
<div class="row">
<div class="col-xs-4"><button class="btn btn-block btn-primary">Like</button></div>
<div class="col-xs-4"><button class="btn btn-block btn-info">Info</button></div>
<div class="col-xs-4"><button class="btn btn-block btn-danger">Delete</button></div>
</div>
We can clean up our code and make our Cat Photo App look more conventional by using Bootstrap's built-in styles instead of the custom styles we created earlier.
All you need to know is the built in classes.
You can use use spans to create inline elements. By using the span element, you can put several elements together, and even style different parts of the same element differently.
<p><span class = "text-danger">Things cats love:</span></p>
Using div
and the custom grid layout we can create our own heading.
<div class="row">
<div class="col-xs-8">
<h2 class="text-primary text-center">CatPhotoApp</h2>
</div>
<div class="col-xs-4">
<a href="#"><img class="img-responsive thick-green-border" src="https://bit.ly/fcc-relaxing-cat"></a>
</div>
Font Awesome is a convenient library of icons. These icons are vector graphics, stored in the .svg file format. These icons are treated just like fonts. You can specify their size using pixels, and they will assume the font size of their parent HTML elements.
<i class="fa fa-thumbs-up"><button class="btn btn-block btn-primary">Like</i></button>
<i class="fa fa-trash"></i>
Will add a trash can icon.
<i class="fa fa-info-circle"></i>
Will add an info icon.
You can use Bootstrap's col-xs-*
classes on form
elements. That will make radio buttons evenly spread out across the page regardless of how wide the screen might be.
Nest all of your radio buttons within a <div class="row">
element. Then nest each of them within a <div class="col-xs-6">
element.
<form action="/submit-cat-photo">
<div class="row">
<div class="col-xs-6"><label><input type="radio" name="indoor-outdoor"> Indoor</label></div>
<div class="col-xs-6"><label><input type="radio" name="indoor-outdoor"> Outdoor</label></div>
<div class="col-xs-6"><label><input type="checkbox" name="personality"> Loving</label></div>
<div class="col-xs-6"><label><input type="checkbox" name="personality"> Lazy</label></div>
<div class="col-xs-6"><label><input type="checkbox" name="personality"> Crazy</label></div>
</div>
<input type="text" placeholder="cat photo URL" required>
<button type="submit">Submit</button>
</form>
You can use Bootstrap's col-xs-*
classes on form elements, too! This way, our checkboxes will be evenly spread out across the page, regardless of how wide the screen resolution is.
Nest all your checkboxes in a <div class="row">
element. Then nest each of them in a <div class="col-xs-4">
element.
You can add the fa-paper-plane
Font Awesome icon by adding <i class="fa fa-paper-plane"></i>
within your submit button element.
<button type="submit" class="btn btn-primary"><i class="fa fa-paper-plane">Submit</i>
We line up the form elements the same way we do with others, using divs.
<div class="row">
<div class="col-xs-7"><input type="text" class="form-control" placeholder="cat photo URL" required></div>
<div class=col-xs-5><button type="submit" class="btn btn-primary"><i class="fa fa-paper-plane"></i> Submit</button></div>
</div>
<h3 class="text-primary text-center"> jQuery Playground </h3>
To make the content responsive, we use the container-fluid
class.
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
</div>
Create a div element with the class row. <div class="row"></div>
Create two div
elements within your row, both with the class col-xs-6
.
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6"> </div>
<div class="col-xs-6"></div>
</div>
</div>
Bootstrap has a class called well
that can create a visual sense of depth for your columns.
Nest one div element with the class well within each of your col-xs-6 div
elements.
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
</div>
</div>
<div class="col-xs-6">
<div class="well">
</div>
</div>
</div>
</div>
Once you have gone deep enough in divs
you can start adding your Bootstrap buttons.
Bootstrap has a button class called btn-default
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
</div>
</div>
<div class="col-xs-6">
<div class="well">
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
</div>
</div>
</div>
</div>
Not every class needs to have corresponding CSS. Sometimes we create classes just for the purpose of selecting these elements more easily using jQuery.
For this we use the target
class on the button
elements.
Recall that in addition to class attributes, you can give each of your elements an id attribute.
Each id should be unique to a specific element. Remember that you can give an element an id like this: <div class="well" id="center-well">
You can add labels to the wells by using the headers <h4>
above the well divs.
We will also want to be able to use jQuery to target each button by its unique id. So we add an unique id to each button.
Just like we labeled our wells, we want to label our buttons.
When we start using jQuery, we will modify HTML elements without needing to actually change them in HTML.
This is a great way to make websites with a particular structure. Remember that you can start a comment with <!-- and end a comment with -->
<!--You shouldn't need to modify code below this line -->
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<h4>#left-well</h4>
<div class="well" id="left-well">
<button class="btn btn-default target" id="target1">#target1</button>
<button class="btn btn-default target" id="target2">#target2</button>
<button class="btn btn-default target" id="target3">#target3</button>
</div>
</div>
<div class="col-xs-6">
<h4>#right-well</h4>
<div class="well" id="right-well">
<button class="btn btn-default target" id="target4">#target4</button>
<button class="btn btn-default target" id="target5">#target5</button>
<button class="btn btn-default target" id="target6">#target6</button>
</div>
</div>
</div>
</div>
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3