Ensure that subsequent content is displayed underneath a variable-height DIV

My CSS-driven tab control is functioning perfectly, with each page having a different height.

However, I am facing an issue where the next control appears beside the tab control when I need it to be positioned below it. This becomes especially challenging due to the varying heights of the tab content that I am selecting. Additionally, I would like to avoid fixing the page height and using scroll bars as a solution.

I would greatly appreciate any assistance in resolving this.

HTML:

<div>
    <div class="tab-control">
        <ul id="navlist" class="tabs">
            <li>
                <input type="radio" name="tabs" id="tab-1" checked="checked" />
                <label for="tab-1">First Tab</label>
                <div class="tab-content">
                    Content of Tab 1.
                </div>
            </li>
            <li>
                <input type="radio" name="tabs" id="tab-2" />
                <label for="tab-2">Second Tab</label>
                <div class="tab-content">
                    Line 1 - Tab 2.<br />
                    Line 2 - Tab 2.<br />
                    Line 3 - Tab 2.<br />
                    Line 4 - Tab 2.<br />
                    Line 5 - Tab 2.<br />
                    Line 6 - Tab 2.<br />
                    Line 7 - Tab 2.<br />
                    Line 8 - Tab 2.<br />
                </div>
            </li>
            <li>
                <input type="radio" name="tabs" id="tab-3" />
                <label for="tab-3">Third Tab</label>
                <div class="tab-content">
                    Content of Tab 3.
                </div>
            </li>
        </ul>
    </div>

    <div>
        This should appear beneath the tab control.
    </div>
</div>

CSS:

.tab-control {
    clear:both;
    width:700px;
    background-color:#0ff;
}

.tabs {
    list-style-type:none;
    padding:0;
    margin:0;
    position:relative;
    width:100%;
}

.tabs li {
    float: left;
}

.tabs li > input {
    display: none;
}

.tabs li > label {
    display: inline-block;
    border: 1px solid #02606d;
    padding: 10px 20px;
    height:10px;
    line-height:10px;
    border-right-width:0px;
    border-bottom-width:0px;
    border-top-left-radius: 7px;
    border-top-right-radius:7px;
    cursor:pointer;
}

.tabs li:last-child > label {
    border-right-width:1px;
}

.tabs .tab-content {
    display: none;
    position:absolute;
    left:0;
    padding:5px;
    width:100%;
    border: 1px solid #000;
}



.tabs li > input:checked + label {
    background-color: #eee;
}

.tabs li > input:checked  ~  .tab-content {
    display: block;
}

Answer №1

Let me share my approach to this issue. I have reorganized the structure of the html elements: input, label, and .tab-content are now siblings. Additionally, it is important to include the corresponding tab-id in the class of the div that contains the content.

 input {
        display: none;
    }
    .tab-content {
        display: none;
        border: 1px solid black;
    }
    input:checked + label {
        background: #ccc;  
    }
    
    input[id=tab-1]:checked ~ div[class~=tab-1],
    input[id=tab-2]:checked ~ div[class~=tab-2],
    input[id=tab-3]:checked ~ div[class~=tab-3]{
        display: block;
    }
<div class="tab-control">
        <input type="radio" name="tabs" id="tab-1" checked="checked" />
        <label for="tab-1">First Tab</label>
        <input type="radio" name="tabs" id="tab-2" />
        <label for="tab-2">Second Tab</label>
        <input type="radio" name="tabs" id="tab-3" />
        <label for="tab-3">Third Tab</label>
        <div class="tab-content tab-1"> 
            Content of Tab 1.
        </div>
        <div class="tab-content tab-2"> 
            Line 1 - Tab 2.<br />
            Line 2 - Tab 2.<br />
            Line 3 - Tab 2.<br />
            Line 4 - Tab 2.<br />
            Line 5 - Tab 2.<br />
            Line 6 - Tab 2.<br />
            Line 7 - Tab 2.<br />
            Line 8 - Tab 2.<br />
        </div>
        <div class="tab-content tab-3"> 
            Content of Tab 3.
        </div>
    </div>

    <div>
        This should come below the tab control.
    </div>

Answer №2

Check out this suggestion: Sample HTML:

<div class="after-Tab">
        This content should be displayed after the tab control.
</div>

CSS style:

.after-Tab{
      border: 1px solid black;
      position: absolute;
      top: 250px;
      width: 450px;
    }

If this doesn't meet your requirements, feel free to elaborate on what you're looking for. Thank you! :)

Answer №3

    <head>
    <style type="text/css">
         ul li{
             display:inline; //updated to show horizontally
         }

        .tabs li > label {
            display: inline-block;
            border: 1px solid #02606d;
            padding: 10px 20px;
            height: 10px;
            line-height: 10px;
            border-right-width: 0px;
            border-bottom-width: 0px;
            border-top-left-radius: 7px;
            border-top-right-radius: 7px;
            cursor: pointer;
        }

        .tabs li:last-child > label {
            border-right-width: 1px;
        }

        .tabs .tab-content {
            display: none;
            position: relative; /*updated - now using relative instead of absolute*/
            left: 0;
            padding: 5px;
            width: 100%;
            border: 1px solid #000;
        }
        .below { /* updated - new class added */
            display:normal;
            position:relative; /* now using relative positioning */
            padding-top:20px;  /* for adding some space above*/
        }


        .tabs li > input:checked + label {
            background-color: #eee;
        }

        .tabs li > input:checked ~ .tab-content {
            display: block;
        }
    </style>   
</head>
<body>
    <div>
        <div class="tab-control">
            <ul id="navlist" class="tabs">
                <li >
                    <input type="radio" name="tabs" id="tab-1" checked="checked" />
                    <label for="tab-1">First Tab</label>
                    <div class="tab-content">
                        Content of Tab 1.
                    </div>
                </li>
                <li>
                    <input type="radio" name="tabs" id="tab-2" />
                    <label for="tab-2">Second Tab</label>
                    <div class="tab-content" >
                        Line 1 - Tab 2.<br />
                        Line 2 - Tab 2.<br />
                        Line 3 - Tab 2.<br />
                        Line 4 - Tab 2.<br />
                        Line 5 - Tab 2.<br />
                        Line 6 - Tab 2.<br />
                        Line 7 - Tab 2.<br />
                        Line 8 - Tab 2.<br />
                    </div>
                </li>
                <li>
                    <input type="radio" name="tabs" id="tab-3"/>
                    <label for="tab-3">Third Tab</label>
                    <div class="tab-content">
                        Content of Tab 3.
                    </div>
                </li>
            </ul>
        </div>
        <div class="below"> /* added a class .below */
            This should come below the tab control.
        </div>
    </div>

</body>

Please copy the code and paste as a html file and test, that if it is what you wanted.

Here, I gave the taqb content position as relative and add a class in the div which have to be shown below. and the posiotion of the div is also relative.

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

Exploring the challenging surfaces of a mesh through raycasting in Three.js

I successfully built a box using three.js var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 100, window.innerWidth/window.innerHeight, 0.1, 1000 ); camera.position.set(5, 5, 10); var geo = new THREE.BoxGeometry(5,2,5); var mat = n ...

Can you help understand the unexpected output produced by this JavaScript code?

When looking at the following JavaScript code, you would expect an output of 32. However, the actual answer is 16. How is this possible? 5 + 1 = 6; 6 + 5 = 11; 11 * 2 = 22; 22 + 10 = 32; (this should have been the correct answer) var x = 5; x = (x++, a ...

Create engaging animations using JavaScript to draw moving circles on an

I'm attempting to achieve a seamless animation of a moving circle from one set of coordinates to another. In this particular scenario, the circle moves from (10, 10) to (50, 50). Below you can find the code for my Bullet class. Whenever the user press ...

Limit the number of characters in the angular expression and include ellipses at the end

I am looking to implement a restriction on the amount of text displayed by an angular expression that returns a block of text. The length of the text can vary greatly, ranging from 1 character to over 3,000. I want to limit the displayed text and potentia ...

Enhancing jQuery's ScrollTop Functionality

My jQuery script for scrolling to the top of a container seems to accelerate after it starts. It begins slowly and then speeds up. Is there a way to prevent this lag at the beginning and maintain a consistent speed throughout the entire scroll? // Once t ...

Looking for a way to retrieve JSON data from an HTML page using JavaScript? Look no further

There is a page that sends me JSON data. You can make the request using the GET method: or using the POST method: argument --> unique_id=56d7fa82eddce6.56824464 I am attempting to retrieve this information within my mobile application created with I ...

Unable to extract properties from event.body

I'm encountering an issue where I am unable to destructure from event.body. exports.handler = async (event, context) => { const { items } = event.body; const allItems = await getProducts(); return { statusCode: 200, bod ...

Executing a jQuery function to remove the parent div on a click event

I'm trying to figure out how to make the parent div remove when clicking on the remove button. Here's the code I currently have: function removeDiv(){ $(this).parent('div').remove(); } <script src="htt ...

What is the best way to display a list of items with a default limit, and then dynamically load more items as the user scrolls

Here is a list of items that needs to be displayed. Scenario a: If there are less than three items (e.g. one, two, or three items), they should be shown normally without any issues. Scenario b: In case the list contains more than three items (e.g. four, ...

Employ JavaScript to tally up the number of checkboxes that are checked within a web table

Here is some HTML code: <table id="tblPortfolio"> <tbody role="alert" aria-live="polite" aria-relevant="all"> <tr class="even"> <td class=" sorting_1"> </td> <tr class="odd"> <td class=" sorting_1"> <input typ ...

Performing an asynchronous AJAX request to a PHP script to compute the total sum and retrieve

I have created an HTML table and utilized AJAX to send a POST request with two arrays. The first array is named rowValues and contains three arrays, one for each row. The second array is called columnValues and also has three arrays, one for each column. ...

Practicing diligently, JavaScript code snippets

In my app using Playframework 2.2, I heavily rely on ajax to load content dynamically. There are times when I need to bind or change an event handler after loading content via ajax. For example, if I want to modify the click handler for a newly retrieved ...

Recreating elements in ng-repeat using ng-click conditionally

I am attempting to swap an anchor tag with an image when clicked by the user. The code snippet I'm using looks like this: <div ng-repeat="postpart in currentPost.parts "> <div ng-if = "!postpart.isclicked"> <img ng-src= ...

What type of design pattern does this belong to?

Today, as I was exploring a TypeScript repository, I noticed that all the classes were structured in a particular way: export class ServiceClass { private static instance: ServiceClass; private constructor() {} public static Instance(): ServiceClas ...

What is the best way to set the screen height without needing to scroll vertically?

How can I make the orange and blue area extend to the end of the screen or have the full screen size minus the height of the navbar? When using `vh-100`, it fills the full height but creates a vertical scrollbar which is not desired. Is there a way in Boot ...

Can the validation messages for each individual element in a collection be customized using jQuery?

I am implementing a method to customize error messages for each field in a form using jQuery. Currently, the error message displayed is: "The true field is required." function addRequired() { var container, inputs, index, input; // Get the ...

What is the significance of "('_' + element + '_')" in coding?

function enlarge(element) { var target = document.getElementById(element); var currentHeight = target.offsetHeight; var scrollHeight = target.scrollHeight; var loopTimeout = setTimeout('enlarge(\'' + element + '&bso ...

Unable to populate SQL Server database from PHP JQuery form

I am facing an issue where hitting save just refreshes the page with a different URL added. Uncertain if it's due to my query or if the form data is not being submitted correctly. require_once ('connectionstring/connectionstring.php'); $con ...

The synchronization and network bandwidth optimization of Angular and Firebase's three-way binding system

Is it possible to synchronize the text box content across various clients using Angular and Firebase? I am curious to know if Firebase will update the database with each letter I type. How does this process impact network bandwidth? Can you explain how it ...

Discovering the Xpath for a checkbox within a <div><input> combo using Selenium

Is there a way to successfully click on the checkbox with the provided code? I've attempted using the xpaths below, however, they are unable to locate the checkbox and do not display any errors in the console. driver.findElement(By.xpath("//*[@id=&ap ...