When one element disappears, the remaining elements expand to fill the empty space

I've set up two DIV elements, but I want them to only appear on one specific page. On all other pages, I need only one of those DIV elements to show. When the second element disappears, I want the first element to automatically expand to full width. Unfortunately, I can't upload images right now, so here's an example:

[div element "one"] occupies 50% width [div element "two"] occupies 50% width

*Now div element "two" vanishes

[div element "one"] takes up 100% width

How can I achieve this layout with just CSS and HTML? It seems challenging without changing the DIV element classes.

Answer №1

You can customize the appearance of your div based on the class of its parent container:

Example for Page 1:

<body class="grid">
  <div id="box1">content</div>
  <div id="box2">content</div>
</body>

Example for Page 2:

<body class="list">
  <div id="box1">content</div>
</body>

CSS stylesheet:

.grid #box1 { width: 50%; }
.list #box1 { width: 100%; }
#box2 {width: 50%; }

Answer №2

Try formatting your code like this:

HTML

<div class="two">two</div>
<div class="one">one</div>

CSS

.two{
    background:blue;
    float:left;
    width:50%;
}
.one{
    background:yellow;
    overflow:visible;
}

View the result on http://jsfiddle.net/gJ22P/

OR

You can also apply the display:table property to achieve a similar effect.

See the example at http://jsfiddle.net/gJ22P/1/

This method is compatible with IE8 and newer versions.

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 a new row to a Bootstrap table while maintaining the consistent style

Is there a way to dynamically add a new table row with different styling using jQuery? I'm facing this particular issue and need help in solving it. Below, I have included some screenshots of my code and the view for better understanding. Here is the ...

Unable to locate element within the HTML code in Angular

Despite using document.getElementByID, I am unable to retrieve the value of the list element in the HTML. The code shows that the element is undefined, even though it has the same id. Here is the snippet of code: fileupload.component.ts: ngOnInit() { ...

Other options besides re-flowing and repainting

After doing some research on various platforms like Stack Overflow, I've come across information stating that re-paints and re-flows can be quite taxing on a browser's resources. I am interested in learning about alternative CSS/JS techniques to ...

Tips for overflowing an image within an Ionic toolbar

I am attempting to adjust the image overflow within the toolbar using the following code: ion-header { background: var(--ion-color-primary); ion-toolbar { --padding-end: 30px; --padding-start: 30px; --min-height: 100px; ion-avatar ...

Graphic descending within container

Struggling to design some divs for image display, I encountered a problem where the image shifts down by 3 pixels. It seems this is due to a 3 pixel margin on the container div, but I'm puzzled as to why it affects the image position. padding:0; marg ...

Angular state correctly maps to the controller but is not reflected in the HTML

I'm currently in the process of setting up a basic Angular application, something I've done many times before. I have defined a home state and another state for a note-taking app, but I am facing an issue where neither state is displaying or inje ...

Navigating through a diagonal path

Struggling to craft a diagonal navigation system similar to the images below. Despite its seemingly simplicity, I'm stuck investing too much time in figuring it out. My goal is for the menu container to smoothly reveal from right to left when clicking ...

Dynamic parallax effect with scroll wheel on WebKit

Currently experiencing some challenges with parallaxing backgrounds. I developed a website for an event organized by my friends, featuring multiple background images that shift between content sections to create a parallax effect. I have implemented code t ...

Rejuvenate a just-launched window.open starting from the about:blank

After receiving data from an ajax result, I am trying to open a pdf in a new window. However, the pdf viewer is only displayed if I manually resize the window (using manual hotspot resizing). How can I ensure that the contents display properly in its popu ...

Issue with ASP.NET C# querying MS Access database

I am experiencing issues with the like query not working. Can someone help me troubleshoot? Below is the code I am using: string Item_Name = txt_search.Text.Trim(); string Conn = ConfigurationManager.ConnectionStrings["AjitConnectionString"].ToString(); ...

Using a loop to draw lines on a canvas

Gaining a preliminary understanding of java-script and canvas. I have managed to create steps and draw the initial two lines. I aim for this sequence to repeat 7 times. 7 p1's and 6 p2's in essence, it descends 7 times and moves across ...

I'm struggling to figure out why the CSS isn't working properly

i found this markup like shown here i am curious why it appears that lines 12 & 13 .notes:link span, .notes:visited span { ... seems to be functioning .comments span, background-position: -16px -16px; } .permalink:link span, .permalink:visited sp ...

Creating a DOM element within a JavaScript function does not result in the generation of HTML code

I have encountered an issue where I am extracting a URL parameter using JavaScript, passing it to PHP via AJAX for database query, and receiving a string as the output. After successful AJAX call, I trigger a function that calls another function containin ...

Guidelines on setting the first weekday to Sunday in Python's Django HTMLCalendar

My objective is to have the weeks in the calendar start with Sunday instead of Monday. I believe using calendar.setfirstweekday(calendar.SUNDAY) should achieve this, but I am unsure where or how to implement it in the code below. I have tried various unsuc ...

Unlimited scrolling feature in Ionic using JSON endpoint

Trying to create an Ionic list using data from a JSON URL. JSON Code: [{"id":"1","firstName":"John", "lastName":"Doe"}, {"id":"2","firstName":"Anna", "lastName":"Smith"}, {"id":"3","firstName":"Peter", "lastName":"Jones"},{......................}] app.j ...

What is the best way to relocate an image or link using CSS?

I've been attempting to adjust the position of this image using CSS, but no matter how many times I try, it just refuses to budge. Can someone please help me troubleshoot what might be causing this issue? #yahoo1 { position: absolute; top: 100p ...

"Jsoup interprets certain characters in a unique way during parsing

I attempted to parse this HTML file using Jsoup: <html><body>Maître Corbeau, sur un arbre perché</body></html> The line of code I used was: Document document = Jsoup.parse(input, "UTF-8"); However, when I tried to print the do ...

Guide on how to select a specific button from a set of buttons in JQuery without using a class name or id

Is it possible to select a specific button from a group of buttons using jQuery without utilizing a class or id? For example: <body> <button>Click Me</button> <button>Click Me</button> <button class="food"& ...

What might be causing the in-viewport javascript to not work in my code?

Why is my in-viewport JavaScript code not functioning properly? Link to JSFiddle code When the Click to move button is clicked, the cat image will slide correctly. However, when implementing the following code: if($("#testtest").is(":in-viewport")) ...

When a user chooses an item from the drop-down menu, a text input field will automatically surface

I am looking to create a function where the input type-text is initially hidden, but when a specific option (like "others") is selected from a drop-down menu as shown below, I want to reveal/show the input-type text element beneath it. <select name=&ap ...