Align a button to the left or right based on its position within the layout

On the left side, there is dynamic text and on the right side, a button is displayed as shown below:

      <div class="dynamic-text">
        {{ dynamicText }}
      </div>

      <div class="some-button">
        <cv-button
          id="some-btn"
          kind="tertiary"
          size="default"
          :icon="someIcon"
          @click="someBtnFunction"
        >
          {{ someBtnLabel }}
        </cv-button>
      </div>

A specific style is applied to it:

    .some-button {
        float: right !important;
        padding-bottom: 2rem;
    }

The current issue observed is that when the window is narrowed, the button moves under the text and is still aligned to the right side. The requirement is that when the text and button are on the same line, the button should align to the right, but when the button is below the text, it should be left-aligned. Since the text's length varies dynamically, the button's position changes at different browser widths based on the text content.

Do you have any suggestions or ideas on how to achieve this behavior?

It is important to note that Carbon and Vue are being used for development. Thank you in advance!

Answer №1

If you're looking for a simple way to achieve this, consider using Flexbox. By wrapping your two elements in a flex container and utilizing the justify-content and flex-wrap properties, you can easily achieve the desired layout.

<div class="flex-container">
  <div class="dynamic-text">...</div>
  <div class="some-button">...</div>
</div>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

With this setup, the button will align to the right when on the same line as the text. When there's not enough space, it will wrap to the next line and stick to the left.

https://i.stack.imgur.com/MpsuD.png

https://i.stack.imgur.com/ppkzy.png

https://i.stack.imgur.com/MD3sS.png

You can remove the float: right; from the button div as it is no longer needed. Additionally, you can adjust the align-items property for vertical alignment. If you're new to Flexbox and want to learn more, check out resources like the CSS Tricks Flexbox Guide and Flexbox Froggy.

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

The issue arises from the fact that the Bootstrap modal fails to open automatically

Having trouble getting my bootstrap modal to open on page load, even though the button to trigger it is working fine. Here is my code: <div id="myModal" class="modal fade" role="dialog"> <div class=" ...

Ensure that radio buttons are positioned next to their respective labels on separate lines

My HTML form is structured with sections as described below: I prefer having the labels inline to prevent the section from being excessively tall, but I'm facing an issue where the radio buttons are not staying aligned with their respective labels. ...

My node.js code is not producing the expected result. Can anyone point out where I may be going wrong?

I've been working on a BMI calculator where I input two numbers, they get calculated on my server, and then the answer is displayed. However, I'm having trouble receiving the output. When I click submit, instead of getting the answer, I see a lis ...

The HTML anchor tag paired with the italic tag is a powerful combination for

Here is some example code: <a href="#"> <i class="some-bg" /> Some Text </a> Along with some Javascript: $("a").bind("touchstart", function (e) { e.preventDefault(); console.log("Tag: " + e.target); console.log("Tag Nam ...

Display elements exclusively when the class XY is in an active state using JavaScript

Hello everyone, I'm new to this platform and excited to share my first post. Currently, I find myself facing a major challenge as I navigate through a bootcamp program. I have been working on a website with different sections that require specific fu ...

Jquery doesn't immediately hide an element after the first click.TabPage.getSelection().extentNode rectangular_HTML

I'm experiencing a peculiar issue with an event listener $(document).on('click', '.suggested-location-item', function(event) { event.preventDefault(); $('#IDsuggestedLocationsList').html(''); $(&apo ...

Implementing JWT authentication in a Nuxt.js application

I'm currently facing an issue with JWT authorization in my Nuxt project. I am looking to restrict access to certain routes for users who do not have a JWT token. While middleware is the recommended place to check authorization in Nuxt, I've encou ...

Is it possible to incorporate Vue and Vuetify into an existing project that requires IE compatibility?

Currently in the process of enhancing a legacy project with new functionality. The front end is currently relying solely on jQuery for all the webpages. I have been tasked with adding another webpage and would like to incorporate Vuetify + Vue due to the i ...

Show Cooking Ideas on the Website

This particular project is focused on showcasing various recipes and facilitating multiple actions such as addition, deletion, and modification of these recipes. However, I encountered an issue where the recipes were not being displayed in the browser desp ...

Creating a loading screen in Angular2: Step-by-step guide

How can you best integrate a preloader in Angular 2? ...

Can Nuxt accurately identify which page is accessing the /slug path?

In my project, I have a post page, a category page, and the usual internal pages all set up. Here's the current structure in the pages folder: pages/category/_id.vue - Sends request for a category pages/post/_id.vue - Sends request for a post pages/p ...

What is the best way to send an HTML form variable to a Perl script using AJAX?

My goal is to pass the HTML variable from the list menu to the Perl script using POST. However, when I try to do this, the jQuery ajax() function executes the Perl script without including the value obtained from document.getElementById. Below is the sni ...

Searching for elements by tag name in JavaScript

Recently, I attempted to create JavaScript code that would highlight an element when a user hovers their cursor over it. My approach involves adding an event listener to every child within the first "nav" tag in the current document: let navigation = docum ...

html text alignment in the center

Is there a way to align text in the center without shifting the starting position of the first letter? before text 1 text text text 2 text text after ...

Guide to playing a downloaded video file in Ionic Capacitor by accessing the file's URI path

I recently created a repository on GitHub based on a blog post by Josh Morony that explores obtaining camera or gallery images and displaying them on a webpage. While the functionality works seamlessly on iOS, I have encountered issues with it not function ...

Struggling to get my Vue.js unit test to pass with test-utils

I'm currently testing a specific part of my Heading.vue component <v-layout column align-center justify-center> <img src="@/assets/images/logo.png" alt="Logo-Choro-des-Charentes" height="200"> <h1 class="mb-2 disp ...

Seeking a light-weight, text-rich editor specifically designed for use on our enterprise website. This editor should be even lighter than TinyMCE

I am in search of a lightweight text editor for an enterprise website, lighter than tinymce with basic buttons for the comment form. It is imperative that the editor also functions properly in IE6. So far, I have tried cleditor 15KB, but it has an issue in ...

Adding a combination of HTML and Javascript to an element can result in unexpected behavior

http://jsfiddle.net/PeKdr/ Encountering an issue when having a JavaScript variable that contains both HTML and JavaScript, resulting in unexpected behavior in the output window. Neither of the buttons - one triggering the function appendTheString() or the ...

Managing the transition of the object in question

I am in the process of designing a navigation bar. I have created an array to store the information of the tabs present in the nav bar. tabs: [ { name: 'All',id: "dash.courses.all", to: 'all', curre ...

Assigning the Style property to an element using a string that includes HTML tags

My HTML string is populated with elements such as button, li, span, and more. I am looking to add specific styles to buttons based on their class name. For example, if button.btn { some styles } and button.btn-success { some other styles } O ...