Creating a 3-column div with varying sizes of text in another div at the center position

I am attempting to create a 3-column layout using only DIVs, but I am facing some challenges.

If I were to use tables in the traditional HTML 4 way, I could achieve this:

<div style="width:100%">
    <table width="50%" align="center">
        <tr>
            <td align="left">
                <img src="whatever.jpg">
            </td>
            <td align="center">
               1 2 3
            </td>
            <td align="right">
                <img src="whateverright.jpg">
            </td>
         </tr>
     </table>
</div>

The table spans 50% and is centered. Here is my attempt with divs:

<div style="width:100%;overflow:hidden;">
    <div>
        <div style="float:left;">
            <img src="whatever.jpg">
        </div>
        <div>1 2 3</div>
        <div style="float:right;">
            <img src="whateverright.jpg">
        </div>
    </div>
</div>

I have only been able to center it by knowing the total size of all elements in pixels or em units. However, this causes problems for text sizes based on screen resolution when creating complex pagination systems.

Is there a way to center a 3-column div inside another div without needing the sum of inner div widths?

I attempted to add margin:auto to the main div within the outer div, but was not successful.

It is essential that the solution works well with IE7. Any suggestions would be appreciated as the internal columns display correctly, but centering the entire structure remains a challenge.

Answer №1

It appears that this solution may resolve your issue.

HTML

<div style="width:100%;overflow:hidden;">
<div>
    <div class="box" style="">
        <img src="whatever.jpg">
    </div>
    <div class="box2">1 2 3</div>
    <div class="box3">
        <img src="whateverright.jpg">
    </div>
</div>

CSS

  div .box,.box2,.box3{
        width: calc(100% - 66.666666%);
        /* Firefox */
        width: -moz-calc(100% - 66.666666%);
        /* WebKit */
        width: -webkit-calc(100% - 66.666666%);
        /* Opera */
        width: -o-calc(100% - 66.666666%);
        width: expression(100% - 66.666666%);
        display: inline-block;
    }
    .box{
        float:left;
        background:purple;

    }
    .box2{
        float:right;
        background:red;

    }
    .box3{
        background:blue;
    }

Answer №2

Make sure to utilize the display properties correctly.

.table{
    width: 500px;
}
.row{
    width: inherit;
    display: block;
}
.cell{
    width: 33.3%;
    height: 50px;
    
    display: inline-block;
    vertical-align: top;
    
    text-align: center;
    margin: 0px -2.5px;
    border: 1px solid #C0C0C0;
}
<div class='table'>
    <div class='row'>
        <div class='cell'>
            <img src="whatever.jpg">
        </div>
        <div class='cell'>1 2 3</div>
        <div class='cell'>
            <img src="whateverright.jpg">
        </div>
    </div>
            
            
    <div class='row'>
        <div class='cell'>
            <img src="whatever.jpg">
        </div>
        <div class='cell'>1 2 3</div>
        <div class='cell'>
            <img src="whateverright.jpg">
        </div>
    </div>
</div>

Answer №3

After some trial and error, I discovered that the most effective solution for me was to float each inner container and assign a percentage of width to each inner container so that their widths would add up to match the width of the outer container. Additionally, each inner container needed to contain content. Here's an example:

<div style="width:100%;overflow:hidden">
    <div style="float:left;width:20%">
     some text at left
    </div>
    <div style="float:left;width:60%">
     some text in middle
    </div>
    <div style="float:left;width:20%">
     some text at right
    </div>
</div>

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 automatically adjusting a vertical side bar to fit between the browser's header and bottom

I'm having trouble with aligning my sidebar vertically between the header and the bottom of the browser window. Currently, the bottom items are extending beyond the browser's bottom edge. How can I adjust this to ensure proper alignment? Here is ...

What is the best method for adding anchors or appending items in aspx pages?

I have created code for an image gallery on my webpage, "gallery.aspx". The situation is that I have an external aspx page with various links. When a user clicks on one of these links, I want them to be redirected to the image gallery but to a specific ima ...

Choosing Elements from Twin Dropdown Lists in PHP

I am currently working on building a dynamic drop-down menu where the options in the second dropdown depend on the selection made in the first one. The initial dropdown consists of a list of tables, and based on which table is chosen, the columns of that s ...

PHP-generated AngularJs Select Element

I'm facing an issue with my PHP function in my AngularJS application. I have created a function to select a default option, but it's not displaying the desired value. Here is the function code: function qtyList($selectName, $selectQty){ $st ...

The error message received states: "materialize-css Uncaught TypeError: Vel is not defined as

My current setup involves webpack as the bundler/loader, and I've successfully loaded materialize css (js/css). However, when attempting to use the toast feature, an error is thrown: Uncaught TypeError: Vel is not a function The library is being inc ...

Troubleshoot: Problem with iPhone Orientation on Bootstrap Framework

While working on a responsive website design, I encountered an issue that needs addressing. Initially, switching from portrait mode to landscape didn't trigger the expected responsive behavior; instead, it simply zoomed into the site. To prevent this ...

One XMLHTTPRequest with multiple replies

My requirement is to have a consolidated XMLHttpRequest call that returns three unique items. These items consist of an HTML formatted table and two separate sets of JSON data for Google Charts. The reason behind this design is that the chart data relies o ...

What is the method for creating a transparent background for Material UI v1.0 Dialogs?

I'm attempting to place a CircularProgress component inside a dialog box. However, I'm facing an issue where the background of the dialog box is white and cannot be set to transparent like in previous versions of Material UI (v0.2). style={{ ...

Tips for preventing the sidebar from extending beyond the footer

I am facing an issue with my sidebar that follows as I scroll. How can I make it stop following when it reaches the footer? Here's what I have attempted so far: $(function() { var floatPosition = parseInt($("#left_menu").css('top')); ...

Scrollbar becomes inactive following the loading of AJAX content

Having an issue with loading a div using Ajax. The div loads, however the scrollbar within it stops working afterwards. In Main.html, I load content from other HTML files like so: <div id="content1" > </div> The content is loaded as follows: ...

When the CSS is rendered, it automatically appends a forward slash

I have successfully implemented CSS on the page. @font-face { font-family: "feather"; src:url("fonts/feather-webfont.eot"); src:url("fonts/feather-webfont.eot?#iefix") format("embedded-opentype"), url(&qu ...

Creating a contact form in WordPress that triggers a separate PHP file

I've been working on integrating a small contact form into the sidebar of my WordPress site. After moving the code from its original HTML file to the appropriate WordPress PHP files, everything seems to be functioning correctly except for the contact ...

What methods can I implement to prevent my boxes from becoming stretched out?

How can I prevent this box from stretching too much? Here is the code snippet along with the output: CSS: .social { padding-left: 1000px; border: 5px inset black; margin: 4px; width: 100px; } HTML: <div class="social"> <p& ...

Make sure to concentrate on the input field when the DIV element is clicked

In my React project, I am working on focusing on an input element when specific buttons or elements are clicked. It is important for me to be able to switch focus multiple times after rendering. For instance, if a name button is clicked, the input box for ...

Enhance Your jQuery Skills: Dynamically Apply Classes Based on URL Like a Pro!

Here is an example of HTML code for a progress meter: <div class="col-md-3" style="margin-left: -20px;"> <div class="progress-pos active" id="progess-1"> <div class="progress-pos-inner"> Login </div> </di ...

What is the best way to collapse the entire navigation menu after clicking a link (nav-link) within it?

I created a navigation menu using HTML and SCSS, but I'm facing an issue where the menu does not close when clicking on links inside it. The menu continues to overlay the content on the page instead of closing. Essentially, I want the navigation menu ...

Avoiding the application of a border-right to the final child of a div in CSS

Check out this custom HTML code: <div class="main"> <div class="parent"> <div class="child"> <span></span> <label></label> </div> <div class="child2"> // some html content </div> ...

What is the best way to contain the CSS for dynamically generated HTML within a div element without impacting other elements on the page?

I am currently facing a challenge where users can input any HTML into a text box, and I need to manipulate that HTML by identifying elements such as anchor tags or divs. To achieve this, I have created a hidden div and copied the pasted HTML into it using ...

Having trouble verifying the selection option in Angular 6

I've been trying to implement Select option validation in Angular 6, but neither Aria-required nor required seem to be effective. The requirement is for it to display a message or show a RED border similar to HTML forms. Here's the HTML snippet ...

"Interactive feature allowing users to edit and view the most recently inserted HTML

My approach involves using a contenteditable div as an input field for entering text and inserting icons through a button that adds small HTML pictures within the text. Everything works fine when the text is narrower than the contenteditable field. Howev ...