Designing with CSS: Achieving a full-width background image layered above content below the window

I'm in need of some assistance. Despite my efforts to search and browse forums, I'm unable to find a solution to my problem.

I want to design a landing page with a tall background image that spans 100% width and adjusts to both the browser window and the varying height of the content. The content should be positioned below the browser window boundary, so that only the image is visible initially and users can scroll down to view the content overlaying the bottom part of the extended image.

Here's my current HTML:

<body>
    <section id="sectionOne">
        <div id="sectionOneLanding"></div>
        <div id="sectionOneContent">CONTENT TO SIT HERE</div>
    </section>
</body>

And my CSS:

html,
body {
    height:100%;
    width:100%;
}

#sectionOne {
    height:100%;
    background-image: url(../images/cliff.jpg);
    background-size: cover;
}

#sectionOneLanding {
    height:100%;
    width:100%;
}

Currently, the image crops to 100% browser height and the additional content appears over a white background instead of the image extension. I suspect this is because the #sectionOne height is set to 100%, but increasing it only pushes the content further down while still maintaining the white background. Changing Background-Size to 100% had the same result as using cover.

Do you have any suggestions or handy CSS tricks to achieve this effect?

I apologize if this is unclear. Feel free to ask any questions you need clarification on, as it can be difficult to explain.

Answer №1

To resolve this issue, it is recommended to utilize positioning.

html, body {
  height: 100%;
  margin: 0;
}
#sectionOne {
  position: relative;
  height: 100%;
  background-image: url(../images/cliff.jpg);
  background-size: cover;
  background-color: #99f;
}

#sectionOneLanding {
  height:100%;
}
<section id="sectionOne">
  <div id="sectionOneLanding"></div>
  <div id="sectionOneContent">CONTENT TO SIT HERE</div>
</section>

There is no need to specify width: 100%, but it is essential to include margin: 0; to override the default margins. I have also added a background color to clearly highlight the full background while keeping the content on the page.

If you require both the image and the content to be displayed on the same page, then utilizing Flex Box is the solution.

FlexBox Example

body, html {
  height: 100%;
  margin: 0;
}
#sectionOne {
  display: flex;
  flex-direction: column;
  height: 100%;
}
#sectionOneLanding {
  background-color: #99f;
  flex: 1;
}
<section id="sectionOne">
  <div id="sectionOneLanding"></div>
  <div id="sectionOneContent">CONTENT TO SIT HERE</div>
</section>

Preview

https://i.sstatic.net/iSSEN.png

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

When the margin-left changes, SVG begins to flicker and shake in a marquee effect

I've been experimenting with a marquee effect using vanilla JS. The effect is working, but I'm encountering some shaking issues with SVG and images as they move. <div class="marquee"> <h1>Nepal <svg version="1.1&qu ...

Integrate JavaScript with WordPress

How can I get this functioning properly within Wordpress? I have been attempting to connect everything together, but it doesn't appear to be working. Is this the correct way to do it, or am I overlooking something? <script type="text/javascript" s ...

Customizing the background color of an option using HTML and CSS

I am currently developing an option form where each selection will appear in a different color as the user scrolls down. Once a selection is made, it should be saved and displayed with the chosen color. https://i.sstatic.net/gvdpt.png My goal is to save ...

Is there a way for me to determine if something is hidden?

My goal is to have selector B toggle when selector A is clicked or when clicking outside of selector B. This part is working fine. However, I'm struggling with preventing selector B from toggling back unless selector A is specifically clicked - not w ...

Having trouble with SCSS styles not being applied after refactoring to SCSS modules?

Currently, I am in the process of restructuring an application to ensure that component styles are separated from global styles using CSS modules. However, I have come across an issue where the styles are not being applied correctly. The original code sni ...

Tips for scaling an image to perfectly fit the browser screen

I have spent a significant amount of time researching and coding, but I am still unable to get this seemingly trivial task to work. Here are the conditions: The browser window size is unknown, so any solution involving absolute pixel sizes will not work. ...

Is it possible to use columns in the Bootstrap 4 grid system to create text that wraps around an image?

I have been searching for a solution all day without success. Can anyone help me figure out how to wrap text around an image using d-flex or flex-wrap in Bootstrap 4's grid layout with columns and rows? Below, I have shared my code and included two im ...

Expanding the image container to fill the entire page in bootstrap is possible

https://i.sstatic.net/EQQi6.pngIt seems like I must be making a mistake as this is only my second day working with Bootstrap. I have a fluid container with two rows. The first row contains a title and an image. However, the image - even though I resized it ...

What could be causing Media-Query to fail when using the max-width media feature?

I recently added a max-width media feature to my code, but for some reason, the colors aren't changing when the width is less than 300px. I've been trying to inspect elements on my laptop to troubleshoot the issue. Additionally, I've made su ...

Equal-height rows and columns using Flexbox

I've been working on a row layout with two columns, each set to 50% width. The left column contains an image, while the right column displays text. Here is my HTML: .row{ display:flex; ...

Mastering objectFit with Next.js 13: Unleashing the Power of <Image>

Within the latest version of next/image documentation, specifically version 13.0.0, it indicates that certain props such as layout, objectFit, objectPosition, lazyBoundary, and lazyRoot have been removed. The documentation for fill in the next/image sect ...

Guide on creating a div container that spans the full width of the HTML page with a horizontal scrollbar

I'm facing an issue where the div container is not extending over the entire HTML page with a horizontal scroll bar. Here's a visual representation of my problem: Image Despite using MathJax to display math formulas, the div container does not ...

Duplicate the image from a specific div to another div, ensuring that only one clone is created

I'm trying to replicate an image in multiple spots within a frame, but I can only manage to get one instead of many. Can someone please tell me what I'm doing wrong? I am creating a map of terrain where I need to clone images from one div to anot ...

Carousel issue with sliding on Bootstrap when deployed on various web servers

I am experiencing a strange issue with my website. I have it installed on two different servers, and while the Carousel works perfectly fine on the first server, it does not slide properly on the second server. Here are the links to each version of the we ...

Applying class to target specific screen size in Bootstrap

I've been utilizing bootstrap for my project. My objective is to assign multiple classes based on screen size, such as sm-col and xs.col. For example, let's say I have a text id <div id="text" class="xs-format lg-format ..."></div> ...

Menu navigation - ensuring the background color extends to cover the entire page, not just the viewport

I'm facing an issue with the left navigation bar's design. Specifically, I'd like the grey color of the bar to extend vertically across the entire page. Currently, the gray bar ends at the viewport's edge. Is there a way to make the ba ...

PHP: Using conditional statements to adjust datetime formatting for CSS styling

My title may not make sense, as I am new to PHP. I'm trying to create a custom member tag for my forum that shows "X Years of Experience" with different colors based on the number of years. For example, red for under 6 months, blue for year one, yell ...

Troubleshooting problems with Bootstrap datepicker in a Vue project

I am facing a problem with the b-datepicker feature while trying to select a date. The dates are not appearing correctly, as shown in the image linked below. Here is my code: <b-form-group class="my-3" > <b-input-group> ...

Is it possible to make an element draggable after it has been prep

Seeking assistance with making a notification draggable when added to a webpage. The notifications are housed in a parent div named notification_holder Here is the structure: <div class="notification_holder"> <div class="container"><b ...

Blurry font rendering occurs when an element is scaled using CSS

I want to add a scale animation to a bootstrap button, but the text appears blurry. I've tried all the solutions mentioned here. How can I resolve this issue? Below is the code snippet (text is slightly less blurry in the snippet compared to my pr ...