如何停止背景颜色覆盖整个屏幕

how to stop background-color covering whole screen

我不希望我的背景颜色覆盖整个屏幕。我该怎么做?

<body>

<h1 style = "color:blue;text-align:center;background-color:green;">text</h1>

</body>

webpage

h1 是一个块元素,你可以添加 display:inline-block ,如果你想要它是 cetnred 你可以将它包装在 div 和 text-align:center;

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<body >
<div style="text-align:center;">
<h1 style = "color:blue;background-color:green;display:inline-block">text</h1>
  </div>
</body>
</body>
</html>

将文本换行并为该跨度应用背景颜色

将 h1 放在 div 中并设置 div 的宽度。

创建一个跨度并将您的样式应用到它。

<body>

<h1 style = "color:blue;text-align:center;">

<span style="background-color:green;">text</span>
</h1>

</body>

h1 是块级元素,大多数浏览器将块级元素的宽度设置为 100%。你应该使用 flex CSS 来解决这个问题:

.container {
  display: flex;
  justify-content: center;
}

.head-tag {
  color:blue;
  background-color:green;
}
 <body>
  <div class="container">
    <h1 class="head-tag">text</h1>
  </div>
</body>

    

有时包装内容不是一种选择(例如,对于内部带有 iframe 的文本编辑器组件),因此您可以尝试以下解决方法:

body {
  background: #f00;
  background: linear-gradient(180deg, #f00 100%, rgba(255,255,255,1) 100%);
  background-repeat: no-repeat;
}