Ways to ensure the child div's border height matches that of the parent div

I am facing an issue with a child div that has padding. When I add a border-right to the child div, how can I make it full height? Below is the code snippet:

<div class="parent">
  <div class="child">
    <h2>Content</h2>
  </div>
</div>

.parent{
  border: 2px solid black;
}
.child{
  width: 25%;
  border-right: 2px solid red;
}

The current output of my code: https://i.sstatic.net/OidU3.jpg

Desired result: https://i.sstatic.net/96YPz.jpg

I'm new to this, any help would be greatly appreciated. Thank you!

Answer №1

To center the text within a parent div, remove the default margin of the h1 element and use padding instead. Set the child div's height to 100% and adjust the CSS as shown below:

.parent{
  border: 2px solid black;
  height:40px;
}
.child{
  width: 25%;
  height:100%;
  border-right: 2px solid red;
}
h2{
  margin:0;
  padding-top:8px;
}
<div class="parent">
  <div class="child">
    <h2>Content</h2>
  </div>
</div>

Answer №2

The issue lies in the fact that the h2 tag inherits default top and bottom margins from the browser. One solution is to reset those margins to zero like this (check example here):

h2 { margin: 0 }

Alternatively, if you prefer keeping the margins, you can set the overflow property of the child's container to hidden:

.child {overflow: hidden;}

For demonstration, consider the following setup:

.parent {
    border: 2px solid black;
}

.child {
    width: 25%;
    overflow: hidden;
    border-right: 2px solid red;
}
<div class="parent">
    <div class="child">
        <h2>Content</h2>
    </div>
</div>

You can view a live demo on JSFiddle.

Answer №3

To ensure that your child element inherits the maximum height from its parent, include the following CSS code snippet in your stylesheet:

.child{
 max-height: inherit;
}

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

Capture screenshots of the web page URLs within the Chrome browser by utilizing PhantomJS

Looking for a way to capture screenshots of various URLs and display them in the Chrome browser using PhantomJS. How can I achieve this? Any assistance would be greatly appreciated, as nothing is currently showing up on the webpage. This is how my code a ...

Refreshing the page after executing a MySQL UPDATE statement

There seems to be a problem with the page refreshing faster than the query execution. I have an UPDATE query to update my database with values in my fields, triggered by pushing a button. However, upon clicking the button (which refreshes the website), th ...

Creating a static HTML page using Flask and incorporating redirects

I have a web application built using Flask that processes a URL, performs some operations, and then redirects the user to another URL. I am looking for a way to display a simple "please wait" message in a static HTML page while the processing is happenin ...

When the user clicks, retrieve the contents of list p and showcase them in a div

Struggling a bit with jQuery, looking for some assistance. I am trying to extract the text within p class="area-desc" when class="caption" is clicked and show it in class="step-area." Here's the code snippet: <ul id="main-areas" class="group"> ...

Switching the class for the pseudo-element :after

Consider a scenario where there is a button: <button href="#" class="search no-style search-active" style="display: inline-block;"> <div class="pointer" style="display:none">::after</div> </button> Upon activation of the button, ...

Are DIV elements really impossible to click using selenium Web Driver?

Can DIV elements be clicked using selenium Web Driver? For example, I'm having trouble clicking the delete button in Gmail. https://i.stack.imgur.com/zsyio.png I've been trying to locate the element using the XPATH = //div[@aria-label='De ...

Reposition and resize an image. Creating a grid layout

I'm having trouble with positioning and cropping an image correctly on my website. I want it to look like this (hero-section): The entire project is based on a grid layout. I need a circular image that is larger than the container, positioned to the ...

Having issues with the CSS block in my Jekyll dropdown navbar

I have been exploring resources from both w3schools and another website to create a navigation bar in Jekyll using frontmatter. However, I am running into issues with the block property in CSS. Most of the navbar is functioning properly, except for the dro ...

Building web navigation using a combination of HTML, JavaScript, and PHP within a category, sub

I've been struggling to find a detailed tutorial on implementing a dynamic website navigation system using javascript or php. It seems like every time I attempt to research this topic, I end up feeling confused and unsure of where to start. My goal i ...

Tips for saving values created with JavaScript as an HTML file

I am looking to download an appended value as a HTML file. In my code, values are being appended from an external HTML file through an iframe. The code is functioning correctly and retrieving the right value. Now, I just need to download that appended valu ...

Is it possible for a table data cell to be nested within another table

Is it possible for a <td> to be a direct child of another <td>? For example: <td class="parent"> <td class="child"> <!-- Some content --> </td> </td> Or is it necessary to always create a new < ...

Error: knockoutjs - unable to locate ko

Here is the knockoutjs code that I have written: $(function () { function QuizViewModel() { var self = this; self.previousQuestions = ko.observableArray([]); self.questions = ko.observableArray([]); self.thisQuestion = ko.observable() ...

I want to have a video playing in the background of my home page, but when viewers access it on their mobile devices, I would like to display a specific image instead

Is there a specific attribute I can include to make the image display instead of the video on mobile or small devices? ...

SCSS: When hovering over one div, display a different div

My goal is to display the elements in another div when hovering over a specific div. However, my current code does not seem to be working as expected. At the moment, there is no response upon hover and it remains non-responsive. I would greatly appreciate ...

Trouble with AngularJs 1.x causing Bootstrap4 menu toggle collapse to malfunction

Issue with Navbar toggle menu not closing properly. I have attempted the solutions provided here and others in an effort to fix this bug. However, none of them seem to work for me. Here is a snippet of my code: <nav class="navbar navbar-expand-lg ...

Error in JavaScript Slideshow

I am attempting to create a slider that changes with a button click. Below is my JavaScript code: var img1=document.getElementById("p1"); var img2=document.getElementById("p2"); var img3=document.getElementById("p3"); function slide(){ ...

Gradient scroll effect for dynamic backgrounds

I am currently working on a code where I want the background to scroll instead of the text. This means that the text will stay fixed in its position while the background scrolls behind it on the page. Any suggestions on how I can achieve this? For an ex ...

Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One o ...

Mapbox map rendering problem in React with 100% high resolution

Currently working on rendering a map using Mapboxgl, Bootstrap 4, and React. The goal is to make the map take up 100% of the page height and display in the first column of a two-column grid. However, there's an issue when using React where the width ...

The vanishing act of CSS divs as you scroll there

I am currently working on a website that relies solely on CSS and hover states to create a drop-down menu. The issue I am encountering is that the navigation items are numerous, causing users on smaller laptops to have to scroll to see all of them. However ...