CSS Frameworks
A CSS framework can save you time when styling applications. A framework is really just a stylesheet that has been designed by someone else that you are implementing into your code.
To implement these styles, you simply add a class to the html element that you'd like to style and give it the class name stated in your framework's documentation.
In addition, you can make changes to the styles using your own stylesheet. Combine and conquer!
You can use Bootstrap to jazz up your projects, but we also want you to be familiar with it because, a lot of times when you jump into a company that has a pre-existing codebase, you will encounter the use of a CSS framework.
#
The framework that we will be looking at todayBootstrap Probably the most popular framework out there right now. Originally created by some guys who were working at Twitter.
#
ActivityWe're going to style this page using Bootstrap:
To look something like this:
#
SetupCreate the following files in a folder and code along.
Open the folder in code and open
index.html
in your browser
<!DOCTYPE html><html> <head> <meta charset="utf-8">
<!-- START Style Sheets --> <!-- Our personal style sheet --> <link rel="stylesheet" href="style.css" charset="utf-8">
<!-- Bootstrap CDN -->
<!-- END Style Sheets -->
<title>Bootstrap Framework Example</title> </head> <nav> <ul> <li><a href="#">HOME</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">WORK</a></li> <li><a href="#">CONTACT</a></li> </ul> </nav>
<!-- START Image --> <div> <img src="img/asset_holder.jpg"/> </div> <!-- END Image -->
<!-- START body wrapping --> <div>
<!-- START Page Header --> <div> <h1>Example page header <small>Subtext for header</small></h1> </div> <!-- END Page header -->
<!-- START Three content sections --> <div> <div> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor. </p> </div> <div> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.</p> </div> <div> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.</p> </div> </div> <!-- END Three content sections -->
<hr> <!-- START Callout box --> <div> <h1>Hello, world!</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in.</p>
<!-- START Learn More link --> <p><a href="#">Learn more</a></p> <!-- END Learn More link --> </div> <!-- END Callout box -->
<hr>
<!-- START Information Table --> <table> <thead> <tr> <th>Name</th> <th>Quantity</th> <th>Location</th> </tr> </thead> <tbody> <tr> <td>Cupcakes</td> <td>26</td> <td>San Francisco</td> </tr> <tr> <td>Lentil Soup</td> <td>42</td> <td>Austin</td> </tr> <tr> <td>Sandwhich</td> <td>20</td> <td>Seattle</td> </tr> </tbody> </table> <!-- END Information Table -->
</div><!-- END of Body wrapping -->
</body></html>
#
Let's Look at BootstrapBootstrap's website catalogs everything that you can use to style your application. Note that their styles are responsive.
To use Bootstrap, you can download a file and keep it in your application or use a link to their CDN.
Bootstrap is a very robust framework! It has pretty much everything that you need (plus more). After this lesson, take some time to explore the site to see more of the offerings. Consider using it in your homework or projects.
#
Code along- Go to Bootstrap's framework page to get their stylesheets.
Direct link to documentation: https://getbootstrap.com/docs/5.1/getting-started/download/
You could download the files (but let's not do that for this exercise):
Let's use the Bootstrap CDN link to get the style's to our page. Add the CDN link into your
index.html
file
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
Refresh your browser and take a look at what happened after just initially linking Bootstrap.
We'll be using classes from the
Layout
,Content
, andComponents
section. You can browse through these sections in the sidebar on Bootstrap's page to see some options for what we can do.
#
Nav barDirect link to documentation: https://getbootstrap.com/docs/5.1/components/navs/
Add the
class="nav"
to theul
of your nav barWe also have to specify which
li
elements are nav items by adding the classnav-item
Further, we need to specify which
a
elements are nav links by adding the classnav-link
We can add additional styling to this. Let's add the class
nav-tabs
to ourul
to adjust the formatting and turn our links into navigation tabs.
#
Adjust the imageDirect link to documentation: https://getbootstrap.com/docs/5.1/content/images/
If we wanted our image to be responsive, we could add the
class="img-fluid"
If we always want it to stay centered, tack on
class="text-center"
onto the image's parentdiv
#
ContainerDirect link to documentation: https://getbootstrap.com/docs/5.1/layout/overview/
- Let's start by wrapping our site content in a container:
class = "container"
. What does this do?
We can use .container for a responsive fixed width container.
If we want the container to span the entire width of the viewport, we can use class="container-fluid"
#
Bootstrap's gridLet's use the built-in grid system that Bootstrap supplies.
Direct link to documentation: https://getbootstrap.com/docs/5.1/layout/grid/
On the three paragraphs below the Example header, let's put
class="row"
on a div that surrounds all three.Let's give each of these paragraphs a
class="col-sm"
#
Update a buttonDirect link to documentation: https://getbootstrap.com/docs/5.1/components/buttons/
Bootstrap has multiple button options:
The primary button style:
class="btn btn-primary"
Quickly style to denote an action (success, danger, warning, etc.)
class="btn btn-success"
class="btn btn-danger"
Change the style to an outlined button instead of filled
class="btn btn-outline-primary"
Or change the size of the button
class="btn btn-primary btn-lg"
- Choose one and add it in the
a
tag to turn theLearn More
link to a button.
#
Style the tableDirect link to documentation: https://getbootstrap.com/docs/5.1/content/tables/
- Add Bootstrap's default table style:
class="table"
- Add the class
table-striped
to give our table zebra stripes
#
Sections that you want to showcaseDirect link to documentation: https://getbootstrap.com/docs/5.1/components/jumbotron/
Bootstrap has something called jumbotron
which has styling to showcase a section.
- Let's add
class="jumbotron"
to the section marked ascallout
in our index.html.
NOTE: Bootstrap is fully responsive! Change the sizes of your screen to check it out.
#
Other CSS frameworksSkeleton Lightweight framework that has a nice, simple grid system.
Materialize This framework is difficult to manipulate, but I like it for the parralax and the cards.
Pure CSS Similar to skeleton, this is a light framework with a grid system and responsive design.