【问题标题】:Stacking divs relative and absolute positions堆叠 div 的相对和绝对位置
【发布时间】:2019-03-13 18:41:31
【问题描述】:

我在尝试进行布局时遇到了很多麻烦。我试图让一个框与图像重叠,我试图使用绝对/相对位置,但我认为我做错了。 here is the layout

这是我目前的代码:

   .trabalhos{
	position: relative;
	margin-top: 100px;
	width: 100%;
	max-width: 960px;
    }
    
    .caixa{
	position: absolute;
	left: 0%;
	top: auto;
	right: auto;
	bottom: 10%;
	overflow: visible;
	width: 30%;
	height: auto;
	padding: 25px;
	border-radius: 10px;
	background-color: #333;
	color: white;
    }
    
    #trabalhos-img-right{
	float: right;
    }

    #trabalhos-img-left{
	float: left;
    }

    #esquerda{
	text-align: right;
    }

    #direita{
	text-align: left;
	right: 0%;
	left: auto;
    }
<div class="section-trabalhos">
		<div class="container">
			<h1>blablablablabla</h1>

			<div class="trabalhos">
				<div><img id="trabalhos-img-right" src="img-01.png"></div>
				<div class="caixa" id="esquerda">
					<h2>Lorem ipsum</h2>
					<p>blablabla</p>
				</div>
			</div>

			<div class="trabalhos trabalhos_2">
				<div><img id="trabalhos-img-left" src="img-01.png"></div>
				<div class="caixa" id="direita">
					<h2>Lorem ipsum</h2>
					<p>blablabla</p>
				</div>
			</div>



 

我必须做 8 个,最好的方法是什么?

提前致谢

【问题讨论】:

标签: html css z-index overlap


【解决方案1】:

我希望我已经解决了这个问题

<html>
<head>



<style>

    .trabalhos{
        width:100%;
        max-width: 960px;
        height: 60%;
        margin-top: 100px;
        position: relative;
    }

    .img{
        width:80%;
        height: 100%; /*Do not change it*/
    }

    .caixa{
        position: absolute;
        bottom:10%;
        overflow: visible;
        width: 30%;
        height: auto;
        padding: 25px;
        border-radius: 10px;
        background-color: #333;
        color: white;
    }




#trabalhos-img-right{
    float: right;
}

#trabalhos-img-left{
    float: left;
}

#esquerda{
    text-align: right;
    left: 0px    
}

#direita{
    text-align: left;
    right: 0px;
}  

    #trabalhos-img{
        width: 100%; /*Do not change it*/
        height: 100%; /*Do not change it*/
    }        


</style>

</head>



<body>

    <div class="trabalhos">

        <div id="trabalhos-img-right" class="img">
          <img id="trabalhos-img" src="img-01.png" /></div>
        <div class="caixa" id="esquerda">
            <h2>Lorem ipsum</h2>
            <p>blablabla</p>
        </div>
    </div>

     <div class="trabalhos">

        <div id="trabalhos-img-left" class="img">
          <img id="trabalhos-img" src="img-01.png" /></div>
        <div class="caixa" id="direita">
            <h2>Lorem ipsum</h2>
            <p>blablabla</p>
        </div>
    </div>


</body>

【讨论】:

    【解决方案2】:

    This CodePen 可能是解决方案。只需将 html 和 css 代码中的 svg 元素替换为您的图像即可。

    另外,this 网站可以帮助您了解 position 属性。

    HTML

    <div class="container">
      <svg width="400" height="110">
        <rect width="500" height="500" style="fill:rgb(0,0,255);stroke-width:3;"/>
      </svg>
      <div class="description">
        <h1>Lorem Ipsum</h1>
        <h2>Lorem ipsum dolor sit amet</h2>
      </div>
    </div>
    
    <div class="containersecond">
      <svg width="400" height="110">
        <rect width="500" height="500" style="fill:rgb(0,0,255);stroke-width:3;"/>
      </svg>
      <div class="description">
        <h1>Lorem Ipsum</h1>
        <h2>Lorem ipsum dolor sit amet</h2>
      </div>
    </div>
    

    CSS

    svg {
      position: relative;
      left: 30px;
    }
    
    .description {
      background-color: red;
      width: 300px;
      position: absolute;
      top: 70px;
    }
    
    .containersecond {
      position: absolute;
      top: 300px;
    }
    

    【讨论】:

    • 我确实需要更多地研究它:/ 我觉得它很混乱。我试过你的代码,但如果我使用相同的类制作另一个“图像”和描述,它们会相互重叠。我必须使用其他课程吗?即便如此,我试过了还是想不通
    • 我刚刚添加了第二张图片并编辑了我的答案。请注意,如果您使用“绝对”位置,您的图像将相对于屏幕边缘移动。如果您使用“相对”位置,您的图像将相对于上面图像的底部边缘移动。如果你需要一些课程,我可以向你推荐 Codecademy 或 FreeCodeCamp。 Sololearn 还提供了一些不错的课程,有多种语言。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多