Table of Contents
Example
Inline block used to create navigation links
Home Web Front-end CSS Tutorial How to make a div element appear inline using CSS?

How to make a div element appear inline using CSS?

Sep 19, 2023 pm 04:45 PM

如何使用 CSS 使 div 元素内联显示?

CSS stands for Cascading Style Sheet, which specifies how HTML elements appear in various media, including print, display, and other print and digital formats ) in appearance. You can save a lot of work with CSS. It can manage the design of multiple web pages simultaneously.

In this article, we will learn how to make a div element appear inline using CSS. To do this, we first need to understand some CSS properties used to make a div element appear inline -

  • Display - The display attribute specifies the element's rendering box type (display behavior). Here we will use display: flex and display: inline-block properties.

  • Float - Using the float property, you can tell an element to float to the left, float to the right, or not float at all. Here we will use the float left property to display a div floating to the left.

  • inline The element does not start on a new line and only takes the required width. You cannot set width and height.

.inline-element {
   display: inline;
   width: 1000px;
   height: 1000px;
}
Copy after login

The following are some elements with default inline attributes -

  • span

  • one

  • img

Format tags that are essentially inline -

  • them

  • powerful

  • I

  • Small

Inline-block Format inline elements that do not start on a new line. However, you can set width and height values.

.inline-block-element {
   display: inline-block;
   width: 1000px;
   height: 1000px;
}
Copy after login
  • block The element starts on a new line and uses all available width. You can set values ​​for width and height.

    The following are some elements with default block attributes -

    • div

    • h1

    • p

    • li

    • part

In order to make the div component display inline, we will first build some basic HTML code and apply various CSS styles.

Example

In this example, the parent div of all div elements has the display: flex and flex-direction: row settings set. Thanks to the flex-direction: row attribute, all components contained within the parent div will appear in one row.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      .main {
         display: flex;
         flex-direction: row;
         font-size: 30px;
         color: red;
         border: 4px double red;
         padding: 5px;
         width: 400px;
      }
      .main div {
         border: 2px solid greenyellow;
         margin: 10px 20px;
         width: 100px;
      }
   </style>
</head>
<body>
   <div class="main">
      <div>Hello, World!</div>
      <div>Hello, World!</div>
      <div>Hello, World!</div>
   </div>
</body>
</html>
Copy after login

Example

In this example, we need to add the display: inlineblock attribute to all divs that need to be displayed inline. If the display:inlineblock attribute is applied, all div components will be placed side by side.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      div {
         display: inline-block;
         color: red;
         border: 2px solid greenyellow;
         margin: 10px 20px;
         width: 120px;
         font-size: 40px;
      }
   </style>
</head>
<body>
   <div>Hello, World!</div>
   <div>Hello, World!</div>
   <div>Hello, World!</div>
</body>
</html>
Copy after login

Example

In this example, in order to display all div elements inline, we will give them the float: left attribute. Additionally, users can utilize the float: right CSS option to display all div components in reverse order starting from the right.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      div {
         float: left;
         color: red;
         border: 2px solid greenyellow;
         margin: 10px 20px;
         width: 120px;
         font-size: 40px;
      }
   </style>
</head>
<body>
   <div>Hello, World!</div>
   <div>Hello, World!</div>
   <div>Hello, World!</div>
</body>
</html>
Copy after login

Example

This method uses span elements instead of div elements. If the user only needs to write text in a div tag, the span tag can be used since all span elements are displayed inline by default.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      span {
         color: green;
         border: 2px solid red;
         margin: 10px 20px;
         width: 100px;
         font-size: 30px;
      }
   </style>
</head>
<body>
   <span>Hello World!</span>
   <span>Hello World!</span>
   <span>Hello World!</span>
</body>
</html>
Copy after login

The main difference with display: inline is that you can use display: inline blocks to set the width and height of elements.

Also preserve display:inline blocks, preserve top and bottom margin/padding, but not preserve in display:inline. The main difference with display: block is that display: inlineblock does not add a newline after the element, so one element can be located next to another element.

The following code snippet demonstrates the different behaviors of display: inline, display: inline-block and display: block.

span.a {
   display: inline; /* the default for span */
   width: 100px;
   height: 100px;
   padding: 5px;
   border: 1px solid blue;
   background-color: yellow;
}
span.b {
   display: inline-block;
   width: 100px;
   height: 100px;
   padding: 5px;
   border: 1px solid blue;
   background-color: yellow;
}
span.c {
   display: block;
   width: 100px;
   height: 100px;
   padding: 5px;
   border: 1px solid blue;
   background-color: yellow;
}
Copy after login

Common display usage: Inline blocks are used to display list items horizontally instead of vertically. The following example creates a horizontal navigation link.

.nav {
   background-color: yellow;
   list-style-type: none;
   text-align: center;
   padding: 0;
   margin: 0;
}
.nav li {
   display: inline-block;
   font-size: 20px;
   padding: 20px;
}
Copy after login

The above is the detailed content of How to make a div element appear inline using CSS?. For more information, please follow other related articles on the PHP Chinese website!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

See all articles