What is the best way to make my div equal in height to its preceding div sibling?

I want my new div to match the height of the previous one. I'm using percentages for width and height to ensure it displays properly on mobile devices as well. I've tried setting the parent elements to 100% width and height based on suggestions from various forums, but that didn't work. I also attempted using display: table-cell, without success. Here's how my setup looks:

index.php:

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "UTF-8"/>
        <title>SiteTitle</title>
        <link rel = "stylesheet" type = "text/css" href = "styles/index.css"/>
    </head>
    <body>
        <div id = "allContent">
            <div id = "header">
                <div id = "siteLogo">
                    <h1 id = "title">Site_Title</h1>
                </div>
                <div id = "shortMenu">
                    <a href = "#">Login</a>
                </div>
            </div>
        </div>
    </body>
</html>

index.css:

body {
    background-color: rgb(0, 0, 0);
    margin: 0px;
    padding: 0px;
    color: rgb(192, 74, 0);
}

div#allContent {
    width: 80%;
    margin: auto;
}

div#allContent div#header {
    margin: 0px;
    padding: 0px;
}

div#allContent div#header div#siteLogo {
    margin: 0px;
    padding: 0px;
    width: 70%;
    border: 1px solid blue;
    float: left;
}

div#allContent div#header div#siteLogo h1#title {
    margin: 0px;
    padding: 0px;
    font-size: 300%;
    font-family: Sans-Serif;
}

div#allContent div#header div#shortMenu {
    width: 20%;
    border: 1px solid green;
    float: left;
}

div#allContent div#header div#shortMenu a {
    font-family: Sans-Serif;
    color: rgb(204, 204, 204);
    text-decoration: none;
}

At this point, I'm not sure what else to try. I basically want the bottom of the Login link to align with the bottom of the website title. In the future, I may switch it out for a logo image so the height could change. I want a solution that adapts automatically without manual adjustments each time. Currently, the Login link is at the top of the page.

EDIT: I included the following code snippet, which unfortunately resulted in each div having a width of 50%

div#allContent div#header {
  margin: 0px;
  padding: 0px;
  display: flex;
}

div#allContent div#header div {
  flex: 1;
}

Answer №1

Give this code a shot

utilizing display:table-cell

body {
    background-color: rgb(0, 0, 0);
    margin: 0px;
    padding: 0px;
    color: rgb(192, 74, 0);
}

div#allContent {
    width: 80%;
    margin: auto;
}

div#allContent div#header {
    margin: 0px;
    padding: 0px;
}


div#allContent div#header div#siteLogo h1#title {
    margin: 0px;
    padding: 0px;
    font-size: 300%;
    font-family: Sans-Serif;
}


div#allContent div#header div#shortMenu a {
    font-family: Sans-Serif;
    color: rgb(204, 204, 204);
    text-decoration: none;
}
div#allContent div#header div#shortMenu {
    width: 30%;
    border: 1px solid green;
    display: table-cell;
    vertical-align: middle;
}

div#allContent div#header div#siteLogo {
    margin: 0px;
    padding: 0px;
    width: 70%;
    border: 1px solid blue;
    display: table-cell;
}
div#allContent div#header {
    margin: 0px;
    padding: 0px;
    width: 100%;
    display: table;
}
<div id = "allContent">
            <div id = "header">
                <div id = "siteLogo">
                    <h1 id = "title">Site_Title</h1>
                </div>
                <div id = "shortMenu">
                    <a href = "#">Login</a>
                </div>
            </div>
        </div>

using display:flex

modify this css code

div#allContent div#header div {
  flex: 1 auto;
}

body {
    background-color: rgb(0, 0, 0);
    margin: 0px;
    padding: 0px;
    color: rgb(192, 74, 0);
}

div#allContent {
    width: 80%;
    margin: auto;
}

div#allContent div#header {
    margin: 0px;
    padding: 0px;
}

div#allContent div#header div#siteLogo {
    margin: 0px;
    padding: 0px;
    width: 70%;
    border: 1px solid blue;
    float: left;
}

div#allContent div#header div#siteLogo h1#title {
    margin: 0px;
    padding: 0px;
    font-size: 300%;
    font-family: Sans-Serif;
}

div#allContent div#header div#shortMenu {
    width: 20%;
    border: 1px solid green;
    float: left;
}

div#allContent div#header div#shortMenu a {
    font-family: Sans-Serif;
    color: rgb(204, 204, 204);
    text-decoration: none;
}
div#allContent div#header {
  margin: 0px;
  padding: 0px;
  display: flex;
}

div#allContent div#header div {
  flex: 1 auto;
}
<div id = "allContent">
            <div id = "header">
                <div id = "siteLogo">
                    <h1 id = "title">Site_Title</h1>
                </div>
                <div id = "shortMenu">
                    <a href = "#">Login</a>
                </div>
            </div>
        </div>

Answer №2

By modifying the selector below, I've added display: inline-block; to avoid any potential bugs in ie11. In order to keep this example concise, I didn't include the vendor-prefix styles.

div#contentWrapper div#topbar div {
  flex: 1;
  display: inline-block;
  display: flex;
  justify-content: center;
  align-items: center;
}

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

tips for creating dynamic visual effects using CSS on filtered images

img:hover{ -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); filter: grayscale(100%); } This CSS code snippet will convert the image from color to grayscale upon hovering. However, I am looking to achieve this effect with a smooth animation ...

Dynamically align text in the center of an image, vertically

I'm trying to create a hover effect where an image and text are displayed in the center of the image when someone hovers over it. Here is how the HTML looks: <article> <div class="entry-content"> <a href="http://www.linktopost.com"> ...

Select multiple options with personalized text using V-select

Can anyone help me with populating a v-select component from Vuetify 1.5 with checkboxes using multiple? The issue arises when I add <template slot='item' slot-scope='{ item }'></template>. It seems that the checkboxes do no ...

The camera continues to move smoothly after the static-body effect has been activated

I recently implemented A-Frame physics using this library, and encountered an issue where applying static-body to the a-mixin voxel in the Minecraft demo still allowed my character to move through the box. My camera is configured with universal-controls f ...

Generating a dataframe with cells that have a combination of regular and italicized text using the sjPlot package functions

I've been trying to set up a basic data table where the genus name in the "Coral_taxon" column is italicized, but the "spp." part following it remains lowercase. I thought of using the expression() function for each row in "Coral_taxon," but so far, I ...

Identifying the Operating System and Applying the Appropriate Stylesheet

I am trying to detect the Windows operating system and assign a specific stylesheet for Windows only. Below is the code snippet I have been using: $(function() { if (navigator.appVersion.indexOf("Win")!=-1) { $(document ...

JavaScript threw an error with message: 'Unexpected identifier' that was not caught

Upon launching Web Developer in Firefox: SyntaxError: missing } after property list note: { was opened at line 7, column 7 ...

Video player on website experiencing issues with playing VAST ads

Hey there! Check out this awesome site for Music Videos: (Music Videos(Player)) I've been testing different options, but if you have a better suggestion, please let me know. Any help would be really appreciated. If I can't figure it out on my o ...

Exploring the potential of utilizing the "wait" function in Selenium WebDriver for

I want to automate a test on my website using the Selenium WebDriver for JavaScript. How can I approach running tests here with content that may not be ready when the page loads, such as data coming from an external API? In my case, the content is loaded ...

Modifying Link Hover Colors with CSS and PHP

One of the challenges I am facing is managing a file that contains an array of navigation links. This setup allows me to easily add new links to the navigation menu without manually updating multiple files. However, I am struggling with assigning different ...

The issue with height percentages being ineffective in CSS

My goal is to utilize the height property with percentages, however it seems to be ineffective. I desire to use percentages so that the layout appears well in various resolutions. <div id="bloque_1" style="height: 80%;background: red"> </div> ...

Utilizing the power of JQuery's each() function alongside UI.sortable() for enhanced

Need help with updating data-priority attribute after sorting an unordered list using jQuery ui. Currently, the attribute is only set on load and not updated after each sort. I have created a fiddle to demonstrate the issue. It seems like a simple fix but ...

What is the best way to enclose a handlebars expression within a span element while also making the span clickable?

In the button, the datePick string is a computed property that can be clicked on to trigger an event. However, clicking outside the datePick value inside the button does not trigger the event. Even after wrapping it inside a span and setting width: 100%, ...

Encountered an issue during my initial AJAX call

Hello everyone, I am currently attempting to use AJAX to send a request with a button trigger and display the response HTML file in a span area. However, I am encountering some issues and need help troubleshooting. Here is my code snippet: //ajax.php < ...

Tips for aligning 2 divs in the same position?

I am working on a slider feature that includes both videos and pictures. I need to ensure that the videos are hidden when the device width exceeds 600px. Here is the HTML code snippet: <video class="player__video" width="506" height="506" muted preloa ...

Enhancing text visibility in dompdf by making it bold or increasing its size

Recently, I began using dompdf v0.6.0beta3 along with the dompdf Codeigniter helper in Codeigniter. To address the issue of missing fonts, I manually moved the Arial font family tff files from the Windows 7 font folder to the /lib/fonts directory within do ...

Discover how to access and manipulate JSON files in an Angular application using

Currently, I am diving into learning TypeScript with Angular and I'm interested in reading a JSON file. The structure of my JSON file is as follows: { "nb": "7", "extport": "1176",, "REQ_EMAIL": ...

Having trouble getting XSLT to work properly with the XML file

My XSL file doesn't appear to be functioning properly with my XML data. Despite the simplicity of my code, I am unable to identify the issue. Upon linking to the XSL spreadsheet and opening the document, my XML seems to transform into plain raw Data. ...

Adjust the top position of a div element at regular intervals

How can I make the top position of an absolutely positioned div with children change every x seconds using jQuery? I tried using setInterval but it only changes once. Here is my code: .com_prices { width: 100%; height: 100%; overflow: hidden; back ...

Styling a header using CSS

I successfully coded a header and navigation bar, but I am struggling with setting up elements like "Contact me" properly. My goal is to separate them, add icons, and align the text to the right. I am using Bootstrap 4, but nothing seems to be working, n ...