Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

HTML & CSS Set minimum with for HTML <dialog> ?

cbreemer

Bronze Coder
I am having fun with the HTML <dialog> element. I like that it adapts its size to fit snugly around its contents, however this can result in a ridiculously narrow element when there is little content. For that reason I want to set a minimum width for a dialog, but I cannot find how to do it. I tried the CSS minWidth attribute but that does not work (it only limits shrinking when you resize the browser window).
Any ideas appreciated 😊
 
Solution
For the record, I have enforced a minimum size by padding the header text (the first <div> in the dialog) with the necessary number of &nbsp; characters.
There is not a built-in CSS property to control the dialog's minimum width like you asked.

HTML:
<dialog id="myDialog">
  This is a dialog content!
</dialog>
<button onclick="document.getElementById('myDialog').showModal()">Open Dialog</button>

CSS:
#myDialog {
  min-width: 300px; /* Set minimum widht here */
}

JavaScript:
function enforceMinWidth(dialogId) {
  const dialog = document.getElementById(dialogId);
  const minWidth = dialog.style.minWidth; 

  if (dialog.offsetWidth < parseInt(minWidth, 10)) {
    dialog.style.width = minWidth;
  }
}

document.getElementById('myDialog').addEventListener('show', function() {
  enforceMinWidth('myDialog');
});

I did not test the code. just wrote it here so it works maybe. You might need to adjust something a bit. But gives you idea how to approach.
 
Thanks @EkBass, that is a clever and useful idea.
I tried it but unfortunately it does not work, the event listener is never fired. No surprise as the documentation says

HTML DOM Events​

EventOccurs WhenBelongs To
A <menu> element is shown as a context menuEvent

I cannot find an event that I could use for this purpose 🤔
Edit - I guess I could use the IntersectionObserver API. Will try that out.
 
Last edited:
IntersectionObserver does the trick, based on your idea.

JavaScript:
    . . .
    const observer = new IntersectionObserver(applyMinWidth, { threshold: 0 });
    observer.observe(dialog);
    dialog.showModal();
}

function applyMinWidth()
{
    const minWidth = _d.style.minWidth;
    if (dialog.offsetWidth < parseInt(minWidth, 10))
        dialog.style.width = minWidth;
}

The only slight drawback is that the original narrow dialog (which unfortunately does not have the CSS styling applied yet) briefly flashes before it is set to the minimum width. It does not seem to matter what value of threshold I use. It's an ugly visual effect, so I may want to prefer seeing the narrow dialog.
 
Last edited:
Back
Top Bottom