简体   繁体   English

将div宽于屏幕对齐为中心和隐藏溢出

[英]Align div wider than screen to be center & hidden overflow

I want to make slideshow which wider than monitor screen. 我想制作比监视器屏幕更宽的幻灯片。

Below codes is if the div smaller than screen: 下面的代码是如果div小于屏幕:

The HTML: HTML:

<div class="testme">
  <img src="http://placehold.it/400x300">
</div>

The CSS: CSS:

html,body{
    padding:0;margin:0;
}

.testme{
    width:400px;height:300px;
    margin-left: auto;
    margin-right: auto;
}

But if the img width wider than monitor screen, I must remove width in CSS to make overflow works. 但是如果img宽度比监视器屏幕宽,我必​​须删除CSS中的width以使溢出工作。 But width is important for align center. width对齐中心很重要。

Use margin-left: -50% do aligning it but not exactly centered 使用margin-left: -50%对齐它但不完全居中

(Most slider I know use html img and not background, which is problem if I want the slider wider than screen) (我知道的大多数滑块都使用html img而不是背景,如果我想让滑块宽于屏幕,这就是问题)

I would try: 我会尝试:

img {
    width: 120%;
    height: auto;
    margin-left: -10%; /*half of width surplus*/
}

not sure if it ll help you.. but in case you just want to make the image at the center of the div.. u can put the image in background property of the div and position it to center.. u don't need to worry about margin or anything.. if the div increases in size according to screen where ur viewing ..it ll readjust to best case center.. 不确定它是否会帮助你..但是如果你只是想让图像在div的中心..你可以将图像放在div的背景属性中并将其定位到中心..你不需要担心保证金或任何东西..如果div根据你观看的屏幕大小增加..将重新调整到最佳案例中心..

.testme {
background: url('..src of image tag');
background-position:center;
}

in case you need it for the slideshow.. you can always dynamically add the background property to the div tag and remove the image tag from the div. 如果您需要幻灯片显示..您可以随时动态地将背景属性添加到div标签并从div中删除图像标签。 :) hope it helps.. :) 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM