Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

📄 Contents

  1. Dynamically Changing Images’ Screen Area
  2. Creating Flexible Collections of Images
Like this article? We recommend

Creating Flexible Collections of Images

You now know several ways to make individual images flexible to either their parent’s dimensions or text size, but what about when you need a whole group of images to be flexible as a whole? Let’s go over how to make two of the most common types of image collections—teaser thumbnail lists and image galleries—flexible too.

Teaser Thumbnail Lists

A teaser thumbnail list is my own personal name for the design convention of a list where each item is made up of a title, short description, and thumbnail image. Figure 2.22 is one example of a teaser thumbnail list, as is the list of featured pets on the home page of our fictional Beechwood Animal Shelter site (Figure 2.43). These types of lists can be built in many different ways, but many techniques result in lists that are not flexible or not as accessible to the end user as they could be.

Figure 9.12 shows the teaser thumbnail list I’ll be using as an example throughout this section. I’ve chosen the following HTML as the most semantic way of marking up each of the elements of this design component:

<h1>Seafood of the Month Club 2008</h1>
<ul>
	<li>
		<h2>January</h2>
		<img src=”january.jpg” alt=”” width=”100” height=”100”>
		<p>Seared sea scallops, served with mushy peas.</p>
	</li>
	<li>
		<h2>February</h2>
		<img src=”february.jpg” alt=”” width=”100” height=”100”>
		<p>Soy-glazed salmon, served with coconut and bell pepper broccoli slaw.</p>
	</li>
	<li>
		<h2>March</h2>
		<img src=”march.jpg” alt=”” width=”100” height=”100”>
		<p>Tuna steak with ginger-shitake cream sauce, served with sesame broccoli and brown rice.</p>
	</li>
</ul>

Figure 9.12 Each teaser thumbnail list item is made up of a title, short description, and thumbnail image.

You’ll note that the img elements follow the h2 heading elements, even though Figure 9.12 shows the images appearing on the same line as the headings. You’ll need to find a way to get the images to move up to sit beside the headings, even though they come later in the source. Luckily, you’re already an expert at doing just that—you have several negative margin layouts you can use to achieve such an effect. A teaser thumbnail list is essentially nothing more than a two-column layout. This particular one has a fixed-width left “sidebar” and a liquid right “main content area,” so any negative margin technique that works for two-column, hybrid liquid-fixed layouts will work here.

To turn this into a negative margin “layout,” the basic steps are:

  1. Create an empty space on the left side of the list.
  2. Use negative margins to pull each image into that space.
  3. Float all the elements within each list item so they can sit side by side.

We’ll create the empty space on the left side of the list using a left margin on the ul element that is equal to the width of the images (100 pixels) plus the width of the gap we want between each image and its accompanying text (15 pixels):

ul {
	margin: 0 0 0 115px;
	padding: 0;
	list-style: none;
}

This rule also gets rid of some of the default list styling, including the bullets. The rest of the default list styling that needs to be overridden is on the list items:

li {
	margin: 0 0 20px 0;
	padding: 0;
}

This removes the default left margin and padding that some browsers add to li elements, as well as adds 20 pixels of space below each list item to space them out from each other.

Figure 9.13 A large empty space on the left side of the list stands ready to receive the thumbnails.

You can now pull the image into the empty space on the left:

img {
	float: left;
	margin-left: -115px;
}

This positions the images correctly horizontally, but not vertically (Figure 9.14). To get them to move up and sit beside the headings, the headings have to be floated, as do the paragraphs:

h2 {
	float: right;
	width: 100%;
	margin: 0;
}
p {
	float: right;
	width: 100%;
	margin: 0;
}

Figure 9.14 Negative left margins pull the images to the left, but don’t pull them up to sit beside the headings.

The width: 100%; declarations ensure that each piece of text fills up the entire width to the right of the images, instead of each element shrinkwrapping to its content, as floats without declared widths do naturally.

The images have now moved up to sit beside the headings, but they overlap each other (Figure 9.15). This is because the list items contain only floated content now, which is out of the flow, and have thus collapsed down to zero height.

Figure 9.15 Floating the text elements to the right allows the images to sit beside the headings, but the list items will not expand to hold the full height of the thumbnails when everything inside the list items is floated.

To address this, we need to use a float containment method to get each list item to encompass all of the floated elements within it. Floating the li elements themselves is one easy way to do this:

li {
	float: left;
	width: 100%;
	margin: 0 0 20px 0;
	padding: 0;
}

The list items are now properly spaced out from each other, whether the text within them is shorter or longer than the thumbnail images (Figure 9.16).

The only problem is that floating the list items made the images disappear in IE 6 and earlier. To fix this, add position: relative; to both the li and img rules:

li {
	float: left;
	width: 100%;
	margin: 0 0 20px 0;
	padding: 0;
	position: relative;
}
img {
	float: left;
	margin-left: -115px;
	position: relative;
}

Figure 9.16 No matter which is longer—thumbnail or accompanying text—the list items remain spaced out from each other.

Thumbnail Image Galleries

Although images are usually fixed in width, you can line them up side by side and still create a block of image thumbnails that can change in total width. You saw an example of this in Figure 2.41, where the thumbnails wrapped onto a variable number of lines to accommodate the liquid width of the content area. Another way to create a flexible image gallery is to make all of the thumbnails scale, using one of the scalable image techniques you learned at the start of the chapter. Let’s go over both options.

Wrapping the Thumbnails

The two behaviors you want thumbnails in a flexible image gallery to achieve—sitting side by side and wrapping onto more lines as needed—are both native behaviors of floats. So, the only thing you need to do to make thumbnails wrap is to float each one in the same direction.

You could just place the images straight into the (X)HTML with no container, and float each of the img elements. But a more semantic way to mark up a group of images is to use an unordered list, which offers you more styling possibilities as well. Put each img into an li element:

<ul>
	<li><img src=”january.jpg” alt=”January” width=”100” height=”100”> </li>
	<li><img src=”february.jpg” alt=”February” width=”100” height=”100”> </li>
	<li><img src=”march.jpg” alt=”March” width=”100” height=”100”> </li>
	<li><img src=”april.jpg” alt=”April” width=”100” height=”100”> </li>
	<li><img src=”may.jpg” alt=”May” width=”100” height=”100”> </li>
	<li><img src=”june.jpg” alt=”June” width=”100” height=”100”> </li>
	<li><img src=”july.jpg” alt=”July” width=”100” height=”100”> </li>
</ul>

Next, remove the default list styling:

ul {
	margin: 0;
	padding: 0;
	list-style: none;
}
li {
	margin: 0;
	padding: 0;
}

Now, simply float the li elements all to the left, and give them some margin on their right and bottom sides to space them out from each other:

li {
	float: left;
	margin: 0 10px 10px 0;
	padding: 0;
}

That’s all you need to do to create a basic, wrapping thumbnail image gallery (Figure 9.17). The perfect number of thumbnails always sits on each line, no matter the viewport width, so you don’t get a horizontal scrollbar or a really large gap on the right. If you didn’t want the gallery to take up the entire width of its parent, simply assign a width to the ul element; as long as the width is a percentage or em value, the list will still be flexible and the thumbnails will still wrap.

Figure 9.17 The number of thumbnails on each line adjusts to the space available in the viewport.

You may have noticed that all of the thumbnails in this example share the same dimensions. Variable widths on thumbnails are not a problem, but variable heights make this wrapping thumbnail technique fail. Figure 9.18 shows the same page with the height of some of the thumbnails increased. When the thumbnails wrap, they move as far over to the left as they can go. But when one of the thumbnails in the previous row hangs down farther than the rest, it impedes the new row of thumbnails from moving any further to the left, and big gaps can be left in the rows.

Figure 9.18 The extra height on the second thumbnail blocks the fifth thumbnail from moving all the way to the left, leaving a gap in the second row. The same problem happens in the third row.

There are a couple ways you can modify the basic technique to work with variable height thumbnails. The simplest is to assign a fixed height to the li elements that matches the height of the tallest thumbnail. This makes all the li elements match in height, instead of depending on the size of the images inside them to dictate their heights, so there are no taller list items sticking down any more that might block the wrapping thumbnails.

If you can’t assign a fixed height to the li elements, though, perhaps because your thumbnails are pulled into the page dynamically and you don’t know what the largest height will be, there’s still hope. You’ll need to use something other than floats to get the thumbnails sitting side by side—and that something is display: inline-block.

An inline block mixes the attributes of block and inline elements. It’s placed on the same line as adjacent content, like inline elements are, but you can assign it width, height, margin, and padding, just like a block element.

Since inline block elements sit side by side by default, when you apply a display value of inline-block to the li elements, you can get rid of the float declaration:

li {
	display: inline-block;
	margin: 0 10px 10px 0;
	padding: 0;
}

In browsers that support inline-block, that’s all you need to do to keep the thumbnails from hanging up on each other when they wrap (Figure 9.19). If you want the thumbnails aligned along their top edges, as they were when we used floats, simply add vertical-align: top; to the li rule.

Figure 9.19 When the thumbnails are turned into inline blocks, instead of floats, they no longer hang up on one another.

In browsers that don’t support inline-block, the thumbnails will just display straight down, each on its own line. These browsers include versions of IE earlier than 8 and versions of Firefox earlier than 3. Let’s take care of the IE problem first.

IE 7 and 6 support inline-block only on elements that are inline by default, so you can trick these browsers into making inline-block work by setting the list items to display: inline. Hide this rule inside a conditional comment that only IE 7 and earlier can read:

<!--[if lte IE 7]>
<style type=”text/css”>
li {
	display: inline;
}
</style>
<![endif]-->

This fixes the problem in IE; now onto Firefox.

Versions of Firefox prior to 3 lacked support for inline-block but had their own proprietary values, -moz-inline-box and -moz-inline-stack, for the display property that worked almost identically. Add either of these values to the li rule:

 li {
	display: -moz-inline-box;
	display: inline-block;
	margin: 0 10px 10px 0;
	padding: 0;
	vertical-align: top;
}

This fixes the problem in Firefox 2 without hurting any other browsers, including Firefox 3—they all just ignore the -moz-inline-box value. If you have links wrapped around the images, however, you’ll have just a bit more work to do. Firefox 2 will position the images incorrectly and not make the entire image clickable when you nest a elements inside the li elements. To fix this, turn the a elements into blocks:

li a {
	display: block;
}

Again, this doesn’t hurt other browsers.

Scaling the Thumbnails

If you want the thumbnails in your image gallery to scale instead of—or in addition to—wrapping, you need to add the scalable foreground image technique (that we went over earlier in the chapter) to the basic wrapping thumbnail gallery CSS.

The first step in making scalable foreground images, you may remember, is to remove the width and height dimensions from the img elements in the (X)HTML:

<ul>
	<li><img src=”january.jpg” alt=”January”></li>
	<li><img src=”february.jpg” alt=”February”></li>
	<li><img src=”march.jpg” alt=”March”></li>
	<li><img src=”april.jpg” alt=”April”></li>
	<li><img src=”may.jpg” alt=”May”></li>
	<li><img src=”june.jpg” alt=”June”></li>
	<li><img src=”july.jpg” alt=”July”></li>
</ul>

Next, add a percentage or em width onto the li elements:

li {
	float: left;
	width: 18%;
	margin: 0 10px 10px 0;
	padding: 0;
}

Finally, add a rule for the img elements that sets their widths to 100 percent so they always fill up the variable size of their parent list items:

img {
	width: 100%;
}

The thumbnails now scale with the browser window (Figure 9.20).

Figure 9.20 The thumbnails still wrap to a small degree, but now their primary method of adjusting to the viewport is to scale with it.

If you want to avoid the blurriness or pixelation that happens when browsers scale images past their native dimensions, you can add a maximum width onto the images that matches their pixel widths:

img {
	width: 100%;
	max-width: 100px;
}

When the thumbnails reach this max-width value, they will stop scaling. The list items, however, will not, so the images will appear to move farther apart from each other, still filling up the available space (Figure 9.21).

Figure 9.21 Once the thumbnails reach their maximum widths, they will stop scaling, but will still adjust to the viewport size by moving farther apart to fill the space.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020