html - child 如何覆盖 parent 的不透明度?

标签 html css

目前我正在为一个社区开发一个网站,它变得有点困难,我有一个漂亮的粉红色>橙色渐变,下面有一张图像,不透明度 为 0.2,以显示两者.看起来像这样。

One

如您所见, Logo 也具有不透明度。我已经找到了一些关于 rgba-color 的信息,但是没有用。

我该如何解决这个问题?我希望带边框的图像完全不透明。

body {
  background-color: #f2f2f2;
  color: #404040;
}

div.navbar {
  height: 600px;
  background: rgba(255, 255, 255, 0.7);
  background-image: linear-gradient(25deg, #ec008c, #fc6767);
  margin-top: -10px;
  margin-left: -5px;
  position: relative;
  width: 105%;
}

img.logo {
  position: absolute;
  margin-top: 4%;
  margin-left: 25%;
  height: 40%;
  padding: 25px;
  border: 25px solid #f2f2f2;
  border-radius: 50%;
}

div.image {
  position: relative;
  height: 100%;
  opacity: 0.2;
  background-image: url("img/slide1.jpg");
  background-repeat: no-repeat;
  background-size: cover;
}

div.nav {
  position: absolute;
  bottom: 0;
  margin-left: 600px;
  margin-bottom: 50px;
}

a.nav-item {
  color: #f2f2f2;
  font-weight: bold;
  font-size: 25pt;
  margin-right: 50px;
  text-decoration: underline;
}

a.nav-item:hover,
a.nav-item .active {
  text-decoration: overline underline;
}
<div class="navbar">
  <img class="logo" src="img/logo.png">
  <div class="image">
  </div>
  <div class="nav">
    <a class="nav-item">Home</a>
    <a class="nav-item">Twee</a>
  </div>
</div>

最佳答案

对图像和渐变使用背景属性。然后从你的十六进制值的 rgba 等价物中获取你的渐变(Chrome 开发工具颜色选择器对此很有用)。

body {
  margin: 0;
}

div.navbar {
	height: 100vh;
  
  /*
    IMPORTANT BITS:
    - ADDED image and gradient to navbar background and
    - REMOVED opacity
    
    THE REST:
    The rest was just to make the demo look better/simpler
  */
	background:
    linear-gradient(25deg, rgba(236, 0, 140, 0.7), rgba(252, 103, 103, 0.7)),
    url(http://placeimg.com/1000/600/arch) no-repeat center;
  background-size: cover;

	position: relative; 
}

.logo-wrapper {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 25%;
  height: 0;
  padding-top: 25%;
  border:25px solid #f2f2f2;
  border-radius: 50%;
  overflow: hidden;
}
.logo {
  width: 90%;
  border-radius: 50%;
  position: absolute;
  top: 5%;
  left: 5%;
}
<html>
<head>
<link rel="stylesheet" href="css.css">
</head>
<body>
  <div class="navbar">
    <div class="logo-wrapper">
      <img class="logo" src="http://placeimg.com/200/200/tech/grayscale">
    </div>
  </div>
</body>
</html>

关于html - child 如何覆盖 parent 的不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54934981/

相关文章:

javascript - CSS float 被反转

html - 如何使用 Markdown 将文本居中?

css - 某些内容移入固定 header 的问题

html - 三 Angular 形等的CSS问题

javascript - 如何在评论多的时候让评论区自动滚动

jquery - Jquery 中的多行图像库

html - 移动设备上的 Twitter Bootstrap 未显示为 "xs"大小

html - 相对水平地放置物体

html - 向元素添加填充而不移动其余元素

css - 如何使网站的打印版本不受屏幕尺寸的影响?