Tips for transforming a vertical list into a horizontal one with the help of Bootstrap and media queries

I have been attempting to change this list to a horizontal layout for smaller screen sizes without success. I have tried various methods but nothing seems to be working. Even though it may be a simple task, as a beginner in Bootstrap, I am currently working on creating a Layout Shifter pattern.

Below is the HTML and CSS code snippet:

.container {
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  flex-flow: row wrap;
  background-color: #E57373;
}

li {
  display: block;
}

@media (min-width: @screen-sm-min) {
  .menu li {
    display: inline;
    width: 100%;
  }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="menu">
      <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
        <ul>
          <li><a style="cursor: pointer;">Home</a></li>
          <li><a style="cursor: pointer;">Contact</a></li>
          <li><a style="cursor: pointer;">About Us</a></li>
        </ul>
      </div>
    </div>
    <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12" style="background-color: #FFCDD2">
      <p>The layout shifter pattern emphasizes responsiveness, utilizing multiple breakpoints across different screen sizes. This method allows content to adjust without dropping below other columns, making it more complex to maintain due to significant differences between major breakpoints which may require changes within elements rather than just overall content layout.</p>
    </div>
  </div>
</div>

Answer №1

One issue that needs to be addressed is the use of display: flex on the .container class.

Flex properties are only effective between parent and child elements.

By setting .container as a flex container, its only child .row becomes a flex item. This results in the navigation items not being aligned properly because flex properties are not passed down the hierarchy.

An alternative approach would be:

.container ul {
  display: flex;
  flex-direction: column;
}

@media ( max-width: 800px ) {
  .container ul { flex-direction: row; }
}

.container ul {
  display: flex;
  flex-direction: column;
  list-style-type: none;
  background-color: #E57373;
}

@media ( max-width: 800px ) {
  .container ul { flex-direction: row; }
  .container ul li { margin: 5px; }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="menu">
      <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
        <ul>
          <li><a style="cursor: pointer;">Home</a></li>
          <li><a style="cursor: pointer;">Contact</a></li>
          <li><a style="cursor: pointer;">About Us</a></li>
        </ul>
      </div>
    </div>
  </div>
</div>

View jsFiddle demo

Answer №2

Give this a shot

Adjust the style of the li tag from display: block to display: inline-block for a specific screen size.

@media (min-width: @screen-sm-min) {
li {
  display: inline-block;
}
}

Answer №3

Is this the solution you've been searching for?

To test it out, I modified the media query to max-width: 800px;. You can easily change it back to its original value.

In essence, I included display: flex; in the <ul> element and centered the text within the <li> element. If you prefer the menu not to span the whole screen, specifying a width would be beneficial. Feel free to reach out if you require any further assistance!


.container {
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  flex-flow: row wrap;
  background-color: #E57373;
}

li {
  display: block;
}

@media (min-width: @screen-md-min) {
  .menu li {
    text-align: center;
    display: inline;
    width: 100%;
  }
  .menu ul {
    display: flex;
  }
}

Answer №4

Looking for something similar to this?

Check out this link for a demonstration.

In this layout, the list items are displayed as block elements on smaller screens and inline-blocks (with added margin for spacing and improved vertical alignment) on wider screens (600px was used for the media query). Furthermore, I adjusted the padding of the unordered list to eliminate its default padding.

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

A guide on adding a div element using PHP and AJAX Post

Hello there! I'm currently working on developing an online quiz application with PHP. My goal is to create a form that includes a text field for the question, as well as four text fields below it for the answers, each accompanied by a radio button to ...

Differences between Bootstrap 3.3.7 and Bootstrap 4

After successfully building my application on Bootstrap 4 and incorporating Bootstrap-Table and Bootstrap-DateTime picker plugins, I ran into display issues when trying out Bootstrap-Select. It was then that things went haywire. Upon closer investigation, ...

Step-by-step guide to dynamically load Bootstrap tab panels through tab clicks

I have implemented Bootstrap tab panels on my website, as shown below: <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#chartcontainer1" aria-controls="chartcontainer1" role ...

Adding a new row to a Bootstrap table while maintaining the consistent style

Is there a way to dynamically add a new table row with different styling using jQuery? I'm facing this particular issue and need help in solving it. Below, I have included some screenshots of my code and the view for better understanding. Here is the ...

Issues with locating the fonts: React-slick paired with nextjs

Incorporating react-slick into a NextJs project has been quite seamless, however, I'm encountering issues with importing the necessary fonts from the CSS files. To address this snag, I've attempted the following steps: npm install slick-carouse ...

Adding a new row to a table is causing issues with jQuery

var info = [{ "pin": "015-08-0011-000-01", "arp": "015-08-0011-000-01", "tin": "342-432-423-000", "firstname": "John", "middlename": "James", "lastname": "Jones", "suffix": "", "qtr": "1st ...

Model-view-controller datasets incorporating viewbags

How can I utilize a dataset in a viewbag to showcase the data in a view? I have obtained a dataset from a model and stored it in a viewbag. My intention is to extract the datarows using a foreach loop within the view. Since I already have a variable bein ...

Show the selected value in a div when it is changed

How can I use jQuery to display the selected value of an option in an empty div? $(document).ready(function() { let from_select = $('#from_select'); let from_text = $('#from'); //example#1 from_select.on('change', ...

Create a layered panel underneath a button that covers a separate element

I am working on a layout that consists of an editor and multiple buttons positioned above it on the right side. My goal is to add a panel just below "Button2" that will overlay the editor. This panel should expand and collapse when "Button2" is clicked, wh ...

Sending a Form via Jquery with Ajax

Fiddle Link And Code Snippet: $("form.signupform").submit(function(e) { var data = $(this).serialize(); var url = $(this).attr("action"); var form = $(this); // Include this line for reference $.post(url, data, function(data) { try { ...

Footer div is being covered by the page

I am currently working on a website built with "WordPress", and I have implemented a mobile footer plugin that is designed to only appear on mobile devices. However, I am encountering an issue where the page content overlaps the footer on phones. I attemp ...

Type of element returned

Is there a way to retrieve the element type? I'm interested in applying the style of a class to HTML5 elements without using the class attribute. <!DOCTYPE> <html> <head> <title>My page</title> </head& ...

Dynamically adjust the label position based on the button position

I am currently developing an interface that allows users to change the position of buttons and add a label text under the button. However, I am facing an issue where the label is not always centered below the button due to variations in the size and number ...

Tips for hiding the calendar icon once a form has been submitted

Below is the HTML code snippet for the date field. <asp:TextBox ID="txtExpiryDate" runat="server" Width="80px" MaxLength="10" CssClass="fromDate" /> And here is the HTML code snippet for the Submit button. <asp:Button ID="cmdSubmit" runat=" ...

Require the header text to appear black on a white background and white on a dark background

As someone who is not experienced in coding, I have tried multiple methods without success. It seems like none of the solutions available are compatible with my Squarespace theme. Any assistance you can provide would be greatly appreciated! Here is a link ...

Encountering difficulties with rendering SVG within a React application

Struggling to incorporate an SVG file from the same folder as my js file into my React application, but all I see is the default no image. Using version 1.6.9 of React. I've attempted dragging and dropping the contents of the SVG file, as well as sea ...

An effective method for centering text within a parent div using absolute positioning

Looking for some assistance with a coding challenge involving Bootstrap 4. I'm trying to overlap text with an image and align it to the center. Here is the code snippet, any help would be greatly appreciated! HTML <div class="container-fluid f ...

Set up four divs in a cascading arrangement across four separate columns

Looking to set up 4 cascading divs that create the appearance of being in 4 columns. Here is the given html structure (unable to be modified) <div class="col1"> 1111 <div class="col2"> 2222 <div cl ...

Challenges with resizing images in SVG using D3

I have an SVG image created using d3. I am facing an issue where the svg is extending beyond its parent div container. Here is the code snippet: <div id="test" style="{width: 500px; height:500px;}"> <svg></svg> </div> ...

The pre tag does not have any effect when added after the onload event

I have been experimenting with a jQuery plugin for drawing arrows, detailed in this article. When using the plugin, this code is transformed: <pre class="arrows-and-boxes"> (Src) > (Target) </pre> into this format: Src --> Target The ...