We're going to take Bootstrap's default carousel styles and apply some significant customization. Let's create a copy of Bootstrap's carousel.less
file and make it our own.
bootstrap/carousel.less
and save it in the less
directory as _carousel.less
..
__main.less
to import our custom file in place of Bootstrap's:@import "_carousel.less";
_feature-carousel.less
:// // Customized Carousel // --------------------------------------------------
Now to begin customizing!
If you unhooked Glyphicons as I did in the preceding section, you'll find that the next and previous carousel controls have disappeared. This is because they relied on Glyphicons. We can fix this using Font Awesome icons instead.
index.html
. Look for the links with the classes left
or right
and carousel-control
:<a class="left carousel-control" ...
span
tags with a generic icon class, plus the Font Awesome icon classes as follows:<span class="icon fa fa-chevron-left"></span> ... <span class="icon fa fa-chevron-right"></span>
_carousel.less
. You'll find it under the selector .carousel-control
, beneath the comment for // Toggles
. I'll paste the block of code with the necessary updates and comments:// Toggles .icon-prev, .icon-next, .glyphicon-chevron-left, .glyphicon-chevron-right, .icon { // added position: absolute; top: 50%; z-index: 5; display: inline-block; } .icon-prev, .glyphicon-chevron-left, &.left .icon { // added left: 20%; // edited was 50% } .icon-next, .glyphicon-chevron-right, &.right .icon { // added right: 20%; // edited was 50% }
Three notes about these edits:
icon
class both in the markup and here, we may use any icon of our choice and our styles will still work&.left
and &.right
constructions reach back in the nesting hierarchy and compile to .carousel-control.left
and .carousel-control.right
left:
and right:
positions, I've nudged the icons closer to the edges of the carouselSave, compile, and refresh. Our new Font Awesome icons should take their appropriate places.
Now, we can move on to aesthetic enhancements.
Let's add some top and bottom padding to the .carousel
element itself and add our @gray-lighter
color for a background color. To help keep track of my work, I'm going to tack on a small comment at the end of each added or edited line. (Again, these will never make it to CSS!)
.carousel { position: relative; padding-top: 4px; // added padding-bottom: 28px; // added background-color: @gray-lighter; // added }
After saving and compiling, you'll see the light gray background appears in our newly created space above and below the carousel images. This provides a bit of framing to set them off from the other elements above and below. In a bit, we'll take advantage of the extra bottom padding to position our carousel indicators in a way that allows them to stand out much more clearly.
First, let's ensure that our images will stretch to fill their space in all circumstances.
We need to force our images to stretch the full width of the carousel, even in wide screens. Our images are 1600px wide to fill most screens. But beyond this width, they will leave a gap at the right edge.
Forcing the images to their full width on windows wider than 1600px may pixelate them slightly; but for images that are large enough, distortions won't be huge.
When time allows, we can add a responsive image solution to load smaller images on smaller screens; and if we desire, we can add larger images for these wider sizes. I'll get you rolling with a responsive image solution in Appendix B, Implementing Responsive Images.
For now, we need to add only two lines to our file. Just below the rules for the .carousel
selector are the nested set of rules we need for this. Under the .carousel-inner
selector are rules for carousel images, including a mixin which ensures they behave responsively and adjust to smaller screens. We can also force them to stretch wider for wider screens by setting min
-width:
.
.carousel-inner {
...
> .item {
...
> img,
> a > img {
.img-responsive();
line-height: 1;
min-width: 100%; // added
}
}
...
}
After implementing this adjustment, we can stretch our browser window as wide as we like, and the image will stretch along with it.
Next, we need to limit the maximum height of our carousel.
As you may have noticed, the carousel grows entirely too tall in the medium and large screen view. Our mock-ups call for a constrained height of approximately 440px. We can accomplish this easily by setting a constraint on the parent of our images, the .carousel-inner > .item
, as follows:
.carousel-inner {
...
> .item {
...
max-height: 640px; // added
Because the .carousel-inner
element has a rule of overflow: hidden
, which constrains the height of the .item
element, it serves as a convenient way to hide the lower portions of the image when it grows beyond the desired height.
Having done this, we can use nested media queries (another nice feature of LESS), along with Bootstrap's medium and large breakpoint variables to adjust the vertical positioning of our images at the widest widths, to keep our designs in the focal area. I'll do it by using the following code:
> img, > a > img { ... @media (min-width: @screen-md-min) { margin-top: -40px; } @media (min-width: @screen-lg-min) { margin-top: -60px; } }
Save, compile, and refresh. You should see that our carousel is taking shape nicely and works well from narrow to wide viewport widths.
At a narrow width, it appears like the following:
And in a wide viewport, it looks like the following:
Now, to style the carousel indicators.
The carousel indicators serve to inform the user how many slides are in our carousel, and highlight the current spot in the rotation. At present, these indicators are barely visible—languishing near the bottom center edge of our portfolio images.
Let's move these indicators into their own space, just below the image:
_carousel.less
, search for the selector .carousel-indicator
. We want the first of its two occurrences, approximately 2/3 of the way down the file. This section opens with a comment.// Optional indicator pips
Notice how the element is vertically positioned.
.carousel-indicators { position: absolute; bottom: 10px;
.carousel-indicators { position: absolute; bottom: 0; // edited margin-bottom: 0; // added
@screen-tablet
and up.// Move up the indicators .carousel-indicators { bottom: 20px; }
Since we no longer need this adjustment, let's simply comment out these lines, thereby removing them from the CSS altogether.
// .carousel-indicators { // bottom: 20px; // }
This brings our desired result. The indicators now stay positioned in the desired space across all screen dimensions.
Now, let's update their appearance to make them larger and easier to see.
We'll make our carousel indicators more visible by using our gray variables. We'll also increase their size a bit. We can get a start in our _variables.less
file.
_variables.less
, just after the @carousel-control
variables, you'll find two variables beginning with @carousel-indicator
:@carousel-indicator-active-bg: #fff; @carousel-indicator-border-color: #fff;
These are used to provide a white border around the default indicators, and then fill the active indicator with the background color.
@gray-light
value.@carousel-indicator-bg: @gray-light;
@carousel-indicator-active-bg: @gray-lightest;
@carousel-indicator-border-color: transparent;
At present, this has the effect of making all but the active item invisible.
Now, for some work in _carousel.less
.
_carousel.less
, move to the first set of rules for .carousel-indicator
where we were previously working..carousel-indicators { position: absolute; ...
li
selector nested under it. Here, let's edit several values. Specifically, we'll perform the following actions:background-color
using our newly created variable @carousel-indicator-bg
li { display: inline-block; width: 18px; // edited height: 18px; // edited // margin: 1px; // edited text-indent: -999px; background-color: @carousel-indicator-bg; // added // border: 1px solid @carousel-indicator-border-color; border-radius: 10px; ...
background-color
for these browsers. Because we have just provided a background color for all of our indicators, this hack is no longer needed. Comment out these lines under the comment. Otherwise, these will interfere with our background color declaration in the preceding snippet.// background-color: #000 9; // IE8 // background-color: rgba(0,0,0,0); // IE9
.active
selector as we no longer want our active indicator to grow larger (nor do we want it to shrink back to 12px)..active { // margin: 0; // edited // width: 12px; // edited // height: 12px; // edited background-color: @carousel-indicator-active-bg; }
:hover
selector along with the .active
selector.li:hover, // added
.active { ...
Carousel adjustments accomplished! We've learned a lot in the process—a lot about Bootstrap and perhaps a little about LESS as well.
Let's move on to the next section. What's remaining is considerably simpler.