Place element H1 below H2 on the webpage, but keep it positioned above H2 in the underlying

I have a snippet of HTML Code:

<html>
<head>
<title>css test</title>
<style type="text/css">
.box{width:100%;float:left;background:red}
</style>
</head>
<body>

<h1>Title To Appear Below Div on Page</h1>

<div class="box">
    <h2>This is a Heading 2</h2>
</div>


</body>
</html>

Looking at the code, the H1 element is positioned above the DIV.

Can the H1 element be placed below the DIV when the webpage is accessed by the user?

Considering that the height of the .box div varies on each page load (due to random text content), is there a way to consistently position the H1 below the DIV?

Any insights on how to achieve this would be greatly appreciated.

Answer №1

Hey everyone, I've got the solution for you!

To achieve the desired layout, make sure to apply the display: table-caption property to the header element and set caption-side: bottom on its container.

Here is a simple example of the HTML structure:

<div class="container">
    <h2>Heading</h2>
    <div class="content">Lorem ipsum dolor sit amet</div>
</div>​

And the corresponding CSS:

h2 { 
    display: table-caption;
}
.container {
    display: table;
    caption-side: bottom;
}
.content {
    display: table-row;
}

Feel free to check out the demonstration on jsfiddle http://jsfiddle.net/abc123/7/

This should be compatible with IE8+ and other modern browsers.

Answer №2

In case the design of the page is static and unchanging, you have the option to utilize absolute positioning for this task. Ensure there is ample white space below the "box" element and apply absolute positioning to the H1 tag. If the height of the "box" element varies, there is a possibility that the H1 tag may overlap with other text. In such extreme scenarios, resorting to JavaScript for dynamically calculating the position is always an option.

It's all akin to trying to scratch your left ear with your right hand.

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

Make a line break occur automatically after every 7 divs are displayed

Looking to create a grid of equal height and width divs using AngularJS to form a 7x5 square: ---------------------- | | | | | | | | ---------------------- | | | | | | | | ---------------------- | | | | | | | | ---------------------- ...

Tips for creating the X Mark using SVG and CSS

I'm struggling to create an animation of an "X" checkmark sign (for failure). Although I found a great example of an animated checkmark sign for success, the code uses a curve-bezier design that I can't replicate for the "X" sign. If anyone cou ...

What's the key ingredient I need to add for a dropdown navigation menu?

What am I overlooking in my HTML and CSS coding to create a standard list dropdown in my navigation menu? When I preview this section of the page in Chrome and IE, the navigation area appears fine until I hover over the Fishing and Guides link. Instead of ...

Retrieve and modify the various elements belonging to a specific category

I'm currently developing a chrome extension and I need to access all elements of this specific type: https://i.stack.imgur.com/sDZSI.png I attempted the following but was unable to modify the CSS properties of these elements: const nodeList = documen ...

The presence of a display block property within a div contained inside an li element results in extra whitespace

I am presenting the code below: <div class="settingsMenu" style="top: 135px; left: 149px; display: block;"> <ul> <li class="download" onclick="downTemplate('template1')">Download</li> <l ...

Issue with iPhone CSS displaying differently on mobile devices

The CSS design does not display correctly on iPhone devices in real-life testing, even though it appears fine on browsers with mobile view emulators. Interestingly, the design also looks great on Android phones but encounters issues specifically on iPhones ...

Ensure that the sidebar in your React application occupies the full height of the page, regardless of the presence of scrolling

In my application, most of the pages fit within the viewport which is why I have been using height: 100vh for my <SideNav /> component. However, some pages now require scrolling and this causes issues with the sidebar as it abruptly ends when scrolli ...

Leveraging the :checked state in CSS to trigger click actions

Is there a way to revert back to the unchecked or normal state when clicking elsewhere in the window, after using the :checked state to define the action for the clicked event? ...

Building a unique mobile number input box with country code in HTML

Struggling to design a unique mobile input box that separates the mobile number and country code with a divider. Need some assistance in creating this custom input field. Design https://i.sstatic.net/M0Wvs.png Current Implementation <div cla ...

Getting the value of a CSS variable under <script> in Vue.js

I am working on implementing a Doughnut chart using chartJs in my application. However, I want to set the color of the chart within the <script> tag and retrieve the color from theme/variables.css. In the current code snippet below, there is a hardc ...

Customizing the Navigation Border-Bottom in WordPress

Currently, I am working on a project with this website. My main focus is on removing the border-bottom in the navigation bar for the last two right navigation points which are "DE" and "FR". Despite trying various CSS solutions, such as: #menu-item-394 { ...

Adjust the height of the tabs bar in the Guake terminal

Is there a way to reduce the height of the tabs bar in the Guake terminal? I found a solution for GNOME terminal on this post, where they mentioned editing ~/.config/gtk-3.0/gtk.css. Is there a similar method for achieving this in Guake terminal? ...

In the past, I've crafted buttons and other elements using Photoshop, and subsequently saved them for web use. However, I'm now contemplating whether it's more advantageous to employ CSS in

In the past, I have utilized Photoshop to create visually appealing buttons and footer bars for my websites. However, I'm now pondering if it's more beneficial to utilize CSS for button creation. I've noticed that manually coding them can re ...

Which web browsers are compatible with the input attribute "multiple"?

I am currently incorporating this particular plugin, and its file input does not support multiple file selection. To address this issue, I have added the 'multiple' attribute. However, I am curious if there are any browsers or other platforms (su ...

Guide to aligning a div element along the right edge of the webpage

I'm struggling to align this div all the way to the right side of the screen. Below is the HTML: <html> <head> title></title> <link rel="stylesheet" type="text/css" href="../css/style.css"/> </head> <h1&g ...

Move the scroll bar outside of the div and set it to automatically overflow

My issue involves a small div with the CSS property overflow:auto;. However, when the scroll bar appears, it covers up a significant portion of the div. One possible solution is to use overflow:scroll;, but this can result in an unsightly faded scroll bar ...

Tips for incorporating shapes and decorative elements into your page design effectively

I have searched everywhere on the internet but couldn't find an answer to this question. The question is simple: How can I add shapes or decorations to a page layout without causing it to look broken inside a responsive container? Click here to view ...

Is it important to position elements based solely on the parent container?

My frustration with element positioning persists: <div id="container"> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> </div> #div1 { right:0; // I want its right border to be 0px from th ...

What’s the best method for enclosing a table and text within a div?

The following code snippet generates the following output: However, my desired outcome is this: When I remove <div>hello world</div>, the table fits perfectly within the outer div. But when I add text to the div along with the table, it doesn ...

SVG Opacity Transitions seem to ignore the constraints of CSS rules and bounce chaotically beyond their designated boundaries

I'm currently working on implementing a hover effect for an SVG rect embedded in HTML. The transition I've set up is not smooth, and the opacity values seem to be inconsistent during the animation. My browser of choice is Firefox. The rect I&apo ...