What is the best way to enclose text within a <div> using Bootstrap's form-control and dropdown styles?

Is there a way to automatically wrap @FormattedText if the selected items from a dropdown menu exceed the size of the div on my razor page?

I've already tried using word-wrap and white-space styles, but they didn't work as expected. The content keeps extending beyond the div and scroll bars are displayed. How can I make sure the content wraps within the div? Any suggestions would be greatly appreciated!

<style>
    .dropdown {
       word-wrap: normal;
       white-space: normal;
    }
</style>


<div class="form-control col-md-4 dropdown dropdown-toggle">
        @FormattedText

        <div class="dropdown-menu">
            @foreach (var item in Items)
            {
                <div>
                    <input type="checkbox" @bind="item.IsUpdated" />
                    <label for="@item.Name">@item.Name</label>
                </div>
            }
        </div>
</div>

List<string> selectedItems = new List<string>();

public string FormattedText
{
    get
    {
        selectedItems.Clear();

        selectedItems.AddRange(Items.Where(s => s.IsUpdated).Select(item => { return item.Name; }));
        
        if (selectedItems.Any())
        {
            return string.Join(",", selectedItems.ToArray());
        }
     }

    set
    {
        selectedText = value;
    }
}

Answer №1

Before we proceed, it's important to note that there is a mistake in your Bootstrap .dropdown structure. Referencing the examples provided on Bootstrap's website, you'll see that both .dropdown-toggler and .dropdown-menu should be at the same level within the .dropdown wrapper. Additionally, each item within the .dropdown-menu should have the .dropdown-item class.

To correct this issue, adjust the structure as follows:

<div class="form-control col-md-4">
    <div class="dropdown">
        <button type="button" class="btn btn-secondary dropdown-toggler"
          data-toggle="dropdown">
            @FormattedText
        </button>
        <div class="dropdown-menu">
            @foreach (var item in Items)
            {
                <div class="dropdown-item">
                    <input type="checkbox" @bind="item.IsUpdated" />
                    <label for="@item.Name">@item.Name</label>
                </div>
            }
        </div>
</div>

Your updated structure will resemble something like this:

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


The reason why the formatted text isn't wrapping within the button is due to the .dropdown-toggler style set to white-space: nowrap;. You can remove this style or utilize the Bootstrap utility class .text-wrap on the .dropdown-toggler

<div class="form-control col-md-4">
    <div class="dropdown">
        <button type="button" class="btn btn-secondary dropdown-toggler text-wrap"
          data-toggle="dropdown">
            @FormattedText
        </button>

    ...

</div>

Furthermore, if the dropdown extends beyond the border of .form-control, it's because .form-control has a fixed height. To resolve this, you can either set the height style to auto or use the Bootstrap utility class .h-auto:

<div class="form-control col-md-4 h-auto">
    <div class="dropdown">
        <button type="button" class="btn btn-secondary dropdown-toggler text-wrap"
          data-toggle="dropdown">
            @FormattedText
        </button>

    ...

</div>

Upon making these adjustments, the overall look should now resemble the following:

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

demo: https://jsfiddle.net/davidliang2008/3bykc4rf/14/

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

When the "x" close icon is clicked, the arrow should toggle back to 0 degrees

I've been tackling the challenge of creating an accordion and I'm almost there. However, I'm facing an issue where the arrow doesn't return to its original position after clicking the close "x" icon. The toggle works fine but the arrow ...

Include a parameter in the URL upon clicking on a Bootstrap Collapse element

I am looking to enhance the URL by adding an additional parameter when triggering a Bootstrap Collapse item. Currently, Bootstrap links to an anchor such as #collapseOne. This anchor remains hidden in the URL. Is there a way to include an extra parameter ...

What is the name of the syntax highlighting plugin utilized by the Bulma CSS framework?

Does anyone know the name of the code highlighter plugin that is featured on bulma.io? I have tried looking through all sources and scripts, but I can't seem to find the plugin's name. Attached is a screenshot for reference: https://i.sstatic.n ...

Button with a Jquery icon design

Hello, I am currently working on implementing an icon button that can collapse and expand text. Although I have successfully implemented the logic for collapsing/expanding, I am facing difficulties in creating the actual icon button. The theme I am require ...

Creating a bootstrap carousel configuration

I recently encountered a problem while using a single bootstrap carousel. The carousel contains multiple items/images to display, with one of them needing to be active initially. Below is the code snippet: <div id="myCarousel" class="caro ...

ways to incorporate audio into a hyperlink

Hello everyone, I am looking to incorporate a sound into my link. Can anyone suggest a jquery or javascript method to achieve this? I tried the following code but it didn't work: <div align="center"><a href="our_product.html" class="footerm ...

The problem arises when the body/html element is narrower than the browser window

I encountered a strange issue while working on a website for a client/design studio. Despite setting the body and html elements to a width of 100%, I noticed that when I resize the browser, a scrollbar appears forcing me to scroll right. Upon scrolling, I ...

jQuery method to extract only the text from a clicked "a" tag

I have generated a list of alphabet items like this: <ul class="search_a_m nav nav-tabs" id="getAlpha"> <?php foreach (range('A', 'Z') as $char) { $default = ""; $default = $char == 'A& ...

The dimensions of the cards adjust automatically when the flex direction is set to row

My cards, created in CSS and React.js, have a set height and width. However, when I change the flex direction from column to row, the cards automatically shrink in size. Why is this happening? Card's CSS and JS code CSS .card{ height: 50%; w ...

Would you be able to create a unique CSS code to style the web form application as needed?

Seeking guidance as a novice in this area and grateful for any help. I have utilized a form building tool (Granicus/Firmstep forms) to create a form, but I am struggling with the CSS to achieve the desired layout. While I can design the form exactly as I ...

Incorporating styling preferences within a directive's template

One of the directives I am currently working on looks like this: app.directive('MessageChild', function($timeout) { return { restrict: 'E', scope: { pos: '=?', msg: '=' ...

Tips for personalizing text and icon colors in the TableSortText element of Material-ui

My Goal: I aim to empower users with the ability to apply customized styles to my EnhancedTable component by utilizing a styles object containing properties like headCellColor, headCellBackgroundColor, bodyCellColor, bodyCellBackgroundColor, and more. The ...

Using the mouse scroll to activate the $anchorScroll effect in Angular instead of a click

I have been searching for a solution to my problem without any luck so far. Hopefully, someone here can provide guidance and help me find a solution. My goal is to replicate the behavior shown in the second example on the following page: https://docs.angu ...

What's the best way to align the hamburger menu to the left in Foundation CSS framework

My experience with SASS is still in its infancy, and I have encountered some issues. According to the documentation, I should make changes to _top-bar.scss by adding the line: $topbar-menu-icon-position: $default-float; (rather than $opposite-direction;). ...

Error message: "React component encountered invalid schema data for the dropdown selection."

In my endeavor to create a straightforward registration form using react and bootstrap, I included drop-down boxes for users to choose from various options. However, upon selecting an option from one of the drop-down lists and opening the app in a browser, ...

Troubleshooting issue: Failure in proper functionality of Jquery's slideDown

My form is divided into 3 sections, with the latter two initially hidden using CSS. When users click the next button in the first section, the second section is supposed to slide down into view. However, I'm experiencing an issue where the animation s ...

Using jQuery to locate a JavaScript variable is a common practice in web development

I recently created a JSFiddle with buttons. As part of my journey to learn jQuery, I have been converting old JavaScript fiddles into jQuery implementations. However, I seem to be facing a challenge when it comes to converting the way JavaScript fetches an ...

Tips for embedding a colorbar image into a select dropdown menu

Currently working on an HTML page that requires a dropdown menu to select between two different colorbars for a graph. The dropdown options should display the colormaps visually, so I am looking to include either: An image of the colorbar A gradient back ...

Creating a menu category for a restaurant using CSS

I'm trying to set up a menu with categories like this: enter image description here However, my result ends up looking like this: enter image description here The description section keeps stacking on top of the photo section; it's strange beca ...

Make sure that bootstrap is set to disregard the taller column heights

Is there a method to manipulate Bootstrap so that the first column of the second row is positioned just below the height of its corresponding column in the previous row? At present, each row adjusts its height based on the tallest column. <div class=&q ...