Tips for styling a list in CSS to prevent the background color from extending to the end of the line

For the small practice exercise in my CSS learning journey, I'm struggling to style a list as my instructor has shown. I've attempted several times but can't seem to achieve the desired outcome.

ul#menu li {
  display: block;
  border-block-end: 0px;
  background-color: #66c;
  margin: 2px;
  list-style-type: none;
  padding-left: 5px;
  padding-top: 2px;
}

#menu {
  background-color: #64a;
}

#menu a {
  color: white;
  text-decoration: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <title>Sample Menu Page</title>
  <link rel="stylesheet" title="Q1" href="style2.css" />
</head>

<body>
  <h1>Sample Menu Page</h1>

  <ul id="menu">
    <li><a href="one.html">Page One</a></li>
    <li class="current"><a href="two.html">Page Two</a></li>
    <li><a href="three.html">Page Three</a></li>
    <li><a href="four.html">Page Four</a></li>
    <li><a href="five.html">Page Five</a></li>
  </ul>

  <div id="main">
    <h2 id="first">First Section</h2>
    <p>This is the first part of the real content of the page.</p>
    <p>This exercise is mostly about:</p>
    <ul>
      <li><abbr title="Cascading StyleSheets">CSS</abbr></li>
      <li>geese</li>
    </ul>
    <p>Actually, there isn't so much about geese. I just wanted to use a <code>&lt;ul&gt;</code> there.</p>

    <h2 id="next">Next Section</h2>
    <p>I'm really running out of stuff to say here. Oh well, fall back to old standards, I guess&hellip;</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

  </div>

</body>

</html>

I've scoured the web for similar questions and consulted various CSS guides without success.

ul#menu li {
  display: block;
  border-block-end: 0px;
  background-color: #66c;
  margin: 2px;
  list-style-type: none;
  padding-left: 5px;
  padding-top: 2px;
}

#menu {
  background-color: #64a;
}

#menu a {
  color: white;
  text-decoration: none;
}

Expected result:

https://i.sstatic.net/CRLdk.png

Answer №1

To achieve the desired layout, you can float the ul element to the right by adding this CSS:

ul#menu {
  float: right;
}

For a better visual appearance, consider setting a specific width for the menu. Additionally, in order to have it appear at the top right corner, you may need to reposition the menu element above the h1 tag in your HTML structure.

Check out this example on JSFiddle for reference: https://jsfiddle.net/shpeuo8m/

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

How can I create multiple vertical lines in an SVG format?

How can I achieve an effect similar to this https://i.sstatic.net/ulZFR.png? I have attempted using strokeDashoffset and strokeDasharray, but I am still unable to achieve the desired result. I know that I can draw multiple lines, but I need a script that ...

Issue with SALON theme causing logo to not display on Wordpress site

It's puzzling why the logo image I uploaded to the WP theme isn't showing up on the front-end. I've reached out to the theme developers multiple times, but it's been two months without a response, and my client, as well as myself, are ...

Placing an image at the bottom right corner and layering it behind an svg element

I am currently attempting to position an image behind an angled SVG separator. Our goal is to achieve this look: https://i.sstatic.net/2YnsY.png However, the current display does not meet our expectations as illustrated here: https://i.sstatic.net/5Dssf ...

Is there a way to ensure that my text is centered on top of an image while maintaining responsiveness?

I'm trying to make the text adjust its center to the height of an image that changes size when the page is resized. Here's what I have so far: HTML <section id="eyeCatcher"> <div id="catchContainer" > <div id="eyeCatchQ ...

Bootstrap breakpoint activated on mobile window

Is there a way I could insert a breakpoint for when the HTML content is displayed in a small window, like xs? <br class="show-when-small-window"> ...

Checking Form Entries with the Power of FOR Loop in JavaScript

Can someone assist me with implementing JavaScript for form validation using a for loop within the JavaScript code? I have a form with 5 fields for usernames and passwords, and I need to use JavaScript to validate the form before submitting it to the data ...

What are the steps to create a scrolling effect for the menu buttons on the mobile version of

When accessing the mobile version of Facebook on a handheld device, you will notice a menu with various options like "About," "Photos," "Friends," "Subscriptions," "Map," and more. This menu is designed to be slideable on your mobile device. Using HTML, ...

The entire framework remains concealed as the anchor component becomes immeasurable

Within a fixed-width paragraph, I have an anchor tag. The CSS for the p tag includes the properties text-overflow: ellipsis and overflow:hidden. However, when the anchor tag's content overflows (e.g., it contains a long string), the right outline is n ...

In React, CSS @media queries are specifically designed to function only within the Device Mode of Developer Tools

As indicated by the title, I have designed a responsive web app using the React framework along with various @media queries. When I resize the page in Developer Tools' Device Mode, the queries work perfectly fine and everything functions as expected. ...

Is it truly impossible to align text around an image using flexbox?

In typical situations, you can easily float an image left or right to wrap text around it. However, in flexbox layouts, floating elements do not work as expected and finding a solution can be challenging. It is worth noting that Bootstrap 4 will be implem ...

Add a border around the div that runs into the header

I am looking to create a border and header similar to this example: [![][1]][1] Is there a CSS solution for achieving this design? One method I have tried that seems to work is: .header{ margin-top:-10px; background:#fff; } Are there any alternate opti ...

Utilize the jQuery .load() method to fetch a specific section of a webpage by passing the href as the parameter

Looking to perform an AJAX call for a specific page I understand that AJAX can be used to load a section of a document using the following code: $('#AJAX').load('/file-directory #content'); Instead of directly inputting the file path ...

Can someone explain what logForm.$invalid.$setValidity is all about?

The code snippet can be found here I am a beginner in this field and currently studying a piece of code. I am having trouble understanding the logForm.$invalid.$setValidity line. I have searched online but couldn't find any information about it. The ...

Searching for all class names or ids within a CSS file that include a background image can be achieved by utilizing regular expressions in conjunction with PHP

Is there a way in PHP to extract the class name or ID from text using regular expressions? For example: .vc_custom_1547091583528{ margin-bottom:40px!important; padding-top:7rem!important; padding-bottom:1rem!important; background:#092746 ...

Using Vue.js to pass an image URL as a prop for CSS animation by converting it to a CSS variable

Need help with a component that features a hover animation displaying 4 rotating images: animation: changeImg-1 2.5s linear infinite; @keyframes changeImg-1 { 0%, 100% { background-image: url('images/wel1.png'); } 25% { background-image: ur ...

How to effortlessly apply a background image to an SVG path element without any tiling or scaling involved

I am looking to implement a solution similar to the one discussed in Fill SVG path element with a background-image, but with a slight variation. Rather than tiling the image to fill the entire background area, I want the image to be centered within the pat ...

Issues arise when implementing a sticky position using CSS with incomplete functionality

I want to create a website with a menu bar that stays at the top even on long pages. However, when I use position: sticky in CSS, the menu disappears after scrolling a certain distance. Does anyone know how to fix this issue? HTML: <nav> <ul cla ...

Tips for breaking out of an infinite loop while loading HTML containing a carousel into a blank <div> within another HTML file using jQuery's $(document).ready() function

Currently, I am studying HTML, CSS, Bootstrap 4, jQuery3, and JavaScript (ES6+), and to enhance my skills, I have devised an exercise for practice and consolidation. I have created 4 HTML files, all featuring responsive design based on Bootstrap 4: inde ...

Issue: Images are not displaying in jQuery slideshow.Possible cause: There

I'm currently working on developing a slideshow using jQuery and HTML. However, I've encountered an issue where only one image is displaying while the other slides are not showing up at all. I've been troubleshooting this problem but haven&a ...

The Input Boxes Are Too Broad

Currently, I am working on a responsive web page that features a form. However, I have noticed that all the input elements are spilling out of the form both from the left and right sides. Can someone please shed some light on why this could be happening? ...