如何使用 CSS 将段落元素显示为内联?
CSS 具有内部和内联样式,可用于以内联方式显示段落元素。CSS 是一种用于为 HTML 和 XML 文本设置样式的语言。更改 HTML 元素的 display 属性是典型的 CSS 工作。元素的 display 属性指示其应如何呈现,例如块、内联或内联块。当以内联方式显示段落元素时,必须将 display 属性从其默认的块值修改为内联。
在这里,我们将学习如何开发一个 CSS 代码,该代码将段落元素显示为内联。
语法
<p style="display : inline">..write the paragraph...</p>
HTML 中的段落由 p 标签表示。display 属性定义内容如何流动。
设置为 display“inline”的元素将显示为内联级框,这意味着它将在文本行内流动。
相反,“块”元素将填充其父容器的整个宽度,并在任何现有内容下方添加新的一行内容。
标题、段落和 div 元素是块级元素的示例,而链接、span 元素和图像则是内联元素的示例。
使用的属性
示例中使用的以下属性为:
color - 定义文本的颜色。
background-color - 定义文本是细还是粗。
display - display 属性指定显示行为。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
在此示例中,我们将开始使用段落元素来设置几行文本。为了将段落元素显示为内联,它将使用 display 属性通过内联 CSS 将值设置为 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 的 display 属性的 inline 值的帮助下控制文本样式的布局。这是一个测试将 display 段落元素更改为内联的好方法。为了保持文本位置的灵活性,有时更改 display 属性是正确的。