search
HomeWeb Front-endCSS TutorialWhat does the display attribute in css mean? how to use? [Example summary]

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>

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>

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>

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!

Statement
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
How much specificity do @rules have, like @keyframes and @media?How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AM

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Can you nest @media and @support queries?Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AM

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

Quick Gulp Cache BustingQuick Gulp Cache BustingApr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In Search of a Stack That Monitors the Quality and Complexity of CSSIn Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AM

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Datalist is for suggesting values without enforcing valuesDatalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AM

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

Front Conference in ZürichFront Conference in ZürichApr 18, 2025 am 11:03 AM

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

Building a Full-Stack Serverless Application with Cloudflare WorkersBuilding a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AM

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

Creating Dynamic Routes in a Nuxt ApplicationCreating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AM

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools