When there are multiple elements inside a bootstrap button, it causes the button to lose its

I've encountered an issue with the bootstrap button. Whenever I insert 2 elements like div or span inside it, the little arrow within the button ends up below the button text. It seems to be caused by a float attribute which then arranges the elements vertically.


<!--HTML Part-->
<div class="btn-group">
<button type="button" class="btn btn-outline-white dropdown-toggle" data-toggle="dropdown>
<span class="english">Site Language</span><span class="farsi">زبان سایت</span>
</button>

/*CSS Part*/
.english {display: none !important;}

.farsi {
        display: none !important;
        direction: rtl;
        font-family: 'Lalezar';
        text-align: right;
       }

.english:lang(en), .farsi:lang(fa) {
        display: block !important;
        }

-

//JS Part
<script>
function changelang2fa() {
  document.getElementsByTagName("HTML")[0].setAttribute("lang", "fa");
}

function changelang2en() {
  document.getElementsByTagName("HTML")[0].setAttribute("lang", "en");
}
</script>

Usually, the button appears as:

https://i.sstatic.net/MrItd.png

But when there's a span tag inside, the style becomes deformed:

https://i.sstatic.net/FF4CN.png

P.S. : I plan on making my website bilingual (English and Farsi)

Answer №1

Instead of delving into alternative methods to achieve your goal, let me point out where you went astray in your code. The <span> elements should have a display:inline property, which will position the down arrow right next to it. By using display:block as you did, the arrow ends up appearing on the next line.

.english {display: none !important;}
.farsi {
        display: none !important;
        direction: rtl;
        font-family: 'Lalezar';
        text-align: right;
       }

.english:lang(en), .farsi:lang(fa) {
        display: inline !important;
        }
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<body>
<!--HTML Part-->
<div class="btn-group">
  <button type="button" class="btn btn-outline-white dropdown-toggle" data-toggle="dropdown">
<span class="english">Site Language</span><span class="farsi">زبان سایت</span>
</button></div>
</body>
</html>

Answer №2

Why waste time trying to reinvent the wheel when Bootstrap 4 has already perfected all the "wheels" you'll ever need? Use a proper Bootstrap 4 drop-down button for a sleek and efficient design.

Check out this functional code example below:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="dropdown">
    <button class="btn btn-outline-white dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Site Language
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">English</a>
        <a class="dropdown-item text-right" href="#">زبان سایت</a>
    </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

Avoid activating jQuery functions based on certain screen widths for menu/navigation system

Recently, I've delved into the world of jQuery and attempted to create a simple menu system. The menu is designed to expand its submenu when hovering over the list item at screen widths larger than 480px, and to expand when clicking on the list item a ...

CSS :empty selector failing to target elements

I have a situation where I utilized the :empty selector within the .error class. However, I'm encountering an issue where even if there is no content inside the div with the error class, the error class does not get removed entirely. Upon examination ...

Preserve the location of a moveable div using jQuery

I have implemented draggable divs in my JSP page, allowing users to move them around freely. After dragging the divs, I would like to save their positions either in a cookie or database for future reference. Could you please advise on the best way to ach ...

Dealing with the challenge of sorting and editing one div at a time

I'm encountering an issue with my script where the input textbox becomes uneditable when I use sortable for a div element. In my code, I have drag and drop functionality where after dropping, the div should be sortable and the input should be editabl ...

Having trouble with the rendering of the Stripe Element Quickstart example

Currently, I am diving into the world of Stripe's Element Quickstart. Take a look at this fiddle that I have been working on. It seems to be quite different from the example provided. Although I have included the file, I can't seem to figure out ...

Is it possible for an <input> tag in PHP AJAX to have an "action" attribute similar to a <form> tag?

Forms typically use an action attribute to specify where the data should be posted, such as a .php file. Do <input> elements have this action attribute as well? The answer is likely no, but I am curious to understand what concept I may be misunders ...

In Bootstrap 4, toasts are like mysterious entities that stubbornly refuse to be closed,

I am aiming to implement floating toasts over my content in the corner of the screen. I am using asp.net MVC core 2 with Bootstrap 4 already integrated. All my other Bootstrap features are functioning correctly. In order to achieve this, I have referred t ...

Leveraging Jquery to Retrieve the Value from a Button with an Embedded Span

Below is a button with some text: HTML: <button><span>fdsfsd</span>Result<span>aasd</span></button> Is there a way to retrieve the value of "Result" from the button above using jQuery? I need to be able to change thes ...

Automatically selecting and fetching checkbox values using JavaScript

I was struggling with coding a function that involved generating checkboxes using JavaScript. My goal is to automatically select the elements "March" and "September" from the array target[] and display them as checked in the text area. So, "March" and "Se ...

Troubleshooting: Button Functionality Issue in HTML and Javascript

Currently, I am in the process of completing my final project for an introductory web development course. While I am very new to javascript and html, the project itself is a simple Mad Libs task. Throughout the day, I have encountered numerous issues attem ...

Choose Selectize.js to auto-populate inputs

I am currently using selectize.js for my project: $("#abc").selectize({ valueField: 'name', labelField: 'name', searchField: ['name'], plugins: ['remove_button'], createOnBlur: true, ...

Horizontal alignment of Inline Block Elements is not achieved

Having trouble with the alignment of a form on a test site? Check out the form located under the div#search-bar. If you inspect the element, I have applied the following CSS: div.nf-field-container { width: 201px; display: inline-block; margin: ...

How come the `table-fixed` class in Bootstrap isn't converting my table into a scrolling one?

I am having some trouble with making my table scrollable. I have around 550 rows in the table and I am using the `table-fixed` style from bootstrap, but it doesn't seem to be working as expected. Additionally, the `table-condensed` class is not regist ...

Ways to distort this shape using CSS?

I've been attempting to replicate this specific block layout, but I can't seem to get it right according to the design. Can anyone provide guidance on how to achieve it? Here is the desired block: https://i.stack.imgur.com/ud0HM.jpg Here's ...

Define the content and appearance of the TD element located at the x (column) and y (row) coordinates within a table

In my database, I have stored the row and column as X and Y coordinates. Is there a straightforward way to write code that can update the text in a specific td element within a table? Here is what I attempted: $('#sTab tr:eq('racks[i].punkt.y&a ...

What is the solution for aligning a button to the right instead of the left in Bootstrap?

I'm having trouble aligning my div next to my label in @EditorFor(model=>model.CourseName). The problem is that my @Razor code is showing below the label instead of next to it. Here is the code snippet: <div class="row"> ...

problem with saving session data

I am attempting to access data from another page using session storage. On my initial page, named home.html function go_to_faq(qnum){ window.open('FAQ.html', '_blank'); sessionStorage.setItem('key2', qnum); } <a s ...

Extracting targeted information from an HTML page using Python: A step-by-step guide

I'm a beginner in python and I need to scrape information from an HTML text file using python 2.7. The example below shows the structure of one firm's data in the HTML file. The same structure is used for all other firms as well, with each firm ...

Can someone assist me in figuring out how to solve selecting multiple radio buttons at once

<script type="text/javascript"> let x = "1.html"; let y = "2.html"; function redirectPage(form){ for(let i=0; i<form.length; i++) { if(form.answerq[i].checked && form.answerw[i].checked && f ...

Import css background-images from a separate local directory that is located outside the root directory of the Next JS

I am facing a challenge with hosting a next.js app within the file structure of a parent website. I need the CSS in the app to use images that are located outside the app's file structure but within the parent website's file structure. Here is a ...