使用CSS堆叠元素/类

时间:2016-03-07 00:33:15

标签: html css

我试图仅使用html和css在链接中创建图像。有许多元素需要"堆叠"彼此重叠。

Example image

我很难理解继承,嵌套等等。这是我到目前为止编写的代码:



.heart {
  position: relative;
  margin-top: 20px;
  background-color: #000000;
  opacity: .8;
  width: 65px;
  height: 30px;
  border-radius: 15px;
  display: inline;
}
.box {
  margin: 75px auto;
  position: relative;
  height: 490px;
  width: 700px;
  background-color: #18a0ff;
  box-shadow: 1px 15px 50px 2px;
  display: flex;
}
.thumbnail_image {
  position: absolute;
  float: left;
  display: inline-block;
  top: 0px;
  left: 0px;
}
.text_container {
  top: 60px;
  left: 200px;
  right: 100px;
  width: 400px;
  height: 338px;
  position: relative;
  display: flex;
}
h1 {
  font-color: #ffffff !important;
  text-transform: uppercase;
  font-size: 60px;
  font-family: Montserrat;
  font-weight: 700;
  line-height: 1.1;
  text-align: left;
}

<div class="box">
  <div class="heart">
  </div>
  <div class="thumbnail_image">
    <img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457298445/Sheldon_Pic_l3cprk.jpg">
  </div>
  <div class="text_container">
    <h1>Don't You think that if I were wrong, &nbsp;&nbsp; &nbsp; I'd know it?</h1>
  </div>
</div>
&#13;
&#13;
&#13;

我关心的是如何正确放置心脏对话框,文本容器和图像叠加层。我似乎误解了正确的继承语法或结构。

4 个答案:

答案 0 :(得分:2)

在心形对话框,文本容器和图像叠加元素上使用position:absolute;,然后使用leftright属性正确定位它们。

答案 1 :(得分:2)

绝对定位 z-index 是使用HTML和CSS堆叠图片所涉及的关键词。

我继续用一些html / css模拟你的图像,让你了解实现。

Z-index在这个特定的例子中是不相关的,因为你只需要在基数上方有一层,这是自动给你绝对定位的,但是如果你有多个层,你需要将z-index设置为a数字值,其中较低编号的z索引显示在底部,较高的z索引显示在顶部。

这是我的代码,希望它有所帮助:

&#13;
&#13;
body {
  background-color: grey;
}

.container {
  position:fixed;
  height: 500px;
  width: 700px;
  background-image: url(http://i.stack.imgur.com/MS8X8.png);
  background-position: 46% 52%;
  background-size: 150%
}

.hearts {
  position: absolute;
  background-color: rgba(149, 165, 166,.5);
  color: white;
  right: 40px;
  top: 15px;
  padding: 15px 25px 15px 25px;
  border-radius: 15px
}


.blue {

  width: 550px;
  height: 500px;
  background-color: rgb(102,173,255);
  float: right;

}

h1, h5 {
  position: absolute;
  font-family: sans-serif;
  color: white;
  text-transform: uppercase;
}

#quote {
left: 200px;
top: 30px;
font-size: 60px;
}

#attr {
  left: 200px;
  top: 450px;
}
&#13;
<!DOCTYPE html>
<html>
  <head>
    <link rel = "stylesheet" href = "main.css">
  </head>
  <body>
    <div class = "container">
      <div class = "hearts">423</div>
      <div class = "blue">
        <h1 id = "quote">don't you <br> think that <br> if i were </br>wrong,<br> i'd know it?</h1>
        <h5 id = "attr">-Sheldon Cooper</h5>
      </div>
    </div>
  </body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

&#13;
&#13;
html,
body,
box,
thumbnail_image,
overlay,
h1,
h3,
h6,
p,
body {
  width: 100%;
  padding-bottom: 25px;
}
input {
  font-family: "Roboto";
  position: absolute;
  top;
  25.5px;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
  background-color: transparent;
  text-align: right;
  border-width: 0;
  width: 100%;
  margin: 0 0 .1em 0;
}
.heart_button {
  position: absolute;
  top: 25.5px;
  right: 55px;
}
heart_button:hover,
heart_button:active,
heart_button:focus {
  color: #dd0239;
}
.heart_background {
  position: absolute;
  top: 20px;
  right: 20px;
  background-color: #000000;
  opacity: .1;
  width: 65px;
  height: 30px;
  border-radius: 15px;
}
.box {
  margin: 30px auto;
  position: relative;
  height: 490px;
  width: 700px;
  background-color: #18a0ff;
  box-shadow: 1px 15px 50px 2px;
}
.quote_image {
  position: absolute;
  opacity: .1;
  top: 62px;
  left: 51px;
}
.image_overlay {
  background-color: #282a37;
  width: 170px;
  height: 490px;
  position: absolute;
  float: left;
}
.thumbnail_image {
  position: absolute;
  float: left;
  opacity: .12;
  display: inline-block;
  top: 0px;
  left: 0px;
}
.text_container {
  left: 200px;
  width: 400px;
  height: 338px;
  position: absolute;
}
h1 {
  color: #fff;
  text-transform: uppercase;
  font-size: 60px;
  font-family: Montserrat;
  font-weight: 700;
  line-height: 1.1;
  text-align: left;
}
.author_name {
  position: absolute;
  left: 206px;
  bottom: 0px;
}
h3 {
  font-family: Open Sans;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  font-size: 14px;
  text-align: left;
  color: #fff;
}
p {
  font-family: "Roboto";
  font-weight: 700;
  font-size: 14px;
  color: #fff;
  text-align: center;
}
h6 {
  font-family: Open Sans;
  font-weight: light;
  font-size: 22px;
  letter-spacing: 2px;
  text-transform: uppercase;
  text-align: center;
}
html {
  background: linear-gradient(209deg, #E5ECEF 40%, #BBC2C5 100%) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
#footer {
  clear: both;
}
&#13;
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href='https://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,800' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>

</head>

<body>
  <div class="box">
    <div class="heart_button">
      <img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457311522/little_heart_jle1j3.png">
    </div>
    <div class="heart_background">
    </div>
    <div class="image_overlay">
    </div>
    <div class="thumbnail_image">
      <img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457298445/Sheldon_Pic_l3cprk.jpg">
    </div>
    <div class="text_container">
      <h1>Don't You think that if I were wrong, &nbsp;&nbsp;&nbsp; I'd know it?</h1>
    </div>
    <div class="author_name">
      <h3> - Sheldon Cooper </h3>
    </div>
    <div class="quote_image">
      <img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457314397/quotations_image_wfwimc.png">
    </div>

  </div>
</body>

<footer>
  <div>
    <h6>A Project by Charles Bateman</h6>
  </div>
</footer>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

了解堆叠顺序

在您的情况下,自然堆叠顺序将完成工作;这很好地解释了over on the MDN。要理解的主要内容是元素将与标记中的元素重叠。用一个简单的例子可以更好地解释这一点:

&#13;
&#13;
div {
  position: absolute;
  background: red;
  height: 100px;
  width: 100px;
  top: 0;
  left: 0;
}
.two {
  background: blue;
  top: 10px;
  left: 20px;
}
.three {
  background: green;
  top: 20px;
  left: 40px;
}
&#13;
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
&#13;
&#13;
&#13;

有了这个......

让我们做这些!

在这个答案结尾处随意跳转到完整的例子!

Final result 1 Final result 2

想要使用一些迂腐的语义吗?

我们的标记现在看起来像这样:

<blockquote>
    <p>Don't You think that if I were wrong, I'd know it?</p>

    <cite>Sheldon Cooper</cite> 

    <a href="#" class="love-counter">
      <3 123
    </a>  

    <nav>
      <a href="#">Previous</a> 
      <a href="#">Next</a>
    </nav>
</blockquote>

CSS

主要背景图像和颜色

这些可以作为blockquote本身的背景。您可以使用background-size确保图片始终具有相同的尺寸。 (这显然会扭曲尺寸不正确的图像)

blockquote {    
    background: #18a0ff url(image-url) no-repeat;
    background-size: 170px 490px;
}

添加透明灰色背景和引号字符

这可以使用绝对定位的before pseudo-element of blockquote添加。使用left / right / bottom拉伸元素以及与图像匹配的宽度。透明的灰色叠加层和透明文字由rgba color提供。

blockquote:before {
  content: '\201C';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  padding-top: 30px;
  font-size: 2.4em;
  text-align: center;
  background: rgba(0,0,0,0.7);
  width: 170px;
  color: rgba(255,255,255,0.3);
}

将主引文文本与其引文对齐

为了合并较小的引号,可以更直观地使主文本垂直居中。我们可以使用display: flex propertyjustify-content来轻松实现这一目标; the flex-direction: column property将主引号叠加在引文的顶部。块引用也被赋予左右填充以适当地水平定位。

blockquote {
  display: flex; 
  justify-content: center;
  flex-direction: column;
  padding: 0 140px 0 200px;
}

定位后退/前进导航和爱情计数器

可以使用position: absolute以及相应的left / right / bottom / top属性轻松找到这些内容。它们看起来像这样:

.love-counter {
  position: absolute;
  right: 20px;
  top:  20px;
}

nav {
  position: absolute;
  left: 0px;
  bottom: 20px;
}

完整示例

兼容性: IE 11+ and all modern browsers.

您可能会考虑a javascript method缩小较大引号的字体大小。

&#13;
&#13;
@import url(https://fonts.googleapis.com/css?family=Passion+One:400,700);
 html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
blockquote {
  background: #18a0ff url(http://i.stack.imgur.com/e3nDc.jpg) no-repeat;
  background-size: 170px 490px;
  height: 490px;
  color: #FFF;
  font-family: 'Passion One', cursive;
  font-size: 4.2em;
  position: relative;
  display: flex;
  justify-content: center;
  flex-direction: column;
  padding: 0 140px 0 200px;
  font-weight: 400;
  line-height: 1;
  width: 650px;
  text-transform: uppercase;
}
blockquote p {
  margin: 0;
  margin-top: 0.75em;
}
cite {
  font-size: 0.25em;
  font-weight: 400;
  margin-top: 2em;
}
cite:before {
  content: '\2014  '
}
blockquote:before {
  content: '\201C';
  font-size: 2.4em;
  padding-top: 30px;
  text-align: center;
  background: rgba(0, 0, 0, 0.7);
  width: 170px;
  color: rgba(255, 255, 255, 0.3);
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
}
.love-counter {
  color: #FFF;
  text-decoration: none;
  font-size: 0.2em;
  position: absolute;
  right: 20px;
  top: 20px;
  font-family: helvetica;
  font-weight: bold;
  background: rgba(0, 0, 0, 0.2);
  padding: 0 10px;
  border-radius: 10px;
  height: 30px;
  line-height: 30px;
  min-width: 60px
}
nav {
  position: absolute;
  left: 0px;
  bottom: 20px;
  font-size: 0;
  width: 170px;
  text-align: center;
}
nav a:before,
nav a:after {
  font-size: 36px;
  width: 50%;
  display: inline-block;
  color: #FFF;
}
nav a:first-child:before {
  content: '<';
}
nav a:last-child:after {
  content: '>';
}
.x-large {
  background-image: url(http://i.stack.imgur.com/qWm5m.jpg);
}
.x-large p {
  font-size: 0.62em;
}
&#13;
<blockquote>
  <p>Don't You think that if I were wrong, I'd know it?</p>

  <cite>Sheldon Cooper</cite> 

  <a href="#" class="love-counter">&#60;3 123</a>

  <nav>
    <a href="#">Previous</a> 
    <a href="#">Next</a>
  </nav>
</blockquote>

<h2>Larger quote</h2>

<blockquote class="x-large">
  <p>Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.</p>

  <cite>Albert Einstein</cite> 

  <a href="#" class="love-counter">&#60;3 123</a>

  <nav>
    <a href="#">Previous</a>
    <a href="#">Next</a>
  </nav>
</blockquote>
&#13;
&#13;
&#13;