'How to style a list in CSS such that the background color doesn't go to the end of line
I'm trying to learn some basic CSS. I'm having trouble styling a list to look like what my instructor has provided for a small practice exercise.
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><ul></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…</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 tried googling people with similar questions and looking at CSS guides but I still can't quite get it.
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:
Solution 1:[1]
Basically, make the ul a floated element which you place to the right, like this:
ul#menu {
float: right;
}
In addition you might want to add width to make it look better, and you might want to move it up in the HTML above the h1 tag to have it floated right at the top like in your example:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

