changing the width of child divs that are displayed inline

Hey everyone, I'm currently working on a project where each child div needs to have the full width of the container and display inline, similar to an inline navigation menu. While I've been able to achieve this effect, my current method is not very efficient as I had to use an empty space HTML character to make it work.

    The code I used for this setup looks like this:


  <!DOCTYPE html>
    <html>
        <head>

            <style type="text/css">

                .parent > div{
                    display: inline;                
                }

                /* Display all child divs inline */

                .parent{
                    width:20%;  
                    position: relative;
                    overflow:hidden;
                    border:solid;
                }
                /* The parent container will be part of a dynamic jQuery carousel so the width is set as a percentage
                */
                /*width does not apply to inline elements*/

                .children{
                    position: absolute;
                    width: 100%;
                }

                /* Shared attributes for all child divs of the parent container */

                .child-1{
                    background-color: #8A2BE2;
                }

                /* Styling for first child div */

                .child-2{
                    left:100%;
                    background-color: #FF7F50;
                }

                /* Styling for second child div */

            </style>

        </head>

        <body>

            <div class="main-parent">
                <div class="parent">
                    <div class="children child-1">
                        First child 
                    </div>

                    <div class="children child-2">
                        Second child
                    </div>
                    &nbsp;
                </div>

            </div>
        </body>
    </html>

Answer №1

When it comes to displaying menu items, don't feel constrained by keeping them in a strict line. Showing them one at a time can actually enhance the user experience.

An effective approach is to make all navigation items block full width elements and adjust the height of the wrapper to match a single nav item. By using JavaScript to manipulate the vertical positioning, you can easily bring the desired item into view without having to deal with the left variable of an absolute positioned element.

If you find it challenging to explain a concept, chances are users will struggle even more to utilize your solution. It might be beneficial to revisit your design and identify any missing details that could improve usability.

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

Place the child DIV at the bottom of its parent DIV

I've searched through open questions but couldn't find a solution. The code snippet in question looks like this: ... <div style="some height"> <div style="some width"> .......and so on.. .. < ...

Four unique classes will each be styled differently using pseudo selectors

Consider using this specific markup - it cannot be altered (Even though it would be simpler to add more classes or IDs). There will always be a maximum of six class "test" elements on the page, so the code can be written without certainty of their surroun ...

How to vertically center align divs of unpredictable height

Currently grappling with a dilemma related to the vertical-align attribute and floating div elements. The heights of these divs are unknown as they are dynamically created based on text content. If you want to take a look at the code snippet, here is the ...

The child element with a minimum height of 300 pixels is failing to inherit the height of its parent

Here is the HTML code for a div block I am working with: <div className={'row ge-container'}> <div className={'a-span3 ge-container-navigation'}> hello </div> <div className={'a-span9 ge-containe ...

The disruptive puzzle of shuffling with jQuery

For this project, I have incorporated the jQuery shuffle plugin from this resource. Here's the scenario: Within a <div class="masterDiv"> element, there are multiple <span class="toBeShuffled"> elements. Each <span> element contains ...

The feature to Select/DeSelect All does not function properly when dealing with individual records

Here's the JavaScript code I'm working with: if (document.forms[0].Check_All.value=="Select All") { for (i = 0; i < chk.length; i++) { chk[i].checked = true; document.forms[0].Check_All.value = "DeSel ...

Maximizing the potential of Bootstrap slider within OpenCart

Has anyone attempted to use Bootstrap slide with the Opencart slideshow module? Here is my code, but I am getting all active classes. Can someone please help me with this? <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"& ...

The initial website design only occupies half of the webpage with HTML

My code is only filling up half the screen, specifically the left half but not the right. I attempted to set the html height and width to 100%, but unfortunately, it did not resolve the issue. Additionally, after this adjustment, I encountered problems wit ...

Issue encountered while attempting to remove a row from a table (JavaScript)

I'm encountering an error when attempting to delete a table row: "Uncaught ReferenceError: remTable is not defined index.html:1:1". When I inspect index.html to identify the issue, I find this: remTable(this) This is my code: const transact ...

My CSS disappears when using an ASP.Net MVC form

Unable to find any similar issue on Google... seeking some assistance. When I apply CSS formatting to style my form as desired, it displays correctly on the page without any issues. However... When I place the actual form within the following code snipp ...

Tips for retrieving the hyperlink within an HTML anchor tag using JavaScript

Here's some HTML with JavaScript: document.addEventListener('mousedown', function(event) { let elem = event.target; let jsonObject = { Key: 'mousedown', Value: event.button }; if (event.button == 2) { console ...

How come the div elements in my list are expanding beyond their container?

Below is a simplified version of my code that illustrates the issue I'm facing. I am trying to enclose all the elements within a box and enable scrolling for the .content section. So far, all my attempts at resolving this problem have been unsuccessfu ...

choose the li element that is located at the n-th position within

Need help with HTML code <div class="dotstyle"> <ul> <li class="current"><a href="javascript:void(0)"></a></li> <li><a href="javascript:void(0)"></a></li> <li><a href="javasc ...

Phantom writing reminiscent of the ghostly prose found on a Tumblr registration

Is there a way to create a URL field similar to the one on ? It's the field where the text is faded and when you click on it to type, it automatically appends ".tumblr.com" that cannot be erased or highlighted. The JavaScript used for this feature is ...

Is there a way to align text in the middle of a full-width jumbotron using bootstrap 4 while also restricting the maximum width of only the div or p container?

Trying to master the art of using Bootstrap 4 has been quite a journey. At first, everything seemed to work smoothly as I attempted to horizontally center some text within a full-sized jumbotron. However, things took an unexpected turn when I decided to im ...

Tips for updating the background color of a select option

I'm looking to customize the background color of select options in my Bootstrap 4 form. Can anyone provide guidance on how to achieve this? `` <div class="form-group "> <select class="form-control > <option va ...

What is the best way to determine if a character is valid for use in HTML rendering?

There are certain characters, like ordinal 22 or 8, that do not appear correctly in HTML (when using Chrome for example when copying and pasting them into this 'Ask question' editor; assuming utf-8 encoding). How can I identify which characters a ...

Utilize the ::before selector to insert white spaces

I attempted this solution, but unfortunately it did not produce the desired outcome: .stackoverflow::before { font-family: 'FontAwesome'; content: "\f16c "; } <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-aw ...

Is it possible to overlay a vertical middle on top of an image with an undefined height?

I've been struggling to make this work for the last 24 hours. Initially, I got it working in Chrome using CSS tricks' block:before method, but then realized it didn't function properly in Firefox. I attempted the table method as an alternat ...

Top method for adding animation to the undeclared feature on a component

During a recent keynote, I heard that in the future versions of React, it's recommended to hide a component or element using the hidden property. However, I'm curious about how to incorporate a transition effect when toggling the visibility of an ...