Circumstances
This HTML code is intended to create a horizontal <ul>
element that spans the entire width of the
<div class="list__wrapper">
container on desktop screens with a minimum width of 1024px. However, for mobile devices with a maximum width of 1024px, the list should display as a dropdown menu.
Snippet
- Link to view the code on CodePen
Challenges
- Ensure all
<li>
elements occupy the full width without any extra space on both desktop and mobile views. - Even when the
expands, make sure the<div class="list__wrapper">
<li>
items take up all available space, which may involve adjusting the CSS rulemax-width: 50%
applied in the.list__wrapper
class.
Current Display
Desktop Preview
https://i.stack.imgur.com/mFUso.pngMobile (collapsed)
https://i.stack.imgur.com/EdHO1.pngMobile (expanded)
https://i.stack.imgur.com/xVGPe.png
Intended Outcome
Desktop View
https://i.stack.imgur.com/KxYiS.pngMobile (collapsed)
https://i.stack.imgur.com/pHD8K.pngMobile (expanded)
https://i.stack.imgur.com/rwxfD.png
Source Code:
var wind = $(window);
var windowWidth = $(window).width();
wind.on('resize load', function() {
if (windowWidth < 1024) {
$("ul").on("click", ".init", function() {
$(this).parent().children('li:not(.init)').toggle();
});
var allOptions = $("ul").children('li:not(.init)');
$("ul").on("click", "li:not(.init)", function() {
allOptions.removeClass('selected');
$(this).addClass('selected');
$("ul").children('.init').html($(this).html());
allOptions.toggle();
});
}
});
.list__wrapper {
max-width: 50%;
background-color: grey;
}
li {
list-style-type: none;
background-color: black;
color: white;
}
@media screen and (max-width: 1024px) {
ul {
border: 1px #000 solid;
text-align: center;
width: 100%;
}
li {
width: 100%;
}
li.init {
cursor: pointer;
}
}
@media screen and (min-width: 1024px) {
.list {
display: flex;
justify-content: center;
flex: 1;
align-items: center;
}
.list li {
padding: 10px;
color: white;
width: 25%;
text-align: center;
border-left: 1px solid white;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="list__wrapper">
<ul class="list">
<li class="init">Option 0</li>
<li data-value="value 1">Option 1</li>
<li data-value="value 2">Option 2</li>
<li data-value="value 3">Option 3</li>
</ul>
</div>