🔙 Quay lại trang tải sách pdf ebook Bootstrap, Responsive Web Development
Ebooks
Nhóm Zalo
Bootstrap
Dave Winer and Jake Spurlock
Bootstrap
by Dave Winer and Jake Spurlock
Copyright © 2010 Jake Spurlock. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected].
Editors: Simon St. Laurent and Meghan Blanchette Production Editor: Marisa LaFleur
Copyeditor:
Proofreader: FIX ME!
May 2013: First Edition
Revision History for the First Edition:
YYYY-MM-DD: First release
Indexer:
Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano
See http://oreilly.com/catalog/errata.csp?isbn=9781449343910 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. !!FILL THIS IN!! and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-1-449-34391-0
[?]
Table of Contents
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Bootstrap Scaffolding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What is Bootstrap 1 Bootstrap File Structure 2 Basic HTML Template 2 Global Styles 3 Default Grid System 3
Basic grid HTML 4 Offsetting Columns 4 Nesting columns 5 Excercise 1 5
Fluid Grid System 6 Container Layouts 7 Responsive Design 7 What is Responsive Design? 8
2. Bootstrap CSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Typography 11 Headings 11 Emphasis classes 13 Lists 17 Code 20 Tables 20 Optional Table Classes 21 Table Row Classes 23 Forms 23 Optional Form Layouts 24
iii
Supported Form Controls 26 Extending Form Controls 29 Form Control Sizing 31 Form Control States 35
Buttons 36 Button Sizes 38 Disabled Button Styling 39
Images 40 Icons 40 Glyphicon Attribution 41 Usage 42
3. Bootstrap Layout Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Dropdown Menus 45 Options 46 Button Groups 46 Button Groups as Radio and Checkboxes 48 Buttons With Dropdowns 48 Split Button Dropdowns 49 Dropup Menus 50 Navigation Elements 51 Tabular Navigation 51 Basic Pills Navigation 51 Stackable Navs 52 Dropdowns 53 Navigation Lists 54 Tabbable Navigation 55 Navbar 58 Nav Links 59 Forms 60 Navbar Menu Variations 60 Breadcrumbs 63 Pagination 64 Pager 66 Labels and Badges 67 Badges 67 Typographic Elements 68 Page Header 68 Thumbnails 69 Alerts 70 Progress bars 72 Media Object 73
iv | Table of Contents
Misc 76 Helper Classes 77
4. Bootstrap Javascript Plugins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Overview 79 Programattic API 80 Transitions 80 Modal 81 Using Modal through Data Attributes 82 Modal Options 82 Methods 83 Events 83 Dropdown 84 Dropdown Usage via the Data API 84 Dropdown Usage via Javascript 85 Methods 85 Scrollspy 85 Usage 85 Scrollspy Methods 86 Options 86 Events 86 Toggleable Tabs 86 Usage 87 Events 88 Tooltips 88 Usage 88 Options 89 Methods 89 Show 89 Popover 90 Usage 90 Options 90 Methods 91 Alerts 91 Usage 92 Methods 92 Events 92 Buttons 92 Loading State 93 Single Toggle 93 Checkbox Buttons 93 Radio Buttons 94
Table of Contents | v
Usage 94 Methods 94 Collapse 95 Usage 96 Options 96 Methods 97 Events 97 Carousel 97 Usage 98 Options 98 Methods 99 Events 99 Typeahead 100 Usage 100 Options 101 Affix 101 Usage 101 Options 102
5. Using Bootstrap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Github Project 103 Customizing Bootstrap 103
Using LESS 104 Text Snippets 106 Photoshop Templates 107 Themes 108
Built with Bootstrap 108 Conclusion 108
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 vi | Table of Contents
Foreword
I was a software developer before there was a Mac, so I remember something not a lot of programmers do. I remember how the tech industry reacted to it. And for the most part, it was with a fair amount of skepticism. And the interesting thing is that the negative things people say about Bootstrap today sound exactly like the negative things people said about the Mac in 1984. And in both cases, the things that people didn’t like were what made them important.
What the Mac realized is that there are a set of things that all software has to do, so why shouldn’t they all do them the same way? If they did, software would be easier to develop and debug, but more important — it would be easier to use. If there was only one way to do menus, then once a user learned how to use the menus of one app, they’d already know how to use the menus of all others. Same with scrollbars, windows, the keyboard, the mouse, printing, sound.
The reason programmers didn’t like it, and I was one of them, was that they took what we did and commoditized it. Further, there were limits to the one-size-fits-all approach. There were some apps that didn’t take to the UI standards very well. What to do about them? Well, you adapted, that’s what you did.
This is a well-known technical process called factoring. If you see yourself doing some‐ thing over and over, do it one more time, really well, and work on the API so it’s really easy and flexible, and that’s it. You never do it again. It’s how you build ever-taller build‐ ings out of software. And what was the leading-edge five years ago, is baked into the operating system today. Progress. It’s a wonderful thing! :-)
The same patterns are observable in the web. In fact, it’s kind of sad how much of a repeat it is, how backward today’s development environment is compared to the one envisioned by the Mac. But at least Bootstrap is out there doing the factoring. If I want to put up a menu, I can just use their code that does menus. Sure, my menu looks like all the others, but that’s a good thing, for users. No need to learn a second or third way to use a menu.
vii
That this is needed, desperately needed, is indicated by the incredible uptake of Boot‐ strap. I use it in all the server software I’m working on. And it shows through in the templating language I’m developing, so everyone who uses it will find it’s “just there” and works, any time you want to do a Bootstrap technique. Nothing to do, no libraries to include. It’s as if it were part of the hardware. Same approach that Apple took with the Mac OS in 1984.
Like all important technologies, Bootstrap is “good enough” but not too good. In other words, the designers, Mark Otto and Jacob Thornton, could have factored more than they did, and created something more compact and perhaps more elegant, that wasn’t nearly as approachable. The great thing about Bootstrap for a guy like me, who has been busy building software behind websites, is that it solves a whole bunch of problems that we all have when putting a user interface on those sites.
However I think that will turn out to be just the beginning. I see the opportunity for Bootstrap to become an integral part of the web. A toolkit that you can count on being present in every environment you work in. Further, someday, perhaps soon, designers will be able to plug in “skins” for Bootstrap, that transform the appearance of a site without any modification to the code or to its styles or scripts. I don’t see any limits to what can be done with Bootstrap. Rather than being a replacement for designers, it creates opportunities for designers to have more power and reach.
These days, part of the maturing process of any new technology is the release of its O’Reilly book. Now Bootstrap has one. The author, Jake Spurlock, a web developer, has built a bunch of sites with Bootstrap, has spoken at conferences about it, and he credits me for getting him started with a series of enthusiastic blog posts I wrote, as I was discovering its power and elegance. Now, I can happily turn you over to Jake’s able hands, to show you how the Bootstrap magic works, so you too can help move the web devel‐ opment world forward.
—Dave Winer, editor, Scripting News, January 2013, New York, NY viii | Foreword
Preface
Bootstrap is a front-end framework for building responsive websites. Whether it is application frameworks, blogs, or other CMS applications, Bootstrap can be a good fit as it can be as vanilla as you like. It’s combination of HTML, CSS, and Javascript make it easy to build robust sites, without adding a lot of code. With a default grid system, layouts come together with ease, and the styling of buttons, navs, and tables make basic markup look great from the get go. A dozen or so Javascript plugins catapult you into adding interactive elements to your site.
Who This Book is For
This books is mostly for people that have a good handle on HTML/CSS and Javascript, and are curious to get started with building responsive sites, adding the Bootstrap Java‐ script plugins, or building sites faster by using this popular open-source framework.
Who This Book is Not For
This book is not for people that get all they need out of the Bootstrap online documen‐ tation. Like a lot of people, the online docs are where I got started, building my first site with Bootstrap 1.3, and then upgrading it to Bootstrap 1.4. After that, a big project was built with Bootstrap 2.0 and on. If you are comfortable writing semantic HTML, then jumping into Bootstrap should be easy for you to get started with.
What This Book Will Do For You
With some background in writing HTML/CSS, and Javascript, this book will help you get off the ground writing some flexible code for responsive websites. In practical terms, the concepts and code syntax should come easily as it follows the patterns for writing semantic HTML and CSS anyways.
ix
How This Book Works
This book tries to built a site with Bootstrap, starting at the foundation of the project, the file structure, moving up through the grid system, and layout types into HTML elements and styling like forms, tables, and buttons. Once the walls are up, we move into the aesthetic elements like navbars, breadcrumbs, and media objects. After that, into the Javascript elements that provide the interaction for a site like dropdowns, the carousel, and modals.
Why I Wrote This Book
I’m not an Bootstrap expert hoping to create more Bootstrap experts to get a lot of work done.
I’m a developer and writer who encountered Bootstrap through a post on Dave Winer’s blog, and I thought that it would be cool to apply it to a new site that I was working on and since felt compelled to share some of that. I’m hoping that the path I followed will work for other people, probably with variations, and that a book written from a begin‐ ner’s perspective (and vetted by experts) would help more people find and enjoy Boot‐ strap.
Other Resources
This book may not be the best way for you to learn Bootstrap. It all depends on what you want to learn and why.
If your primary interest is to get started building Bootstrap websites, the online docu‐ mentation at http://twitter.github.com/bootstrap/ may likely suit you perfectly. The authors, Jacob Thornton and Mark Otto have been meticulous in providing examples of the codebase, and HTML code samples, and more to kick start your project. It is top notch, and where I have gathered the structure for this book.
If you want to contribute to the work of the open source project, you can submit pull requests, or use the issue tracker on the Github project, http://github.com/twitter/boot strap/, for updates, downloads, documentation, and more.
Are You Sure You Want Bootstrap?
If you are simply looking for Javscript plugins, or a CSS reset, Bootstrap maybe overkill. If you aren’t in love with some of the default interface elements that can be overwritten easily, or just strip out the associated tags. If you are looking for an easy way to build fast, responsive websites, Bootstrap is a great way to get going. I use it on all of my projects, and I’m really happy with it.
x | Preface
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐ mined by context.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Using Code Examples
The examples in this book are meant to teach basic concepts in small bites. While you may certainly borrow code and reuse it as you see fit, you won’t be able to take the code of this book and build a stupendous application instantly (unless perhaps you have an unusual fondness for bacon and cats). You should, however, be able to figure out the steps you need to take to build a great website.
You can download the code from the Examples link on the book’s page at hhttp:// shop.oreilly.com/product/0636920027867.do.
This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount
Preface | xi
of example code from this book into your product’s documentation does require per‐ mission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Bootstrap, Responsive Web Development, by Jake Spurlock (O’Reilly). Copyright 2013 Jake Spurlock, 9781449343910.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected].
Help This Book Grow
While I hope that you will enjoy reading this book and learn from it, I also hope that you can contribute to helping other readers learn Bootstrap here. You can help your fellow readers in a number of ways:
• If you find specific technical problems, bad explanations, or things that can be improved, please report them through the errata system at http://oreilly.com/cata log/errata.csp?isbn=0636920027867.
• If you like (or don’t like) the book, please leave reviews. The most visible places to do so are on Amazon.com (or its international sites) and at the O’Reilly page for the book at http://shop.oreilly.com/product/0636920027867.do. Detailed explana‐ tions of what worked and what didn’t work for you (and the broader target audience of programmers new to Bootstrap) are helpful to other readers and to me.
• If you find you have much more you want to say about Bootstrap, please consider sharing it, whether on the Web, in a book of your own, in training classes, or in whatever form you find easiest.
I’ll update the book for errata, and try to address issues raised in reviews. Even once the book is complete, I may still add some extra pieces to it. If you purchased it as an ebook, you’ll receive these updates for free at least up to the point where it’s time for a whole new edition. I don’t expect that new edition declaration to come quickly, however, unless the Bootstrap world changes substantially.
Hopefully this book will engage you enough to make you consider sharing. Safari® Books Online
Safari Books Online is an on-demand digital library that delivers ex‐ pert content in both book and video form from the world’s leading authors in technology and business.
xii | Preface
Technology professionals, software developers, web designers, and business and crea‐ tive professionals use Safari Books Online as their primary resource for research, prob‐ lem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐ zations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐ fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐ ogy, and dozens more. For more information about Safari Books Online, please visit us online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://www.oreilly.com/catalog/.
To comment or ask technical questions about this book, send email to bookques [email protected].
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Many thanks to Dave Winer for interesting me in Bootstrap in the first place, and to Simon St. Laurent for the opportunity to write this. Detailed feedback from my friends Roseanne Fallin, and Tony Quartorolo has made it possible, I hope that this book can
Preface | xiii
get readers started on the right track. Thanks to Melissa Morgan for letting my take a few risks and develop the way that I like at MAKE too.
In particular, thanks to my wonderful wife Melissa for putting up with me, and en‐ couraging me to finish. To my son Rush for understanding that I needed to “Work”, and to my daughter Hailey for the warm smiles and huge hugs.
xiv | Preface
CHAPTER 1
Bootstrap Scaffolding
What is Bootstrap
Bootstrap is an Open Source product from Mark Otto and Jacob Thornton who, when initially released were both employees at Twitter. There was a need to standardize the front end toolsets of engineers across the company. In the launch blog post, Mark Otto introduces the project like this:
In the earlier days of Twitter, engineers used almost any library they were familiar with to meet front-end requirements. Inconsistencies among the individual applications made it difficult to scale and maintain them. Bootstrap began as an answer to these challenges and quickly accelerated during Twitter’s first Hackweek. By the end of Hackweek, we had reached a stable version that engineers could use across the company.
— Mark Otto
https://dev.twitter.com/blog/
bootstrap-twitter
Since Bootstrap launched in August, 2011 it has taken off in popularity. It has evolved away from being an entirely CSS drivin project to include a host of Javascript plugins, and icons that go hand in hand with forms and buttons. At its base, it allows for re‐ sponsive web design, and features a robust 12 column, 940px wide grid. One of the highlights is the build tool on [Bootstrap’s](http://getbootstrap.com) website where you can customize the build to suit your needs, choosing what CSS and Javascript features that you want to include on your site. All of this, allows front-end web developement to be catapulted forward, building on a stable foundation of forward-looking design, and developement. Getting started with Bootstrap is as simple as dropping some CSS and Javascript into the root of your site.
Starting a project new, Bootstrap comes comes with a handful of useful elements to get you started. Normally, when I start a project, I start with tools like Eric Meyer’s CSS reset, and get going on my web project. With Bootstrap, you just need to include the
1
bootstrap.css CSS file, and optionally the bootstrap.js Javascript file into your web‐ site and you are ready to go.
Bootstrap File Structure
bootstrap/
├── css/
│ ├── bootstrap.css
│ ├── bootstrap.min.css
├── js/
│ ├── bootstrap.js
│ ├── bootstrap.min.js
├── img/
│ ├── glyphicons-halflings.png
│ ├── glyphicons-halflings-white.png
└── README.md
The Bootstrap download includes three folders: css, js, and img. For simplicity, add these to the root of your project. Included are also minified versions of the CSS and Javascript. Both the uncompressed and the minified versions do not need to be included. For the sake of brevity, I use the uncompressed during development, and then switch to the compressed version in production.
Basic HTML Template
Normally, a web project looks something like this:
Basic HTML Layout.
Bootstrap 101 Template
Hello, world!