不管页面大小如何,都使标题图像居中

如何解决不管页面大小如何,都使标题图像居中

|| 我的网页的标题图像始终保留在窗口顶部。当窗口最大化时,标题图像居中,但是在小窗口(或智能手机浏览器)中查看时,标题设置在窗口的左侧,右侧被裁剪。因为标题div比页面内容宽,所以当窗口小于标题而不是页面时,水平滚动条就会出现。 我希望标题图像始终保持在页面的中心,如果窗口小于标题,则左右两侧都将被裁剪。同样,水平滚动条仅在窗口宽度小于页面宽度时才打开。 该图像可能会更清楚地解释它: http://postimage.org/image/49ne3k3o/ 到目前为止,我的代码: CSS:
#headerwrap {
    position: relative;
    width: 998px;
    height: 100px;
    margin: 0 auto;
    pointer-events: none;
}
#header{
    position: fixed;
    top: 0;
    width: 998px;
    margin: 0;
    background: url(\'../images/top.png\') no-repeat;
    height:100px;
    z-index: 1;
    pointer-events: none;
}
和HTML实施:
<div id=\"headerwrap\"\\>
<div id=\"header\">
</div>
</div>
(页面宽690px,标题宽998px) 任何帮助将非常感激。     

解决方法

        您需要在具有
position: fixed
的标头元素上设置
left: 50%
,然后应用该元素宽度一半的负边距将其居中。
#header {
  height: 100px;
  width: 998px;
  position: fixed;
  top: 0;
  left: 50%;
  margin-left: -499px;
  background: url(\'../images/top.png\') no-repeat;
}
这是一个快速的小提琴:http://jsfiddle.net/blineberry/w8P2S/ 您实际上并不需要包装器。     ,        正如布伦特所说,您确实需要负的左边距,但是该边距将取决于用户的浏览器分辨率而变化,如果用户调整浏览器的大小,则可能会出现问题。 为了弄清楚利润,我使用了一个简单的jquery方法,每次都像超级按钮一样工作:
jQuery(window).resize(function() {
  var width = jQuery(document).width();
  var margin = (yourImageWidth-width)/-2;
  jQuery(\'.yourCssClassForYourHeader\').css(\'margin-left\',(margin)+\'px\');
});
必须在准备就绪的文档上首次调用此函数,然后在每次用户调整浏览器大小时都会调用此函数。     ,        标头缩小以适合窗口大小是否可以接受? 另外,您是否需要将其作为纯HTML / CSS解决方案? 编辑: 这是我刚刚写的一个小脚本,它将两个div的大小调整为浏览器窗口的大小。它不需要任何库(如jQuery)。
<script type=\"text/javascript\">
var clientWidth = document.documentElement.clientWidth;

document.getElementById(\'headerwrap\').style.width = clientWidth + \"px\";
document.getElementById(\'header\').style.width = clientWidth + \"px\";
</script>
或者,如果您想在双方之间都留有空隙,可以执行以下操作:
var padding = 50;
document.getElementById(\'header\').style.width = (clientWidth - (padding * 2)) + \"px\";
document.getElementById(\'header\').style.left = padding + \"px\";
所以,总而言之:
<script type=\"text/javascript\">    
    var clientWidth = document.documentElement.clientWidth;
    document.getElementById(\'headerwrap\').style.width = clientWidth + \"px\";

    var padding = 50;
    document.getElementById(\'header\').style.width = (clientWidth - (padding * 2)) + \"px\";
    document.getElementById(\'header\').style.left = padding + \"px\";
</script>
另外,将包装器更改为
position: absolute; top: 0px
,将内部div更改为
position: absolute; top: 0px;
希望这可以帮助!
Also,you have a forward slash in your wrapper div that needs to go :)
    ,        另一个选择是从#header div中删除background属性,然后将其放在#headerwrap中,如下所示:   编辑:      我添加了评论以帮助解释   CSS更改的影响。      编辑@布伦特的答案:      布伦特的答案也不错,但是   确实提出了一个问题   内容定位自己。   http://jsfiddle.net/MarbleStrain/FdL44/   注意Test div是如何不可见的   如果窗口不够大。一世   不想破坏布伦特的答案或其他任何东西   他的答案同样有效,取决于   您想要哪种流量。我只是   想指出差异   在两个结果之间。
#headerwrap {
    background: url(\'../images/top.png\') no-repeat fixed center 0; /*this will put the image at the top-middle of this div*/
    width: 100%; /*this sets this div to whatever size the window is,which helps with removing the horizontal scrollbar*/
    height: 100px;
    text-align: center; /*this will move any child elements to the center (specifically for ie)*/
    pointer-events: none;
    overflow: hidden; /*this hides any child elements that are outside the range of this element*/
}
#header{
    width: 998px;
    margin: 0 auto; /*center header for all browsers except ie*/
    height:100px;
    z-index: 1;
    pointer-events: none;
    text-align: left; /*makes sure that contents are lined up to the left (otherwise inherited from the parent which is center)*/
}
我还更改了一些样式,以使所有内容都跨浏览器居中。为了避免显示水平滚动条,请将#headerwrap \的溢出属性设置为隐藏。 那应该解决您的两个问题。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-