Which tag is used to create the definition list. HTML: Numbered and bulleted list. Special and nested lists in HTML code
Lists are part of everyday life. The to-do list defines what is done. Navigation routes offer a step-by-step list of directions. Recipes require a list of ingredients and a list of instructions. Lists are found almost everywhere, so it's easy to see why they are also popular on the Internet.
When we want to use a list on a website, HTML offers three different types to choose from: bulleted, numbered, and description list. Which type of list to use, and whether to use a list at all, depends on the content and the semantically most appropriate option for displaying it.
In addition to the three different types of lists available in HTML, there are several ways to style these lists via CSS. For example, we can choose what type of bullet to specify for the list. The marker may be a square, a circle, a number, a letter, or possibly none. In addition, we can decide how the list should be displayed - vertically or horizontally. All of these options play an important role in styling our web pages.
Bulleted lists
A bulleted or unordered list is simply a list of related items for which the order does not matter. Creating a bulleted list in HTML is done using a block list element
- . Each individual item in the list is marked with an element
-
.
By default, most browsers add vertical margin and left padding to the element
- , and before each element
- puts a single-color point. This point is called a list marker and can be changed using CSS.
- Orange
- Green
- Blue
Show bulleted list
Numbered lists
Numbered or ordered list of items
- Very similar to a bulleted list, individual bullet points are created in the same way. The main difference between lists is that for an ordered list, the order in which the items are presented matters.
- Walk along Apricot Street
- Turn onto Vinogradnaya
- Walk along Apricot Street
- Turn onto Vinogradnaya
- Stop on Shady Street
- Walk along Apricot Street
- Turn onto Vinogradnaya
- Stop on Shady Street
- in a numbered list to change its value in the list. The number of any list item that appears below a list item with a value attribute will be recalculated accordingly.
As an example, if the second list item has a value attribute set to 9, the number of that list item will be output as if it were the ninth one. All subsequent list items will be numbered starting from 9.
- Walk along Apricot Street
- Turn onto Vinogradnaya
- Stop on Shady Street
Value attribute demonstration
Description Lists
Another type of list you'll see online (but not as often as bulleted or numbered lists) is a list of descriptions. Such lists are used to identify several terms and their descriptions, as in a glossary, for example.
Creating a description list in HTML is done using a block element
- . Instead of using element
- To mark up list items, a description list requires two block elements:
- for the term and
- for description.
A description list can contain many terms and descriptions, one after another. In addition, such a list may contain several terms per description, as well as several descriptions per term. A single term can have multiple meanings and be subject to multiple descriptions. Conversely, one description may fit several terms.
When adding a description list element
- must go to the element
- . The term and the description that immediately follows it are related to each other. Therefore, the order of these elements is important.
By default, element
- includes vertical padding, like elements
- includes the left margin by default.
- studies
- Devoting time and attention to gaining knowledge of the subject being taught, especially through books.
- project
- A submitted plan or drawing that shows how a building, clothing, or other item will look and function before it is built or made.
- Existing goals, plans, or intentions before they are embodied or realized in a tangible object.
- business
- Job
- A person's regular occupation, profession or craft.
Description List Demonstration
Nested Lists
The feature that makes lists very powerful is the nesting feature. Each list can be nested within another list and they can be nested multiple times. But the ability to nest lists endlessly does not give you the freedom to do so. Lists should be reserved specifically where they retain the most semantic meaning.
The trick to nesting lists is to know where each list and list item begins and ends. Speaking specifically about bulleted and numbered lists, the only element that can appear directly inside
- And
- . Let's repeat - the only element that we can put as a direct descendant of the elements
- And
-
.
However, inside the element
- a standard set of elements can be added, including any elements
- or
- Walk the dog
- Fold laundry
- Go to the store and buy:
- Milk
- Bread
- Cheese
- Mow the lawn
- Cook dinner
- . Element
- can contain any regular element you want. However, the element
- must be a direct descendant of either element
- , or
-
.
Any value for the list-style-type property can be added to a bulleted or numbered list. With this in mind, you can use numbering in a bulleted list and non-numeric bullets in a numbered list.
- Orange
- Green
- Blue
Ul ( list-style-type: square; )
Demonstration of the list-style-type property
list-style-type values
As mentioned earlier, the list-style-type property contains a handful of different values. The following table shows these values and their corresponding contents.
Using an Image as a List Marker
There may come a time when the default values for the list-style-type property are not enough and we want to define our own list marker. This is most often done by placing a background image for each element.
-
.
The process involves removing any default list-style-type property value and adding a background image and margins to the element
-
.
More details - setting the list-style-type property to none will remove existing list markers. The background property will set the background image along with its position and repeat if necessary. And the padding property will provide space to the left of the text for the background image.
- Orange
- Green
- Blue
Li ( background: url("arrow.png") 0 50% no-repeat; list-style-type: none; padding-left: 12px; )
Showing an Image as a Marker
list-style-position property
By default, list bullets are positioned to the left of the content in the element
- . This positioning style is described as outside , which means that all content will appear directly to the right, outside the list marker. With the list-style-position property we can change the default value of outside to inside or inherit .
outside places the list marker to the left of the element
- and do not allow any content to flow below this marker. The inside value (which is rarely used and can be seen) places the list marker on the first line of the element
- and allows the content to wrap around the marker if necessary.
- Cupcakes...
- Sprinkling...
Ul ( list-style-position: inside; )
List-style-position property demonstration
Generic property list-style
The list properties we recently discussed, list-style-type and list-style-position , can be combined into one generic property, list-style . In this property, we can use one or all of the list property values at the same time. The order of these values should be: list-style-type followed by list-style-position .
Ul ( list-style: circle inside; ) ol ( list-style: lower-roman; )
Horizontal list display
Sometimes we want to display lists horizontally rather than vertically. Perhaps we want to divide the list into several columns to build a navigation list or place several list items in one row. Depending on the content and desired appearance, there are several ways to display lists as a single line, for example by taking the display property value of the elements
- like inline or inline-block or via the float property.
List display
The fastest way to display a list on one line is to give the elements
- display property with the value inline or inline-block . This will put all the elements
- on one line with equal spacing between each list item.
If the spaces between elements
- are causing problems, they can be removed using the same techniques we discussed in Lesson 5, Positioning Content.
Much more often we will use the inline-block value instead of the inline value. The inline-block value makes it easy to add vertical padding and other space to elements
- , while the inline value does not.
When the value of the display property is changed to inline or inline-block , the list marker, whether a dot, number, or other, is removed.
- Orange
- Green
- Blue
Li ( display: inline-block; margin: 0 10px; )
Demonstration of a list with inline-block
Lists with floats
Changing the display property to inline or inline-block is quick, but it removes the list markers. If they are needed then adding a float to each element
- is a better option than changing the display property.
Installation for all elements
- float properties like left will align all elements horizontally
- directly next to each other without any gaps between them. When we use float for
- , the list marker is displayed by default and will be positioned on top of the item
- next to him. To prevent the list marker from appearing on top of other items
- , horizontal margin or padding must be added.
- Orange
- Green
- Blue
Li ( float: left; margin: 0 20px; )
Demonstration of a list with float
As with any floated elements, this breaks the flow of the page. We must not forget to clear the float and return the page to normal flow - the most common method is via clearfix.
Navigation list example
We often design and find navigation menus that use unordered lists. These lists are typically laid out horizontally using one of the two methods mentioned earlier. Here, for example, is a horizontal navigation menu laid out using an unordered list, in which the elements
- are displayed as inline-block .
Navigation ul ( font: bold 11px "Helvetica Neue", Helvetica, Arial, sans-serif; margin: 0; padding: 0; text-transform: uppercase; ) .navigation li ( display: inline-block; ) .navigation a ( background: #395870; background: linear-gradient(#49708f, #293f50); border-right: 1px solid rgba(0, 0, 0, .3); color: #fff; padding: 12px 20px; text-decoration: none; ) .navigation a:hover ( background: #314b60; box-shadow: inset 0 0 10px 1px rgba(0, 0, 0, .3); ) .navigation li:first-child a ( border-radius: 4px 0 0 4px; ) .navigation li:last-child a ( border-right: 0; border-radius: 0 4px 4px 0; )
Navigation List Demonstration
On practice
Now that we know how to create lists in HTML and CSS, let's take a look at our Styles Conference site and see where we could use lists.
- ) will set the structure of our navigation menus. These new elements, however, will render our navigation menus vertically.
We're going to change the display value for our elements
- to inline-block so that they all line up horizontally. When we do this, we must also take into account the empty space on the left between each element
- . Recalling from Lesson 5, Positioning Content, we know that opening an HTML comment at the end of an element
- and closing the comment at the beginning of the element
- will remove this space.
With that in mind, the markup for the navigation menu inside our element
will look like this: In the same vein, markup for the navigation menu inside our element
Don't forget to make these changes to all our HTML files.
With our bulleted list, let's make sure the list items are aligned horizontally and clean up their styles a bit. We'll use the existing nav class to specify our new styles.
Let's start by making sure that all the elements
- inside any element with a nav class attribute value were rendered as inline-block to enable horizontal margins and to allow elements to be aligned vertically.
Additionally, we will use the :last-child pseudo-class to determine the last element
- and reset its right margin to 0. This ensures that any horizontal space between elements
- and the edge of its parent will disappear.
In our main.css file, below the navigation styles, add the following CSS:
Nav li ( display: inline-block; margin: 0 10px; vertical-align: top; ) .nav li:last-child ( margin-right: 0; )
You're probably wondering why our list doesn't include any list bullets or styles by default. These styles were removed by resetting at the top of our style. If we look at the reset, we see that the elements
-
,
- include zero margin and padding, and for
- And
- list-style is set to none .
The navigation menu isn't the only place we'll use lists. We will also use them on some of our internal pages, including the Speakers page. Let's add some speakers to our conference.
In the speakers.html file, just below the intro section, we will create a new section where we will introduce all our speakers. By reusing some existing styles we will use the element
with a row class to wrap all our speakers and apply a white background and margins behind them. Inside an element we will add an element with the grid class to center our speakers on the page and this will allow us to include multiple columns as well.So far our HTML below the intro section looks like this:
Inside the grid, each speaker will be marked with its own element
, which includes two columns. The first column measures two-thirds of the element and will be marked with element . The second column measures the remaining third of the elementand will be marked using the element
- And
- include zero margin and padding, and for
Now navigation menus in elements
And Using an unordered list (via element
- ) and list items (via the element
- ) will set the structure of our navigation menus. These new elements, however, will render our navigation menus vertically.
-
.
It's also worth noting that when lists are nested inside other lists, their markers will change depending on the depth of the nesting. In the previous example, a bulleted list nested within a numbered list uses a circle instead of a dot as markers. This change occurs because the bulleted list is nested one level inside the numbered list.
Luckily, we can control how bullet points look at any level, which we'll look at next.
Styling List Items
Bulleted and numbered lists use list item bullets by default. For bulleted lists these tend to be solid colored dots, while for numbered lists these tend to be numbers. Using CSS, the style and position of these markers can be customized.
list-style-type property
The list-style-type property is used to set the contents of the list item marker. Available values range from squares and decimals to Armenian numbering and CSS styling can be added to elements
-
,
- or
-
.
-
.
To nest a list, start a new list before closing the list item. Once the nested list is complete and closed, close the enclosing list item and continue with the original list.
Demonstration of nested lists
Since nested lists can be a bit confusing and display unwanted styles if done incorrectly - let's take a quick look at them. Elements
- And
- can only contain elements
- , is
-
.
- is an element
- And
- . In addition, the element
Because order matters, a numbered list uses numbers instead of a period as the default bullet.
Numbered list demonstration
Numbered lists also have unique attributes available to them, including start and reversed.
start attribute
The start attribute specifies the number at which the numbered list should begin. By default, lists start at 1, but there may be situations where the list should start at 30 or some other number. When we use the start attribute on an element
- , then we can accurately determine from which number the counting of the numbered list should begin.
The start attribute accepts only integer values, although numbered lists may use different numbering systems, such as Roman numerals.
Demonstration of the start attribute
reversed attribute
The reversed attribute when added to an element
- allows the list to be displayed in reverse order. A list of five items numbered 1 to 5 can be reversed and numbered 5 to 1.
The reversed attribute is a Boolean attribute and as such it does not take on any value. It can be true or false. False is the default value, the value becomes true when the reversed attribute appears on the element
-
.
Demonstration of the reversed attribute
value attribute
The value attribute can be applied to individual elements
- puts a single-color point. This point is called a list marker and can be changed using CSS.