The line segment refuses to remain hidden beneath the h2 tag

Just getting started with HTML/CSS here! I'm trying to figure out how to keep a line directly under an h2 tag. In my code, there's an h2 tag labeled 'Instructions' followed by a div containing 3 other divs making up a line segment. By default, the line sits on the left side, but I want it to stay fixed under the h2 tag when the browser is resized.

I found a resource at that explores absolute/relative positioning which seems like it could help. However, my attempts haven't been successful so far.

Please see below for my HTML/CSS and a jsfiddle (note: the jsfiddle does not display the line behavior accurately when the browser size changes). Any guidance or additional resources would be greatly appreciated!

I understand this might seem trivial, but I'm working hard to learn. There were multiple methods I came across, but they all seemed quite complex.

HTML

<div id="instructions_box">
  <h2>Instructions</h2>
  <div class="line_divider">
    <div class="blue_line"></div>
    <div class="yellow_line"></div>
    <div class="blue_line"></div>
   </div>  
</div>

CSS

#instructions_box{
   display: inline-block;
   //position: relative;
}

.line_divider{
   background-color: aqua;
   //position: absolute;
   //bottom: 0;
   //right: 2rem;
}

.blue_line{
   height: 2px;
   width: 50px;
   background-color: rgb(0,0,139);
   float: left;  
}

.yellow_line{
   height: 2px;
   width: 90px;
   background-color: yellow;
   float: left;  
}

#instructions_box h2{
   text-align: center;
}

https://jsfiddle.net/10szzwvs/1/

Thanks

Answer №1

The issue with the wrapping you are experiencing may be attributed to the fixed widths in your code. Consider changing the line width to percentages, which will prevent wrapping on screens of any size. Keep in mind that you will need to adjust visual spacing elsewhere, such as on the h2 element.

#instructions_box{
   display: inline-block;
}
#instructions_box h2{
   text-align: center;
   padding: 0 25px 0;    /* visual spacing */
   margin: 0;
}

.line_divider{
   background-color: aqua;
}

.blue_line{
   height: 2px;
   width: 30%;    /* dynamic width here */
   background-color: rgb(0,0,139);
   float: left;  
}

.yellow_line{
   height: 2px;
   width: 40%;    /* dynamic width here */
   background-color: yellow;
   float: left;  
}
<div id="instructions_box">
  <h2>Instructions</h2>
  <div class="line_divider">
    <div class="blue_line"></div>
    <div class="yellow_line"></div>
    <div class="blue_line"></div>
  </div>
</div>

Answer №2

To achieve the desired layout, CSS3 Flexbox is a great solution. Simply ensure that the div containing the lines and the h2 are within the same container, as you have already done. Then, apply the following CSS:

#container_div{
   display: -webkit-flex;
   display: flex;
   -webkit-flex-direction: column;
   flex-direction: column;
    -webkit-align-items: center;
   align-items: center;
   -webkit-justify-content: center;
   justify-content: center;
}

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

Tips for refreshing a specific div element at set intervals using JQuery AJAX?

I need to make updates to a specific div element without refreshing the entire HTML page. The code below currently works, but it reloads the entire HTML page. Additionally, I am working with different layouts where I have separate files for the header, l ...

Effective ways to implement Select2 tags while utilizing the newline character tokenSeparator

I'm currently working on a select2 input feature that allows users to paste CSV data and have it converted into tags. One issue I am facing is the inability to create tags from vertically copied data. This is due to select2 using an <input type="te ...

Guide to extend the width of h1 container to fill the entire parent div

Here is a snippet of code showcasing parent and child elements along with their current styling main #main-content-wrapper div { padding: 0; margin: 0; } img { border-radius: 8px; } .overlay1 { position: absolute; top: 0; right: .1px; bo ...

Embed a static label inside an input field that remains constant even while text is inputted, without using a placeholder. Crafted using HTML,

Take a look at the screenshot below - what you see on the left side is my current setup, while on the right side is the desired outcome achieved without any plugins or HTML5 attributes The left side scenario occurs when I have 2 input fields - one with th ...

Learn how to effectively share an image using the Math.random function

Is there a way to display a random number of images on a webpage using JavaScript? Let's say we want to show X number of images, where X is a randomly generated number. For the sake of this example, let's set X to be 10. <input class="randomb ...

When I attempt to incorporate multiple sliders on a single page, I encounter difficulties in determining the accurate stopping position if the number of slides varies

I am having trouble setting the correct stop position for sliders with different numbers of slides on a page. When I have the same number of slides in each slider, everything works fine. However, I need to have a different number of slides in each slider ...

Struggling with Duplicated Images

Currently working on a website using only HTML and CSS. I am still learning the ropes in this area. My goal is to create a layout with a header, body, and footer that repeat to fill up the entire page, similar to envato.com. Here is a snippet of the code I ...

From JSON object to HTML table: converting structured data into a user

I'm currently facing a challenge when trying to iterate through JSON data retrieved from an API and display it in an HTML table. After parsing the original JSON, I accessed its elements as follows: JSON (data retrieved from API): {"PrekesID" ...

When a checkbox is selected, new input fields will dynamically appear

I have a table with 3 text fields and I would like to dynamically add the same set of text fields when a checkbox is clicked. Below is the code snippet I have, how can I achieve this using PHP and JavaScript? echo "<td>Screen ".$i."</td>"; ...

What are some ways to create a larger-than-life mui button size?

The current MUI button supports size prop values as the following small medium large Although this feature is handy, I find myself wishing I could specify the height and width using the style prop to customize the size like <Button v ...

Steps to create a hover effect similar to that of a website (increasing grid size on hover)

Looking to create a hover effect for my boxes similar to the one on this website: I've examined the code of the website above and searched extensively for a similar feature without any luck. Could anyone offer assistance with this, please? ...

Is there a way for me to implement this code to achieve the functionality shown in the demo link

$('select').change(function() { var sum = 0; var sum = parseInt($('select[name="bathrooms"]').val() * 25) + parseInt($('select[name="bedrooms"]').val() * 8); $("#sum").html(sum); }); <script src="https://ajax.googleap ...

Having trouble with my PDO insert query functionality

I've been trying to input the data from my form into the database, but unfortunately, my current code doesn't seem to be working. I followed some tutorials on YouTube for this particular project, and despite following them closely, the data I ent ...

Change the type of field from a div to an input when clicked

I'm trying to achieve something unique with a div on my webpage. When the user clicks on it, I want the div to transform into an input field. Initially, the div looks like this: <div>This is the content.</div> But when clicked, I want i ...

Enhancing File Uploads with Vuetify

For my Vue.js project, I am attempting to upload a file using Vuetify and then store that uploaded file in my data object. This is the HTML code: <input id="file-upload" type="file" @change="onFileChange"> Within my methods section, I have the fol ...

Adjust the class of a div element located close to its ancestor using JavaScript

I'm trying to change the class of the element #sidePanel from .compact to .expanded dynamically in this code snippet: <div id="sidePanel" class="compact"></div> <div id="topbar"> <div id="buttonContainer"> <div ...

Tips for positioning buttons using HTML and CSS

Looking for some help with my HTML page design. I have a few buttons that I want to style like this: https://i.stack.imgur.com/3UArn.png I'm struggling with CSS and can't seem to get them positioned correctly. Currently, they are displaying "bel ...

Changing the size of an iframe and closing it by pressing the ESC

I am developing an application that requires the ability to resize an iframe. When the user clicks the "Full Screen" button, the iframe should expand to occupy the entire screen. The iframe should return to its original size when the user presses the "Es ...

Ways to include scrolling specifically for one template

As a newcomer to frontend development, I have recently started learning Vue and the Quasar Framework. I am currently working on a table and trying to set a fixed table name with only the table items scrollable. <template> <q-table virt ...

What is the proper way to address the error message regarding requestAnimationFrame exceeding the permitted time limit?

My Angular application is quite complex and relies heavily on pure cesium. Upon startup, I am encountering numerous warnings such as: Violation ‘requestAnimationFrame’ handler took 742ms. Violation ‘load’ handler took 80ms. I attempted to resolve ...