When utilizing bootstrap, encasing an input text element in a label results in the text becoming bold and not occupying the entire width

I have been experimenting with wrapping my text input fields in labels. While this technique works well with pickers on my form, it seems to cause issues when applied to text boxes. If I choose not to wrap the text boxes, the alignment between the label and text box appears off compared to other wrapped elements.

The following setup does not produce the desired result:

<div class="form-group">
    <label>
        Title:
        <input  type="text" class="form-control" data-bind="value: title" />
    </label>
</div>

However, this configuration works as expected:

<div class="form-group">
    <label>
        Date:
        <div class="input-group">
            <input type="text" class="form-control" placeholder="mm/dd/yy" data-provide="datepicker" data-bind="value  :date" id="Date" />
            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
        </div>
    </label>
</div>    

In addition, the content within the text inputs should remain in regular font, without any bold formatting.

https://i.sstatic.net/8TElZ.png

Answer №1

If you're looking to make some adjustments to your CSS, consider changing the display of your labels to block or adjusting their width to 100%. Additionally, you can style input elements within labels by setting their font-weight to normal.

label {
    display: block;
}
label input {
    font-weight: normal;
}

https://jsfiddle.net/user123/samplecode/

Answer №2

Instead of wrapping elements for activation, you can simply utilize the for attribute:

<label for="title">Title</label>
<input id="title" type="text" class="form-control" data-bind="value: title" />

If you prefer not to do this, then it's important to have a good grasp on cascading styles.

Answer №3

There is no need to enclose the input within a label tag. The 'for' attribute of the 'label' element achieves the same result you desire. To correctly create a form control in Bootstrap, refer to this example:

<div class="form-group">
  <label for="title">Title:</label>
    <input type="text" class="form-control" data-bind="value: title" id="title" />      
</div>

Why isn't the input taking up full width and why is the text bold? This is due to the CSS styling applied to the label by the Bootstrap library.

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}

In your case, if you nest the input within the label, the input inherits the 'font-weight: 700' property from its parent (label) making it bold. Additionally, the parent label's 'max-width: 100%' restricts the input width. If it had 'width: 100%', the input would occupy the full width. It is important to understand the distinction between 'max-width' and 'width' in CSS.

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

Reorganizing Arrays in Javascript: A How-To Guide

I have an array in JavaScript called var rows. [ { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f4f2e4f3b0c1e4f9e0ecf1ede4afe2eeec">[email protected]</a>' }, { email: '<a hre ...

Pass a data variable to an HTML file using express's sendfile function (quick inquiry)

Currently utilizing Node.JS, path, and express for running an HTML webpage. I need to pass a variable to the HTML file for its utilization: var client_cred_access_token = 'fakeToken'; ... app.get('/', function (req, res) { res.se ...

How to modify a value in a document within a MongoDB collection

I'm having an issue with updating the 'panel' field in both the cards collection and projects collection. Here is my code snippet: const project = await Project.findOne({"code":currentUser.groupcode}); // this works const ...

How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers ...

Create styles that reveal a div element upon hovering over a nested anchor link

When a user hovers over a link, I want to display a div box. For example: If there is an image inside a href, I would like a div to appear above it with the text 'Click here'. This is the React style code: a: { '& :hover':{ ...

Build an upvote feature using PHP and WordPress that allows users to vote without causing a page reload

Just starting out as a developer and currently working on a website using WordPress. I want to implement an upvote button that allows users to upvote a post without the page needing to refresh. I understand that I'll need to use JavaScript to highligh ...

What is the best way to choose a random image in a Django template?

I am currently facing an issue with selecting a random image in my Django template each time the page loads: <!--<img src="{% static 'images/640px-Waaah.jpg' %}"/>--> <img src="{% static 'images/Crying_Baby.jp ...

The negative z-index is causing issues with my ability to select classes using jQuery

By setting a z-index of -3 for several divs in my background, I thought they wouldn't affect the formatting of elements in the foreground. But now I'm facing an issue where I can't click on those background divs when trying to target them wi ...

The specified property 'XYZ' is not found in the type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'

Whenever I try to access .props in RecipeList.js and Recipe.js, a syntax error occurs. Below is the code snippet for Recipe.js: import React, {Component} from 'react'; import "./Recipe.css"; class Recipe extends Component { // pr ...

Exploring the possibility of detecting page scrolling in Javascript by clicking on scroll bars

I have implemented a unique feature on my page that allows users to scroll up and down using custom buttons I created. This functionality is achieved by smoothly transitioning between anchor points on the page using jQuery's animate function. However ...

Closing the nested accordion will collapse all sections within the accordion

I'm currently facing an issue with my nested accordions. I've been attempting to nest them in a way that doesn't require writing additional jQuery code for each new one added. As an example, I've created a jsfiddle... https://jsfiddle. ...

Numeric input of 10 not recognized in the textbox

My validation for a textbox only accepts two digits, such as 11 or 12. However, it does not accept the digit 10. Check out my code snippet: <td width="60" height="20" class="case_txt" align="center"> <input type="text" onblur="CheckUnitValue ...

What is the correct way to format the headings for sub-level pages, like chapters within a novel or sections within a lengthy article?

At times, the text and image content becomes too expansive to fit on a single page. However, preserving the context of the content is crucial, making it necessary to display it explicitly from a user experience perspective. This could include chapters of a ...

Expanding a class functionality by incorporating a method through .prototype

My goal is to define a class called "User" and then add a method to the class using the "prototype" keyword. I want the method "who_auto" to be accessible to all future instances of "User". When testing this code in JSFiddle, I encountered the error messa ...

Injecting variables into HSL color values within Three.JS

In my current code, I have set 2 HSL colors: scene.background = new THREE.Color("hsl(0, 100%, 50%)"); var light = new THREE.AmbientLight("hsl(180, 100%, 50%)"); I am attempting to pass a variable for 'hue' from my CSS. After ...

I require an HTML <select multiple> element that includes a disabled option

Can anyone help me figure out how to create a multi-select box with a disabled element? I want the box to have the following options: All ----or---- option 1 option 2 I don't want the "----or----" option to be selectable. Here's the code I&a ...

webpack - compile one TypeScript file separately (2 actions)

In summary... When my Vue files are combined with background.ts, webpack processes them to create bundled vue files along with background.js I'm unable to run the background.js script I expected background.js to only contain "console.log(' ...

Sort firebase information by chronological order based on timestamp

I'm currently working on sorting track IDs from firebase based on their timestamp (createdAt). The function is functioning correctly, but the ordering doesn't seem to work as expected. I'm not sure where the issue lies. Any assistance or sug ...

"jquery-ajax was unable to complete the request, whereas it was successfully executed using

I am currently using jQuery to trigger an ajax request. However, when I make the request using jQuery, I encounter an "Unexpected end of input" error with no response coming from the PHP file. Strangely enough, if I manually copy the request from the Chrom ...

Encountering an issue in Laravel when trying to retrieve data using relationships and the paginate() method

When I try to fetch data using paginate(10), Vue.js does not work. However, if I use paginate(5), it works fine. The code in the Controller with relationships in the model files is working fine and returns a 200 OK response. $results = Posts::with([' ...