Develop expandable objects containing a set size of items using HTML and CSS

I need help creating a flexible content area using only HTML/CSS.

The width of this area should be able to vary from 0 to 50% within its container, which can itself vary from 0 to 100% of the window size.

I have two items that need to be positioned next to the red item, each with a fixed width of 100px.

If the window size is less than 200px, only the fixed items should be visible.

Here is an example of what I'm trying to achieve:

http://jsfiddle.net/UsNmU/

The problem is that the blue and green items are staying on the right side, but I want them to stay to the right of the red item as a group (with green on the right of blue).

Here is the HTML code:

<div id="content">
    <div id="item2"></div>
    <div id="item3"></div>
    <div id="item1">
        <form>
            <input type="text" />
            <br />
            <input type="text" />
        </form>
    </div>
</div>

And here is the CSS code:

div {
    height: 50px;
}
#content {
    width: 100%;
}
#content #item1 {
    background: #f00;
    width: 50%;
}
#content #item1 input {
    width: 80%;
}
#content #item2 {
    background: #0f0;
    width: 100px;
    float: right;
}
#content #item3 {
    background: #00f;
    width: 100px;
    float: right;
}

Below is an image for reference:

Thank you for your assistance.

Answer №1

To enhance the layout, consider removing the 50% width setting on item 1 and using absolute positioning to adjust the left and right offsets.

#content #item1 {
    background: #f00;
    position:absolute;
    left:0px;
    right:200px;
}

View example on JSFiddle

If the expandable area should not exceed 50% width, you can set a max-width of 50%.

#content #item1 {
    background: #f00;
    position:absolute;
    left:0px;
    right:200px;
    max-width:50%;
}

Answer №2

If I grasp your question correctly, I believe I have the solution...

Check out the jsFiddle link

In my approach, I floated all the div elements to the left instead of the right. This ensures that the green and blue boxes always stack next to the red one. I also made a tweak in your syntax by placing the red div before the others.

I hope this explanation clears things up for you! :)

Edit: Oops, I forgot about positioning the "green on the right of the blue". In that case, we may need to revisit the markup once again by rearranging the placement of the green div before the blue one.

Answer №3

To achieve the desired layout, try floating the left side and using overflow hidden on the right side:

http://example.com/layout/1

.left {
  float: left;
  border: solid;
}
.right {
  overflow: hidden;
  border: solid;
}


  <div class="left">left</div>
  <div class="right">right</div>

Answer №4

To achieve the layout depicted in your diagram (where the red box flexes from 0% to 50% of the container while the other two boxes remain fixed width and sit adjacent to it), I recommend floating all elements left and utilizing calc along with max-width to determine the width of the red box.

Here's the HTML structure:

<div id="content">
    <div id="item1">
        <form>
            <input type="text" />
            <br />
            <input type="text" />
        </form>
    </div>
    <div id="item2"></div>
    <div id="item3"></div>

</div>

And here is the corresponding CSS styling:

div {
    height: 50px;
}
#content {
    width: 100%;
}
#content #item1 {
    background: #f00;
    width: -webkit-calc(100% - 200px);
    width: -moz-calc(100% - 200px);
    width: calc(100% - 200px);
    max-width: 50%;
    float: left;
}
#content #item1 input {
    width: 80%;
}
#content #item2 {
    background: #0f0;
    width: 100px;
    float: left;
}
#content #item3 {
    background: #00f;
    width: 100px;
    float: left;
}

You can view a live example on jsfiddle: http://jsfiddle.net/ABC123/

The use of calc for the red box ensures it adjusts fluidly between 0% to 50% based on the width of the container, while the max-width property limits its expansion beyond half the container's width due to the fixed-width boxes beside it.

Answer №5

Another option is to utilize a media query. HTML:

<div id="content">
    <div id="item1">
        <form>
            <input type="text" />
            <br />
            <input type="text" />
        </form>
    </div>
    <div class="extra_container">
        <div id="item2"></div>
        <div id="item3"></div>
    </div>    
</div>

CSS:

div {
    height: 50px;
}
#content {
    width: 100%;
  position:relative;
}
#content #item1 {
    background: #f00;
    position:absolute;
    width:auto;
    left:0;
    right:200px;
}
#content #item1 input {
    width: 80%;
}
.extra_container{
    float:right;
}
#content #item2 {
    background: #0f0;
    width: 100px;
    float: right;
}
#content #item3 {
    background: #00f;
    width: 100px;
    float: right;
}

@media (min-width: 401px){
    #content #item1{
        right:50%;
    }
    .extra_container{
        float:left;
        margin-left:50%;
    }
}

In this scenario, absolute positioning is used to define both the left and right boundaries of the red div. This allows the div to adjust its size based on these coordinates. By default, the CSS instructs the div to stretch from 0px from the left edge of the container to 200px in from the right edge of the container. This setup enables the red div to shrink when the window is 400px or smaller and the fixed-width elements need to make the div narrower. The fixed-width divs are contained within a floated div positioned to the right.

The media query kicks in when the window exceeds 400px in width. It adjusts the positioning of the red div from 0px in from the left to 50% in from the right, resulting in a 50% width. Additionally, the other two divs switch to floating left with a margin of 50% to align next to the red div's edge.

To view an example, visit the following jsfiddle link: http://jsfiddle.net/FW4CM/2/

Keep in mind that this method requires wrapping divs for items 2 and 3 within another container. This prevents their positions from switching due to the float changes applied by the media query.

While I personally find the calc method simpler and more versatile for nesting elements, using media queries may be preferable if targeting older browsers. For information on browser support, check out these resources: media query support: http://caniuse.com/#feat=css-mediaqueries calc support: http://caniuse.com/calc

Lastly, consider exploring flexbox as it offers a robust solution, although support may vary across different browsers. It's worth investigating depending on your specific requirements.

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

What are some ways in which I can utilize the Ember Handlebars helper?

Similar Question: Using logical operators in a Handlebars.js {{#if}} statement Below is the code snippet provided: {{#each dataArray}} <li>{{#isTrue idVal1 idVal2}} display some text1 {{else}} display some text2 {{/isTrue}} & ...

What is the best way to target outer elements only using CSS selectors?

Having trouble with a CSS selector: $("td.cv-table-panel-element") This selector returns a list of elements, shown in the screenshot linked below: https://i.stack.imgur.com/RoVMj.png Each element represents a column in a table with unique content. The ...

Various height divs are arranged in two columns and float accordingly

My goal is to have two columns that stack divs of varying heights in the order they appear. These divs are created dynamically, causing some challenges with alignment when floated at 50% width. For example, div #4 may end up being significantly taller tha ...

Creating a dynamic input box that appears when another input box is being filled

I have a form set up like this: <FORM method="post"> <TABLE> <TR> <TD>Username</TD> <TD><INPUT type="text" value="" name="username" title="Enter Username"/><TD> </TR> <TR> <TD>A ...

When viewing HTML emails in dark mode on iOS Gmail, the CSS appears to be inverted

While sending an HTML email from a nodejs app, I encountered an issue with Gmail on iOS where elements with background-color or color properties were getting reversed. This was most noticeable on black and white elements. Despite consulting various guides ...

Arranging blocks within blocks? Placing additional text underneath a block

Looking to append some text at the end of a block called .label. Inside this block, there is a sub-element called .label_title, and below that is a picture/link enclosed in another block. Under the picture/link, I wish to include a short description while ...

Utilizing inline javascript to dynamically update the selected option of a radio button

A form on my website includes two radio buttons: one labeled 'trigger_unit' and the other labeled 'normal_unit'. In addition, there is a select drop-down menu with four values and a default NULL option. <select rel="dropdown" name=" ...

Adding a new column to a table that includes a span element within the td element

I am attempting to add a table column to a table row using the code below: var row2 = $("<tr class='header' />").attr("id", "SiteRow"); row2.append($("<td id='FirstRowSite'><span><img id='Plus' s ...

center the view on the specified element

Utilizing ReactJs, I am developing a horizontal timeline that showcases dates. For instance, there is a list of 100 elements, each containing a date and text content. The dates are displayed horizontally using flex-direction="row" and overflowX ...

Getting the css property scaleX with JQuery is as simple as executing the

I am struggling to print out the properties of a specific div element using jQuery. My goal is to have the different css properties displayed in an information panel on the screen. Currently, I am facing difficulties trying to show scaleX. Here is my curr ...

How can I make CSS images appear beside specific links?

Currently, I have implemented the following CSS: .entry a { padding: 2px 0 0 8px; background: transparent url(images/link_arrow_light.gif) left top no-repeat; text-decoration: underline; } It is functioning correctly, but the image is being added t ...

Incorporating A Full-Fledged Office Word Editor Into My Web Application

In our web project, we are looking to integrate a feature that allows users to create, edit, and save their word documents for reporting purposes. While Microsoft Office offers an online option here, it seems limited to working within the Microsoft OneDriv ...

What is the best way to utilize JSONP to display an entire HTML code with an alert?

When I attempt to use cross-domain ajax to retrieve the entire HTML page code, I keep receiving a failed message. The console log shows: "Uncaught SyntaxError: Unexpected token <" Below is my AJAX function: function fetchData(){ var url = documen ...

Guide on implementing iterative data path in v-for loop using Vue

I'm just starting out with Vue and I want to use image file names like "room1.jpg", "room2.jpg", "room3.jpg" in a loop Below is my code, where the second line seems to be causing an issue <div v-for="(p,i) in products" :key="i"> <img src ...

What is the best way to establish a maximum limit for a counter within an onclick event?

I'm struggling to figure out how to set a maximum limit for a counter in my onclick event. Can someone help me? What is the best way to add a max limit to the counter of an onclick event? Do I need to use an if statement? If yes, how should it be w ...

Tips for locating a webpage element with Selenium?

click here for image driver=webdriver.Chrome() driver.get("https://compass.tinkoff.ru/") driver.maximize_window() driver.implicitly_wait(20) elem = driver.find_element(By.XPATH, '//button[text()="Open Full Map"]').click() tim ...

I am currently working with CakePHP and am interested in learning how to expire the admin session

I'm having issues with the code in my UserController. When I open the admin panel in two tabs within the same browser and log out from one tab, I am unable to redirect to the login page from the other tab when clicking on any menu. function beforeFil ...

Retrieving chosen items from dynamically generated checkboxes using Angular 2+

I have a piece of HTML code where I am trying to capture the value of all selected checkboxes whenever any checkbox is checked or unchecked, triggering the (change) event. <div class="checkbox checkbox-primary" *ngFor="let category of categories; let i ...

What is the best way to intelligently cache specific segments of a page in PHP?

Issue: I am working on a website with over 50,000 pages, the majority of which are only updated once at the time of creation. I am in the process of implementing caching for these pages since they do not require frequent content changes. However, I am fa ...

Leveraging the power of Javascript and Firebase within the Angular framework

When attempting to integrate Firebase with Angular, I encountered an issue where my localhost was not able to function properly with my JavaScript code. The strange thing is that everything works fine when the code is placed directly in the index.html file ...