Implementing text wrapping in a dropdown select menu

Is there a way to adjust the text wrapping inside an option menu so that the option value matches the width of the select picker? Short text looks fine, but long text causes issues.

I've attempted various solutions on this platform without success. I am utilizing bootstrap classes and have not added any extra css yet.

Select option menu with short text

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

Select option menu with long text

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

<div class="form-group col-md-5">
                        <label for="programstrategis_id">Program Strategis</label>
                        <select id="programstrategis_id"
                            class="selectpicker data-content form-control @error ('programstrategis_id') is-invalid @enderror"
                            name="programstrategis_id">
                            <option selected disabled>--silahkan pilih program strategis berikut--</option>
                            @foreach ($programstrategis_id as $dataprogramstrategis)
                            <option value="{{ $dataprogramstrategis->id }}">{{$dataprogramstrategis->program_strategis}}
                            </option>

                            @endforeach
                        </select>
                        @error('programstrategis_id')
                        <div class="invalid-feedback">
                            {{ $message }}
                        </div>
                        @enderror
                    </div>

                    <div class="form-group col-md-5">
                        <label for="sasaran_id">Sasaran</label>
                        <select id="sasaran_id"
                            class="selectpicker form-control @error ('sasaran_id') is-invalid @enderror"
                            name="sasaran_id">
                            <option selected disabled>--silahkan pilih sasaran berikut--</option>
                            @foreach ($sasaran_id as $datasasaran)
                            <option value="{{ $datasasaran->id }}">{{$datasasaran->sasaran}}</option>
                            @endforeach
                        </select>
                        @error('sasaran_id')
                        <div class="invalid-feedback">
                            {{ $message }}
                        </div>
                        @enderror
                    </div>

Answer №1

I have implemented bootstrap classes and have not added any extra CSS yet.

When utilizing bootstrap, consider creating dropdown menus using <div> or <a> tags instead of <select>. This method allows for easier text wrapping.

To customize the dropdown width, you can adjust the default width of the dropdown-menu class and update the white-space property for all child items under .dropdown-item.

.dropdown-menu {
  width: 170px;
}

.dropdown-menu .dropdown-item {
  white-space: normal;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a7a657a7a6f782460794a3b243b3c243a">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here Something else here Something else here Something else here Something else here Something else here Something else here</a>
  </div>
</div>

Answer №2

Learn how to use CSS to manually wrap text as needed. The options for the wrapped text are set to 'disabled', allowing only the 'titles' to be selected, but this restriction can be lifted if necessary.

    <select style="width: 150px;">
    <option selected="selected" disabled>Wrap Demo</option>
    <option>-- All --</option>
    <option>Title A</option>
    <option disabled>&nbsp;&nbsp;Lorem ipsum dolor sit amet, consectetur adipiscing elit, </option>
    <option disabled>&nbsp;&nbsp;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </option>
    <option>Title B</option>
    <option disabled>&nbsp;&nbsp;Lorem ipsum dolor sit amet, consectetur adipiscing elit, </option>
    <option disabled>&nbsp;&nbsp;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </option>
    <option>Title C</option>
    <option disabled>&nbsp;&nbsp;Lorem ipsum dolor sit amet, consectetur adipiscing elit, </option>
    <option disabled>&nbsp;&nbsp;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </option>
    </select>

https://i.sstatic.net/hkyf1.jpg

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

Change the element's color to match the default anchor link

Can CSS be utilized to match an element's color with the default <a> color? If affirmative, what is the method? ...

What is the best way to align a Font Awesome icon within an SVG circle?

Can anyone help me position the trophy inside the circle, just below the number 2000? <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7974746f686f697a6b5b2e352835 ...

Is there a way to generate unique authentication numbers daily without the need to manually adjust any code

I am building a web application for commuters using vuejs and firebase. My goal is to implement the following feature: The employer provides the employee with a unique authentication code daily, which the employee (user) must enter when clicking the "go t ...

Thymeleaf is essential for proper functioning of external CSS stylesheets

I am puzzled about why I need to include the thymeleaf in both my HTML tags and in the link to my external CSS stylesheet in the head section. If I remove any of it, the stylesheet becomes disconnected from the HTML document. So, what is the necessity of ...

CSS: Display the index of each ordered list item within a child element

I'm working on creating a unique accordion-style widget similar to the design shown in this image: https://i.sstatic.net/EBymf.png My plan is to utilize an ordered list where each <li> element will include a question, answer, and an expand/col ...

Why is this dropdown list extending beyond the window?

Why is my dropdown list extending beyond the window? Despite trying to change the position of the dropdown list elements to fixed and top:0px, I am still facing this issue! I am unable to figure out why the list is going off the page. Your help in solvin ...

Utilizing Javascript to work with an xPath XML file that contains namespaces

I've been through a mountain of research and I'm still stuck at a dead-end :( Check out my XML file (test.xml): <bookstore> <book genre="autobiography"> <title>The Autobiography of Benjamin Franklin</title> ...

The background-size CSS property may experience malfunctioning on iOS devices when using Safari browser

My website has a body background size set, which works well on Android and Windows devices. The background size is intended to be 85% width and 5% height of the body. However, when loading the site on an iPhone, the height appears much larger than 5%. It s ...

Implement an innovative solution to automatically close the navbar component in tailwindcss on

I've been attempting to create a unique tailwind navbar component for React, but I've encountered issues when trying to make it close on scroll for mobile view. I found the initial code on this website. After that, I tried implementing the code ...

Adjusting the rotation transform by deleting and reapplying canvas is causing the chart to not display data after redrawing

I am facing a challenge with my previous issue and I am exploring different solutions. One approach I am considering is to remove the canvas element completely and then re-add it, effectively resetting all rotations on the canvas. Although this method alm ...

Show/Hide sections of content that transition and rearrange the layout of the page

I have a layout with 9 squares on a page. When clicking on a square, I want a full-width div to appear underneath it, pushing the other squares down. I've tried following a tutorial to understand the JavaScript behind this functionality, but I'm ...

Stylize the final element within a webpage using CSS styling techniques

In the code snippet below, my goal is to apply a specific style to the content of the final occurrence of "B". <div class="A"> <div> I am <span class="B">foo</span>. </div> <div> I like <span class="B"> ...

Use PHP to create a new JSON file on the server by submitting a form, then utilize a second form to update

My current goal is to create a json file using a form and then update that file with another form. Both of these processes are handled in the process.php file. I have managed to successfully update the json file if it is named as data.json initially in pro ...

Looking to create an HTML page for a Thermal Printer within an ASP.NET web forms application

I am working on my application using asp.net web forms and need to print receipts on a thermal printer. Currently, the receipt prints on a normal A4 size page, but I want it to also print on a thermal printer. How can I achieve this? Should I redesign the ...

Using node appendChild() in HTML for animating elements

As someone who is brand new to the world of web development, I am trying my hand at creating a webpage that expands as a button is clicked. Recently, I stumbled upon this helpful link which includes some code: The HTML code snippet is: <ul id="myList" ...

Panel with Bootstrap Collapse feature causes a slight movement when padding is applied

After applying the collapse behavior to a panel element, I noticed that the animation stops abruptly at the padding of .panel-body, causing it to snap instantly to height. This issue can be observed in the basic example on codepen: http://codepen.io/FiveSi ...

Position multiple divs at the bottom of aligned divs

I'm currently facing issues with aligning divs at the bottom within multiple vertically aligned divs. <p>Top of page</p> <div id="container"> <div class="message">area1</div> <div class="message">area2</ ...

What is the method for creating a new line in a text box using "Shift + Enter"?

I've come across a few discussions regarding the differences between text area and text box, but I'm still unsure about it. Also, I am not sure how to create a new line in a text box by using "Shift + Enter." Thank you to everyone who can help m ...

Utilizing the nth-child selector to apply border-spacing to all rows except the first and last

I've been attempting to add border-spacing to all rows except the first and last ones using nth-child, but for some reason, it's not having the desired effect. Can anyone point out what mistake I might be making? table { border: 1px solid blac ...

Only displaying sub items upon clicking the parent item in VueJS

I'm in the process of designing a navigation sidebar with main items and corresponding sub-items. I want the sub-item to be visible only when its parent item is clicked, and when a sub-item is clicked, I aim for it to stand out with a different color. ...