两张彼此相邻的图像

时间:2018-12-24 19:51:05

标签: html css

我试图将quote1.jpg放置在yoda.png旁边,但是无论我将div类放置在什么地方,它最终都放置在其他位置。首先,它位于标题的顶部,现在它掩盖了Yoda。当您将鼠标悬停在Yoda上时,它应该显示其报价,但会显示quote.jpg。我要做的就是将quote.jpg放在Yoda旁边。

.header {
    background: black;
    color: white;
    font-family: cursive, Haettenschweiler, 'Arial Narrow Bold', 
    sans-serif ;
    margin: 20px;
    padding: 20px;
    text-align: center;
    }

p {
    background-color: blanchedalmond;
    font-size: 26px;
    color: black;
    opacity: 1;
    }

  .container {
    position: relative;
    width: 50%;  
}

.image {
    opacity: 1;
    display: block;
    width: 100%;
    height: auto;
    transition: .5s ease;
    backface-visibility: hidden;
  }
  
  .middle {
    transition: .5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%)
  }
  
  .container:hover .image {
    opacity: 0.3;
  }
  
  .container:hover .middle {
    opacity: 2;
  }
  
  .text {
    background-color: rgb(13, 53, 228);
    color: white;
    font-size: 25px;
    padding: 16px 32px;
  }

/* No matter what I do I cannot 
place the quote1.jpg in the correct place */
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial- 
     scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Here We Go!</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class = "header">
     <h1>I Am Tying Hard To Learn Html</h1>
     <p>"Do Or Do Not There Is No Try"</p>
     </div> 
    <div class= "container">
            <img src="yoda.png" alt="Yoda" 
            class= "image" style = "width:50%">
    <div class="text">That right, you got.  
      Herh herh herh.
      <div class ="middle">
          <img src="quote1.jpg" alt="quote">
    </div>
              
        </body>
</html>

HTML Code


CSS Code

3 个答案:

答案 0 :(得分:0)

您好,如果我理解您的问题,可能会对您有所帮助。

.container {
  display: flex;
  flex-direction: row
  flex-wrap: wrap;
}

.custom-img {
  height: 45vh;
  width: 100%;
  object-fit: cover
}

.sections {
  position: relative;
  margin: 2px;
  border: 1px solid green
}

.section-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  background-color: #000;
  padding: 2px;
  font-size: 18px
}
<div class="container">
  <div class="sections">
      <div class="section-text">This is the left section</div>
     <img class="custom-img" src="https://slicedinvoices.com/wp-content/uploads/edd/2015/12/better-urls.jpg" />
  </div>
  <div class="sections">
      <div class="section-text">This is right section</div>
     <img class="custom-img" src="https://yazoogle.com.au/wp-content/uploads/2017/04/urls.jpg" />
  </div>
</div>

答案 1 :(得分:0)

希望这会有所帮助

  1. 将quote1 div移到yoda img标签下方。

2。修改以下CSS类

.middle {
    transition: .5s ease;
    opacity: 0;
    display: inline;
    transform: translate(-50%, -50%);
}

.image {
    opacity: 1;
    display: inline;
    height: auto;
    transition: .5s ease;
    backface-visibility: hidden;
}

答案 2 :(得分:0)

使用javascript或jquery来实现您想要的目标更容易。 这是示例代码:

.header {
background: black;
color: white;
font-family: cursive, Haettenschweiler, 'Arial Narrow Bold', 
sans-serif ;
margin: 20px;
padding: 20px;
text-align: center;
}

p {
background-color: blanchedalmond;
font-size: 26px;
color: black;
opacity: 1;
}


.image {
opacity: 1;
display: block;
width: 100%;
height: auto;
transition: .5s ease;
backface-visibility: hidden;
}


.text {
display:block;
opacity: 0;
background-color: rgb(13, 53, 228);
color: white;
font-size: 25px;
padding: 16px 32px;
}

.flex-container {
display: flex;   
flex-flow:row wrap;    
}

  .block{  
  display: block;    
  background: red;
  margin: 10px;
  padding: 10px;
  width:100px;     
 }


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial- 
     scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Here We Go!</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script>
        function show() {
            var element = document.getElementsByClassName("text")[0];            
            element.classList.toggle("open");

        }

        function hide() {
            var element = document.getElementsByClassName("text")[0];
            element.classList.toggle("open");
        }
    </script>
</head>

<body>
    <div class="header">
        <h1>I Am Tying Hard To Learn Html</h1>
        <p>"Do Or Do Not There Is No Try"</p>
    </div>

    <div class="parent">
        <div class="flex-container">
            <div class="block yoda-div" id="yoda">
                <img src="yoda.png" alt="Yoda" class="image"  onmouseover="show()">
                <div class="text" onmouseout="hide()">That right, you got.
                    Herh herh herh.
                </div>
            </div>
            <div class="block" id="quote">
                <img src="quote1.jpg" alt="quote">
            </div>

        </div>
    </div>
</body>

</html>