背景颜色仅在文本字段上

时间:2016-12-25 15:52:12

标签: html css

这是我的代码:

HTML:

<div id="hello">
Hello
</div>

CSS:

#hello {
  font-size:30px;
  background-color:red;
}

正如您在小提琴(https://jsfiddle.net/en27xg3b/)中看到的那样,背景颜色在所有div上。我希望它只是“你好”的地方。

我很确定这是一个简单的代码,但我忘了怎么做。

4 个答案:

答案 0 :(得分:2)

将文字包裹在范围内。喜欢这个

#hello span{
  font-size:30px;
  background-color:red;
}
<div id="hello">
  <span>Hello</span>
</div>

答案 1 :(得分:1)

  1. 使用<?php //1. create a database connection $dbhost = "localhost"; $dbuser = "widget_cms"; $dbpass = "funkyguy"; $dbname = "widget_corp"; $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); // Test database connection if(mysqli_connect_errno()) { die("database connection failed: " . mysqli_connect_error() . "(" . mysqli_connect_errno() . ")" ); } ?> <?php //this are the form values from $_POST $id = 6; $menu_name = "Delete me"; $position = 4; $visible = 1; //2. perform database query $query = "UPDATE subjects SET "; $query .= "menu_name = '{$menu_name}', "; $query .= "position = {$position}, "; $query .= "visible = {$visible}, "; $query .= "WHERE id = {$id}"; echo $query; $result = mysqli_query($connection, $query); // test if there was a query error if($result) { echo "success"; }else { die(" Database query failed: " . mysqli_error($connection)); } ?> <!doctype html> <html lang="en"> <head> <title>Database</title> </head> <body> </body> </html> <?php //5. Close the connection mysqli_close($connection); ?> inline-blockhttps://jsfiddle.net/en27xg3b/1/
  2. inline
    #hello {
      font-size:30px;
      background-color:red;
    display: inline-block;
    }

    1. 或者将文字换成<div id="hello">Hello</div>https://jsfiddle.net/en27xg3b/2/
    2. span
      #hello span {
        font-size:30px;
        background-color:red;
      }

答案 2 :(得分:1)

<div id="hello"><span>Hello</span></div>标记显示为<div>,因此它填满了背景。

在这种情况下,您只需为block代码

设置display : inline;

喜欢这个https://jsfiddle.net/en27xg3b/4/

答案 3 :(得分:0)

我会看看http://www.w3schools.com/cssref/tryit.asp?filename=trycss_text_background

但你可以做的是有一种风格

<style>
span.highlight {
  background-color:red;
}
</style>

现在在你的HTML中你可以做到

<div id="hello">
  <span class="highlight">
    Hello
   </span>
   More Text
</div>