如何使用 CSS 将段落元素显示为内联?

cssweb developmentfront end technology更新于 2024/7/3 6:04:17

CSS 具有内部样式和内联样式,可用于以内联方式显示段落元素。CSS 是一种用于设置 HTML 和 XML 文本样式的语言。更改 HTML 元素的 display 属性是 CSS 的典型工作。元素的 display 属性指示其应如何呈现,例如 block、inline 或 inline-block。当以内联方式显示段落元素时,必须将 display 属性从其默认的 block 值修改为 inline。

在这里,我们将学习如何编写 CSS 代码,将段落元素显示为内联。

语法

<p style="display : inline">..编写段落...</p>

HTML 中的段落用 p 标签表示。 display 属性定义内容的流动方式。

设置为"inline"显示的元素将显示为行内框,这意味着它将在一行文本内流动。

相反,"block"元素将填充其父容器的整个宽度,并在任何现有内容下方添加一行新内容。

标题、段落和 div 元素是块级元素的示例,而链接、span 元素和图像是行内元素的示例。

使用的属性

示例中使用的属性如下:

  • color - 定义文本的颜色。

  • background-color - 定义文本的粗细。

  • display - display 属性指定显示行为。

示例

在本例中,我们将开始使用段落元素设置几行文本。为了将段落元素显示为内联,我们将使用内联 CSS 将 display 属性的值设置为 inline。然后设置一些元素的样式,例如 body、p,这些元素用于在网页上与用户交互。

<!DOCTYPE html>
<html>
<title>Display paragraph elements as inline using CSS</title>
<head>
</head>
<body style="background-color: powderblue;">
   <center>
      <h1>Rich Dad and Poor Dad</h1>
   </center>
   <p style="background-color:#a89032; color: white; font-style: italic; display: inline;">Rich
      Dad Poor Dad, by Robert Kiyosaki, was initially published in 1997 and immediately became a must-read
      for anybody interested in investment, money, or the global economy. The book has been translated into
      dozens of languages and sold all over the world, becoming the best-selling personal finance book of all
      time. Rich Dad Poor Dad's overriding theme is how to use money as a tool for wealth growth. It debunks
      the idea that the wealthy are born wealthy, explains why your personal residence may not be an asset,
      defines the distinction between an asset and a liability, and much more.
   </p>
   <p style="font-weight: bold; text-align: right; display: inline; color: darkgreen;">@tutorialspoint.com</p>
</body>
</html>

结论

在上面的输出中,我们可以看到如何利用 CSS 显示属性中的内联值来控制文本样式的布局。这对于测试显示段落元素的内联更改是一个好主意。为了保持文本位置的灵活性,有时更改显示属性是正确的。


相关文章


有用资源