I am have difficulty with some CSS. I’m trying to put an element above two elements. I’ve tried several solutions but nothing is helping.
.grey_font {
background-color: #FAF6EE;
height: 280px;
z-index: 1;
}
.ads {
background-color: #007181;
height: 160px;
z-index: 2;
margin-left: 7%;
margin-right: 7%;
border-radius: 30px;
position: relative;
display: flex;
}
footer {
background-color: #00525D;
height: 369px;
z-index: 1;
}
<header>
<nav id="nav">
<img src="Logo-horizontal.png" id="img_snark" />
<ul id="tabs">
<li>Qui Sommes-nous</li>
<li>Blog</li>
<li>FAQ</li>
<li>Contact</li>
<li id="compte">Mon compte</li>
</ul>
<img src="united-kingdom.png" id="uk_images">
</nav>
</header>
<img src="Union.png" id="union_images">
<div class="images_first-plan">
<div id="sentences">
<img src="entree.png" id="entree_images">
<img src="cake.png" id="cake_images">
<img src="par Nicolas Cooking.png" id="nicook_images">
</div>
<div class="images-play">
<img src="image 3.png" id="video_images">
<img src="PLAY 1.png" id="play">
</div>
</div>
<div class="grey_font">
</div>
<div class="ads">
</div>
<footer>
</footer>
This is the case in picture :
When I put two elements in position ‘fixed’ or ‘absolute’. They disappear.
This what I want:
6
Answers
Add position ‘absolute’ to the element you need to flow, and also add ‘top’ and ‘left’ to control its position
Then add position ‘relative’ for the upper main element you want to connect with
When you change the position to
absolute
then the width collapses. You need to set a width to make it visible and I also did the other positioning like below to meet your requirement:as far as your html code is not complete and I couldn’t figure it out which element is which, I name the div with search bar searchBox and the other one without search box, noSearchBox.
css:
HTML code –
CSS code –
Based on the images and the provided code, it looks like you want the elements with class grey_font and ads to appear above the images in the foreground. To achieve this, you’ll need to adjust the positioning and stacking context of the elements.
Here’s how you can modify your CSS to achieve the desired result:
css
Copy code
/* Rest of your existing CSS */
Set the parent container with class images_first-plan to have position: relative;. This establishes the stacking context for its child elements with absolute positioning.
For the element with class grey_font, use position: absolute; and set top: 0; left: 0; right: 0; to make it cover the entire width of its parent container. Adjust the height as needed.
For the element with class ads, also use position: absolute; and set bottom: 0; left: 7%; right: 7%; to position it at the bottom with a margin on the sides. Adjust the height and other properties as needed.
The z-index property is used to control the stacking order. Elements with higher z-index values appear above elements with lower z-index values.
By using position: absolute; and adjusting the positioning and z-index, you can layer the elements above the images in the foreground as shown in your desired result.
If you can update your HTML, you can "fake" the overlay by wrapping the
.ads
element in a parent and setting the parent background to alinear-gradient
with the same colors as the section before and thefooter
section.I removed the unnecessary HTML elements.