Positioning content using absolute in CSS3 for a unique layout that pushes down other elements

I'm working on a responsive webpage where I have to insert different content with varying heights. The layout includes:

  • An image at the top with a red gradient overlay.

  • Below that, a div with a blue background to create a seamless transition between the two sections.

The challenge I'm facing is making the yellow content push down the blue div based on its height. However, the yellow div must remain absolutely positioned to maintain its placement. Is there a way to achieve this using pure CSS, or would I need JavaScript to calculate the height of the blue div?

Here is my JSFiddle link for reference.

HTML

<div class="pr">
    <img src="http://www.cphrecmedia.dk/musikdk/stage/demobilleder/detartistchannel2.jpg">
    <div id="campaignreleases"></div>
    <img id="camlogo" src="http://www.cphrecmedia.dk/musikdk/stage/css/lyttesession.svgz" alt="MusikDK logo">
</div>
<div id="camrel">
    <img src="http://www.cphrecmedia.dk/musikdk/stage/demobilleder/detlyttealbum.jpg" alt="ALBUMNAME">
    <p>I need to add more content here that should push down the "camrel" div, but it also needs to be positioned just below the logo inside the "pr" div.</p>
</div>

CSS

body {
    background: #ddd
}
.pr {
    position: relative;
    width: 800px
}
.pr img {
    width: 100%
}
#campaignreleases {
    position: absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    background: -moz-linear-gradient(top, rgba(26, 188, 156, 0.2) 0%, rgba(26, 188, 156, 1) 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(26, 188, 156, 0.2)), color-stop(100%, rgba(26, 188, 156, 1)));
}
#camlogo {
    position: absolute;
    -webkit-transform: translateX(-50%);
    left: 50%;
    top:15%;
    opacity: 1 !important;
    max-width: 500px
}
#camrel {
    background: #1ABC9C;
    text-align: center;
    overflow: auto;
}
#camrel img {
    width: 250px;
    .roundedcorners;
}

Answer №1

To achieve this design using only CSS, you will need to adjust the positioning of the red, blue, and yellow div elements. The red and blue divs should have the CSS property position: absolute; since their height does not change. On the other hand, the yellow div should have position: relative; to ensure it sets the height of the document.

You may encounter issues with background colors, but these can be resolved by setting the body background color to match that of the blue div.

If you find this explanation confusing or still need assistance implementing it, feel free to reach out. I am more than happy to provide further help.


Update: I have created a Fiddle based on your code to demonstrate the changes described above.

Answer №2

My approach to your design involves leveraging a background pseudo element and utilizing z-index for a streamlined layout. The .content:after utilizes position:fixed to stay attached to the viewport, ensuring a consistent gradient.

Check out an example here!

Here's the HTML structure:

<div class="wrap">
    <div class="header">
        <img id="camlogo" src="image-source" alt="MusikDK logo">
    </div>

    <div class="content">
        <!-- Your content goes here! -->
    </div>
</div>

And here's the corresponding CSS:

* { 
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    background: #ddd;
}
.wrap {
    width: 800px;
    margin: 0 auto;
}
.header {
    background: url(http://www.cphrecmedia.dk/musikdk/stage/demobilleder/detartistchannel2.jpg) center 0 no-repeat;
    height: 600px;
    background-size: 800px;
    z-index: -3;
    position: relative;
}
.content:after {
    content: '';
    height: 100%;
    display: block;
    background: -moz-linear-gradient(top, rgba(26,188,156,0.2) 0%, rgba(26,188,156,1) 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(26,188,156,0.2)), color-stop(100%,rgba(26,188,156,1)));
    position: fixed;
    bottom: 0;
    z-index: -2;
    left: 50%;
    width: 800px;
    margin-left: -400px;
}
#camlogo{
    position: absolute;
    -webkit-transform: translateX(-50%);
    left: 50%;
    top:10%;
    opacity: 1 !important;
    max-width: 500px
}
.content {
    margin-top: -400px;
    padding: 0 200px;   
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Aligning a 900px wide element with an orange background on the left and a white background on the right

I have a section on my website that I want to position in the center of the screen. It's a simple task, but I have a specific design in mind. I want the left side of the section to have an orange background, while the right side should be white. I&apo ...

Using a pre-existing HTML template in an Angular project

Looking at my html template file named range-details-dialog.tpl.html <div class="modal-header clearfix text-left"> <h5>Update Range</h5> </div> <div class="modal-body"> <form name="form" ...

Css shaky letters transformation

[SOLUTION] To resolve this issue, you can utilize the will-change CSS property that enforces GPU rendering: will-change: transform; [ORIGINAL QUESTION] After extensive searching on the internet, I have yet to find a solution to a seemingly simple prob ...

Guide to customizing the CSS styles of various components within a Material UI Dialog

Let's take a look at this snippet for creating a Material Dialog in code: <Dialog open={this.props.open} onClose={this.props.closeAtParent} PaperProps={{ style: { minHeight: '75vh', minWidth: '75vw', ...

Is there a way for me to automatically retrieve lost data following a POST request?

Is there a way to automatically retrieve data from the server after submitting a form via POST in the browser? I have an HTML form The form's data is sent to a PHP function like saveRecord() The PHP code stores the data in a MySQL database If the da ...

Modify the animation for displaying material-ui dialog

Is it feasible to customize the display animation of a dialog in material-ui for react by using CSS? My understanding of CSS is limited, but I've heard about concepts like transitions and transforms that may be helpful in accomplishing this. ...

A guide on utilizing radio buttons to delete specific rows within a table with the help of AngularJS

My current issue involves using radio buttons to delete rows in a table. The problem I'm encountering is that multiple rows are being deleted at once instead of just one. You can view an image of my table here. As we all know, with radio buttons, onl ...

I can't get Toggleclass to switch classes properly, am I missing something?

I have been experimenting with creating a button that toggles a class and reveals another div below it. Feel free to check out the example on jsFiddle that I created for you. While the first function of showing and hiding the div is working perfectly, I ...

Revising text for real-time editing

Most of us are familiar with the functionality on StackOverFlow that allows you to preview your text before posting a question. I'm interested in implementing a similar feature on my website and am looking for some guidance on how to get started. I ...

What's the best way to ensure that your item list automatically updates the rendered list when an item is deleted

I've developed a web cart using Redux. The cart functions as expected, except for one issue - when I delete an item from the cart, the changes are only displayed after refreshing or navigating to another page. How can I update the view dynamically as ...

Issues with escaping HTML encoding

Currently working on an Android app that involves querying a webservice and receiving a JsonObject. Once I have the desired String, I encounter characters like: est&amp;aacute; I've experimented with two methods: StringEscapeUtils.escapeHTML4(te ...

Interactive canvas artwork featuring particles

Just came across this interesting demo at . Any ideas on how to draw PNG images on a canvas along with other objects? I know it's not a pressing issue, but I'm curious to learn more. ...

Steps to add a label below a text input field

Within a form, I have two text boxes and I would like to add labels underneath each of them for better clarification. To illustrate my idea, here is a simple diagram: (input1)<--space-->(input2) <label1><--space--><label2> Is ther ...

Tips for showing a variety of headers on your page

I am struggling to align multiple headers on the same horizontal line. Despite my efforts, only two of them display correctly while the third keeps dropping down a line. To see what I mean, take a look at this image: view current layout Here is the code ...

How can I optimize the display of 4 million 2D squares on a web browser?

With a display resolution of 7680x4320 pixels, I aim to showcase up to 4 million distinct colored squares. My objective is to adjust the number of squares using a slider. Currently, I have two versions in mind to achieve this goal. The first version involv ...

Angular select elements connected through data binding

I am working on selecting values in an Angular application. Specifically, I want to establish a connection between two select elements so that changing the value of one will also change the value of the other. <select class="form-control" id="example ...

Making the Bootstrap 4 card-footer expand to occupy the remaining height of the column

I'm currently developing a project that incorporates bootstrap cards. There are 3 cards, each for a different column, and I need them to have equal heights. Here is how they currently look: https://i.sstatic.net/Nau98.png The B and C cards should oc ...

Using a script to properly close HTML tags

It seems like a straightforward task, but I'm not sure where to start looking for the solution. What I'm trying to accomplish is this. I have a script that scans for the strings [gallery]. When it finds [gallery], it replaces the tag with an ima ...

Steps to display a modal within an inactive tab:

I have been using ngx-bootstrap tabset, although I believe this issue could apply to all bootstrap tabs as well. <tabset> <tab class='active'> <angular-modal-component> </angular-modal-component> </tab> <tab> & ...

What is the best way to keep the navbar contained within the parent div (hero image) no matter the size of the screen?

After working on adjusting my website layout for different screen sizes, I encountered an issue with the navbar overflowing the parent image when viewed on my laptop. Despite trying both relative and absolute positioning for the .navbar element and setting ...