Applying Media Queries Based on Element Height

Situation: In my scenario, there is an image with two buttons below it, all of which share the same width.

Issue: When the viewport is too wide horizontally, the buttons are not visible without scrolling down. This poses a challenge for tablets and small laptops. The current CSS styling I am using is as follows:

 .image{
        width:90%;
        height: 50%;
        max-width: 500px;
        max-height: 250px;
        margin-bottom: 10px;
    }


    .buttonAnswer{
        width: 90%;
        max-width: 500px;
        font-size: 25px;

I am looking for a solution, whether through media queries or any other method, to resize both the buttons and the image so that they are visible without requiring scrolling on any device size.

It's somewhat challenging to demonstrate the issue. Check the content below, then reduce the viewport to a narrow height and wide width, you will notice that only the image is visible initially and you have to scroll to see the buttons.

  .image {
  width: 90%;
  height: 50%;
  max-width: 500px;
  max-height: 250px;
  margin-bottom: 10px;
}

.buttonAnswer {
  width: 90%;
  max-width: 500px;
  font-size: 25px;
<img class='image center-block' id='image2' src='https://i.imgur.com/AMTXZ2R.jpg' alt="Girl trying to get a job">
<button id='stepOneYes2' type="button" class='buttonsQuestion2 btn btn-info center-block buttonAnswer buttonSpace'> Yes </button>
<button id='stepOneNo2' type="button" class='buttonsQuestion2 btn btn-info center-block buttonAnswer'> No </button>

Answer №1

Implement max-height: 50vh to ensure it occupies half of the screen when displayed.

@media (min-width: 970px) {
  .outer {
    height: 100%;
    min-width: 100%;
  }
  .image {
    max-width: 100%;
    height: auto;
    max-height: 50vh;
  }
  .buttonAnswer {
    width: 90%;
    font-size: 25px;
  }
}

@media (max-width: 970px) {
  .outer {
    height: 100%;
    min-width: 100%;
  }
  .image {
    width: 90%;
    height: auto;
    max-height: 50vh;
  }
  .buttonAnswer {
    width: 90%;
    font-size: 25px;
  }
}
<div class="outer">
  <p style="text-align:center"><img class='image center-block' id='image2' src='https://i.imgur.com/AMTXZ2R.jpg' alt="Girl trying to get a job"></p>
  <button id='stepOneYes2' type="button" class='buttonsQuestion2 btn btn-info center-block buttonAnswer buttonSpace'> Yes </button>
  <button id='stepOneNo2' type="button" class='buttonsQuestion2 btn btn-info center-block buttonAnswer'> No </button>
</div>

Answer №2

To address your issue, implementing media queries might be the ideal solution. It would involve specifying a different size for the image, particularly adjusting the max-width, for the necessary viewports.

Alternatively, you could utilize viewport units to set the image size dynamically. Currently, you have a fixed max-width in place without considering the viewport size. By setting it to 50 percent of the viewport height and adapting other values accordingly:

.image{
    max-height: 50vh;
}

An effective approach may be to combine both solutions for optimal results.

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

Retrieve the child element index of a specific element using jQuery

I am dealing with a group of 'DIV' elements, each containing a list of 'p' elements. Take a look at the example below: <div class="container"> <p>This is content 1</p> <p>This is content 2</p> ...

Transforming a vertical table into a horizontal layout

I'm facing an issue with transforming the database object for table display. My database contains a table structured like this. name total date Awoc1 100 9/14/2022 Awoc1 200 9/15/2022 Awoc1 300 9/16/2022 Awoc2 100 9/14/2022 Awoc2 ...

Is there a way to extract a list of words from a designated element using selenium?

I have been trying to scrape the content of this webpage 10 Fast Fingers in order to extract the words that need to be typed into a list. My intention is to then input these words into a text box for typing practice. The code seems to be running fine, but ...

What is the best way to structure the layout: Subdomains or External CSS for Web and Mobile Web?

I'm currently working on designing an application that needs to be compatible with both web and mobile web browsers. Due to the differences in screen sizes, I understand that I will need to create different layouts and views to ensure optimal user exp ...

Are you struggling to get basic HTML and JS code to function properly?

I'm currently working on developing a racing game, and my initial step was to create the car and implement movement functionality. However, I've encountered an issue where nothing is displaying on the canvas - neither the rectangle nor the car it ...

The function "onClick" within an external .js file is being referenced

Just starting to learn Javascript and experimenting with an onClick event for an image in an external .js file using the HTML tag. <script type="text/javascript" src="embed.js"> The code found in "embed.js" is as follows: var image1=new Image(); i ...

What is the best way to simulate a library in jest?

Currently, I am attempting to simulate the file-type library within a jest test scenario. Within my javascript document, this particular library is utilized in the subsequent manner: import * as fileType from 'file-type'; .... const uploadedFil ...

Using jQuery to animate a div when clicked by the mouse

Here's the scenario: I have 3 block elements arranged in a specific order: Image Hidden Text Block (.hidden) Footer Block (.blockimage) When the page loads, the image is displayed over the hidden text block (which contains further infor ...

What is the process for defining an outcome when a draggable element is placed into a droppable area using Jquery?

Currently, I am working on a task where I need to increase the value of a counter (var counter = 0;) every time a draggable image is dropped into a dropzone, which is also an image rather than a div. $( "#goldbag" ).draggable({ revert: "invalid", containm ...

Errors in vue.js conditions

I am currently attempting to validate whether my variable is empty. Despite reviewing my code, I am facing issues with its functionality. My current version of vue.js is 2.5.13 Below you can find the snippet of my code: <template> <div v-if ...

Is there a way for ASP.NET MVC to automatically notify when a record is edited or updated?

Having recently used the NotifyIcon class in a windows application, I found it to be quite useful. As someone who primarily works as a web developer, I am curious if there is something similar available for websites. The website that I am working on inclu ...

Switch between a list of labels dynamically with checkboxes in React

I'm currently working on a React component that displays an array of cars. I want to show a list of labels with the names of all diesel cars by default, and then have a checkbox that, when clicked, toggles to show all cars. interface ICars { name ...

Unable to achieve horizontal alignment with DIV element set as inline-block

Currently, I have five columns that are aligned using the display:inline-block. Everything seems to be working fine, but there's a peculiar issue that arises when I refresh the page multiple times. Occasionally, while the first column remains at the t ...

Is it possible to dynamically create an interface using an enum in TypeScript through programmatically means?

Recently, I defined an enum as shown below: enum EventType { JOB, JOB_EXECUTION, JOB_GROUP } Now, I am in need of creating an interface structure like this: interface EventConfigurations { JOB: { Enabled?: boolean; }; JOB_EXECUTION: { ...

Troubleshooting the Ineffective Replace Method in React Component

When working in my React component, I have implemented the use of the replace method to substitute a word. However, it seems that this functionality is not generating the expected outcome: const myComponent = () => { const [textVal, setTextVal] = ...

The relative positioning is currently being obstructed

Having some trouble using position:relative to shift my logo to the left of my webpage. Oddly, moving it 20px seems to have no effect, but shifting it by 300px downwards causes it to vanish completely. Here's a snippet of my current code: .container ...

converting JSON data fetched from an API into state using setState

My goal is to properly map a JSON response into a state by excluding the first array and only displaying its children. Here is an example of what I have attempted: fetch(api).then((response) => { response.json().then((data) => { data.children. ...

Having difficulty retrieving information from Redux store

In my project, I utilize the Redux store to manage data. Through Redux-DevTools, I can observe that initially the data is null but upon refreshing the page, the data successfully populates the store. However, when attempting to retrieve this data within on ...

Executing an ajax post when a user clicks on a link or button

<tr> <td> <span id="id_1"> <a href="/Path" >Name</a> <a href="#">Delete</a> </span> </td> &l ...

What is the encoding for Javascript in UTF-8?

My current ajax call is able to successfully send the request and retrieve the expected response. However, I am facing difficulty in correctly displaying it in li items. $.ajax({ url: "{% url 'users:profile_page_tags_get' 'primary&apos ...