Tips for arranging headers next to each other on a restaurant menu

I'm currently working on a website for a local restaurant, my very first project of this kind. As I draft the menu section, I'm struggling with displaying Starters and Main Courses side by side on the screen. Additionally, the layout falls apart when viewed on smaller screens.

Despite my efforts with bootstrap 4 and CSS styling using col-sm-6, float, and flex properties, I haven't been able to achieve the desired result.

<div class="menu-title">
      <h1>STARTERS</h1>
      <div class="menu-items">
        <div class="menu-item-body">
          <span class="number">1.</span>
          <span class="name">Vegetable Spring Roll</span>
          <span class="price">4.50&nbsp;€</span>
          <div class="description">
          Crispy spring roll with shredded cabbage, carrot, sweetcorn and soy sauce.                 
          </div>
        </div>                 
      </div>

      <div class="menu-items">
        <div class="menu-item-body">
          <span class="number">2.</span>
          <span class="name">Chicken Cheese Roll</span>
          <span class="price">4.50&nbsp;€</span>
          <div class="description">
          Crispy chicken cheese roll with chicken, onion, spring onion and cheese with sweet chilli sauce.                 
          </div>
        </div>                 
      </div>

CSS

.menu-title{
  flex-direction: column;
}


.menu-items {
  width: 100%; 
  padding: 50px 30px;
  flex-flow: row ;
}

.menu-item-body {
  float: left; 
  position: absolute;
  overflow-x: hidden;
  padding-left: 25px;
  margin: 0 1%; 
  width: 31%; 
}
.number {
  position: absolute;
  left: 0;
}
.name {
  background-color: #fff; 
}

.name:after {
  float: left;
  width: 0;
  white-space: nowrap;
  content: ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . ";
}
.price {
  padding-left: 5px;
  float: right !important;
  background-color: #fff;
}
.description {
  font-size: 14px;
  font-weight: 500;
  opacity: 0.8;
  margin-bottom: 10px;
}

My goal is to create a responsive design that displays 2 or 3 columns of menu items on larger screens.

Answer №1

If you want to create a responsive layout that stacks content in a normal flow for smaller viewports but sets containers side by side for larger viewports, you can achieve this easily with a media query and some simple floats and pseudo-selectors.

Check out this float-based layout example that allows for multiple parallel sections that respond well on various viewports:

body {
  background: gray;
}

.parent {
  background: white;
  overflow: hidden;
  padding: 0 1em;
}

@media (min-width: 40em) {
  .parallel:not(:only-child)  {
    box-sizing: border-box;
    clear: both;
    float: left;
    width: 45%;
  }
  
  .parallel:nth-of-type(even) {
    float: right;
    clear: right;
  }
}
<div class="parent">
  <div class="parallel">
    <h2>Section 1</h2>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
  </div>
  <div class="parallel">
    <h2>Section 2</h2>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
  </div>
  <div class="parallel">
    <h2>Section 3</h2>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
  </div>
  <div class="parallel">
    <h2>Section 4</h2>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
  </div>
</div>

<div class="parent">
<div class="parallel">
    <h2>Section 5 &#8212 Oops! No siblings!</h2>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
</div>
</div>

Floating elements in this way can handle the layout effectively without needing complex techniques or heavy markup. The design stays simple for smaller screens and adjusts seamlessly for larger screens through a media query.

Some key points of this technique include:

  • :not(:only-child) ensures that the split layout is applied only if the parallel item has neighboring items within its row. This makes it easy to manage individual sections without modifying the HTML structure.
  • Clear both for left-floated elements and clear right for right-floated elements help organize items within parent containers and create rows effortlessly.
  • To allow sections to stagger instead of align evenly, you can remove clear: both; from the first selector in the media query.

While there are more modern solutions available, this float-based approach is effective, straightforward, and adaptable with minimal markup required.

Answer №2

You can utilize the Bootstrap Grid System and specify the maximum container width.

For instance:

col-xl-12 - Full width on Extra Large screens. col-xl-4 - 1/3 screen width on Extra Large screens.

Take a look at my code: I set up the container, row, and defined grid options.

Visit CodePen to observe how it functions on smaller devices.

CodePen

html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

.container-fluid {
  height: 100%;
}

.row {
  height: 100%;
}

.menu-title {
  width: 100%;
}

.menu-items {
  width: 100%;
  padding: 50px 30px;
  flex-flow: row ;
}

.menu-item-body {
  float: left;
  position: absolute;
  overflow-x: hidden;
  padding-left: 25px;
}
.number {
  position: absolute;
  left: 0;
}
.name {
  background-color: #fff;
}

.name:after {
  float: left;
  width: 0;
  white-space: nowrap;
  content: ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . ";
}
.price {
  padding-left: 5px;
  float: right !important;
  background-color: #fff;
}
.description {
  font-size: 14px;
  font-weight: 500;
  opacity: 0.8;
  margin-bottom: 10px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css" />
  <title></title>
</head>

<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-xl-4 col-lg-4 col-md-12 col-sm-12 col-xs-12 d-flex justify-content-center">
        <div class="menu-title">
          <h1>STARTERS</h1>
          <div class="menu-items">
            <div class="menu-item-body">
              <span class="number">1.</span>
              <span class="name">Vegetable Spring Roll</span>
              <span class="price">4.50&nbsp;€</span>
              <div class="description">
                Crispy spring roll with shredded cabbage, carrot, sweetcorn and soy sauce.
              </div>
            </div>
          </div>
          <div class="menu-items">
            <div class="menu-item-body">
              <span class="number">2.</span>
              <span class="name">Chicken Cheese Roll</span>
              <span class="price">4.50&nbsp;€</span>
              <div class="description">
                Crispy chicken cheese roll with chicken, onion, spring onion and cheese with sweet chilli sauce.
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="col-xl-4 col-lg-4 col-md-12 col-sm-12 col-xs-12 d-flex justify-content-center">
        CENTER COLUMN CODE
      </div>
      <div class="col-xl-4 col-lg-4 col-md-12 col-sm-12 col-xs-12 d-flex justify-content-center">
        RIGHT COLUMN CODE
      </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

Answer №3

In this solution, a straightforward implementation of Bootstrap 4 is utilized with customized styles purely for aesthetics.

The structure of the rows can be defined using the col-* classes; however, if you need each column to cater to different content types, treating them as rows might be necessary. In such cases, setting flex-column allows for a more suitable layout, although some additional adjustments are required. This approach is demonstrated in the second example solely utilizing Flexbox.

.item {
  margin-bottom: 3em;
  border: 1px solid grey;
}

.item header {
  border-bottom: 1px solid grey;
  padding: 15px 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row justify-content-between">
    <div class="col-12 col-sm-5 item">
      <header>Item 1</header>
      <div class="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Corrupti illum consectetur laborum sint sequi in hic dolore illo distinctio dolorum, earum quia dignissimos doloremque nesciunt deleniti nulla veritatis nemo nobis?</div>
    </div>
    ...
    ...
  </div>
</div>

A simpler alternative using only Flexbox is shown below:

.container {
  display: flex;
  height: 100vh;
}
...
...
<div class="container">
  <div class="starters item">
    <h1>Starters</h1>
    <hr>

    <div class="menu-item">
      <header>Vegetable Spring Roll</header>
      <div class="content">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Odit quam quo quaerat illum, vero numquam eligendi pariatur, est debitis voluptate dolores. Nesciunt doloremque cupiditate quo voluptatibus provident dicta iste ad?
      </div>
    </div>
    ...
    ...
  </div>
</div>

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

Rotational orientation of a progress circle image in SVG

I am currently working on developing a circular progress bar, similar to the one shown in the image below. The progress of this bar is determined by percentages and it moves around the circle accordingly. I have managed to get the progression moving, b ...

Having trouble with displaying the modal dialog in Twitter Bootstrap 3?

There seems to be an issue with my Twitter Bootstrap modal as it is not rendering the dialog box correctly, and I'm puzzled about the reason behind this. HTML <p class="text-center btn-p"><button class="btn btn-warning" data-toggle="modal" ...

Tips for displaying or concealing data in table cells in Google Charts

Using a Google charts table to display server exceptions, but the errors are too long for cells. How can I show only an excerpt of error messages in each cell with a + expansion sign for full details in a modal box on click? I have successfully implement ...

Why is the jQuery class not responding to the is(:visible) selector?

Having trouble with this issue, I created a fiddle to demonstrate what I'm trying to achieve: http://jsfiddle.net/x2btM/9/ Below is the code snippet: <div id="ZodOneDragBox"> <div id="aquariusSelectedComp1" class="killSelectedComp1" sty ...

Ensuring that my divs remain stationary despite changes in the size of the browser window

Hey there! I'm new to web design/development and I've been working on a simple website to get the hang of things. I managed to make my webpage look good at full screen, but as soon as I resize the window, everything starts shifting around and it ...

Adjust the size of an iFrame's content according to the dimensions of the iFrame

I am looking to use an iFrame for embedding a map, and my goal is to resize the map according to the values in the iFrame tag. Most tutorials I have found focus on auto-sizing the iFrame based on content, which is not what I need. Just to make it clear, I ...

What is preventing me from loading js and css files on my web pages?

I have developed a web application using SpringMVC with Thymeleaf and I am encountering an issue while trying to load javascript and CSS on my HTML5 pages. Here is a snippet from my login.html: <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...

"Woops! An error occurred with the message: "SassError: Could not locate the specified target selector" while working with SCSS Modules and incorporating

Currently, I am working with Next.js and utilizing SCSS Modules. To incorporate Bootstrap into my project, I opted to install it using the command npm install bootstrap. Following this installation, I went ahead and created a new file titled common.scss wi ...

The Art of HTML and CSS Layouts

My code is displaying a layout where the 'Login or Signup' link is not aligning to the right side of the page as intended. Can anyone help me with this issue? HTML <header> <h1>Heading</h1> <p>A clean, minimal, gri ...

Remove Chosen Pictures (Checkbox/PHP/MySQL)

I am currently displaying images from a Database using the following HTML code: <li> <input type="checkbox" id="1" /> <a rel="gallery_group" href="images/big/1.jpg" title="Image 1"> <img src="images/small/1.jpg" alt="" ...

Switch out HTML dynamically using JavaScript/JQuery, embracing the principles of DRY coding

As a newcomer to front-end development, one issue I frequently encounter is avoiding repetition when dynamically generating HTML using JS/jQuery. Imagine you have a DOM object that has various states. Typically, all you need to do with JS is switch betwee ...

Setting up automatic filtering for additional dropdown lists in Laravel is a useful feature that can enhance user

Hello! I am working with an application form and I would like to implement a feature where other dropdown lists automatically update based on the selection made in a previous dropdown. Specifically, I am looking to add a new dropdown list called "Country" ...

What is the best way to retrieve an image URL from a CSS file located in a public folder within a

Trying to set a background image in a css file located inside the public/ folder of Rails, but want the image to be sourced from the app/assets/images/ directory. Currently using the code below: background: url("bg-noise.png"); This method typically work ...

Tips on how to horizontally center align a div when using the flex direction column technique

In my design, there is a container that consists of both a table and a div. The div has its display property set to flex with the flex-direction as column. I am trying to horizontally center align the div within the container. Below is the code snippet: ...

Is there a way to change the color of a number's font based on whether it is preceded by a "+" or "-" sign?

I am currently working on a stocks widget and I have a specific requirement. Whenever there is a change in value in the Change or Changeinpercent columns, I need to dynamically set the font color to red for a decrease (-) or green for an increase (+). Her ...

Combining multiple images into one CSS image sprite to create 4 clickable

Could someone please review this segment of the CSS to ensure its accuracy? CSS #nav-list { list-style-type: none; background-repeat: no-repeat; background-image:url("../_img/Footersprite.png") } #nav-list li, #nav-footer a { height: 40 ...

Nested Flexbox Elements

Looking to nest elements using flexbox in order to achieve the following layout: https://i.sstatic.net/bEKps.png Does anyone know how to make this happen? ...

Determining the optimal time to utilize the addClass function in jQuery and CSS

One of my HTML elements has the class "article": <article> Content </article> In my jQuery script, I want to add specific classes that have been predefined: $('article').addClass('rounded box-shadow gradient-bright'); I ...

The asynchronous ajax request is leading to a browser freeze

In the HTML page, I have two sets of a and p elements that are initially set to display:none. At the bottom of the page, there is a function being called with their respective ID's and values, which will enable one of them based on certain conditions ...

Changing the content of a PHP div with an Ajax callback inside another Ajax callback?

In the index.php file, there is a script that triggers an ajax call when a header element is clicked. This call sends a $selection variable to ajax.php, which then replaces #div1 with the received HTML data. <script> $(document).ready(function(){ ...