What does the display attribute in css mean? how to use? [Example summary]

藏色散人
Release: 2018-08-29 11:43:41
Original
20092 people have browsed it

When we develop web front-end, in order to make the website more visually beautiful and richer in effect, the powerful properties of css naturally play an indispensable role. Then here is a crucial attribute to mention, the css display attribute. So some novices may ask, what does display in css mean? How to use css display?

This article will give you a detailed introduction to the specific usage of the css display attribute, hoping to solve your questions. (In order to make it easier for everyone to understand this article, it is recommended to read my article [How are html elements distinguished?] which has a detailed introduction to block-level elements and inline elements)

First, let me list several commonly used values ​​​​of css display: none, block, inline, inline-block

Next, we will explain the usage of css display attributes in detail through specific code examples.

1. Introduction to specific examples of css display block attributes

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css display:block属性实例</title>
    <style>
        *{padding: 0;margin:0;list-style: none;}
        ul li{float: left;}
        a{
        display:block; 
        width: 30px;height: 30px;
        color:#fff; 
        background: green;margin: 5px; 
        text-decoration: none;
        text-align: center;
        line-height: 30px;
        }
    </style>
</head>
<body>
<div class="demo">
    <ul>
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
        <li><a href="">3</a></li>
        <li><a href="">4</a></li>
        <li><a href="">5</a></li>
    </ul>
</div>
</body>
</html>
Copy after login

The above code is accessed through the browser, and the effect is as shown in Figure 1:

What does the display attribute in css mean? how to use? [Example summary]

We comment out the display:block; attribute in the a tag, and the effect is as shown in Figure 2:

What does the display attribute in css mean? how to use? [Example summary]

Then through Figure 1 and Figure Comparing 2, we can find that in Figure 1, because the display:block attribute is set, the element block in a can display the width, height, and inner and outer margin settings. In Figure 2, just the display block attribute is removed, which results in the width and height not being set for all element blocks.

That is to say, the display block attribute can set the inline element to a block-level element, and then set its width, height, top, bottom, left, and right margins, padding, and margin. We can use this attribute to achieve the effect we want.

2. Specific examples of css display none attribute

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css display:none属性实例</title>
    <style>
        *{padding: 0;margin:0;list-style: none;}
        ul li{float: left;}
        a{
            display: none; width: 30px;height: 30px;color:#fff; background: green;margin: 5px; text-decoration: none;text-align: center;line-height: 30px;}
    </style>
</head>
<body>
<div class="demo">
    <p>a元素不会显示出来</p>
    <ul>
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
        <li><a href="">3</a></li>
        <li><a href="">4</a></li>
        <li><a href="">5</a></li>
    </ul>
</div>
</body>
</html>
Copy after login

The effect is as shown in Figure 3:

What does the display attribute in css mean? how to use? [Example summary]

As can be seen from Figure 3, after we set the display:none; attribute to a, all its elements are hidden and will not be displayed. So it is obviously decided that the display attribute value is the none attribute. display none is usually used in the design of the first and second columns of the navigation bar.

3. Introduction to css display inline attribute

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css display:inline属性实例</title>
    <style>
        p {display: inline}
     </style>
</head>
<body>
    <p>p标签是块级元素。</p>
    <p>此时显示为内联元素,不换行。</p>
</body>
</div>
</body>
</html>
Copy after login

The effect is as shown in Figure 4:

What does the display attribute in css mean? how to use? [Example summary]

You can see from Figure 4 You know, the css display inline attribute can turn an element into an inline element, that is, an inline element, and there are no line breaks before and after the element.

4. Introduction to css display inline-block attribute

As the name suggests, the inline-block attribute definitely combines the characteristics of the inline and block attributes, which means that this attribute can Turn the element into an inline block element. You can not only display the specified elements in the same line, but also set the width and height of their elements.

Then this article is an introduction to the specific usage of the css display attribute, including specific usage examples of none, block, inline, and inline-block. It has certain reference value and I hope it will be helpful to friends in need.

The above is the detailed content of What does the display attribute in css mean? how to use? [Example summary]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template