What is the best way to have an image fill the remaining space within a 100vh container automatically?

I have a container with a height of 100vh. Inside it, there is an image and some text. The text's height may vary based on the screen size and content length. I want the text to occupy its required space while the image fills up the remaining available space.

This code works as intended, but I would like to remove the fixed 83vh for the image. Can the image automatically adjust to take up the rest of the space? Is this achievable?

Thank you in advance!

(Note: I included the Javascript tag because I'm unsure if a Javascript solution is needed.)

.container {
    height: 100vh;
    max-height: 100vh;
    background-color: #CCCCCC;
}

img {
    max-height: 100%;
    width: 100%;
    object-fit: cover;
    object-position: center -180px;
    /* dynamically adjust height */ 
    height: 83vh
}

.text-wrapper {
    padding-left: 15px;
}
<div class="container">
    <img src="https://img.example.com/img/image.jpg">
    <div class="text-wrapper">
        <h1>Insert Text Here</h1>
        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor <br>invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
    </div>
</div>

Answer №1

To create this layout, you can utilize CSS grid. The grid's cells are set to 1fr (fill available space) and auto for height.

This code snippet demonstrates moving the image to a background-image within a div, covering its cell and centering it.

* {
  margin: 0;
}

.container {
  position: relative;
  height: 100vh;
  width: 100vw;
  max-height: 100vh;
  background-color: #CCCCCC;
  display: grid;
  grid-template-rows: 1fr auto;
  grid-template-columns: 1fr;
}

.img {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center center;
  background-image: url(https://img.example.com/img/sample.jpg);
}

.text-wrapper {
  padding-left: 15px;
}
<div class="container">
  <div class="img"></div>
  <div class="text-wrapper">
    <h1>Some Text goes here</h1>
    <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor <br>invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
  </div>
</div>

Keep in mind: if the text exceeds the viewport's height, consider how you want it to be displayed in a fully responsive manner.

Answer №2

One possible solution is to implement the following CSS:

.container {
    height: 100vh;
    max-height: 100vh;
    background-color: #CCCCCC;
    display: flex;
      flex-flow: column;
      height: 100%;
}

.image-wrapper {
    flex: 1 1 auto;
    max-height: 100%;
    overflow: hidden;
}

img {
    max-height: 100%;
    width: 100%;
    object-fit: cover;
    object-position: center -180px;
}

.text-wrapper {
    padding-left: 15px;
    flex: 0 1 auto;
}
<div class="container">
    <div class="image-wrapper">
        <img src="https://img.100r.systems/img/be10bc26de653473f80b4348addfc6b5.jpg">
    </div>
    <div class="text-wrapper">
        <h1>Some Text goes here</h1>
        <p>I think it works now<br> and I'm very happy now<br>It works, right?
    </div>
</div>

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

What does the visual representation of <html> look like, exactly?

Where does the scroll bar belong, within the <html> or <body> tags? I've always included it in every HTML file, but unsure of its specific purpose. ...

transfer chosen data from a text box to a dropdown menu

Looking for: I am in need of a feature where users can input a movie title in a textbox and upon clicking the "move to selectedlist" button, the entered movie title should be added to a dropdown list that displays all the movies selected by the user. Addit ...

Automatically navigate to a specific element as the user scrolls down the page

I am attempting to achieve a similar effect as seen on the App Builder Website. When the user reaches the header with the background image/video and scrolls down, the website smoothly transitions to the next div/section. If the user scrolls back up and ret ...

Embarking on the journey of creating mobile web applications - where should you begin?

How can I begin learning to create mobile web apps? I have a keen interest in the design aspects, particularly the effects and animations that give them a native appearance. I suspect there are differences in CSS, HTML, and possibly JavaScript compared to ...

How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers ...

Sass error: Unable to locate the stylesheet for import while trying to import bootstrap

I am currently working on integrating bootstrap into my project directory using npm. Below is an overview of my file structure: https://i.sstatic.net/HjExY.png Within my style.scss file, I am utilizing the @import feature to include Bootstrap as follows: ...

Deleting a li element within an AJAX response function can be accomplished by targeting the specific

I am attempting to remove the li element that is the parent of the clicked a element. Here is the code I am using: function vanish(id_arg){ $.ajax({ url: "/vanish/", type: "POST", data: {id_to_delete: id_arg}, ...

Strategies for creating a grid-inspired layout: tips and tricks

Recently, I've been tasked with creating a catalogue section for our e-commerce site. My designer came up with this layout which I find really appealing, but I'm having trouble figuring out how to implement it. I attempted using tables, but it di ...

Code - Capture user's input

I have multiple input fields in my HTML document and I want to retrieve the text entered into one of them. Here's an example of one such input field: <TH> <FORM> <input name="designation" type="text" size="12" /> < ...

JavaScript and jQuery validation issues persisting

Here is the HTML code I am using: <script src="js/validate.js"></script> <label>FIRST NAME:</label> <td> <input type="text" class="firstname" id="firstname" onKeyUp="firstname()" /> </td> <td> <label id=" ...

Combine multiple values into a single input in the number field

I have a number type input field... What I am looking for is: Age-Height-Weight 20-180-80 Is there a way to ensure that users input data in this exact format and then have the final result inserted into the input field with type="number" and submitted? ...

Tips for using jQuery to adjust HTML attributes based on conditions

I'm still trying to figure out how to assign a different class attribute to an element based on another element's dynamic numberical attribute. Here is the HTML & PHP code I am working with: <?php for($i=1;$i<=$total;$i++){ ?> <a hr ...

Error: The argument passed to int() must be a string, bytes-like object, or number, not 'NoneType' (Issue when sending HTML input to Python)

#!C:\Users\aan\AppData\Local\Programs\Python\Python39\python.exe import numpy as np import skfuzzy as fuzz import cgi from skfuzzy import control as ctrl print("Content-type:text/html\r\n\r\n ...

The tabbing feature in bxslider disrupts the alignment of slides, making it

After encountering accessibility issues, I upgraded the bxslider library to version 4.2.3 for better support. Check out this example of bxslider where you can easily tab through the controls: http://jsfiddle.net/qax7w8vt/2/embedded/result/ The problem a ...

Utilizing jQuery for Summation Calculation

How can I use jQuery to calculate the total sum of prices for all products in a table with the column name "price"? Once calculated, how do I store this sum in a variable? <table id ="tab"> <th>Product</th><th>Price</th> ...

Issue with Angular FormControl Pattern Validator failing to validate against regex pattern

My goal is to restrict a text input field to specific characters only. I am looking to allow: alphanumeric characters (a-z A-Z 0-9) 3 special characters (comma, dash, single quotation mark) : , - ' A few accented characters: à â ç è é ê î ô ...

What could be causing the malfunction in my JavaScript random selector?

Can anyone assist me with this issue? I'm attempting to implement JavaScript functionality to highlight randomly selected picks that I input, but it's not functioning correctly. Every time I inspect my JS code on the webpage, I encounter an erro ...

I am unable to see the last row of the table when I apply a condition after the table name

I need to display all the data from my table. However, if I include ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, the last row does not appear. <?php include "db.php"; $tbl_name="report"; // Table name $sql="SELECT * FROM $tbl_ ...

Issue with ngClass not updating during Route Event

I am using a bottomNavigation component that changes its style to indicate which route we are currently on. Below is the template for the bottom-navigation: class="menu-icon" [ngClass]="{ 'active-item': buttonActivated.value == '/my-goa ...

Showing text in a textarea while maintaining formatting (such as using <br>)

Can someone help me with formatting a block of text like this: hello <br> how is your day <br> 123 To look like this: hello how is your day 123 I also need to display it in a textarea field. I attempted to do so using the following code: $ ...