css - design your layout with floating div elements

I used to design layouts using tables, but now I'm trying it out with divs.

However, I'm still struggling to get the layout right. It's not as straightforward as using tables. For instance, my code looks like this:

var html_output = 
"<div id='infoPlanet' style='width:400px;'>"+
"   <div id='gambarPlanet' style='padding: 0px; width:200px; margin-left:auto; margin-right: 200px;'>"+
"       <IMG SRC='\\Planets\\planet"+gambarPlanet+ ".jpg'>"+
"   </div>"+ 
"   <div id='kadarSehari' style='padding: 0px; width:200px; margin-left:auto; margin-right: 200px;'>"+
"       <table class='kadarPlanet'>"+
"       <tr><td>Kadar Kelahiran</td><td>"+kadarmanusia+"</td></tr>"+
"       <tr><td>Kadar Perlombongan</td><td>"+kadarkristal+"</td></tr>"+
"       <tr><td>Kadar Penjanaan</td><td>"+kadarnadir+"</td></tr>"+
"       </table>"+
"   </div>"+
"</div>"

Despite my efforts, gambarPlanet ends up on the left and kadarSehari on the right. How can I make kadarSehari appear below gambarPlanet?

I adjusted the width of gambarPlanet to 300px so that gambarPlanet.width + kadarSehari.width = 300+200 > 400, but kadarSehari spills over the right boundary of the main div (infoPlanet). The kadarSehari div refuses to move below gambarPlanet.

I've experimented with gambarPlanet.style.float="top" and kadarSehari.style.float="bottom", but nothing changes. I've also tried position: absolute and position: static, without success.

If you wouldn't mind, could you advise me on the correct structure for two divs: 1) to display next to each other (like my current output, which is more guesswork than understanding) 2) to stack on top of each other

And if I wanted four divs: left, middle-left, middle-right, right - how should I organize the divs?

I could achieve this in 5 mins with , but I've spent 6 hours attempting to code it with divs and have hit a wall.

If you're able to help, please guide me on what steps to take. Hopefully, I can learn something new.

Answer №1

If you need two divs to appear next to each other, simply include float:left;:

<div id="infoPlanet" style="width:400px;">
    <div id="gambarPlanet" style="width:200px;float:left;"></div>
    <div id="kadarSehari" style="width:200px;float:left;"></div>
</div>

If you want the second div to be below the first one, use:

<div id="infoPlanet" style="width:200px;">
    <div id="gambarPlanet" style="width:200px;"></div>
    <div id="kadarSehari" style="width:200px;"></div>
</div>

To see a live demo, visit this fiddle

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

Retrieving the ID value and activating a click function for each link sharing a common class using jQuery

I'm facing a challenge in enabling the click function for every link with the same class name and unique id. Currently, the click function is only working on the first link, but there could be any number of links - 1, 2, 3, or more... To tackle this ...

Tips for resizing a background image horizontally without changing its height

Having a background image that is taller than the page and setting it as the background for the wrapper has caused some issues. Resizing the window results in a large amount of white space under the background image due to the fixed height of the wrapper. ...

The Consequences of Using Undeclared Variables in JavaScript

What is the reason behind JavaScript throwing a reference error when attempting to use an undeclared variable, but allowing it to be set to a value? For example: a = 10; //creates global variable a and sets value to 10 even though it's undeclared al ...

The vertical spacing in Font "bottom-padding" appears to be larger than anticipated

Recently, I purchased the font Proxima Nova for my project and I am encountering issues with vertical alignment. Despite setting margins, line-heights, and padding to match those of Arial, the results are not consistent as the Proxima Nova appears misalign ...

Customizing the primary CSS style of an element without the need to individually reset every property

Is there a way to reset just one property of a selector that has multiple properties stored in main.css, without listing all the properties and setting them to default values? I always struggle with CSS interference and constantly need to reset properties ...

Issue with implementing MUI Grid within a dialog across various screen sizes

While working with a MUI dialog and MUI grid, I encountered an issue. The code I am using is directly from the website, with only minor modifications to the dialog function names and the box wrapping the dialog. Despite changing the size of the dialog, t ...

Steps for configuring a switch to display a color at random

Looking for a way to modify colors of a basic switch <body> <label class="toggle"> <input type="checkbox"> <span class="slider"></span> </label> </body> .toggle { --width: 80px; ...

Fluctuating problem arises when placing one div over another div while hovering

Sorry if the title isn't clear. Basically, I have a div that when hovered over, triggers another div to appear with display:block, showing it above the original div. It works well but there is some flickering when moving the cursor over the second div ...

Top way to include an HTML and javascript file in an Ext.Panel within Sencha Touch

My main goal is to efficiently include external HTML files and display them on an Ext.Panel in Sencha touch 2.3 by creating a wrapper module for the HTML file that can be instantiated using xtype, with an external Javascript file for event handling. Updat ...

Creating a CSS3 web design inspired by the Facebook-image style - here's how!

Looking to create a design using CSS that mimics the facebook-image-container, similar to this example: . Seeking advice on how to achieve this effect. The concept is to have a centered box on the page, with one half displaying an image and the other conta ...

How to use addclass in AngularJS?

Can someone assist me with converting this to jQuery? $('.toggle').on('click', function() { $('.container').addClass('active'); }); $('.close').on('click', function() { $('.container&ap ...

Could someone please clarify a specific aspect of how floats work?

Could someone simplify this explanation for me? A float is positioned next to a line box if there is a vertical space that meets these conditions: (a) at or below the top of the line box, (b) at or above the bottom of the line box, (c) below the top margi ...

A nifty little JavaScript tool

Recently, I created a bookmarklet that opens a new window with a specified URL and passes variables to a PHP script using the GET method. However, I now require it to load the same PHP script and pass the same variables but within a div element this time. ...

Footer overlapping occurs on a single page

My webpage is having an issue where the footer is overlapping. It seems to be fine on all other pages except for this one. I would prefer not to make any changes to the footer since it's working correctly on other pages. Is there a way to adjust the C ...

What could be the reason for the absence of the loading sign in Chrome, even though it appears when the code is run on Firefox?

I implemented a function to display a loading screen on my HTML page with Google Maps integration. However, when I called the function popUpLoadingScreen() and dismissLoadingScreen() to show and hide the loading message while rendering map markers, the loa ...

Header that sticks to the top of a container as you scroll through it

Many questions arise regarding sticky elements in the DOM and the various libraries available to handle them, such as jquery.pin, sticky-kit, and more. However, the issue with most of these libraries is that they only function when the entire body is scro ...

Exploring the process of transferring a jQuery array from a Rails controller to a PostgreSQL array column

For my project, I successfully pass a JavaScript array to a Rails controller using AJAX. The JavaScript array consists of upload image names. Within my postgresql database, there is an array column called "images." In the Rails controller, I attempted the ...

What could be the reason for the failure of this click binding to get bound?

Take a look at this fiddle: http://jsfiddle.net/BxvVp/11/ I have created a view model with a function that replaces the content of a div with some hidden content on the page. After this replacement, the text binding seems to work fine, but the click bindi ...

How do I print a QTableWidget using QT5 and resize the table to fit perfectly on one side of an A4 sheet?

My QT5 application is designed to generate a monthly rota/timesheet in csv format, which can be easily read and printed using Excel or LibreOffice. However, I want to enhance the functionality by printing the table directly from Qt to the printer. I have ...

Dynamic content in CSS horizontal alignment

In a parent div with fixed width, I have dynamically created divs that I want to distribute horizontally without knowing the exact number of elements. I tried using the "Using inline-block and justified text" technique from a CSS Tricks page but it did not ...