Tips for utilizing flexbox with lists in HTML

As I was building my portfolio page, I included 4 main subheadings: About, Resume, Blog, and Portfolio.

To neatly organize these headings at the top center of my div, I decided to create an unordered list with a display inline property. This successfully brought all the headings together in one line.

Now, I am looking for a way to add spacing between the headings without resorting to using margin or padding. Is there a direct solution utilizing flexbox that would allow me to achieve this goal while minimizing the need for media queries on smaller screens?

Answer №1

To create a flexible layout, the ul can act as the flex container with elements evenly spread using justify-content: space-between:

.header {
  display: flex;
  width: 50%;
  margin: 0 auto;
  padding: 0;
  justify-content: space-between;
  list-style: none;
}
<ul class="header">
  <li class="header__item">About</li>
  <li class="header__item">Resume</li>
  <li class="header__item">Blog</li>
  <li class="header__item">Portfolio</li>
</ul>

Answer №2

To center elements, you can utilize justify-content along with pseudo elements for positioning.

nav {
  display:flex;
  justify-content:space-around;
}
/*increase space from side at first and last child */
nav:before,
nav:after {
  content:'';
  width:25vw;/* or % . tune this or remove pseudo if unneeded */
}
a {
  margin:0 1em;
}
<nav>
  <a href="#">About</a>
  <a href="#">Resume</a>
  <a href="#">Blog</a>
  <a href="#">Folio</a>
</nav>

Answer №3

.header {
  display: flex;
  width: 50%;
  margin: 0 auto;
  padding: 0;
  justify-content: space-between;
  list-style: none;
}
<ul class="header">
  <li class="header__item">About</li>
  <li class="header__item">Resume</li>
  <li class="header__item">Blog</li>
  <li class="header__item">Portfolio</li>
</ul>

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

Creating a vertical scrolling marquee to showcase a list in React - step by step guide

Trying to create a marquee effect that displays a list of items in a vertical auto-scroll. After reaching the end of the list, I want it to loop back to the beginning seamlessly for continuous animation. The code snippet below shows my progress so far - a ...

The <footer> element refuses to be placed at the bottom of the page

Struggling to keep my <footer> tag at the bottom of my page despite trying various techniques. Check out my website below to see the issue in action: ...

The CSS click event is not triggering

I can't seem to trigger the click event in CSS This is my code: <input name="btn" type="button" onclick="~/Default22.aspx" value="Home" class="classname" style="position:absolute; left: 286px; top: 160px; margin-bottom: 0px;"/> and I've ...

Is there a way to utilize the passValue function twice within Javascript?

Is there a way to display the value of an input field in multiple spots throughout the code? Currently, my code only passes it once. How can I rewrite it to show up again later in the code? //HTML: <input type="text" name="amount" onchange="passValue ...

What is the best way to eliminate base tags from jQuery mobile scripts?

In jQuery Mobile, how can I remove the base href tags from the page and disable base href? Code Snippet: // Function to test for dynamic-updating base tag support function baseTagTest() { var fauxBase = location.protocol + "//" + location.host + l ...

Is there a way to create a table with "cells that can be controlled individually" similar to the image provided using HTML and CSS?

I have been searching online for solutions on how to create a table similar to the one in this image, but so far I haven't had much luck. I attempted to use colspan, however, it didn't yield the results I was hoping for. Does anyone have any alt ...

Utilizing the empty space to the right of an image in HTML

What could be causing the empty space to the right of my image? This is how the HTML file appears: ... <body> <div class="image"> <img src="image.jpg" alt="Picture here" id="image" align="middle"></img> </div> ...

Leveraging the combination of <Form>, jQuery, Sequelize, and SQL for authentication and navigation tasks

My objective is to extract the values from the IDs #username-l and #pwd-l in an HTML form upon the user clicking the submit button. I aim to compare these values with those stored in a SQL database, and if they match exactly, redirect the user to a specifi ...

Image of outer space enclosed by a circular boundary

Check out this fiddle I created here. It's a simple concept of an image inside a red circle. Is there a way to add some spacing around the image within the circle? This is the markup I currently have: <div style=" width: 50px; he ...

Enhancing Buttons with Stylish CSS Coloration

Looking for a way to style a list of buttons with the same shape and size but different colors? The buttons have unique IDs and all belong to the same class. Take a look at the code snippet below: --html snippet <ul id="query_type"> <li class= ...

As I adjust the size of my browser window, my images automatically resize to properly fit within it. However, when I zoom in or out, the images remain at a consistent

What is the solution to this issue? My images are set to percentages, but setting a height percentage does not seem to have any effect. The relevant HTML code snippet: <div class="header"> <img src="pictures/header.png" width="30%"> < ...

A guide on changing the background color to complement the light/dark theme in Bootstrap 5.3

In the latest version of Bootstrap, 5.3, users have the ability to select a theme for their websites. The built-in options include a Dark theme and a Light theme. For instance, by setting <html data-bs-theme="dark">, you can specify that t ...

Is it possible to utilize both CSS assets and the public folder within a single application?

I am facing the challenge of migrating a legacy application to utilize the Rails 3 asset pipeline. With over 100 stylesheets being imported on a template by template basis using a content_for :stylesheets block, compiling them into a single stylesheet i ...

Is it possible to dynamically load records in real time with the help of PHP and jQuery?

I developed a custom system using PHP that allows users to post status messages, similar to a microblog. The system utilizes a database table structured like this: posts_mst ------------------ post_id_pk post_title post_content date_added Initially, I su ...

Expanding the padding of a span element without displacing the text inside

I'm looking to enhance a span with highlighted text, complete with some padding, while ensuring the surrounding text in the div stays put. Currently, the padding seems to work vertically (with the edge of my span overlapping lines above and below) but ...

Ways to prevent an array from being reset

My issue involves the clothes and orders tables, along with an array based on Clothes and Orders models. Whenever I add a clothes element into the Orders array and specifically update the amount and price of the selected item, it also updates the Clothes a ...

Can anyone advise on the proper way to import CSS within a React component?

I'm currently working on a React component named Comp1 and I've included a CSS file within it like so: import "./style.css"; Within the style.css file, I'm using the following line: @import "https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi ...

Hidden menu does not work with Jquery

As I work on creating a responsive website, I am faced with the challenge of implementing a menu that only opens on mobile devices when clicked. I came across some code on Stackoverflow that I thought would solve this issue for me. However, I seem to be en ...

Words displayed on an image, perfectly aligned in the center

Check out this fiddle: http://jsfiddle.net/WdZeC/ <div class="text_align_center" style="text-align: center;"> <div style="position: relative;"> <img width="27" height="28" src="http://www.kavoir.com/img/text-over-image.jpg"> ...

Selected Angular Radio Button

Back in the good ole days of basic HTML and CSS, I was able to achieve the following: input:checked+label { background-color: #f00; } <div class="col-xs-6"> <input type="radio" id="template-1" name="template" value="template1" checked> ...