帮助将绝对定位的图像 CSS 固定在浏览器的左上角

发布于 2024-08-08 16:19:37 字数 1757 浏览 8 评论 0原文

我是CSS的初学者,我反复尝试将图像(透明png)放置在居中的表格上,但由于某种原因,图形不会从浏览器的左上角移动。绝对定位应该允许我将元素放置在我想要的任何地方。如果有人可以帮助确定我做错了什么,我将非常感激。

------------------------------------ html------------- -----------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Welcome to Knock on Wood Productions!<title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />

</head>

<body>
<div id="branch">

</div>

    <table width="803" border="0" cellpading="0" cellspacing="0" align="center">
    <tr>

        <!-- The 1st nested table starts here. -->

        <td width="355" height="180" background= "images/grn1.png">
 </td>
        <td width="10" height="180" background= "images/grn2.png"></td>
        <td width="438" height="180" background= "images/grn3.png"></td>
    </tr>    
    </table>

</body>

</head>

--------------------------- --------- CSS ------------------------------------

body                {   background-color: #fff8a7;

                       }

#wrapper         {  position: absolute; width: 803;
                        margin-left: auto ;
                         margin-right: auto ;


                      }

#branch          {  position: absolute; width: 548;
                    top: 60;
                        left: 50;
        /* background-image: url(images/gbranch.png); */
        z-index: 2;
                      }

td                { border: solid thin red;

I am a beginner to all things css and I've tried repeatedly to position an image(transparent png)over a centered table, but for some reason the graphic won't budge from the upper left corner of the browser. Absolute positioning should allow me to place the element anywhere I want. If someone could help determine what I've done wrong, I'd really appreciate it.

------------------------------------ html------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Welcome to Knock on Wood Productions!<title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />

</head>

<body>
<div id="branch">

</div>

    <table width="803" border="0" cellpading="0" cellspacing="0" align="center">
    <tr>

        <!-- The 1st nested table starts here. -->

        <td width="355" height="180" background= "images/grn1.png">
 </td>
        <td width="10" height="180" background= "images/grn2.png"></td>
        <td width="438" height="180" background= "images/grn3.png"></td>
    </tr>    
    </table>

</body>

</head>

------------------------------------ css ------------------------------------

body                {   background-color: #fff8a7;

                       }

#wrapper         {  position: absolute; width: 803;
                        margin-left: auto ;
                         margin-right: auto ;


                      }

#branch          {  position: absolute; width: 548;
                    top: 60;
                        left: 50;
        /* background-image: url(images/gbranch.png); */
        z-index: 2;
                      }

td                { border: solid thin red;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-08-15 16:19:37

为父元素(在您的情况下为居中表格)指定“位置:相对”。

Give the parent element (in your case the centered table) a "position: relative".

青柠芒果 2024-08-15 16:19:37

您为宽度、顶部和左侧属性指定了没有单位的长度。您应该这样写:

#branch {
   width: 548px;
   top: 60px;
   left: 50px;
}

另外,您可能还想指定该元素的高度。

You specified lengths without units for the width, top and left properties. You should write:

#branch {
   width: 548px;
   top: 60px;
   left: 50px;
}

Also you might want to specify an height for that element, too.

鹊巢 2024-08-15 16:19:37

Fumblr 的权利 - 声明高度至少等于图像的高度。 Fumblr 的代码应该可以工作。

@Fumblr - 我会给你竖起大拇指,但我还没有被允许!

Fumblr's right - declare a height equal to the height of the image at least. Fumblr's code should work.

@Fumblr - I'd give you the thumbs up but I'm not allowed yet!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文