Instructions on dynamically positioning a collection of div elements at the center of a webpage container

I am looking to create a set of divs that are centered on the page and adjust in size according to the length of the username.

.Container1
{
  display: table;
  width: 100%;
  padding:0;
  margin:0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.Container2{
   position: absolute;
   left:0;
   right:0;
   margin-right:auto;
   margin-left:auto;
   width:100%;
   max-height:30px;
   padding:0; 
   overflow:hidden;
}

<div id="maincontainer" runat="server" class="Container1">
            <div class="Container2" runat="server">
                <div class="leftbar"><img src="Some image" runat="server" /></div>
                <div id="child1container" runat="server">
                    <span class="textclass" runat="server">UserName</span>
                    <span class="textclass" id="BankName" runat="server">BankName</span>
                    <span class = "Col"> ---Some Content --- </span>
                    <span class="barBtn"> ----Logout Button----
                    </span>
                </div>
                <div class="rightbar"><img src="Some image" runat="server"/></div>
            </div>

    </div>

The sections inside Container2 should be centrally positioned at the top of the webpage, with their container width adjusting based on the length of the username. For longer names, the entire container should still be centered.

I want this container to behave like the one shown in this JSFiddle example: https://jsfiddle.net/c2t017sx/

When the username length changes, I expect the container to expand from the center while maintaining its central position all the time. However, implementing similar changes in my application does not work as expected. I need guidance on which properties to set for container2 and child1container to achieve this behavior. Can it be done using CSS alone or do we need to use JavaScript to adjust the container size (CSS style) based on input values?

Answer №1

make sure to include text-align: center; in the styles for .Container2, which serves as the main container:

.Container2 {
  border: 1px solid black; // this line is used to test the container border
  display: table;
    text-align: center;
}

https://jsfiddle.net/dalinhuang/tc7kkzyz/2/

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

Adding dynamic content to CSS in Next.JS can be achieved by utilizing CSS-in-JS

Currently diving into the world of Next.JS, I've managed to grasp the concept of using getStaticProps to retrieve dynamic data from a headless CMS and integrate it into my JSX templates. However, I'm unsure about how to utilize this dynamic conte ...

Using the last child as a parent element in Selenium with JavaScript

I am trying to access the input element of the last child in this code snippet. Specifically, I want to retrieve the text Text Reply - Delete. <div class="priority-intent-div"> <div class="row add-priority-intent-div"> ...

Customize and Enhance Code for Website Integration

My code is fetching a script from an external website. var url = "//example.com/script-url.js"; $.ajax({ url: url, dataType: 'jsonp' }); Although it functions properly, the script retrieved is created by a different website. I need to make ...

Ways to eliminate white space in bootstrap CSS menu bar

My goal is to create a responsive navigation bar using Bootstrap and customize it to fit my design needs. Although I was able to implement the dropdown on mobile-sized windows, I encountered a margin issue that I couldn't remove on the right side. I ...

Animations of bezier curves created with Three.js

UPDATED: SOLUTION FOUND I am in need of guidance on how to create animation for the movement of points along a curve to simulate string motion in 3D while keeping performance in mind. Imagine multiple strings between two points, for example. Check out t ...

Exploring VueJS Router History Mode with NGinx web server

After conducting research, I discovered several issues similar to the one I am facing. Currently, I have a docker-compose setup on Digital Ocean with NGinx, VueJS, and a Static Landing Page. Everything was working fine until I added a new route with a fo ...

Achieving unique proportions with HTML5 video resizing

On my page, I plan to showcase a video specifically for desktop browsers. I have devised some code to determine whether the video is in landscape or portrait orientation. The challenge lies in deciding what to do based on this orientation: If the video is ...

Using static assets within VueJS Components

I am currently working on a VueJS project that was customized from a base "webpack-simple" template provided by vue-cli, and it follows the folder structure below: https://i.sstatic.net/C3vxY.png My main focus right now is developing the "clickableimage. ...

jQuery mobile collapsed list view is not functioning correctly when used with search feature

I have successfully implemented a listview in jquery with a listdivider that includes a filter function. The search feature works perfectly, however, when collapsing either of the list dividers, the search functionality stops working altogether. <!DOCT ...

Step-by-step guide: Uploading files with Ajax in Codeigniter

I am facing an issue with updating data and uploading an image when editing a row in my grid. Although the data is successfully updated, I am encountering difficulties in saving the image file to a folder. Here is what I have tried: While using AJAX, I ...

An issue with ASP.Net 2.0: Invalid postback or callback argument when using the browser back button

My webpage has a simple initialization process where it binds data to a GridView. This GridView has sorting and paging functionalities enabled, and is contained within an UpdatePanel. However, I am encountering an error when the user performs the followin ...

Initiate a timer with intervals of 3 seconds upon reaching a designated section in a React application

useEffect(() => { console.log(window.scrollTo) console.log(textInput.current.offsetTop); }, [textInput,]) click here for more information check out the bottom of this page for a similar countdown feature - any ideas on how to implement it? ...

Can you modify a specific column in a table using mat-table in Angular material?

For my project, I am utilizing Angular Material's table to present data in a tabular format. However, due to a new requirement, I now need to enable in-line editing for the last 2 columns alongside highlighting another column when the user clicks on t ...

Tips for correctly displaying diacritics with Webpack and Typescript

While working on my project, I encountered an issue with diacritics marks (such as German or Polish characters) when using Webpack with Typescript. Unfortunately, the problem arises when trying to display these marks in the console or on a webpage. It seem ...

Showing error messages on HTML forms using PHP

Is there a way to show error messages in an HTML form using PHP variables that are populated with values from 'register-form_v2.php'? I am facing an issue where the variables I want to echo out in the form do not contain any values. Upon submitt ...

Delay in changing the z-index of FloatingActionButton on hover

In my current issue, I am facing a problem where a div that is meant to always stay below a FloatingActionButton (FAB) ends up temporarily appearing above it when z-index values are changed. The scenario is such that upon clicking the FAB, an invisible ove ...

Save the value of a webpage element into a variable and utilize it across multiple JavaScript files in TestCafe

We are working in the insurance domain and have a specific scenario that we want to achieve using TestCafe: 1st step: Login into the application 2nd step: Create a claim and store the claim number in a global variable 3rd step: Use the globally declared c ...

Customizing the appearance of ASP.NET file uploader for various sizes

Has anyone successfully changed the width of the ASP.NET file uploader? I attempted by adding the CssClass, but it did not work. Is there an alternative method to adjust the width? Thanks in advance. ...

Having trouble with RethinkDB filter not returning any matches?

Encountering an obstacle with RethinkDB while using NodeJS. Attempting to utilize a basic .filter() function, but for some mysterious reason, it fails to retrieve a result. The current code snippet in question looks like this: const someID = 1234; ...

Break down the text of a paragraph into separate words, placing each word within its own span element, and then add animations

I am facing an issue with my paragraph element that has the display property set to hidden. I have split each word of the paragraph and placed them inside individual span elements. My goal is to create an effect where each word comes from different locatio ...