Leveraging CSS display:inline-block or float:left for arranging elements containing different lengths of text

Seeking expert advice on the best way to style this HTML structure:

<body>
-Some content-
<div class="parent" style="height:50%;">
<div class="child" style="background-color:#FF9999;">An image</div>
<div class="child" style="background-color:#9999FF;">Some text</div>
</div>
</body>

My goal is to achieve the following behavior:

Here are the criteria I need to meet:

  • The container div, .parent, should be a block element that spans the entire width of the browser window.
  • I have the width of the first/left inner div in pixels, but not as a percentage.
  • The width of the second/right inner div is unknown and should automatically fill the remaining space.
  • If the first/left div is shorter than the second/right div, it should stretch to match the height.
  • If the first/left div is not present, the border between it and the second/right div should not appear.
  • I cannot use JavaScript tricks.

Here are the solutions I've attempted based on my experience and research:

  1. Float: Using float:left does not allow me to stretch the first/left div to match the height of its sibling or the .parent div.

  2. Inline-block: .parent {background-color:#999999;} .parent > .child {display:inline-block;vertical-align:top;height:100%;}

Using display:inline-block works well until the text in the second/right div wraps to the next line, causing it to grow wider and wrap under the first/right div.

Any insights or suggestions would be greatly appreciated!

Answer №1

It's best to avoid using table based markup. However, if you're not concerned about supporting older versions of IE like IE7, you can utilize display:table to solve your layout issues. Take a look at this quick demo I put together. Experiment by adjusting the content in the second child div to see how it affects the layout.

Check out this demonstration of display:table on jsfiddle

.parent {
  display:table;
}

.child {
  display:table-cell;
}

In essence, by setting the parent element to behave like a table and the child elements to act like table cells, you can achieve a table-like layout without the accessibility issues and extra clutter of traditional HTML tables. Just remember, this technique does not have full support in IE7. If you need to cater to older versions of IE, you might have to resort to less elegant solutions.

Answer №2

Tables may have received a negative reputation, but in this scenario, they serve as a suitable solution.

<body>
    -Additional text-
    <table class="parent" style="height:50%;">
        <tr>
            <td class="child" style="background-color:#FF9999;" width="10">An illustration</td>
            <td class="child" style="background-color:#9999FF;">Some information</td>
        </tr>
    </table>
</body>

By specifying a minimal width, the content will be aligned properly regardless of size, and the table will adjust to accommodate the taller column, ensuring a smooth layout.

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

When I hover over my images, they begin to flash before my eyes

My layout is set up so that when I hover over the printer image, it should switch to another image. However, instead of smoothly transitioning, the image starts to flash. This issue occurs in all browsers, indicating that I have made a mistake somewhere. ...

Achieving space between elements using Flexbox with a line separating each row

I have already found a solution to this question, but I am interested in exploring better alternatives. My goal is to utilize flexbox to create rows of items with equal spacing and dividing lines between them. The examples provided in the code snippet achi ...

Turn off the custom CSS section in the WordPress Customizer for WordPress themes

Looking for assistance in locking or disabling the Appearance -> Customizer -> Additional CSS area on Wp-admin. https://i.sstatic.net/FXXSE.png I have referred to this codex: https://codex.wordpress.org/Theme_Customization_API. However, I couldn&ap ...

A guide on wrapping text within a Material-UI MenuItem

I am struggling to display text in a popover in multiple lines for UI reasons. I have tried updating the code but so far, I have not been successful. Any suggestions? <Popover anchorEl={target} open={open} anchorOrigin={{ horizontal: 'middle& ...

CSS for a navigation menu with underlined links

I am attempting to create an underline effect when hovering over a link. However, I am facing an issue where the underline does not align perfectly with the line below it. Can someone please guide me on how to modify this CSS so that when the link is hov ...

CSS - Issue with dropdown sub-menu alignment despite using absolute positioning in relation to parent button

Title fairly explains it all. I'm having trouble getting my dropdown submenus to align perfectly with the bottom of the parent list item element. The list item has a relative position, while the submenu has an absolute position. I've attempted ...

How about designing a button with a dual-layered border for a unique touch

Looking for a specific button: My attempted CSS code that's not working: button { font-family: 'Ubuntu', sans-serif; font-size: 1em; font-weight: bold; color: white; border: 3px double #f26700; background: #f26700; ...

Is it possible to move my Home button to the upper left corner of the header using CSS?

`<header> <img src="images/chefs-hat.png"> <p>Popular Recipes</p> <button class="home"> <a href="index.html">Home</a> </button> </header> .home { width: 100px; text-decorati ...

Styling Challenges with CSS/AngularJS Accordion in Footer

I want to create a page layout as shown below: +---------------------------+ | Auto-fill / v scrollable ^| | || | || | v| +---------------------------+ | Fixed [][][] ...

Placing two parent flex containers on top of each other

I'm in a situation where I have two .container elements, both set at a height of 50%. Within these containers are the child divs .element, which belong to their respective parent divs .container. The problem arises when applying media queries with f ...

Avoiding triggering the parent event from the child in React

I'm facing a situation where clicking on a parent element results in it flipping to display a child element with different colors. However, the issue arises when the user tries to click on one of the colors in the child element, as it also triggers th ...

What is the proper HTML tag and syntax used for adding a text box within the content of a webpage?

Question about HTML: How can I add a text box or block to an HTML page that is filled with plain text? I want the text box to align to the right and be surrounded by the plain text, essentially wrapping around it. I also need to specify the dimensions of ...

Using CSS to apply hidden box shadow to nth child element

I am encountering a challenge with using the CSS selector :nth-child(...) in combination with the box-shadow effect. The intended outcome is as follows: The even-numbered div elements within a specific container should have alternating background colors. ...

What causes a webpage to overflow when using the position:absolute property?

Despite my understanding that an element positioned absolutely should be removed from the normal content flow, why does it still cause the page to overflow? Running the code snippet below reveals a horizontal scrollbar, which I didn't expect. .rela ...

Organizing an angular expansion panel

I am currently dealing with an expansion panel that has a lot of content. The layout is quite messy and chaotic, as seen here: https://ibb.co/Y3z9gdf It doesn't look very appealing, so I'm wondering if there is a way to organize it better or ma ...

Header formatting issue when attempting to implement tablesorter plugin

I implemented the widget-scroller and widget column Selector in my table using the table sorter plugin in jQuery. Has anyone encountered a problem like the one shown in this picture? It seems that my table headers do not align with the columns properly an ...

Navigate the child div while scrolling within the parent div

Is there a way to make the child div scroll until the end before allowing the page to continue scrolling when the user scrolls on the parent div? I attempted to use reverse logic Scrolling in child div ( fixed ) should scroll parent div Unfortunately, it ...

Sophisticated filter - Conceal Ancestry

Check out this snippet of my HTML: <td> <a class="button" href="#"> <input id="download">...</input> </a> <a class="button" href="#"> <input id="downloadcsv">...</input> </a> </td> I am ...

The CSS file is not appearing, causing the styling not to be applied

I've included the following line in my html file: <link rel="stylesheet" href="css/styles.css"> Despite having the css file linked, the styling is not visible on the page. The HTML remains plain. The structure of my files is as follows: file ...

Can we dynamically adjust font size based on the width of a div using CSS?

My div is set to 70% width and I have a specific goal in mind: To fill that div with text To adjust the font size so that it takes up the entire width of the div https://i.stack.imgur.com/goVuj.png Would it be possible to achieve this using only CSS? B ...