How to make a div element appear inline using CSS?

WBOY
Release: 2023-09-19 16:45:04
forward
1367 people have browsed it

如何使用 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

Inline block used to create navigation links

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!

source:tutorialspoint.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!