Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 与背景图像的相对位置_Css_Position - Fatal编程技术网

Css 与背景图像的相对位置

Css 与背景图像的相对位置,css,position,Css,Position,我有一个我想要的div,我把它放在离顶部25%的位置。然而,25%是根据背景图像的大小而不是可视屏幕的大小来计算的。如何解决这个问题 更新:现在上页边距有效,但左页边距无效:( 有线索吗 body { background: #eeeeee url('pix/bg-noether-2.jpg') no-repeat center top; background-size: auto 100%; background-attachment: fixed; overf

我有一个我想要的div,我把它放在离顶部25%的位置。然而,25%是根据背景图像的大小而不是可视屏幕的大小来计算的。如何解决这个问题

更新:现在上页边距有效,但左页边距无效:( 有线索吗

body {
    background: #eeeeee url('pix/bg-noether-2.jpg') no-repeat center top;
    background-size: auto 100%;
    background-attachment: fixed;
    overflow: hidden;
    align: center;
} 

#container {
    background-color: #ffffe4;
    position: absolute;
    width: 776px;
    height: 400px;
    top: 25%;
    margin-left: auto;
    text-align: center;
    overflow: auto;
}
使用css属性:

div#myDiv {
position:absolute;
top: 25%;
}
在要放置在距可见屏幕顶部25%的俯冲位置。

使用css属性:

div#myDiv {
position:absolute;
top: 25%;
}
在俯冲时,要将其放置在距可见屏幕顶部25%的位置。

1)使用绝对定位:

#myDiv { position: absolute; top: 25%; }
2) 确保您的div不在另一个定位元素内(如果您对此不确定,只需将其放在
标记内,无其他内容)

1)使用绝对定位:

#myDiv { position: absolute; top: 25%; }

2) 确保您的div不在另一个定位元素内(如果您对此不确定,只需将其放在
标记内,无其他内容)

如果使用相对位置,则将从父元素计算百分比

如果使用绝对位置,百分比将根据屏幕大小计算

因此,尝试使用绝对位置而不是相对位置

编辑注释的答案,只需使用id包装器添加额外的div并更改位置,请参见下面的示例:

<html>
<head>
<style type="text/css">
body { 
background: #eeeeee url('pix/bg-noether-2.jpg') no-repeat center top;  
background-attachment: fixed; 
overflow: hidden; 
text-align: center; 
}
#wrapper {
position:absolute;
width:100%;
height:100%;
} 
#container { 
background-color: #ffffe4; 
position: relative; 
margin:0 auto;
width: 776px; 
height: 400px; 
top: 25%; 
text-align: center; 
overflow: auto; 
}

</style>
</head>
<body>
<div id="wrapper">
    <div id="container">
        bla bla bla
    </div>
</div>
</body>
</html>

正文{
背景:#eeeeeeeeurl('pix/bg-noether-2.jpg')无重复中心顶部;
背景附件:固定;
溢出:隐藏;
文本对齐:居中;
}
#包装纸{
位置:绝对位置;
宽度:100%;
身高:100%;
} 
#容器{
背景色:#ffffe4;
位置:相对位置;
保证金:0自动;
宽度:776px;
高度:400px;
最高:25%;
文本对齐:居中;
溢出:自动;
}
呜呜呜呜

如果使用相对位置,则将从父元素计算百分比

如果使用绝对位置,百分比将根据屏幕大小计算

因此,尝试使用绝对位置而不是相对位置

编辑注释的答案,只需使用id包装器添加额外的div并更改位置,请参见下面的示例:

<html>
<head>
<style type="text/css">
body { 
background: #eeeeee url('pix/bg-noether-2.jpg') no-repeat center top;  
background-attachment: fixed; 
overflow: hidden; 
text-align: center; 
}
#wrapper {
position:absolute;
width:100%;
height:100%;
} 
#container { 
background-color: #ffffe4; 
position: relative; 
margin:0 auto;
width: 776px; 
height: 400px; 
top: 25%; 
text-align: center; 
overflow: auto; 
}

</style>
</head>
<body>
<div id="wrapper">
    <div id="container">
        bla bla bla
    </div>
</div>
</body>
</html>

正文{
背景:#eeeeeeeeurl('pix/bg-noether-2.jpg')无重复中心顶部;
背景附件:固定;
溢出:隐藏;
文本对齐:居中;
}
#包装纸{
位置:绝对位置;
宽度:100%;
身高:100%;
} 
#容器{
背景色:#ffffe4;
位置:相对位置;
保证金:0自动;
宽度:776px;
高度:400px;
最高:25%;
文本对齐:居中;
溢出:自动;
}
呜呜呜呜

您是否有任何标记/css可以展示给我们?请确保它与其他div无关,并使用
position:absolute;最高:25%
您是否有任何可以展示给我们的标记/css?请确保它不是相对于anotehr div的,并使用
position:absolute;最高:25%没有理由在#myDiv之前加上“div”-每页应该只有一个具有相同ID的元素,因此-指定它是哪种类型的元素是多余的(特别是当ID中有“div”时)。我同意。我这样做是出于习惯,显然不会将元素命名为myDiv。我这样做只是为了演示。无论ID的名称是什么——选择ID为的元素在任何方面都是过度杀伤力。没有理由在#myDiv之前使用“div”——每页应该只有一个ID相同的元素,所以——指定它是什么类型的元素是过度杀伤力(特别是如果ID中有“div”),我同意。我这样做是出于习惯,显然不会将元素命名为myDiv。我这样做只是为了演示的目的。无论ID的名称是什么,选择带有ID的元素在任何方面都是过分的。上边距有效,但在左边:nada。div粘在左边距上。代码在下面。有线索吗?正文{background:#eeeeee url('pix/bg-noether-2.jpg')无重复中心顶部;背景大小:自动100%;背景附件:固定;溢出:隐藏;对齐:中心;}#容器{背景色:#ffffe4;位置:绝对;宽度:776px;高度:400px;顶部:25%;左边距:自动;文本对齐:居中;溢出:自动;}顶部边距有效,但在左侧:nada。div粘在左边距。代码在下面。有线索吗?正文{背景:#eeeeeee url('pix/bg-noether-2.jpg')无重复中心顶部;背景大小:自动100%;背景附件:固定;溢出:隐藏;对齐:中心;}#容器{背景色:#ffffe4;位置:绝对;宽度:776px;高度:400px;顶部:25%;左边距:自动;文本对齐:居中;溢出:自动;}