Two divisions placed side by side

The current structure I am working with looks like this:

<div id="container">
    <div id="msg"><p>Lorem ipsum dolor sit amet.</p></div>
    <div id="img"><div id="content"></div></div>
</div>

#container{
    position:absolute;
    width:182px;
    height:60px;
}
#msg{
    width:95px;
    height:60px;
    float:left;
    display: table; 
    background:#666;
}
#msg p{
    font-size:13px;
    color:#eee;
    text-align: center;
    vertical-align: middle;
    display: table-cell;   
}
#img{
    width:87px;
    height:60px;
    float:right;
    background:#333;
}
#content{
    position:absolute;
    top:7px;
    width:80px;
    height:45px;
    background:#ccc;
}

I am aiming to achieve a layout where, when the div with the id "img" is hidden (using display:none), the div with the id "msg" expands to occupy 100% width of its parent element. However, the text within "msg" should remain centered.

Link to Example

Answer №1

To optimize the efficiency of your goal, it is recommended to apply the display:table; rule to the main container and its two child elements.

Try hovering over the demo to hide the #img element and observe how the #msg element fills the entire width of the #container:

Check out the DEMO

#container {
    display:table;
    width:182px;
    height:60px;
}
#msg {
    width:95px;
    display: table-cell;
    vertical-align: middle;
    background:#666;
    text-align: center;
}
#msg p {
    font-size:13px;
    color:#eee;
}
#img {
    width:87px;
    vertical-align:middle;
    display: table-cell;
    background:#333;
}
#container:hover #img {
    display:none;
}
#content {
    width:80px;
    height:45px;
    background:#ccc;
}

Answer №2

Perhaps adjusting the message width could be the solution:

$("#msg").on('click', function(){
    $("#img").css('display', 'none');
    $("#msg").css('width', '100%');
});

Answer №3

Yes, implementing this functionality is achievable through the use of jQuery. Below is a script that addresses this specific issue. To test its functionality, I have included a display:none; property in the #img id.

<style type="text/css" >
#container{
    position:absolute;
    width:182px;
    height:60px;
}
#msg{
    width:95px;
    height:60px;
    float:left;
    display: table; 
    background:#666;
}
#msg p{
    font-size:13px;
    color:#eee;
    text-align: center;
    vertical-align: middle;
    display: table-cell;   
}
#img{
    width:87px;
    height:60px;
    float:right;
    background:#333;
    display:none;
}
#content{
    position:absolute;
    top:7px;
    width:80px;
    height:45px;
    background:#ccc;
}
.w100{
    width:100% !important;
}
</style>

<div id="container">
    <div id="msg"><p>Lorem ipsum dolor sit amet.</p></div>
    <div id="img"><div id="content"></div></div>
</div>

<script language="javascript" >
if ($('#img').css('display') == 'none') {
    $( "#msg" ).addClass( "w100" );
}
</script>

Answer №4

Check out this alternative approach that eliminates the need for jquery by modifying the structure:

<style type="text/css" >
#container{
    position:relative;
    width:200px;
    height:100px;
    border: 1px solid black;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
#msg{
    width:50%;
    height:100%;
    background-color: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
}
#img{
    width:50%;
    height:100%;
    background-color: #ccc;
    display: flex;
    align-items: center;
    justify-content: center;
    display: none;
}
#img.active {
  display: flex;
}
</style>

<div id="container">
    <div id="msg">
        <p>Lorem ipsum dolor sit amet.</p>
        <button onclick="document.getElementById('img').classList.toggle('active')">Toggle Image</button>
    </div>
    <div id="img">
        <img src="image.jpg" alt="Image">
    </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 adding CSS styling to the action button within the ShinyWidgets package in Shiny applications

I have created a shiny app example below. In order to align the actionButton with selectInput, I had to include style='margin-top:25px'. The Shinywidgets package offers actionBttn widgets with predefined styles. For instance, I prefer the one wit ...

Encountering an issue when running the command "ng new my-app" after updating the @angular/cli package

npm version 7.5.4 has been detected, but the Angular CLI currently requires npm version 6 to function properly due to ongoing issues. To proceed, please install a compatible version by running this command: npm install --global npm@6. For more information ...

What steps can be taken to avoid my Bootstrap banner wrapping to a new line?

I am facing an issue while designing a top banner using Bootstrap. The condensed menu always wraps to a new line for the resolution of 1024x768. It works fine for resolutions greater than 1024x768. I need help with correcting my HTML so that the banner rem ...

Aggregated Mvc Razor components

After creating a layout in ASP.NET MVC, I utilized a script bundle to load the jQuery library. Unfortunately, it seems that the jQuery library is not being loaded properly. The structure of my layout is as follows: <!DOCTYPE html> <html> < ...

CSS border margin-bottom attribute not functioning correctly

I am trying to create a page border by wrapping it around the content using a <div> element. The parent element is the actual page itself. However, I'm having trouble with the margin-bottom property as it doesn't seem to be working properly ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

Issue with CSS line height - Struggling to determine the precise number of lines

Despite finding an old post on this topic, the most popular answer and other solutions didn't work for many people. Even years later, I am still facing the same issue. My goal is to create a div that is precisely 19 lines tall (ideally aiming for 19. ...

Struggle with the accordion collapse animation in Bootstrap4?

My implementation of accordion collapse in Bootstrap 4 is functional, but lacks smoothness. Can anyone offer suggestions on how to improve this? I have attempted to nest divs inside each other, but it has not yielded the desired outcome. Below is the code ...

Why is the label on my styled checkbox shifting position?

While custom styling a checkbox, I'm facing an issue where the label next to it keeps shifting. When unchecked, it aligns itself at the bottom of the box, but upon checking, it moves to center align with the box. .check { width: 100%; font-we ...

Angular List Selector: A versatile multiple selection component with a stylish list design

I'm in need of a select component similar to the one shown in https://i.sstatic.net/mo6NH.png The issue is that Material Angular doesn't have this specific component, so I opted to use the default HTML select inside my component. Everything was ...

Adding data to an AngularJS template

I am encountering an issue with a flag that I am toggling between true and false to alter the display of certain elements on my page. While it works outside of the template, integrating this value into the template itself has proven challenging. restrict: ...

The intersection observer fails to detect any new elements or entries that are appended to the page after it has

When I press the "add section" button to create a new section, the intersection observer does not seem to observe it. Even when I try to run the observer again after pressing the button, it still doesn't work. I suspect that I need to reassign the `se ...

Static CSS box positioned above dynamic scaling box

Let's Think About It <div id="foo"> Lorem ipsum </div> <div id="bar"> sit amet </div> Is there a way to maintain the size of foo as it is while allowing bar to fill the remaining space on the page when the window size cha ...

Implementing pagination within an Angular 11 Mat-table with grouping feature

Encountering an interesting issue with MatTable pagination and grouping simultaneously. I have two components each with a Mat-table featuring Pagination+Grouping. ComponentOne functions smoothly without any issues. When choosing to display 5 elements pe ...

Utilize Custom Field to incorporate background videos from websites like Youtube or Vimeo into your content

I've been working on implementing a video background for my website. I came up with a custom code to embed URL links from Youtube and Vimeo using "oEmbed". The process is functioning well, but I'm facing some challenges in setting the background ...

Emphasizing the chosen element in a list using angular

After retrieving an array of items from the database, my list is constantly changing, causing the HTML display to update dynamically. However, I am struggling with highlighting only the selected item in the list, as the entire list gets highlighted upon se ...

Is it possible to customize the arrow on a drop-down menu?

Here is an example of the page I am facing issues with: If you look at the right side of the bottom bar, you will notice a selection option for different themes. I have been attempting to replace the down arrow with an image. Below is the code I have used ...

I'm looking to replicate the header design of the classic olx website. Is there anyone who can guide me on how to extend the search bar to match the original image?

Could use some help stretching my search input field to match the original picture. Any tips or suggestions on how to achieve this? I'm utilizing bootstrap and react.js for this project. Uploaded are the original image and my resulting image for compa ...

Dynamic JavaScript Calculations Based on User Input in Real Time

I am in the process of creating a dynamic calculator that updates in real-time based on user input from a form. I have successfully implemented this feature using a sample div, but I am encountering difficulties when attempting to display the total inside ...

Customize printing options by hiding the print button

I am looking to exclude the print button from a hard copy when it is printed. <html> <head> <title>New Page 1</title> </head> <body> <p> This is just a paragraph, and I do not want t ...