Positioning an HTML div

Currently, I am diving into the world of HTML and have included a relative div in my code.

Although there are two additional divs positioned before it, they do not overlap; unfortunately, the first two still occupy space above the relative one. My desire is for it to be at the top of the page.

The code snippet :

.foo {
    font-family: 'Tahoma';
    font-size: 30px;
    color: #fff;
    background-color: black;
    height: 180px;
    width: 280px;
    text-align: center;
    position:relative;
    left:540px;
    border:5px solid #fff;
    top:-50px;
}

.lastpage {
    position:relative;
    text-align:left;
    width:20%;   
}

.index {
    position:relative;
    text-align:left;
    width:20%;
}

body {
    background-color:lightgray;
    text-align:center;
    align-content:center;
    color: #990099;
    font-family: 'Tahoma';
    font-size: 15px;
}
<div class="foo">
    <br><br>F O O - B A R
</div>
<div class="lastpage">
    <form method=get action="http://www.boomy.web44.net/CURRY.html/">
        <button type="submit">Last Page</button>(2/2)
    </form>
</div>
<div class="index">
    <form method=get action="http://www.boomy.web44.net//">
        <button type="submit">Index</button>
    </form>
</div>

I strive to elevate the "Foo bar" div so that it aligns with the two buttons on the same level.

Answer №1

Your CSS code has been modified by me :

.foo
{
float:right;
margin-right: 540px;


font-family: 'Verdana';
font-size: 32px;
color: #fff;
background-color: black;
height: 180px;
width: 280px;
text-align: center;
position:relative;
border:5px solid #fff;
top:-50px;
}

.lastpage
{
position:relative;
text-align:left;
width:20%;   
float:left;
}

.index
{
float:left;
position:relative;
text-align:left;
width:20%;
}

I have included float:right and margin-right in the foo class, as well as added float:left to the lastpage and index classes.

This should resolve your issue!

Answer №2

To ensure the divs appear on the same line, consider adding the CSS rule display: inline-block. By default, div elements are displayed as blocks, so they will not naturally align horizontally.

Alternatively, you can use the CSS property float: left on the lastpage div to keep it positioned to the left and have it align with the other div on the same line.

Would implementing these suggestions resolve your issue?

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

How can I ensure my game or website fits perfectly on the screen without any need

I have recently developed a fun Erase-A-Man game that is optimized for mobile devices. However, I am facing an issue where users need to scroll to view all the letters on their screens. My goal is to make the game fit perfectly on all phone screen sizes so ...

Error: Collision detection in Three.js console output

I am attempting to create a collision detection system using Three.js (a WEBGL library). However, I keep encountering an error stating "cannot call method multiplyVector3 of undefined". Is there anyone who can help me identify what I may be doing incorrec ...

Is there a method available that would allow me to display my HTML code within a frame using Selenium WebDriver and Java?

Currently, I am in the process of automating an Application that includes several embedded iframes. I am wondering if there is a method to view my HTML source code within these frames. ...

What is the best way to design a polygon-shaped responsive div?

I've been attempting to design a unique layout with a pentagon-shaped div, however, I'm encountering some challenges. Despite using SVG for the shape, it does not display correctly in Firefox. Additionally, I need the div to be responsive and sca ...

What is the best way to extend a column across multiple rows with bootstrap?

https://i.sstatic.net/uPsLe.jpg Looking to convert the design above into HTML and CSS, specifically trying to achieve a layout where the image spans across 2 rows. However, I've been having trouble making it responsive even after trying some code sni ...

Tips for utilizing the jQuery selectbox plugin on multiple elements within a single page

On my webpage, there are 3 select boxes that I want to customize using a jQuery selectbox plugin. However, the issue I'm encountering is that the plugin only works on the first select box and the others remain unchanged. Does anyone know why this mig ...

PHP is causing disruptions to HTML

Every time PHP code is present in my file, the page appears blank and upon checking the source code, there are no tags visible on the page. However, once I remove the PHP code, the form displays correctly without any issues. <?php $title = $_POST[&apos ...

Displaying a MySQL blob image in an HTML file with Vue.js: A step-by-step guide

Here is a Vue file that I have: export default { data(){ return{ info: { name: '', image: '', }, errors: [] } }, created: function(){ thi ...

Center nested rows vertically within an alert box using Bootstrap

Can anyone provide some insight into why the inner row is not vertically centered within its alert-box? I feel like it might have something to do with the nested row structure. Here is a link to the code: https://jsfiddle.net/d2pg4xta/ <div class=" ...

The horizontal rule element is not displaying

My <hr> element seems to be hidden. This is the HTML code: <div id="line"><hr /></div> And this is the CSS code: hr { border: 0; width: 96%; color: #FFFF00; height: 1px; } #line { float: left; width: 73 ...

I'm interested in learning the best way to insert the images from this JSON file into the corresponding div elements

I have completed developing a clone of an Instagram web page. Although I can retrieve data from the JSON file and insert it into the <img> tag, I am facing difficulty in displaying each image above its respective .below-image div with consistent spac ...

What is preventing me from translating an element using transform: translateY()?

I attempted to move the letter 'h' down by 40px on its own, but the translation only seems to work if I relocate the entire word. I also experimented with positioning 'h' absolutely relative to the parent element, but then 'ello&ap ...

What can be done to prevent parallel images from getting displaced while using CSS3 transformation effects?

Currently facing an issue where three separate images are being transformed using CSS3 upon hover. Various attempts have been made, including placing them in divs, a table, or just plain images. Trying to float them all in one direction, with #1 left, #3 ...

Utilizing a drop-down selection menu and a designated container to store chosen preferences

My form includes a select dropdown that displays available options (populated from a PHP database). Users can choose options from the list, which are then added to a box below to show all selected items. However, I am facing a challenge with the multiple s ...

The child element is bigger than its parent due to the maximum height set. There is no visible overflow

My goal is to create a parent element with a maximum height, and have a child element fill this parent. If the content in the child exceeds the parent's dimensions, a scrollbar should appear. I attempted to achieve this by: div.parent { max-hei ...

Eliminate spaces within a string using JavaScript

In my quest for knowledge about trimming strings in JavaScript, I came across this intriguing discussion with a regex-based solution. After learning from the thread, I was under the assumption that trim would eliminate the space between "Hello" and "World ...

Arrange the divs at the top and bottom of a Bootstrap 5 column

I am working with a Bootstrap 5 row that contains multiple columns. Each column consists of an image and a paragraph. Since the images have varying heights, I am trying to align everything by: setting the child div to have a height of 100% using h-100 al ...

Unable to process form submission with AngularJS + Stormpath

I am facing an issue with form submission. Even though I believe that the login and password data are being sent correctly, nothing happens when I submit the form. I am attempting to submit the form without using ngSubmit because it is not feasible in my s ...

What causes two elements with display: inline-block; position: absolute; to overlap instead of appearing side by side?

It is recommended to use position: relative; in order to achieve inline block behavior for both elements and prevent them from overlapping. This positions the elements inline next to each other as they are recognized as blocks. When using position: absolu ...

Is there a way to turn off predictive text for reCAPTCHA on my mobile device?

Using reCAPTCHA on a sign-up form can get frustrating on mobile devices, with constant dictionary word suggestions while typing. I am aware of how to disable this feature for normal input fields by adding attributes through JavaScript, but shouldn't ...