Place an element in the center without specifying its height, while making sure it does not affect

Encountering a challenging issue where alert messages need to be displayed on the page just below the navigation and above the content. The problem is demonstrated in this fiddle http://jsfiddle.net/53b6g/1/. It's tricky because the style used can't account for varying numbers of alerts, making it difficult to determine the height of the wrapper element.

HTML:

<div class="nav">
    <ul>
        <li>first</li>
        <li>second</li>
        <li>last</li>
    </ul>
</div>

<div class="wrapper">
    <div class="table">
        <div class="tr">
            <div class="td">
                Let us center this and have the same width as below
            </div>
        </div>
        <div class="tr">
            <div class="td">
                shorter but same width 
            </div>
        </div>
        <div class="tr">
            <div class="td">
                however, there could be 3, 2, or 1 alert so height calculations are unreliable
            </div>
        </div>
    </div>
</div>

<div class="content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in dui rhoncus, porttitor nibh ornare, tristique est. Nullam condimentum quam ac metus varius, at fermentum nibh congue. Fusce vel posuere justo, vel vulputate enim. Cras varius libero et felis venenatis ultricies. Vivamus ante est, tempus et aliquet eget, varius id nisi. In a consectetur lacus, vitae lacinia mauris. Nulla pulvinar tellus id laoreet tempus. Integer feugiat quis odio eget ullamcorper.</p>
</div>

CSS:

.table{
    display: table;
    margin: 0 auto;
}

.tr{
    display: table-row
}

.td{
    display: table-cell;
    background: red;
}

.nav{
    background: green;
}

.nav ul {
    margin: 0;
}

.nav li{
    display: inline-block;
}

.content{
    background: blue;
}

A separate fiddle demonstrates the issue without using the commented code here http://jsfiddle.net/8Awze/. The challenge remains whether this can be resolved solely with CSS.

HTML:

<div class="nav">
    <ul>
        <li>first</li>
        <li>second</li>
        <li>last</li>
    </ul>
</div>

<div class="wrapper">
    <div class="table">
        <div class="tr">
            <div class="td">
                Let us center this and have same width as below
            </div>
        </div>
        <div class="tr">
            <div class="td">
                shorter but same width 
            </div>
        </div>
    </div>
</div>

<div class="content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in dui rhoncus, porttitor nibh ornare, tristique est. Nullam condimentum quam ac metus varius, at fermentum nibh congue. Fusce vel posuere justo, vel vulputate enim. Cras varius libero et felis venenatis ultricies. Vivamus ante est, tempus et aliquet eget, varius id nisi. In a consectetur lacus, vitae lacinia mauris. Nulla pulvinar tellus id laoreet tempus. Integer feugiat quis odio eget ullamcorper.</p>
</div>

CSS:

.table{
    display: table;
    margin: 0 auto;
}

.tr{
    display: table-row
}

.td{
    display: table-cell;
    background: red;
}

.nav{
    background: green;
}

.nav ul {
    margin: 0;
}

.nav li{
    display: inline-block;
}

.content{
    background: blue;
}

Answer №1

It appears that you should be able to utilize the position: absolute property in the following manner:

.container{
    position: absolute;
    width: 100%;
    top: 50px;
}

http://jsfiddle.net/98c2h/7/

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

The Jquery AJAX request does not seem to be functioning properly as it consistently triggers the error function

Currently, I am in the process of developing a reset password page. However, I am encountering an issue when attempting to submit the data to the backend via an AJAX call, as it consistently redirects to the error function. Despite my attempts to troublesh ...

Steps for accessing a particular tab on a separate webpage

I am working on a home page that includes an IFrame displaying a separate HTML page. Within the embedded page, there is a link that, when clicked, should open another page in the same IFrame with a specified tab displayed. This new page contains 3 tabs, ...

Having issues with a PHP script that is supposed to generate a webpage

I have been studying web development through the "PHP and MongoDB Web Development" book, where they provide a code snippet for creating a blog post creator. <?php $action = (!empty($_POST['btn_submit']) && ($_POST['btn_submit&apo ...

What is the best way to make just the background image transparent using HTML and CSS?

Hey there! I'm trying to make just the background image transparent, while leaving everything else non-transparent. Is this possible with Bootstrap? Here's a snippet of my HTML: HTML Section: <!-- Homepage Section --> <section id= ...

The enduring lifespan of an Android web page session

I am looking to display telemetry data from a web server on an HTML page using AJAX, without the need to create a native Android application. My goal is for the Android browser to continuously show the HTML page without going into sleep mode. Are there a ...

Attempting to establish a login system using MongoDB

I am currently working on a function to verify user login details entered in a form and compare them with data stored in MongoDB, such as email and password. However, the function seems to be malfunctioning. The expected behavior is that when a user ente ...

Error in Angular 8: Property 'name' is undefined and cannot be read

Delving into the world of Angular, I encountered an interesting error: ERROR TypeError: Cannot read property 'name' of undefined. Here's the snippet of code: recipe-list.component.ts import { Component, OnInit } from '@angular/core&ap ...

Unable to display background image on Webpage due to blank page issue while uploading it with HTML and CSS

I've been attempting to build a webpage with an image as the background, but I seem to be facing some issues. Instead of seeing the desired image, all I get is a blank white page. The folder named "images" is located in the same directory as my HTML a ...

An online tool for converting USD to EUR or any desired currency using HTML and JavaScript

Having trouble converting currencies with this tool Convert US Dollars to Euros 2 Amount: From USD EUR To ...

Mysterious attributes of angular 6's <table mat-table> tag

This particular question regarding the angular material table has not been duplicated in any other discussions. Other similar questions pertain to angular versions 2-5, not version 6 The issue I am encountering is as follows: Can't bind to 'dat ...

Tips for altering the placeholder color in Angular Material?

Currently, I am working with Angular 9 and Angular Material version 10.2.0. This is the code that I have: <mat-form-field> <input matInput type="text" name="" placeholder="title" id="title" [(ngModel)]=&q ...

The text from the textarea in the form is not directly saved in mongoose, but it can be accessed and logged in the server-side code as req

<form> <input1> ... ... <textarea name="yourbio" rows="3" value="<%= user.bio %>"></textarea> <submit....> <form> Utilizing $set: {req.body} to make updates to the user data. All fields can be successfully updated ...

Tips for eliminating checkboxes from a form

function addCheckbox(){ var labels = document.form1.getElementsByTagName('label'); var lastLabel = labels[labels.length-1]; var newLabel = document.createElement('label'); newLabel.appendChild(Checkbox(labels.length)); ...

When a JSON stringified string contains a space-separated value, it can cause the data attribute to break

let dataObject = { "Cast_Code": "NA", "Mat_Code": "xxxx", "Pin_Num": "xxxx", "MatDetail": [ { "Batch_Number": "Patch PA", "Batch_Expiry": "No Expiry", "Batch_Quantity": "xxx", "Return_ ...

Adjust the size of every card in a row when one card is resized

At the top of the page, I have four cards that are visible. Each card's height adjusts based on its content and will resize when the window size is changed. My goal is to ensure that all cards in the same row have equal heights. To see a demo, visit: ...

The contact form fails to transmit preset values

I am currently working on a form that is functional, but it does not send predefined values. Some fields have predefined values such as the user and total amount. While it does send the values entered by the user, it doesn't include the predefined one ...

Create a custom email login feature for accessing a website securely, without the need for an API

Short version: Can an email be sent with a login button that automatically logs in a user on a web page? Is it possible to embed this in an HTML button or use SQL injection for this purpose? If so, where should I begin my research? My current task involve ...

What is causing the CSS transition to fail in the updated DOM?

When attempting to apply a CSS transition as shown in the following code snippet: https://jsfiddle.net/sunyuu/e58sfeva/17/ var Main = { data () { return { isActive: false, childStyle: {} }; }, methods: { sho ...

The Bootstrap theme showcases a narrower layout than its parent container

I'm struggling with managing the width of the bootstrap container and row. Inside the row, I've placed all the content, but for some reason, the sizes are showing differently on jsfiddle and the image preview. <!DOCTYPE html> <html lang ...

The Bootstrap navigation bar fails to expand

After extensive searching, I still can't figure out why my bootstrap menu isn't opening as expected. I made sure to link JQuery first to try to resolve the issue, but it doesn't seem to have made any difference. I'm starting to wonder ...