Positioning elements to the left and right of each other with CSS: tips and tricks

I'm currently working on aligning these elements one below the other, with one on the left and the other on the right: Here is my code snippet:

Check out the fiddle here: https://jsfiddle.net/ak9hxfpt/2/

I want to position the test2 to the right side of the test1 div, with it appearing below the test1 div. The desired outcome should look like this: https://jsfiddle.net/ak9hxfpt/3/

However, when I apply float: right to all the divs, the layout gets messed up as seen in this fiddle: https://jsfiddle.net/ak9hxfpt/4/

The "remove link" content should be displayed below the "some content" for each div. Do you have any suggestions on how to achieve this?

.test2 {
  margin-bottom: 30px;
}

.test1 {
  border: 1px solid #000;
  margin-bottom: 10px;
  width: 93%;
}
<div class="test2">
  remove link
</div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>

Answer №1

simply include a margin-left

.test2 {
  margin-bottom: 30px;
  margin-left:80%;
}

.test1 {
  border: 1px solid #000;
  margin-bottom: 10px;
  width: 93%;
}
<div class="test2">
  eliminate link
</div>

<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    eliminate link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    eliminate link
  </div>
</div>

or

.test2 {
  margin-bottom: 30px;
  width:93%;
  text-align:right;
}

.test1 {
  border: 1px solid #000;
  margin-bottom: 10px;
  width: 93%;
}
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    eliminate link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    eliminate link
  </div>
</div>

Answer №2

If you're looking to enhance your code structure, consider wrapping it in a container and applying display: flex; along with flex-direction: column;

.test2 {
  float: right;
}

.test1 {
  border: 1px solid #000;
  margin-bottom: 10px;
  width: 93%;
}

.container {
  display: flex;
  flex-direction: column;
}
<div class="container">
  <div class="test">
    <div class="test1">
      some content
    </div>
    <div class="test2">
      remove link
    </div>
  </div>
  <div class="test">
    <div class="test1">
      some content
    </div>
    <div class="test2">
      remove link
    </div>
  </div>
  <div class="test">
    <div class="test1">
      some content
    </div>
    <div class="test2">
      remove link
    </div>
  </div>
  <div class="test">
    <div class="test1">
      some content
    </div>
    <div class="test2">
      remove link
    </div>
  </div>
  <div class="test">
    <div class="test1">
      some content
    </div>
    <div class="test2">
      remove link
    </div>
  </div>
</div>

An alternative approach is to apply display: flex; to test with flex-direction: row;, then assign test2 a width of 7% while test occupies 93%. To create spacing, utilize gap. See the code snippet below for reference.

.test2 {
  width: 7%;
}

.test1 {
  border: 1px solid #000;
  margin-bottom: 10px;
  width: 93%;
}

.test {
  display: flex;
  flex-direction: row;
  width: 100%;
  gap: 10px;
}
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>
<div class="test">
  <div class="test1">
    some content
  </div>
  <div class="test2">
    remove link
  </div>
</div>

Answer №3

To make your test2 class work as intended, simply include the following CSS code:

display: flex; justify-content: flex-end
. For more clarification, refer to the image link provided: view image description

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

"An error occurs when attempting to clear Rad Grid data with javascript: htmlfile: Invalid argument thrown

Hello, I am currently developing an ASP.NET application. I am facing an issue where I need to clear the data in a Rad grid upon button click using JavaScript. The code snippet that I have attempted for this is as follows: document.getElementById(&a ...

immediate removal upon pressing the button

Struggling to remove data from a datatable when clicking the delete button for quick admin side action. My code isn't functioning properly, even after attempted fixes. Here's my code: <div class="table-responsive"> <table id="datas" cl ...

Having trouble with bootstrap carousel malfunctioning on Firefox browser?

I'm experiencing an issue with the carousel slider on my website. It seems to only be working in Chrome and not in Mozilla Firefox. You can view the live site at: Below is the code I have used for the carousel: <header id="myCarousel" class="car ...

Elements not being styled by CSS properties

I am facing an issue with a CSS file designed to theme a page in white on black, as opposed to the usual black on white. body { background-color: black; } h1 h2 h3 h4 h5 h6 p { color: white; } However, the color: white; property is not being applied ...

Tips for designing a sleek and seamless floating button

I attempted to implement a floating button feature that remains in view while smoothly flowing using jQuery. I followed a tutorial I found online to create this for my website. The tutorial can be accessed at this link. However, after following all the ste ...

Can a layer be sliced to create a text-shaped cutout?

I want to achieve a cool text effect where the background is visible through the letters. I have explored options with background images and colors, but I haven't found any examples where the underlying layer is revealed. Is this even possible? Imag ...

Error encountered while attempting to insert YouTube video with video.js player using an iFrame

I'm currently using video.js along with the vjs.youtube.js plugin to incorporate YouTube videos directly into the video.js player on my website. Everything is working smoothly and I am able to dynamically add videos to the page. However, I've en ...

Angular material tree with nested branches broken and in disarray

I'm facing a challenge with the UI issue of expanding trees on the last node, as it always creates excessive lines from the nodes, similar to the image below. I attempted to adjust the left border height but saw no change at all. Does anyone have any ...

Is there a way to utilize a single function on two separate div elements?

Looking for a way to optimize my code that contains the addRow() and deleteRow() functions. Currently, I have duplicated these functions for both radio buttons and checkboxes. Is there a more efficient way to achieve this? function addRow(tableID) { ...

The background image fails to show up on the mobile device display

Currently, I am working on an app with Ionic, but unfortunately, the background image is not showing despite all my efforts. I have gone through various solutions provided in previous questions, but none of them seem to work for me. Here is the CSS I am u ...

What could be causing my "onChange" function to not work as expected?

export function UpdateData(props){ return( <div> <div className='updateForm'> <form> <div className="form-group"> <label>Your ID:& ...

When using JavaScript, I have noticed that my incremental pattern is 1, 3, 6, and 10

I am currently using a file upload script known as Simple Photo Manager. Whenever I upload multiple images and wish to delete some of them, I find myself in need of creating a variable called numDeleted (which basically represents the number of images dele ...

Validate HTML 5 using Jquery assistance for best results

I have a form that includes HTML 5 Validations. I am looking to display a loader only when there are no error messages from the HTML 5 validation process. I have experimented with using the onclick event of the submit button and also attempted to trigger i ...

Utilizing the CSS grid framework to efficiently create repetitive rows and columns in code

When I work with CSS frameworks like Bootstrap or Materialize, I often find myself repeatedly typing the same HTML code: <div class="row"> <div class="col s12"> <!-- Some text/ a button / something --> </div> </d ...

The border color of the text input is not changing as expected when in focus

Is there a way to change the border color of a text input field to blue when it is not selected and then change it to orange when the field is selected? input[type="text"]{ border: 1px solid blue; } input[type="text"]:focus{ border: 1px solid ora ...

Exploring ng-view navigation in AngularJS

I have come across an SEO issue while working on a static website in AngularJS. Google Webmasters Tools report no crawl errors, but when I attempt to fetch different routes, it consistently returns the same home page result. It seems to be ignoring what ...

Unable to reset the HTML5 slider successfully

Despite multiple attempts, I have finally reached out for assistance. Here is the code snippet for my slider: <input autocomplete="off" type="range" min="0" max="100" value="50" class="myslider" id="mysliderID-slider" data-fieldid="something"> <i ...

Why does CSS respect height:auto for relative positioned parent elements but not width:auto?

Although CSS position properties may seem simple at first, when building a layout, tons of weird edge cases can come out of nowhere. One thing that I always overlooked was using height: auto or width: auto. To gain a better understanding, I stumbled upon ...

What is the best way to conceal a set of columns and reveal them upon clicking a header column?

Currently working with Bootstrap 3 and successfully split the top into two columns. I am aiming to replicate the design shown in the sample image without relying on JavaScript or jQuery. Upon clicking "Quick Info", there should be a new set of columns reve ...

"Which is better for maximizing the efficiency of an image grid: CSS or Jquery? What are the key

As a UX Designer looking to enhance my coding skills, I must admit my code may not be perfect. Please bear with me as I navigate through this process. I am in the process of revamping my portfolio website. The original seamless grid was created using a Ma ...