Arrange the children dynamically

I'm struggling to position my ul menu at the top right corner of the page and adjusting the spacing between the items. Whenever I try to add padding, it disrupts the inline display. What am I missing here?

#menu {
    top:0;
    width:100%;
    height:12%;
    position:absolute;
    display: flex;
    align-items: center;
}

li {
    display:inline-block;
    position:relative;
}

#menu ul {
    float:right;    
    margin-right:5%; 
}

h1 {
    margin-left:5%;
}
<div id="menu">
    <h1>My Headder</h1>
    <ul>
        <li><a href = "index.html">Hello</a></li>
        <li><a href = "index.html">home</a></li>
        <li><a href = "index.html">CONTACT</a></li>
    </ul>
</div>

Answer №1

When using <code>flex in CSS, the float property has no effect on its children. Instead, consider using margin, align-self: end;, or justify-content: space-between on the parent element to achieve desired layout.

For example, you can use margin-left: auto to push a flex child to the right.

#menu {
  top: 0;
  width: 100%;
  height: 12%;
  position: absolute;
  display: flex;
  align-items: center;
}

li {
  display: inline-block;
  position: relative;
}

#menu ul {
  margin-left: auto;
  margin-right: 5%;
}

h1 {
  margin-left: 5%;
}
<div id="menu">
  <h1>My Headder</h1>
  <ul>
    <li><a href="index.html">Hello</a></li>
    <li><a href="index.html">home</a></li>
    <li><a href="index.html">CONTACT</a></li>
  </ul>
</div>

For more information and useful tips on flexbox, check out this resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Answer №2

To create space between menu items, include justify-content: space-between; in the #menu style:

#menu {
    ...
    justify-content: space-between;
}

For adding space between li elements, apply padding to them:

#menu ul li {
    padding: 10px;
}

Ensure proper styling for #menu ul:

#menu ul {
    margin-right: 5%;
}

Avoid using float: right; for the ul element.

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

Footer at the bottom and a sticky navigation bar

I have experimented with various CSS guides in order to place the footer at the bottom of the page, and I have found the example below to be the simplest method. html, body, #root { height: 100%; } #root { display: flex; flex-direction: column; } ...

Adjust the brightness of a specified oklch color by 50% using CSS oklch(from ...) in combination with the calc() function

We have implemented a design system that includes various CSS custom properties for colors. For example: :root { --color--blue-1: oklch(88.6% 0.023 238); } Our latest addition is the utilization of the oklch color format. Currently, I am tasked with ca ...

The `webkit-animation-fill-mode` property does not seem to be functional in Firefox

I attempted to create a progress bar design using webkit animation fillmode, but unfortunately, it is not compatible with Firefox. I also tried converting % values to pixels, but the issue persisted. <!doctype html> <html> <head> < ...

Font malfunctioning on Microsoft Edge and Internet Explorer

Apologies if this seems like a silly question, but I recently imported a font from my computer into a CSS file. While it displays correctly in Chrome, it's not working in Microsoft Edge or Internet Explorer. Unfortunately, I don't have access to ...

transfer a product attribute value to a different product attribute within the Magento platform

There is an attribute called Weight : Attribute Code: weight Scope: general Catalog Input Type for Store Owner: Text Field Values Required: yes Apply To: Simple Product, Bundle Product Allow HTML Tags on Frontend: yes Also, there is a General Weight attr ...

The loading time for the Docker index HTML file page is unacceptably slow

Sample Dockerfile: FROM ubuntu:22.04 RUN apt-get update RUN apt-get install -y nginx COPY -r dist/ /var/www/html/ CMD service nginx start && tail -F /var/log/nginx/error.log After that, run the following commands: docker build -t website . docker ...

What could be causing my iframe to not adjust its size according to its content?

My attempt at resizing a cross-site iframe is not working as expected, despite following the guidance provided here. The iframe on my page seems to remain unaltered in size, and I can't figure out what mistake I might be making. The current location ...

Trouble with Safari browser windows and Mac Safari compatibility

Hello everyone, I have a CSS code that I need help with. See below: .left { margin-bottom: 20px; } It works fine on Windows OS with Safari and Chrome browsers, but it's all messed up on Mac OS Safari. After debugging in Mac, I found that it need ...

Is there a way to compel the browser to re-cache an image?

In my website, each user has a cover image on their profile page with the constant name main_cover.jpg. The issue I am facing is that when a user uploads a new image, the old image remains cached in the browser and I have to refresh the page (ctrl+f5) to ...

Anomalous Link Behavior on iOS

I am encountering a strange issue with links on the provided website in iOS: When I try to tap on the links under the "Galleries" menu, such as "Benny," nothing happens. It appears that Safari is trying to load the new page, but then it fails to do so. H ...

Expanding your selection capabilities using BeautifulSoup

My objective is to harness the power of BeautifulSoup in a unique way. Within my HTML files, there are two specific tags that catch my interest. The first one, which I'll refer to as TagA, is <div class ="A">...</div> The second tag, k ...

Progress bar display not available

I have recently developed a JavaScript quiz application and included a progress bar feature. While it works flawlessly offline on my laptop, I encountered an issue when uploading the files to Codepen.io - the progress bar fails to display. I would appreci ...

Display one div and conceal all others

Currently, I am implementing a toggle effect using fadeIn and fadeOut methods upon clicking a button. The JavaScript function I have created for this purpose is as follows: function showHide(divId){ if (document.getElementById(divID).style.display == ...

Tips for effectively showcasing the counter outcome amidst the increase and decrease buttons

Currently, I am in the process of learning Angular and have created a component called quantity-component. Within the quantity-component.component.html file, I have implemented 2 buttons for increment (denoted by +) and decrement (denoted by -). The decrem ...

Troubleshooting: Brackets Live Preview feature experiencing issues with jQuery

I have been working on a note-taking website as an experiment. Everything was running smoothly in the live preview from brackets until I saved the files, and now none of the jQuery functions are working. I suspect it's an issue with my code, but it co ...

Utilizing Bootstrap's responsive design to create optimal spacing between boxes

I have designed this webpage layout using Bootstrap. Although I have manually added margin between the rows, I now want to include vertical spacing between the columns when the page is resized. Can anyone help me achieve this? Full-size browser: Resize ...

When the user clicks on an element, my JavaScript code dynamically updates the CSS property by changing the window

When a tag is clicked in HTML triggering an onclick event, the CSS property that was updated in the JavaScript does not persist. The changes appear momentarily and then disappear once the window is refreshed. Javascript: <script type="text/javascript"& ...

Is there a way to achieve a full-page inset shadow using absolute positioned elements?

Currently, I am facing a challenge with a page that has an inset box shadow on the body element. When there are absolute and relative positioned divs on the page that expand enough to generate a scrollbar, the box shadow is cut off even when the height is ...

Bootstrap's landing page problem

I am currently working on creating a landing page using bootstrap and have encountered an issue with the text alignment in my section containing icons and tags. In order to make the text appear clean and concise, I want the text to be displayed in either o ...

Issue with text decoration in Html, Css, and Bootstrap

this image shows the issue Hi there, I'm facing an issue with my project involving bootstrap. Whenever I include bootstrap in my project using a link, all "a" tags end up with text decoration. How can I fix this problem? ...