The image is exceeding the boundaries of its parent element, even though a height has been specified

I need assistance with aligning an image inside a div that should take the height of its parent div, specifically the .hero class. Despite setting the image's height to 100%, it still extends beyond the parent div and doesn't adhere to the specified height in .hero.

I have ensured that everything is using border-box and I am utilizing TailwindCSS. Is there a crucial detail that I might be missing? Could it be that border-box is not functioning as intended?

Vue file

<template>
  <div class="hero grid grid-cols-12">
    <div class="hero-left col-span-5 grid-cols-12">
      <div class="hero-left-top col-span-12">
        <h1>Let's start something big together.</h1>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. consectetur
          adipisicing elit
        </p>
        <div class="bottom">
          <a class="btn" href="">Register</a>
        </div>
      </div>
      <div class="hero-left-bottom mt-10 col-span-12 grid gap-9 grid-cols-12">
        <div class="col-span-4">
          <h2>30+</h2>
          <p>Years of Excellence</p>
        </div>
        <div class="col-span-4">
          <h2>30+</h2>
          <p>Years of Excellence</p>
        </div>
        <div class="col-span-4">
          <h2>30+</h2>
          <p>Years of Excellence</p>
        </div>
      </div>
    </div>
    <div class="hero-right col-span-7">
      <img class="" src="/imgs/businessman.jpg" alt="" />
    </div>
  </div>
</template>

<script>
export default {
  name: "Hero",
  components: {},
};
</script>

<style lang="scss" scoped>
@import "../../../sass/app.scss";

.hero {
  margin: 50px 0;
  padding: 0 $padding;
  height: 900px;
  background: red;

  &-left {
    &-top {
      .bottom {
        height: 60px;
        display: flex;
        align-items: center;
      }

      p {
        margin: 30px 0;
      }

      .btn {
        height: auto;
        background: $purple;
        padding: 15px 50px;
        font-size: 20px;
        font-weight: 700;
        color: white;
      }
    }
  }

  &-right {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: flex-end;

    img {
      height: 100%;
      width: 100%;
    }
  }
}
</style>

Global SCSS file

* {
    box-sizing: border-box;
    font-family: 'Source Sans Pro', sans-serif !important;
}

Answer №1

When you set the height of an image to 100%, it does not become relative to the parent element; instead, it remains relative to its own height. For example, if an image has a height of 300px and a width of 500px, setting height: 100% in CSS will still make the image's height 300px even if the parent element's height is only 120px.

However, there is a way to make an element's property relative to its parent using the value inherit.

For instance:

div {
    height: 200px;
    width: 300px;
}
img {
    width: inherit;
    height: inherit;
}

This can sometimes create an aspect ratio issue for the image, especially if it is a logo, icon, word, or a person. In such cases, adjusting the height or width to auto or max-content can help.

div {
    height: 200px;
    width: max-content;
}
img {
    height: inherit;
    width: auto;
}

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

Issues with Opera displaying specific characters when using webfonts

Perhaps it's not supposed to work this way, but hear me out. I'm utilizing Google Web Fonts and adding the PT Sans font like this: <link href="https://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" ty ...

"Struggling with a Bootstrap problem in my Accordion Table

I'm working with a Bootstrap table that includes an accordion feature. My goal is to have the second row hidden by default. However, when I click to reveal it, the table shifts. How can I smoothly animate the opening without causing any shifting? ...

Merging columns in Bootstrap 4 grid system

I have been working on creating a Bootstrap grid with the following structure: .facevalue { background: #ffffff61; font-family: Dubai; font-size: 18px; font-weight: 400; line-height: 30px; padding: 0 30px; border: 1px solid #e9e9e9; ma ...

Instructions on creating a number increment animation resembling Twitter's post engagement counter

I've been attempting to replicate the animation seen on Twitter's post like counter (the flipping numbers effect that happens when you like a post). Despite my best efforts, I can't seem to make it work. Here is what I have tried: $(fun ...

What is the best way to align li elements in a ul list at the

How can I center ul elements on a web page? I have checked similar questions on this site and tried using text-align in my CSS file to center the elements, but it didn't work as expected. I also attempted to use margins which did center the elements, ...

Transferring information from template to associated component

Is it possible to transfer data from a template to a component without the need for an event trigger like a button or form submission? For instance, in the code snippet provided, how can we pass the 'item' from the 'items' array to the ...

Make sure to blur all images whenever one of them is clicked

I am currently facing an issue with my webpage where I have 3 images displayed. I have implemented an event listener to detect clicks on the images, and once a click occurs on one of them, I want everything else on the page to become blurred, including the ...

Align the asp.net content generated towards the left side

I am looking to optimize the layout of my asp.net code by shifting the content on all pages to the left side of the screen for better efficiency. Despite my efforts, I have been unable to find a suitable CSS solution that works universally across all view ...

Height of the div dynamically increases upwards

Just a quick question - is there a way to make position: fixed divs at the bottom of an element (such as the body) grow upwards as content is dynamically added? Maybe something like anchor: bottom or increase: up? I'm thinking that using JavaScript m ...

Choose the div element that is immediately before the current div

Attempting to create a harmonica effect by adjusting the height of divs upon hovering over another. The HTML is structured as follows: <html><body> <div class="section1"></div> <div class="section2"></div> <di ...

Navigate the conversation within the dialog without affecting the content below

Is it possible to have a dialog with scrollable content on top of a page that is also scrollable, but when trying to scroll inside the dialog using the mouse wheel, only the dialog body scrolls and not the page below it? What approach can be used to accom ...

Resolve Bootstrap columns with varying heights

My Bootstrap row contains a variable number of columns determined by a CMS, sometimes exceeding the space available on one line. I am looking for a way to neatly display all the columns with equal heights. <div class='row'> <div cl ...

I am looking to create buttons that can switch between two different styles of a specific element, like an h1 tag, when clicked. However, instead of toggling

//In this HTML document, I am trying to achieve a functionality where my buttons can toggle the style of an h1 element between the colors yellow and purple when clicked. However, I have encountered an issue where the buttons disappear during a transition ...

Changing text on a button with JavaScript toggle functionality: A step-by-step guide

I am looking to implement a feature where I can hide and show overflow content in a div. When I click on the expand button, it expands the content. However, I want this button to toggle and change text as well. Thank you! function descriptionHeightMo ...

How can CSS and JavaScript be used to strategically position two upright images next to each other within a dynamically resizing container?

Looking for a way to display two portrait images side by side within a flexible container with 100% width? The challenge I'm facing is accommodating varying widths of the images while ensuring they are the same height. <div class="container"> ...

Unusual CSS inconsistencies between running on VS Web Server and IIS

My current dilemma involves a discrepancy in the appearance of my site when viewed locally through the visual studio web server (http://localhost:3452/) compared to when accessed on IIS7 (http://server/myproject/). Initially, I suspected a CSS issue causi ...

The implementation of subnavbars within a navigation bar using HTML and CSS

I am looking to align the Login button with the settings icon on the same line above the links in my navbar. Is there a way to accomplish this? Check out this fiddle that demonstrates my issue: FIDDLE Here is the code I have written: <body> < ...

Fade background position rollover effect

I'm facing a challenge with animating my background image using background positioning. The CSS is functioning correctly, but when I attempted to enhance it by adding jQuery for a rollover effect, the animation isn't working as expected. Can anyo ...

Tips for bridging the space between jumbotron and about section using Bootstrap

How can I eliminate the gap between the jumbotron and the about section? I have attempted to reduce the margin between the two sections, but it is not working properly. <section id="atas"> <div class="jumbotron jumbotron-flu ...

Make the Angular Modal scrollable except for when a specific div is being click dragged

I am working on an Ionic app that uses AngularJS. One issue I encountered is with a modal popup that can sometimes extend beyond the visible area. To address this, we applied overflow: hidden to its CSS class. However, there is a specific functionality i ...