javascript - 根据窗口宽度的中间制作一个 div 偏移量?

标签 javascript html css

基本上,我有代码:

<div id="footer">
    <a href="http://site.com/" target="_blank"><img src="image here.jpg"></a>
    <div style="position:absolute;left:5px;top:0px;">
        text here
    </div>
</div>

CSS:

#footer { line-height:125px; margin:0 auto; width:500px;height:100%; text-align:center;color:#ECECEC; }

它在一台计算机上运行良好,然后如果我将它加载到另一台计算机上,文本会根据显示器/分辨率进一步向左或向右偏移。

如何设置它使其居中但悬停在图像上方/下方以偏移到图像的右侧?

编辑

小屏幕:http://i.imgur.com/El2k8jG.png 大屏:http://i.imgur.com/0Nsskrz.png

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>stuff here</title>
<style type="text/css">
#footer_container {border:0px solid #666; bottom:0; height:95px; left:0; position:fixed; width:100%; }
#footer { line-height:125px; margin:0 auto; width:500px;height:100%; text-align:center; }
#footer2 { line-height:165px; margin:0 auto; width:500px;height:100%; text-align:center;  }
</style>
</head>
<body>
<div id="container">
stuff here
</div>
<div id="footer_container">
    <div id="footer">
        <a href="http://site.com/" target="_blank"><img src="http://i.imgur.com/nx8pHGH.png"></a>
        <div style="position:absolute;left:500px;top:0px;">
            <i><font color=black>dasdasdasd dsad dasdasdas dsadsad <a href="http://site.com/" target="_blank">ssdasdasd</font></a>.</i>
        </div>
    </div>
    <div id="footer2">
        <div style="position:absolute;left:500px;top:0px;">
            <font color=black><b>dasdsa dasdasd dasdasdasd dasdsadsad .</b></font>
        </div>
    </div>
</div>
</body>
</html>

最佳答案

如果您希望文本相对于页脚绝对定位,则需要在页脚上设置 position: relative;

DEMO

或者你可以删除所有的定位和巨大的行高

DEMO

关于javascript - 根据窗口宽度的中间制作一个 div 偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19737767/

相关文章:

javascript - 圆心的字符串 - Fabrics.js

html - Chrome 每帧解码大图像

php - (Wordpress) 不规则的 3-4-3-4 帖子结构

html - 表单代码 : How to get submit button next to text box

jquery - Flex Slider 在最后一张幻灯片上反转

javascript - 无法通过 systemjs 导入 ng-bootstrap

javascript - 想要删除任何 HTML 标签

javascript - 减少 javascript 对象

JQuery 可放置功能不起作用

html - 使绝对定位的 div 扩展父 div 高度