Styling the carousel

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.

  1. Copy bootstrap/carousel.less and save it in the less directory as _carousel.less.

    .

    Styling the carousel
  2. Update the relevant import line in __main.less to import our custom file in place of Bootstrap's:
    @import "_carousel.less";
  3. Customize the opening comment in _feature-carousel.less:
    //
    // Customized Carousel
    // --------------------------------------------------

Now to begin customizing!

Setting Font Awesome icons for the controls

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.

  1. First, update the icons markup in index.html. Look for the links with the classes left or right and carousel-control:
    <a class="left carousel-control" ...
  2. Update the 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>
  3. Next, we need to add new class selectors in _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%
      }

    Note

    Three notes about these edits:

    • By using a basic icon class both in the markup and here, we may use any icon of our choice and our styles will still work
    • The &.left and &.right constructions reach back in the nesting hierarchy and compile to .carousel-control.left and .carousel-control.right
    • By altering the value for the left: and right: positions, I've nudged the icons closer to the edges of the carousel

Save, compile, and refresh. Our new Font Awesome icons should take their appropriate places.

Setting Font Awesome icons for the controls

Now, we can move on to aesthetic enhancements.

Adding top and bottom padding

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.

Forcing images to their full width

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 images to their full width

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.

Tip

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.

Constraining the carousel height

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:

Constraining the carousel height

And in a wide viewport, it looks like the following:

Constraining the carousel height

Now, to style the carousel indicators.

Repositioning 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.

Repositioning the carousel indicators

Let's move these indicators into their own space, just below the image:

  1. In _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;
  2. We want to move these down even closer to the bottom edge, into our light gray area created by the padding we added above. So, let's adjust the bottom positioning. In addition, we need to remove the default bottom margin by zeroing it out.
    .carousel-indicators {
      position: absolute;
      bottom: 0; // edited
      margin-bottom: 0; // added
  3. Save, compile, and test. You may notice that on small screen sizes, this positions our indicators where we want them. On larger screen sizes, however, they return to their previous position. As it turns out, this is the result of a rule under media query where we were just working—down near very bottom of the file.
  4. Find these lines near the bottom of the file, within the media query for @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.

Repositioning the carousel indicators

Now, let's update their appearance to make them larger and easier to see.

Styling the indicators

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.

  1. In _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.

  2. Let's add a default background color variable here, so that we may fill the default indicators with our @gray-light value.
    @carousel-indicator-bg:          @gray-light;
  3. Then, we'll update the active background color.
    @carousel-indicator-active-bg:    @gray-lightest;
  4. Finally, we'll make the border-color transparent.
    @carousel-indicator-border-color: transparent;
  5. Save, compile, and refresh.

At present, this has the effect of making all but the active item invisible.

Styling the indicators

Now, for some work in _carousel.less.

  1. In the file _carousel.less, move to the first set of rules for .carousel-indicator where we were previously working.
    .carousel-indicators {
      position: absolute;
      ...
  2. Look for the li selector nested under it. Here, let's edit several values. Specifically, we'll perform the following actions:
    • Increase the width and height to 16px
    • Remove the margin
    • Add background-color using our newly created variable @carousel-indicator-bg
    • Remove the border line altogether (the transparent value we set for the border variable is now merely a failsafe)
    • I've commented these changes in the following code snippet
      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;
        ...
  3. Notice the following hack for IE 8-9 to supply the indicators with a 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
  4. Next, we need to remove the margin, width, and height values under the .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;
    }
  5. Finally, let's add a hover effect by adding a :hover selector along with the .active selector.
    li:hover, // added
    .active { ...
  6. Save, compile, and refresh. And check out the result!
    Styling the indicators

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset