简体   繁体   English

CSS:ul-li菜单中的背景图像不透明度

[英]CSS: background image opacity in ul-li menu

I have a menu using ul/li items, and they have a background image. 我有一个使用ul / li项目的菜单,并且它们具有背景图像。

All over the Internet, and in stackoverflow, there is information on how to hack background image opacity. 在整个Internet上以及在stackoverflow中,都有有关如何破解背景图像不透明度的信息。 For example: https://scotch.io/tutorials/how-to-change-a-css-background-images-opacity 例如: https : //scotch.io/tutorials/how-to-change-a-css-background-images-opacity

But not for my particular use case when using menus. 但不适用于我在使用菜单时的特定用例。 It seems particularly tricky. 似乎特别棘手。 From everything I have tried, one of the solutions in the aforementioned website seems to work the best. 从我尝试过的所有内容来看,上述网站中的一种解决方案似乎效果最好。

But still, the image is not vertically aligned. 但是,图像仍未垂直对齐。 I cannot seem to be able to center the image in the menu... 我似乎无法在菜单中居中放置图像...

  ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li {float: left;} li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover {background-color: #4CAF50;} .my-container { position: relative; overflow: hidden; } .my-container a { position: relative; z-index: 2; } .my-container img { position: absolute; left: 0; top: 0; width: 100%; height: auto; opacity: 0.2; } 
  <ul> <li><div class="my-container"><a href="a">Aaaa</a> <img src="http://www.joaobidu.com.br/jb/content/uploads/2012/03/cancer3.png"></img></div></li> <li><div><a href="b">Bbbb</a></div></li> <li><div><a href="c">Cccc</a></div></li> <li><div><a href="d">Dddd</a></div></li> <li><div><a href="e">Eeee</a></div></li> </ul> 

Hi,Please try this one. 嗨,请尝试这个。

If we will use top:-14px,It will affecting the modern browser like chrome and safari which is not accepting negative values.So we can use below format for background images. 如果我们使用top:-14px,它将影响不接受负值的chrome和safari等现代浏览器。因此,我们可以使用以下格式的背景图片。

.my-container {
    background-image:url("http://www.joaobidu.com.br/jb/content/uploads/2012/03/cancer3.png");
    background-size:cover;
    background-repeat:no-repeat;
    background-position:center;
}

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

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