问答详情
源自:3-4 改变 HTML 样式

怎样让字居中

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>改变html样式</title>
</head>
<body>
<h1 id="con">这是一首简单的小情歌</h1>
<script type="text/javascript">
var char=document.getElementById("con");
char.style.color="black";
char.style.backgroundColor="red";
char.styke.fontsize="25px";
char.style.width="100px";
char.style.margin="auto";

</script>
</body>
</html>

提问者:digit 2016-11-19 10:09

个回答

  • 甜甜圈1991
    2016-11-21 16:20:22
    已采纳

    补充上面回答的问题,没看清楚您问的是字体居中,以下是效果和代码,望采纳:

    5832ae020001cb9505000166.jpg

    5832ae020001fda505000409.jpg


  • 甜甜圈1991
    2016-11-21 16:05:35

    您写的源码有一个style写错了,所以下面的属性没执行,其实您的效果已经居中了

    代码:

    5832aa7b0001db0c05000250.jpg

    效果:

    5832aa82000167b305000186.jpg


  • 疯狂的boy
    2016-11-19 11:03:03

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>练习页面</title>
        <script type="text/javascript" src="jQuery/jquery.js"></script>

        <style type="text/css">
            * {
                margin: 0px;
                padding: 0px;
            }
    //加个css样式就好了
            h1 {
                text-align: center;
            }
        </style>

        <script>  
        </script>

    </head>

    <body>

        <h1 id="con">这是一首简单的小情歌</h1>
        <script type="text/javascript">
            var char = document.getElementById("con");
            char.style.color = "black";
            char.style.backgroundColor = "red";
            char.styke.fontsize = "25px";
            char.style.width = "100px";
            char.style.margin = "auto";
        </script>
    </body>
    </html>