What is the reason behind the changes in the CSS rendering behavior of Fieldset legends over the past few months?

A form on my website contains nested <fieldset> elements, each with a unique <legend>. Recently, I discovered an issue with the page rendering on various browsers (Chrome, Firefox, Opera) that was not present before.

One element,

<span class="undo">
, is supposed to display a green arrow when the surrounding <fieldset> has the class "modified", and remain hidden otherwise by adjusting its width.

The old behavior (still visible on Chrome OS 76.0.3809.136) looks consistent with the intended design:

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

The new behaviour (on Chrome 87.0.4280.88 on Windows 10) shows the "Foo configurator" text wrapping to two lines for unknown reasons.

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

I am curious if recent changes in browser rendering caused this unexpected behavior. If so, how can I resolve it?

Here is an excerpt of the HTML code:

<!DOCTYPE html>
<head><style type="text/css">
  form {
    max-width: 500px;
  }
  body {
    font-family: "Segoe UI", "Lucida Grande", Arial, sans-serif;
  }
  fieldset {
    padding: 0;
  }
  fieldset > legend {
    font-size: 90%;
    margin-left: 0.5em;
  }  
  span.undo {
    background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAABnRSTlMA/wD/AP83WBt9AAAACXBIWXMAAAsTAAALEwEAmpwYAAADEklEQVQ4jY2UXSxbYRjHn/ecnm5lLFTbxdIlbENTq0x8JD62RBHRm/lKCBIXJGw3jbhtuCERVy6wyDJEYhLbMuKiDYmwIRITESQNq1LZaFg5ynrannN2ceR1VrT7X53n/f/PL8/7CXxQzdnnGicbg2cESeAW8cD3f+83mo3qSDUPPAJ0W1IQ4nn++ijDMs2W5r7lPp7npaS0VFOqCFNoFBp9nD5BnvC/oAP3QeXHytnd2etpAhHZ6uzWl6158XkBPQaCln8uV4xV2E/swWaBUP3z+u6ibhkluxk0YZ2o/lzt9rqDULD0cfrxqvFwKlwoyba2Nuyp76s1MRqaoffoPY7n8LiMkoVRYQzLiEE7Jzs7rp0STQlCKBBEkZRWqa3V1VY/q5bL5I5Th...
    

Answer №1

It appears that if the issue is visible on Chrome 76, it could be due to the activation of LayoutNG in Chrome 77, which was released back in 2019. However, some aspects of this change may have been introduced later. Personally, I encountered some inconsistencies in textarea layouting and had to resort to a workaround until recently.

To confirm whether this is indeed the cause, testing with Chrome 77 would be advisable.

Upon closer inspection, it seems that the conflicting styles margin-right: -2px and width: 0em are causing the layout to overflow and trigger a new line.

My observation using BrowserStack revealed that the behavior changed in Chrome 86, coinciding with the implementation of flex and grid for fieldsets. It's possible that subtle adjustments were made to accommodate these features.

Answer №2

The use of negative margin is causing the legend's right border to move left by 2px, resulting in a reduction of the legend's width by 2px. Even when you apply width:0em to span.undo, the negative margin continues to affect the layout. However, this leads to the title text wrapping since legend now falls short by 2px.

To demonstrate this behavior, refer to the code comments.

* {
  font-family: "Segoe UI", "Lucida Grande", Arial, sans-serif;
  font-size: 16px;
}

/* Making legend behave like a block element with width fit-content */
div {
  border: 1px solid;
  width: fit-content;
}

/* Undo icon as inline-block */
span {
  display: inline-block;
}

/* Styling for title text */
span:first-child {
  color: green;
}

/* Applying negative margins to undo elements */
.negative-margin1 {
  margin-right: -20px;
}

.negative-margin2 {
  margin-right: -40px;
}

.negative-margin3 {
  margin-right: -49px;
}
<div class="one"><span>Foo efg </span> <span>undo</span></div>
<div class="two"><span>Foo efg </span> <span class="negative-margin1">undo</span></div>
<div class="two"><span>Foo efg </span> <span class="negative-margin2">undo</span></div>
<div class="two"><span>Foo efg </span> <span class="negative-margin3">undo</span></div>
As the negative margin affects the width, the inline text starts wrapping.
The visual output may vary slightly based on your font size, so here is the intended appearance:

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


To avoid using negative margin, we can make a minor adjustment by adding padding:

form {
  max-width: 500px;
}

body {
  font-family: "Segoe UI", "Lucida Grande", Arial, sans-serif;
}

fieldset {
  padding: 0;
}

fieldset>legend {
  font-size: 90%;
  margin-left: 0.5em;

  /* Setting padding-right to zero */
  padding-right: 0;
}

/* Adding 2px right margin for the title text */
span.ttl {
  margin-right: 2px;
}

span.undo {
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSU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I9NN70wGRIMIZ5aLJfHNW2bXnQsOmiHj/VFy6K1Sm1BfEGyMll8Y7H+AvtLjkoh3mWsAAAAAElFTkSuQmCC');
  display: inline-block;
  height: 13px;
  width: 1em;
  font-size: 13px;
  background-repeat: no-repeat;
  background-size: 13px;
  margin-top: 2px;
  
  /* Remove unnecessary margin */
  /*margin-right: -2px;*/ 
}

legend>span.undo {
  width: 0em;
}

fieldset.modified>legend>span.undo {
  width: 1em;
}

div.description {
  padding-left: 4px;
  font-size: 90%;
}
<form>
  <fieldset class="field">
    <legend title=""><span class='ttl'>Foo configurator</span><span class="undo" title="Restore default"></span></legend>
    <label title="">
    <input name="foo_type" type="radio" value="123">Foo #1</label>
    <div class="description">First foo</div>
    <span class="field-content">
      <label><input name="foo_enable" type="checkbox" value="value">Enable</label>
      <span class="undo" title="Restore default"></span>
    <div class="description">Zoppity</div>
    </span>
  </fieldset>
  <fieldset class="field modified">
    <legend title=""><span class='ttl'>Bar configurator</span><span class="undo" title="Restore default"></span></legend>
    <label title="">
    <input name="bar_type" type="radio" value="123">Bar #1</label>
    <div class="description">First bar</div>
    <span class="field-content">
      <label><input name="bar_enable" type="checkbox" value="value">Enable</label>
      <span class="undo" title="Restore default"></span>
    <div class="description">Zoppity</div>
    </span>
  </fieldset>
</form>

Instead of relying on negative margins, adjusting the padding on the parent element can achieve the desired spacing.


In the current Chrome version (100.0.4896.88), default CSS properties assigned to legend elements are as follows:

legend {
  display: block;
  padding-inline-start: 2px;
  padding-inline-end: 2px;
  border-width: initial;
  border-style: none;
  border-color: initial;
  border-image: initial;
}

We should verify if these settings differ in earlier versions.

Answer №3

The cause behind the change in behavior remains unknown to me.

However, you can rectify this behavior by incorporating

white-space: nowrap;

Into your legend element, or

legend > span.undo { display: none; }
fieldset.modified > legend > span.undo { display: inline-block; }

It seems that removing the negative margin also helps:

span.undo { margin-right: 0; }

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

How can you eliminate a line from the HTML input mask in Gravity Forms?

With Gravity Forms, I am utilizing the "input masks" feature to prompt users to enter data in a specific format. However, it seems to be creating an unwanted line within the input field itself as shown in the image below: https://i.stack.imgur.com/8gQ3K.j ...

Struggling to align the footer of your webpage and ensure it adjusts proportionally with the window size?

After trying several codes that have worked before, I ran into a problem when attempting to place two div side by side for the footer. No matter what code I tried, I couldn't get it centered and responsive to the user window ratio. Even after consulti ...

Using jQuery to create a seamless transition in font size as you scroll

Currently, I have a jQuery animation function in place to adjust the font size of the .header-wrap text when the document is scrolled beyond 50px. While this method does work, I am not completely satisfied with it as the transition is not very smooth. Idea ...

Tips on how to showcase a picture with the focus on the center by enlarging it within

We have a long frame on the page, with an aspect ratio of about 3.5:1 width to height. However, most of the photos being displayed are in a 4:3 ratio, which is larger and not a proper fit for the frame. The customer really wants the long frame and has no ...

The menu-item is unable to be centered in its location

I've exhausted all my efforts, but still can't seem to center this menu-item. Here is the code I've been working with: <div class="nav nav-tabs mb-2" id="nav-tab" role="tablist"> <a class="nav-item nav-link active" id="na ...

Positioning a div absolutely within a targeted div

Is there a way to set a div as absolute within a specific relative div rather than just the parent? For instance, I have a div that's nested inside of a row. However, I would like it to be positioned absolutely in the section instead of the row. Both ...

How can JavaScript be integrated with Django static URLs?

I have a Django application where I store static images on Digital Ocean Spaces. Displaying these static images in my template is simple:<img>{% static 'images/my_image.png' %}</img> When I inspect the HTML page after loading, I see ...

Is there a way to replicate the Vista Command Link in HTML?

This particular dialog mimics the design of a Windows Vista Command Link: View image here: http://i.msdn.microsoft.com/Aa511455.commandlinks01(en-us,MSDN.10).png I'm curious if anyone has created HTML code that replicates the appearance and function ...

The hamburger icon is not visible on smaller screens when using the Bootstrap 5 navbar

'The stylesheets for Bootstrap CSS and JS have been included in the file. The content shows up fine with default colors, but not when I customize them.' ''' <li class="nav-item"> <a class= ...

Chrome behaving differently with CSS for pop-out menu

My issue lies with a pop-out menu behaving differently in Chrome compared to IE or Firefox. Below is the HTML code: <body> <ul> <li><a href="url">Level One</a> <ul> <li><a href="url">Le ...

Activate SVG graphics upon entering the window (scroll)

Can anyone assist me with a challenging issue? I have numerous SVG graphics on certain pages of my website that start playing when the page loads. However, since many of them are located below the fold, I would like them to only begin playing (and play onc ...

What is the best way to eliminate the space between two paragraphs?

Check out my awesome image! I need some help with formatting HTML data into text. Can anyone advise me on how to eliminate the space between two paragraphs? func processGetDescriptionResponse(json: JSON) { let status = json.dictionaryObject!["statu ...

Thymeleaf: Automating the Adjustment of Content Fragments

In my project using Spring MVC and Thymeleaf, there is a default fragment that sets up the page structure. Here's what it looks like: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/ ...

Tweaking the placement of the dropdown menu in the subnavigation area

Just getting back into coding after a long hiatus - I've dabbled in the past but it's been about 6 years since I've worked with any code. Currently, I'm working on replicating a website for practice. I have created a nav bar and a drop ...

How can one go about incorporating the feature "updating list upon clicking a label" on a webpage?

On my website, I currently display a comprehensive list of publications. However, I am looking to organize these publications by various research topics using labels. Ideally, when clicking on a specific label, only the papers related to that topic will be ...

Problems arising from positioning elements with CSS and organizing list items

I've been working on creating a navigation sidebar using Angular and Bootstrap, but I'm having trouble getting my navigation items to display correctly. APP Component HTML <div class="container-fluid"> <div class="row" id="header"&g ...

The div content is aligning at the baseline rather than the middle

The social icons on my website are currently displaying at 40x40, with the text appearing below them. I am looking to center the text within the icons themselves. Any tips on how I can achieve this? Your help is much appreciated! Check out the fiddle here ...

Adjust the size of an HTML image for printing using a JavaScript window.print() function

On my website, I have a print option set up where specific elements are hidden using @media. How can I adjust the size of an image within the @media query when my JavaScript print function is triggered? I am able to modify a regular div element easily, but ...

Preventing Text Truncation in THREE.js Texture Created from 2D Canvas

Many people tend to use the 2D canvas texture method for creating text billboards, sprites, and overlays in THREE.js scenes instead of relying on imported image files. You can check out an example by Lee Stemkoski here. However, I have noticed that when a ...

How can I trigger a CSS animation to replay each time a button is clicked, without relying on a timeout function?

I am having trouble getting a button to trigger an animation. Currently, the animation only plays once when the page is refreshed and doesn't repeat on subsequent clicks of the button. function initiateAnimation(el){ document.getElementById("anima ...