Achieving Dynamic Center Alignment for One or Two DIVs

My goal is to create a page layout with three DIVS arranged as Left|Center|Right, while still being able to display different combinations such as Center, Left|Center, or Center|Right in the center of a wrapper div.

I have been using jQuery toggle to switch between displaying either the Left, Right, or both DIVS off (display:none). However, I have encountered difficulties making this work smoothly. When I tried setting the DIVS as inline-block and used align-text: center, it resulted in unwanted formatting for the text inside the DIVS. On the other hand, floating all the DIVS left and adjusting the width of the wrapping div with margin: 0 auto; centered them only partially and added unnecessary margins. This caused one of the DIVS to go to the next line more quickly when resizing the browser compared to if the width was set to 100%.

For example, you can see my code in action at this JSFIDDLE.

HTML

<div id="wrapper">
    <div id="left">ONE</div>
    <div id="center">TWO</div>
    <div id="right">THREE</div>
</div>

CSS

#wrapper{
    width: 80%;
    margin: 0 auto;
    overflow: auto;
}
#left{
    display: none;
    width: 100px;
    height: 100px;
    background-color: #880000;
    float: left;
}
#center{
    width: 100px;
    height: 100px;
    background-color: #000088;
    float: left;
}
#right{
    width: 100px;
    height: 100px;
    background-color: #008800;
    float: left;
}

Answer №1

Does this look right to you?

Example

<div id="container">
    <div class="center-content">
        <div id="left-pane">ONE</div>
        <div id="center-pane">TWO</div>
        <div id="right-pane">THREE</div>
    </div>
</div>

CSS Style

#container{
    width: 100%;
    min-width: 320px;
}
.center-content{
    margin: 0 auto;
    width: 300px;
}
#left-pane{
    width: 100px;
    height: 100px;
    background-color: #880000;
    float: left;
}
#center-pane{
    width: 100px;
    height: 100px;
    background-color: #000088;
    float: left;
}
#right-pane{
    width: 100px;
    height: 100px;
    background-color: #008800;
    float: left;
}

You might want to incorporate a media query to address how the elements behave when #container becomes too small for its inner divs. Alternatively, setting a min-width like demonstrated above could also work.

Answer №2

After experimenting with different methods, I discovered a solution that worked well for me. By creating a wrapper div with margin-left: auto; and margin-right: auto;, and making all inner divs float left, I was able to achieve the desired layout. Additionally, I used JavaScript to dynamically adjust the width of the wrapper div based on the content inside it whenever a toggle event took place.

Answer №3

To achieve the desired layout, ensure that you correctly set the display: table/-cell values and remove the width property from #wrapper.

<div id="wrapper">
    <div id="left">ONE</div>
    <div id="center">TWO</div>
    <div id="right">THREE</div>
</div>

<style>
    #wrapper {display: table; margin: auto; width: auto;}
    #wrapper div {display: table-cell;}
    /* #wrapper #left {display: none} */ /* uncomment to hide just two cells */ 
</style>

http://jsfiddle.net/cjf4Q/35/ // 3 cells
http://jsfiddle.net/cjf4Q/34/ // 2 cells

This approach allows for flexible resizing without the need for a fixed width on #wrapper, ensuring that the layout adjusts accordingly.

Answer №4

It appears that this code snippet may be what you need:

#container{
    width: 80%;
    margin: 0 auto;
    position:relative;
    height:100px;
    min-width:300px;
}

#container section{
    top:0;
}

#left-pane{
    display: block;
    width: 100px;
    height: 100px;
    background-color: #880000;
    left: 0px;
   position:absolute;
}
#center-pane{
    width: 100px;
    height: 100px;
    background-color: #000088;
    margin:auto;
    position:relative;
}
#right-pane{
    width: 100px;
    height: 100px;
    background-color: #008800;
    right: 0px;
    position:absolute;
}

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

Excessive content within the div element causing overflow

In my Laravel project, I am facing an issue with the patient history form. The content overflows under a div as shown in the image below. View first image To address this problem, I tried using overflow:hidden in the CSS section. However, this caused my ...

Use the no-repeat feature to set the background image in an Asp:Chart

I have been working with the asp:Chart control and have found that its BackImage property functions well. However, I have encountered two issues while trying to set the background image. I am unable to successfully set the no-repeat property of the Bac ...

Utilizing flexbox, absolute positioning, and 100% height in web design

While experimenting with a flexbox inside an absolute box within a div of defined height, I encountered a problem. Let me explain the issue step by step. Here is a link to a fiddle demonstrating the problem: https://jsfiddle.net/8ub9tyub/ When hovering o ...

Guide on setting the BackColor of a table cell based on a database value dynamically

In the project I am currently working on, there is a request to change the background color of specific table cells based on their values. In order to achieve this, I am handling the RadGrid_ItemDataBound event and attempting to set the BackColor property ...

The ng-include directive is having trouble finding the partial HTML files

I've been working on setting up a basic tabset with each tab pulling content from a separate partial view with its own controller. To test everything out, I created three dummy HTML files with simple text only. However, I'm having trouble getting ...

Change the <base> element programmatically during server-side rendering

Searching for a way to obtain the base URL for an HTML page, so that relative URL requests from the browser utilize that base. If you are looking for answers, check out this link: Defining root of HTML in a folder within the site root folder When serving ...

What is the best way to ensure my jQuery slides are responsive?

I have been developing a personal practice website and have successfully integrated a jQuery slide show for showcasing photographs. However, I am facing a challenge in making these slides responsive when the screen size changes. I have gone through numerou ...

I'm in need of assistance with Laravel 6 to implement a search functionality that can search for users by name, specialty, and country

Whenever I click the Search button, an error (Undefined nom) pops up. I am looking for someone who can help me troubleshoot and fix my code. Here is the code that needs correction: Controller: doctors.php <?php namespace App\Http\Cont ...

Can Selenium be used to retrieve a list of pinned IDs from a website?

I am currently developing a web scraper that is required to open multiple tabs of items with filled icons. Specifically, each page that needs to be opened contains the div class="course-selector-item-pinned" in its source code. <dropdown-conte ...

The jQuery navbar's active class fails to function properly when an href is added

I'm facing a puzzling situation. I created a navbar using jQuery Mobile following the instructions on their official website: jQuery Mobile Navbar Demos Everything functions smoothly until I introduce href attributes in the list elements within the u ...

Crafting web design with media queries to ensure responsiveness

I have been working on creating a responsive webpage. While the header and footer are resizing properly when the browser is reduced in size, the navigation section is not behaving the same way. Currently, shrinking the page is causing the navigation block ...

What is the best way to combine table cells in HTML to create a "T" shape?

Is there a way to merge the two blank cells (one above 'Be' and one above 'B') with a large blank space in the middle? I attempted using colspan and rowspan in various ways without success. https://i.stack.imgur.com/tw5SE.png This is m ...

Font reference in IE and Chrome causing HTTPS to fail

Take a look at in both Internet Explorer and Chrome. The header tags (h1 through h6) all have font specifications that rely on a javascript fonts.com specification. However, there seems to be an issue with rendering properly. Interestingly, the fonts ar ...

Issues with the width of Table TD cells not being responsive

I'm having trouble adjusting the width of my dropdown menu columns. I've tried various methods, but can't seem to get each column to be 200px wide as desired. .dropbtn { background-image: url("../sliki/meni.png"); width: 220px; heig ...

margin around the masterpage

Currently, I am utilizing a master page in ASP.NET (using Visual Studio Express 2015) and overall everything is functioning properly. However, there seems to be an unsightly space around the edges when viewing it in a browser. While not a critical issue, ...

What is the best way to create non-standard text wrapping in HTML and CSS that is both semantic and sleek, avoiding square or circular shapes?

Is there a way to achieve text wrapping like this using semantic and clean HTML and CSS that is compatible with all browsers? The only solution I can think of is adding different classes to the <p> element. However, the drawback of this approach is ...

Compressed search bar when in mobile mode

My search bar is getting squashed on mobile devices. Any suggestions for fixing this issue? <div id="menu" class="py-4 d-flex justify-content-center"> <div class="row "> <div class="col-sm-auto "> ...

CSS techniques for aligning content

I am currently working on designing a website template, and I have encountered an issue with positioning three individual columns that contain blocks of content. My objective is to have the first block of content in each column start at the top of their re ...

Embellishing Bootstrap with a splash of color class

After exploring Bootstrap, I've noticed the limited number of "color classes" available (primary, success, danger etc.). Is there a simple way to add more classes similar to those? For instance, the "primary" class can be used across various elements ...

Issue with conditional comment in IE 8 not functioning as expected

Struggling with changing the SRC attribute of my iFrame specifically for users on IE 8 or older. This is the code I have written: <script> var i_am_old_ie = false; <!--[if lte IE 8]> i_am_old_ie = true; <![endif]--> </script> ...