What is the most effective method to align two elements within a container in CSS? (one on the left and one on the right of the container)

I need help with positioning two colored box elements within a container using CSS.

You can see my test page here:

The code for the content in the center area of the website is as follows:

HTML/CSS code:

#header2 {
  background-color: #DEB887;
}
#container {
  /* allows positioning an element at the center of its container */
  margin: 0 auto;
  overflow: hidden;
  width: 100%;
}
#first {
  /*background-color: #8FBC8F;*/
  /* BACKGROUND GRADIENT: */
  background-image: linear-gradient(to bottom, #35F2EC 0%, #16B7D6 50%, #016D94 100%);
  width: 200px;
  min-height: 300px;
  border-radius: 10px;
  float: left;
  margin-bottom: 20px;
  box-shadow: 6px 7px 15px rgba(0, 0, 0, 0.37);
}
#second {
  background-color: #8FBC8F;
  width: 200px;
  min-height: 300px;
  border-radius: 10px;
  float: right;
  margin-bottom: 20px;
  box-shadow: 6px 7px 15px rgba(0, 0, 0, 0.37);
}
<div id="container">
  <div id="header2">
    <p>TITLE</p>
  </div>

  <div id="first">
    <p>BLA BLA BLA</p>
    <p>BLA BLA BLA</p>
    <p>BLA BLA BLA</p>
  </div>

  <div id="second">
    <p>BLA BLA BLA</p>
    <p>BLA BLA BLA</p>
    <p>BLA BLA BLA</p>
  </div>

</div>

The #container div contains an external #header2 and two columns created by first and second divs.

I want the first column to be on the left side of the #container, while the second column should be on the right side of the #container.

To achieve this, I have used the float: left property for the #first column and float: right property for the #second column.

Is this a good solution or could it create potential issues?

Thanks,

Andrea

Answer №1

It seems like there won't be any issue with achieving this, as there are multiple other methods that could be used.

  • An alternative approach would be to enclose #first and #second within a div.wrap, applying display:inline-block; to both items while using display:inline; on the enclosing div.wrap. For more details, check out resources discussing the use of inline-block vs. float techniques.
  • Another option is to only apply float:left; to #first, allowing #second to inherit the float property and display content side by side.
  • Alternatively, you could experiment with using display:table-row; for div.wrap and display:table-cell; for #first & #second, although it's generally recommended to avoid this method.

Answer №2

One technique I really enjoy using is flexbox, as demonstrated in the following example:

#wrapper {
  display: flex;
  width: 500px;
  flex-direction: row;
  justify-content: space-between;
  background-color: #DDDDAA;
}

#column1, #column2 {
  width: 35%;
  text-align: justify;
  padding: 10px;
  background-color: #DDDDFF;
}
<div id="wrapper">
  <div id="column1">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
  <div id="column2">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
</div>

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 retrieving the file name from the <input type="file" tag within a JSP page

I am looking to retrieve the file path from an HTML input type="file, which is selected by the user in the file dialog. <script> function OpenFileDialog(form) { var a = document.getElementById("inputfile").click(); SampleF ...

What is the best way to set up a clickable image without making the entire div clickable?

Is it possible to make just a specific image clickable within a div without making the entire div clickable? I have an image inside an anchor tag and a surrounding div that is clickable. The issue is that the entire space around the first image, .home, is ...

Ways to create a gap between a div and the bottom of a webpage

My div is extending beyond the page height and causing the white spacing underneath to disappear. How can I fix this issue and restore the white spacing? div { background-color: grey; position: relative; top: 136px; } <div> <p>Lorem ...

The Django static files were uncooperative and refused to function

I've encountered an issue with my HTML files while using Django. Some files work perfectly fine when I load static files, but others don't seem to cooperate. I'm perplexed as to why this inconsistency exists! Have I overlooked something impo ...

When the previous textbox is filled, the cursor will automatically move to the button

Utilizing an RFID reader where students tap their ID to display basic info, a hidden button on the form opens a modal with various purposes for selection. The challenge is shifting focus of cursor to the button and automatically clicking it when the last ...

Is it possible to utilize attr() within background url() in CSS?

Can the following be achieved using HTML? <a href="http://host.com/cat.png">text</a> and in CSS: a { background-image: url(attr(href)); } I have been trying but it doesn't seem to work for me. Is this even possible? ...

Crafting a template for mixing up images and quotes without any specific connection - here's how!

I came across some interesting templates that can generate random quotes and others that create random pictures. My goal is to develop a template that generates both a random quote and a random picture, similar to this example, without any relation betwee ...

Deprecated: Asynchronous XMLHttpRequest on the primary thread is no longer supported

Currently, I am working on the extjs framework. I have developed an application using extjs. However, whenever I launch the application in the browser, I encounter some warnings in the console. The warning message states that Synchronous XMLHttpRequest o ...

SVG set to fill currentColor but does not dynamically change in high contrast mode

I'm currently working with in-line SVGs on my website. Here's an example: svg { fill: currentColor; } <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" height="25px" viewBox="-13 0 120 30" width="74px"> < ...

When the sidebar is set to position: fixed, the icons magically vanish

This CSS is specifically for the side navigation bar. .side-nav { // position: sticky; top: 0; left: 0; height: 100vh; z-index: 100; width: calc(3.5vw + 3.5vh); // position: fixed; &.content { height: 100%; display: flex; fl ...

Problem with Jquery fetching data from specified div not resolved

Here is a working example: var myvar; $.get('formElements.html', function(data) { myvar = data; }); However, this code does not work as intended: var myvar; $.get('formElements.html .className', function(data) { myvar = data; } ...

What is the best method for confining a position:fixed element within boundaries?

Can you share a method for implementing a stopping point or boundary on a position:fixed menu that remains fixed while scrolling and stops when it reaches a specific point? ...

How can I retrieve the row index in an Angular Mat-Table that has expandable content?

Having a mat-table with expandable content on a page, I am seeking a solution to record the row number of the table when clicked. While I successfully achieved this with a regular table in the HTML file using: <tr mat-row *matRowDef="let row; columns: ...

Providing a BR tag with a distinctive on-screen look (prior to the break happening)

After coming across this particular inquiry, I discovered that in the past, styling the line break (BR) tag with CSS was not possible. Nevertheless, thanks to the latest browsers, this limitation is a thing of the past now. My aim is to give certain line b ...

Ways to perfectly align three images side by side using CSS

I am trying to align 3 pictures horizontally, but they keep ending up on separate lines. Here is the code I have been using: HTML <section class="features"> <figure> <img src="....jpg" alt="..."> ...

The inputs are not responding to the margin-left: auto and margin-right: auto properties as expected

I'm having trouble aligning an input in a form to the center. Even though I have included display: block in the CSS, using margin-left:auto and margin-right: auto is not yielding the desired result. To illustrate the issue more clearly, I've cre ...

Steps to altering the color of a second button with a button click

My goal is to create a button click function that allows only one button to be clicked at a time. For instance, when btn1 is clicked, its background color changes from transparent to green. Then, if btn2 is clicked, btn1's background color should chan ...

Creating Eye-Catching Images by Incorporating Image Overlays Using Just One Image

I'm facing a bit of a challenge here. I need to figure out how to overlay an image onto another image using just a single image tag. Specifically, I want to add a resize icon to the bottom right corner of an image to let users know they can resize it ...

if the input field is empty, eliminate the class

Within the code snippet, a function is present which performs the following actions. It searches for an email in the input field, then converts the email to an md5 hash, and finally generates a Gravatar URL that is inserted into a class using fadeIn(); ...

Tips for generating an HTML table in FPDF

I'm new here and facing a challenge converting an HTML table using fpdf. Below is my HTML code: <html> <body> <table border=1 align=center> <tr> <tr> <td>&nbsp;</td> <td>PO1</td> <td>PO ...