格式化链接,使它们出现在CSS中的图像顶部

问题描述 投票:0回答:4

我创建了一个网站,我试图在图像上放置一些其他网页的链接,有点像导航栏。到目前为止,我一直在尝试使用position属性并尝试修改HTML的div标签,但是,似乎没有任何东西能够获得理想的结果。

这是相关的HTML和CSS:

.content {
  position: relative;
  color: #eaeaea;
  text-align: left;
}

.navigation {
  padding: 10px;
  float: left;
  margin: 0px;
  overflow: hidden;
  background-color: #ffffff;
}

.navigation a {
  float: left;
  display: block;
  color: #61ba48;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  background-color: #dee0de;
}

.navigation a:hover {
  background: #61ba48;
  color: #ffffff;
}

.subscribe {
  padding: 10px;
  float: right;
  margin: 0px;
  overflow: hidden;
  background-color: #ffffff;
}

.subscribe a {
  float: left;
  display: block;
  color: #61ba48;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  background-color: #dee0de;
}

.subscribe a:hover {
  background: #61ba48;
  color: #ffffff;
}

.Title {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background-color: black;
  color: #61ba48;
  padding-left: 20px;
  padding-right: 20px;
  width: 200px;
  opacity: 0.8;
}
<div class="content">
  <img src="background.jpg" alt="Background" style="width: 100%;">
  <div class="Title">
    <h1 style=f ont-size:70px>IFN</h1>
    <p>Title</p>
  </div>
</div>

</div>

<hr>
<div class="navigation">
  <a href="pg1.html"> Home</a>
  <a href="pg2.html"> pg2</a>
  <a href="pg3.html"> pg3</a>
  <a href="pg4.html"> pg4</a>
</div>



<div class="subscribe">
  <a href="register.html"> Subscribe </a>
</div>

这是现在的样子:

enter image description here

这就是我想要实现的目标:enter image description here

html css
4个回答
0
投票

我建议你将这些链接包装在div中并将该图像设置为该div的背景。

或者使用比链接少的z-index值使.content绝对值。

.content {
  position: absolute;
  /* Changed from relative to absolute */
  z-index: -1;
  /* Added */
  color: #eaeaea;
  text-align: left;
}

.navigation {
  padding: 10px;
  float: left;
  margin: 0px;
  overflow: hidden;
  background-color: #ffffff;
}

.navigation a {
  float: left;
  display: block;
  color: #61ba48;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  background-color: #dee0de;
}

.navigation a:hover {
  background: #61ba48;
  color: #ffffff;
}

.subscribe {
  padding: 10px;
  float: right;
  margin: 0px;
  overflow: hidden;
  background-color: #ffffff;
}

.subscribe a {
  float: left;
  display: block;
  color: #61ba48;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  background-color: #dee0de;
}

.subscribe a:hover {
  background: #61ba48;
  color: #ffffff;
}

.Title {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background-color: black;
  color: #61ba48;
  padding-left: 20px;
  padding-right: 20px;
  width: 200px;
  opacity: 0.8;
}
<div class="content">
  <img src="http://via.placeholder.com/2000x850" alt="Background" style="width: 100%;">
  <div class="Title">
    <h1 style=f ont-size:70px>IFN</h1>
    <p>Title</p>
  </div>
</div>



<hr>
<div class="navigation">
  <a href="pg1.html"> Home</a>
  <a href="pg2.html"> pg2</a>
  <a href="pg3.html"> pg3</a>
  <a href="pg4.html"> pg4</a>
</div>



<div class="subscribe">
  <a href="register.html"> Subscribe </a>
</div>

0
投票

我想到了两种方法来实现这一目标。 1.使用绝对位置。这很简单。我知道这可能是你不想做的事情。 2.一些“非常糟糕”的事情(见最佳答案):How to put text over an image without absolute positioning or setting the image as backbround。 (我不确定它是否仍然有效)

编辑:现在看到所需的结果,我认为你可以使用绝对定位。此外,您可以将图像设置为背景,如果这也为您提供了预期的结果。

希望能帮助到你


0
投票

请参阅附件代码段。将背景图像移动到CSS。该解决方案不需要z-index也不需要position:absolute / relative。

为链接方框的背景添加了一些不透明度,因为背景图像是随机选取的,因此可能很难获得链接的黑色文本。

我建议你也引入CSS网格来更好地控制布局。

body {
  background-image: url("http://source.unsplash.com/random/800x200");
  background-repeat: no-repeat;
}

.nav {
border: 2px solid black;
width: 40%;
height: 10px;
padding: 0px 10px 40px 10px;
margin: 0px 0px 0px 10px;
float: left;
background-color:rgba(255, 255, 255, 0.5)
}

.nav ul a {
  margin: 5px;
  color: black;
  text-decoration: none;
}

.subscriber ul a {
  margin: 5px;
  color: black;
  text-decoration: none;
}

.subscriber {
border: 2px solid black;
width: 20%;
height: 10px;
padding: 0px 10px 40px 10px;
margin: 0px 120px 0px 10px;
float: right;
background-color:rgba(255, 255, 255, 0.5)
}
  <div class= "nav">
    <ul>
       <a href= "pg1.html"> Home</a>
       <a href= "pg2.html"> pg2</a>
       <a href= "pg3.html"> pg3</a>
       <a href= "pg4.html"> pg4</a>
      </ul>
   </div>

   <div class= "subscriber">
     <ul>
        <a href= "pg1.html"> Subscribe</a>
      </ul>
    </div>

0
投票

尝试研究此代码以进行绝对定位。

.imgWrapper {
  position: relative;
  /* so child's position absolute (top, bottom, left, right), 
will by relative to .imgWrapper */
  width: 350px;
  height: 160px;
  /* controlling size of img */
}

.imgWrapper img {
  width: 100%;
  /* fill imgWrapper 100% with image */
  height: 100%;
}

.imgWrapper .topLeft {
  position: absolute;
  top: 0;
  /* play with this to move box */
  left: 0;
  /* play with this to move box */
}

.imgWrapper .topRight {
  position: absolute;
  top: 0;
  right: 0;
}

.imgWrapper .bottomLeft {
  position: absolute;
  bottom: 0;
  left: 0;
}

.imgWrapper .bottomRight {
  position: absolute;
  bottom: 0;
  right: 0;
}


/* tempolary colors */

.topLeft {background: #f00;}
.topRight {background: #0f0;}
.bottomLeft {background: #0f0;}
.bottomRight {background: #f0f;}

/* temporary box size */
.topLeft,
.topRight,
.bottomLeft,
.bottomRight {
  width: 50px;
  height: 20px;
}
<div class="imgWrapper">
  <img src="http://via.placeholder.com/300x150">
  <div class="topLeft"><!--Put here something--></div>
  <div class="topRight"><!--Put here something--></div>
  <div class="bottomLeft"><!--Put here something--></div>
  <div class="bottomRight"><!--Put here something--></div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.