How do I open my jQuery modal as wide as my elements but no wider?
I'm using JQuery 1.12.4. I have the following DIV, which I intend to open as a dialog.
<div id="loginBox" style="display:none">
<div>Login/Sign Up</div>
<div id="loginLogos">
<a href="/auth/google"><img border="0" alt="Google" src="http://www.racertracks.com/assets/google_plus_icon-8d7622763257233cf58e0465806f58d7c4b82b85271c2d03d0e02b2fadc4ac76.jpg"></a>
<a href="/auth/facebook"><img border="0" alt="Facebook" src="http://runtrax.devbox.com:3000/assets/facebook-b74d7c49fd91aa6ca76518bf46c7b75fa3b23f47028bea4b2ffaa39f5776dd91.png"></a>
<a href="/auth/twitter"><img border="0" alt="Twitter" src="http://runtrax.devbox.com:3000/assets/twitter_icon-7dbf8db24c3ad328ea28cba340e4f53e033b64b149850324822cdb622d77f331.png"> </a>
</div></div>
What I'm noticing is that when I reduce the width of my screen to around 320 pixels (to simulate a mobile browser width), the dialog is opening wider than the total width of the elements — there is a lot of white space to the right of the images. See the Fiddle here -- https://jsfiddle.net/g4a60Lq7/3/ . How can I make the dialog open as wide as the elements and no wider? Below is how I'm opening the dialog …
var opt;
opt = {
autoOpen: false,
modal: true,
width: 'auto',
dialogClass: 'noTitle',
focus: function() {
return $(this).dialog('option', 'width', $('#loginBox').width() + 50);
}
};
$("#loginBox").dialog(opt);
return $("#loginBox").dialog("open");
Edit: Wanted to post the image in response to pritishvaidya's second plunker ...
You can try this jsfiddle: https://jsfiddle.net/43f5qjc8/7/. Since the image source is not very reliable, here is another jsfiddle with smaller images from another source: https://jsfiddle.net/43f5qjc8/13/.
It involves some Javascript to calculate the minimial width needed to display the content, which may wrap on several lines. If there is a way to get the same result with CSS only, I haven't found it.
Here is the Javascript code:
$(function () {
$('.opendialog').click(function () { openDialog(); });
function openDialog() {
var opt = {
autoOpen: false,
modal: true,
width: 'auto',
dialogClass: 'noTitle',
focus: function () {
var width = 0;
$('#loginLogos > .logoRow').each(function () {
var x = $(this).position().left;
var w = $(this).outerWidth();
var tmpWidth = x + w;
width = Math.max(width, tmpWidth);
});
$('#loginLogos').width(width);
var outerWidth = $('#loginBox').outerWidth();
$('#loginLogos').width('auto');
return $(this).dialog('option', 'width', outerWidth);
}
};
$("#loginBox").dialog(opt);
return $("#loginBox").dialog("open");
}
});
The HTML markup:
<a href='#' class="opendialog">Open</a>
<div id="loginBox" style="display:none">
<div>Login/Sign Up</div>
<div id="loginLogos">
<div class="logoRow">
<a href="/auth/google"><img border="0" alt="Google" src="..."></a>
<a href="/auth/facebook"><img border="0" alt="Facebook" src="..."></a>
</div>
<div class="logoRow">
<a href="/auth/twitter"><img border="0" alt="Twitter" src="..."></a>
<a href="/auth/linkedin"><img border="0" alt="LinkedIn" src="..."></a>
</div>
</div>
</div>
And the CSS styles:
body
{
background-color: gray;
}
#loginBox
{
font-family: 'russo_oneregular';
font-size: 20px;
display: inline-block;
}
#loginLogos
{
position: relative;
}
.logoRow
{
display: inline-block;
}
Just use Bootstrap modal popup structure that is totally responsive.
easily customize by bootstrap js attribute
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
https://jsfiddle.net/zigri2612/0o41yogx/
You need a bit of CSS to modify the max-width
of the modal and the box-sizing. Here's your modified example: https://jsfiddle.net/elektronik/3ez3tm3f/1/
The main change is .ui-widget.ui-widget-content[class] { max-width: 90vw; }
.ui-widget.ui-widget-content[class] { max-width: 90vw; }
.
vw is a very useful modern, widely supported CSS unit.
The few other changes (compared to your original fiddle) prevent the modal content from overflowing. box-sizing: border-box;
is present in all modern CSS resets.