Strategies for increasing the height of a child element relative to its parent container

<body>
  <section class="container">
    <div class="child">
      //content goeshere
    </div>
  </section>
<body>

I have a HTML code snippet above. I am trying to set the height of the 'child' class div based on the body tag using CSS. Can someone help me achieve this?

Answer №1

If you want the .child element to fill the entire height of the screen, follow these steps:

  1. Set the body height to 100vh - where vh represents viewport height units, with 1vh being equal to 1% of window height.

  2. Set the height of the container to 100% of its parent, in this case, the body.

  3. Set the height of the child to 100% of its parent, which is the container.

body {
  height: 100vh;
}
.container {
    height: 100%;
} 
.child {
    height: 100%;
    background: orange;
}
<body>
  <section class="container">
    <div class="child">
      //content goes here
    </div>
  </section>
</body>

Here is an alternative solution where the height of the child is not bound by the container's height. (Different backgrounds and widths/heights are added for a better visual appeal)

body {
     position: absolute;
     width: 80%;
     height: 80%;
     border: 1px solid green;
     background: lightgreen;
}
.container {
    width: 20%;
    height: 100px;
    background: teal;
} 
.child {
    position: absolute;
    width: 50%;
    height: 100%;
    margin: 0 auto;
    left: 0;
    right: 0;
    top: 0;
    background: orange;
}
<body>
    <section class="container">
     Before content
    <div class="child">
      //content goes here
    </div>
    After content
  </section>
</body>

Answer №2

employ identical class name........

example :

<header>
  <div class="parent">
    <article class="parent">
      //insert your content here
    </article>
  </div>
<header>

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 is the best way to align two divs next to each other while keeping the second one centered with flexbox?

Here are my two div elements: <div class='container'> <div class='left'></div> <div class='centered'></div> </div> I am looking to center the second inner div and have the first inner ...

What is the reason for the malfunction of this JavaScript code designed for a simple canvas operation?

I'm having trouble with the code below. It's supposed to display a black box on the screen, but for some reason it's not working properly. The page is titled Chatroom so at least that part seems to be correct... <html> <head> &l ...

Ways to showcase an icon within a select options tag

Is there a way to show Font Awesome icons in select options tag? I have tried using it outside like this <i class="fas fa-chart-pie"></i> But I'm not sure how to display it within the <option> tags instead of text. <select id="s ...

Creating a Sleek MenuBar in JavaFX

At the top of my BorderPane, I have added a MenuBar containing a "File" Menu and a "Close" MenuItem: https://i.sstatic.net/5TAGS.png I want to make it appear thinner like the MenuBars in most software applications, similar to the image below: https://i. ...

The vanishing footer issue arising while adding the back to top button

When attempting to place the "back to top" button on the right-hand side just above the footer, the footer disappears. I am striving to position the button just above the footer while ensuring it aligns correctly. Prior to adding the "back to top" button, ...

Issues reported with Adobe Air and Canvas functionality not functioning as expected

I am attempting to create a simple Air App using Dreamweaver CS5.5, but I am running into an issue with loading an image onto the Canvas element through a function. While I can successfully load the image onLoad, my goal is to draw the image on the canva ...

Two distinct sets of radio buttons with separate functionalities

I have a unique situation where I have two distinct sections that contain sets of radio buttons with different values and IDs. However, I am facing an issue as I can only select one button at a time for both sets. My requirement is to have these two sets ...

Guide to creating a distortion-free layout with Bootstrap 4

I am in need of creating a design layout using Bootstrap 4 with the following specifications: https://i.sstatic.net/JeUHh.png Here is my code : <div class="row"> <div class="col-md-7"> A </div> <div class="col-md-5"> <div ...

store user settings in local storage

After writing some code with a link that toggles text visibility upon click, I now want to incorporate saving this state in web storage so that it persists upon page reload. As a beginner in JavaScript and HTML, this task has proven challenging for me. Th ...

Mobile browser registers double click/tap event

My mobile web application has a feature where clicking a button will automatically create a team of players for you. If you don't have any players in your team yet, you won't receive a warning. However, if there are already players in your team, ...

Create a jQuery dropdown menu within the same division element

I need help displaying a drop-down menu on mouseover using only one div. Currently, I am employing 2 divs and sliding up another div to show the submenu; however, I want to achieve this with just 1 div. Is there a way to do that? Below is my code snippet: ...

How can I speed up the loading time of my background image?

I have noticed that my background image is loading slowly on my website. I expected it to be cached so that subsequent pages using the same background would load instantly. However, the background image is only loading once the entire page is drawn. My CS ...

What is the ideal amount of HTML code to include in an Angular controller?

What are the implications of storing long paragraphs in controllers? For instance, if you have to display multiple lengthy paragraphs using ng-repeat, you may create a data structure like this within your controller: $scope.paragraphs['300 word para ...

What is the process for implementing filtered HTML attributes in a directive?

I’ve created a custom directive that generates a popup menu by extracting data from HTML content. The purpose is to transform a group of Bootstrap carousel-compatible elements into a structured list. However, each .item element contains an attribute with ...

How come my padding isn't working on an anchor tag, but it works fine on a button element?

Consider this code snippet: .ul { list-style-type: none; } .li { display: inline-block; border: black solid 1px; margin-left: 20px; } .btn { padding: 20px; text-transform: uppercase; } <ul class="ul" ...

Creating a polished look with CSS through rounded font edges

Can CSS be used to create rounded edges on a font? I'm looking for an effect similar to the smooth edges in Photoshop https://i.sstatic.net/w4Zd4.jpg I attempted using an SVG filter, but found it quite complex. Is there a simpler method? Here is ...

What's the reason for the align-self: flex-end CSS property not working as expected on a flex div element?

I am trying to align the "Click me" button to the left of the div that contains it. Here is my code: render = () => { return ( <Wrapper> <Body> <span>Title</span> <Desc ...

Need help incorporating a "section trail" into your website's navigation sidebar using JS/jquery? Here's how you can do it!

My website is quite extensive and contains numerous elements. There are times when I find myself wanting to navigate back and forth between different sections of the page. I've noticed that some websites have a feature called a "page trail," which di ...

Navigate through the options on the left menu by sliding your

I'm attempting to create a menu that slides in a submenu from the left on hover, but the issue is that with this code, all submenus open instead of just the one related to the hovered link. Here is my HTML code: <ul id="NavMenu"> ...

Stop access to geojson files through targeted media queries

Using a vue.js app, I have the ability to choose locations using either a map or an input field. However, for mobile users, I want to exclude the map and only serve the search engine. The issue is that it's not very user-friendly on mobile devices be ...