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

Creating a compilation of HTML file links using Gulp

Is there a way to utilize Gulp in order to compile a single HTML file containing links to all pages within a specific directory? For instance, if I have two files contact.html titled "Contacts" and faq.html titled "Frequently asked questions" located in t ...

Extracting multiline value from a textarea using JavaScript

I'm trying to extract a multiline value from a textarea using JavaScript or jQuery and store it in a cookie. Below is the code snippet I am using: HTML: <textarea id="cont" cols="72" rows="15"> JavaScript: var txt = $('#cont').val( ...

Using ASP .NET controls in conjunction with Bootstrap

Is there a way to easily apply bootstrap classes to all asp .net controls in my application at once? Here is an example of how I formatted my menu control using bootstrap. I am looking for a solution where I can apply the bootstrap theme commonly to all m ...

How can I create a hyperlink that leads to multiple pages?

I am looking to create a hyperlink that will lead to a random webpage each time it is clicked from a selection of URLs. Can anyone suggest a suitable function to get started on this task? ...

Can you inherit from a class in a different file using SASS?

All the information needed is in this question. For example, if I decide to utilize Twitter Bootstrap, would it be possible for me to create classes in my own SASS stylesheet that inherit from Bootstrap's CSS classes? Or is SASS inheritance restricte ...

Is there a way to efficiently integrate text from a webpage into a "Twitter Share" button automatically?

I have a cool feature on my website - a random quote generator where users can tweet the current quote at the click of a button. I've set up a Twitter share link using jQuery that dynamically updates with each new quote: $('.twitter-share-button ...

Enhance the numerical value displayed in jQuery UI tooltips

I have folders with tooltips displaying text like '0 entries' or '5 entries' and so on. I am looking for a way to dynamically update this tooltip number every time an item is added to the folder. The initial count may not always start a ...

Mobile version shows white spaces when using particles.js and bootstrap 4.6

This is how I implement particles.js: <div id="particles-js"></div> @RenderSection("Header", required: false) <main role="main" class="container"> @RenderBody() </main> <script> ...

Instructions for submitting information from a web form using Python without needing to know the form's ID or name

I am currently attempting to automate the submission of data into the online forms found on this webpage: Unfortunately, I am unable to determine the specific names of these forms by inspecting the source code. I have tried using the requests python modul ...

Is there a way to dynamically add <td> based on the quantity of values in a JSON object?

I have developed a program that retrieves values from JSON and I aim to display these values in a table. Currently, only the last array value is being displayed in the table even though all values are available in the console. The objective now is to dynam ...

What is the best way to create inline-block elements that stretch the entire width of their container?

How can the input field and button behavior be optimized for this specific display: ...

Reposition text below images in Meta Slider

I have a website where I am utilizing Meta Slider within Wordpress. I would like to position the captions below the images instead of on top of them. Meta Slider claims this feature is only available in the Pro version, but could it be achieved by adjustin ...

Utilizing preg_match in PHP to validate a textarea for alphanumeric characters, numeric values, and spaces

I've almost got everything working, but I'm stuck on how to utilize preg_match. I checked the manual here http://php.net/manual/en/function.preg-match.php, but it doesn't clearly explain the syntax for creating my own validation. For example ...

Deactivate a div based on a Razor variable in ASP.NET MVC4

Is there a secure method to disable a div based on a Razor variable in ASP.NET MVC 4.0? I attempted using a CSS class, but it was easily bypassed with developer tools. .disableddiv { pointer-events: none; opacity: 0.4; } Any advice on how to effectively ...

Shifting Transition for Customer Reviews

I am working on implementing a testimonial slider on my webpage located at . I am trying to make it automatically slide by itself, but have been unsuccessful so far. I would appreciate if you could visit the demo page on my website and help identify what ...

I'm seeking assistance in enhancing this code within tb for extracting values using JavaScript

Currently, I am extracting values from a table using JavaScript. The code seems to be functioning correctly, but I am looking for ways to optimize it and ensure that it is compatible with most modern browsers. The list in the table undergoes frequent chang ...

Updating a Specific Database Entry in Django Using Multiple Field Values

Hey there, I'm still learning the ropes and haven't had much time to really dive into it yet. My end goal is to create a "check-in/check-out" system by updating a timestamp from NULL to the current time based on employee number and work area. He ...

Utilize AJAX to retrieve the output of a PHP randomizer

Current situation: I have a PHP file with a randomizer function and HTML that utilizes this function to display strings from a separate text document. The Function: <?php function rand_line($fileName, $maxLineLength = 4096) { $handle = @fopen($fileN ...

What is the most efficient way to cycle through HTML image elements and display a unique random image for each one?

I'm currently working on coding a simple game that involves guessing the Hearthstone card based on its flavor text. I plan to add a button later to progress in the game, but I haven't implemented that yet. To start, I created 4 image elements an ...

Enhanced Fancybox Version 2 adjusts iframe width to fit content

I have been attempting to adjust the width of the iframe in fancybox v2 to fit my content properly. However, every time I try, the content appears too large within the iframe and requires horizontal scrolling to view it all. My goal is to see the entire wi ...