How to center an unordered list - html

I've tried multiple times now and I can't figure out how to place my unordered list in the middle of my div.
1: Describes how it looks like right now
2: Describes how I wish it to look like
HTML:
<div id="wrapper">
<div class="container">
<img src="flower.jpg"/>
</div>
<div class="header">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
CSS:
#wrapper {
position:absolute;
background-color: white;
width:100%;
height:100%;
top:0px;
left:0px;
}
img {
width: 100%;
height: 100%;
display: block;
}
.header {
width: 100%;
height: 100px;
background-color: green;
}
.header a {
color: black;
text-decoration: none;
margin: 50px;
padding: 10px;
font-size: 40px;
text-align: center;
}
ul {
list-style-type: none;
position: absolute;
}
li {
float: left;
}
What am I doing wrong? I've tried these in multiple elements:
position: relative/absolute
text-align: center
margin
padding
display: inline
Thanks for the help <3

You can change the styles for the ul element to match the code below:
ul {
list-style-type: none;
position: absolute;
left: 0;
right: 0;
display: flex;
justify-content: center;
}

You can achieve this with flex:
.header {
display: flex;
width: 100%;
height: 100px;
background-color: green;
justify-content: center;
}
Note: you can also change the value of justify-content. A very good explanation about flex and the different properties is this one: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

ul {
display: flex;
justify-content:center;
text-align: center;
list-style-type:none;
width:80%;//depending on how wide you want it and note justify center wont work
unless you specify width on most cases
}

You can do that using flex, like this:
ul {
display: flex;
flex-direction: row;
justify-content: center;
list-style-type:none;
}
You can also try space-around value for justify-content property, and items will have space before, between, and after them.
Here is the working example: https://jsfiddle.net/tLc9zmoy/26/

Related

CSS border bottom on Navigation bar

I have a navigation bar and I added a red line on the bottom when hovering any item of the list, but I want to move that red line under the header (something like "Services"), any idea how to achieve this?
I added an small sample in codepen so you can easily check the HTML and CSS code
header {
background-color: lightblue;
padding-top: 1rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li:hover {
height: 100%;
border-bottom: 2px solid red;
}
<header>
<a href="/">
<p>Whatever logo</p>
</a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
CONTACT
</header>
Link to check the code
You can fix the header height and also fix the height of navbar items.
Also, you had one issue where on hover li elements are moving. You can also fix that with always adding border with transparent color to the element, so the overall height of the element won't change on hover state.
Here is the fixed CSS
header {
background-color: lightblue;
position: sticky;
display: flex;
height: 60px;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
height: 60px;
}
header li {
display: flex;
align-items: center;
border-bottom: 2px solid transparent;
height: 60px;
}
header li:hover {
border-bottom: 2px solid red;
}
https://codepen.io/swarajgk/pen/JjZewPo?editors=1100
I think just giving height to all list elements the same as the header will work.
Like this:-
header {
background-color: lightblue;
padding-top: 1rem;
height: 3rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
height : 100%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li{
height: inherit;
}
header li:hover {
border-bottom: 2px solid red;
}
<body>
<header>
<a href="/"
><p>Whatever logo</p></a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
CONTACT
</header>
</body>
Hope this solves the issue.
header {
background-color: lightblue;
padding-top: 1rem;
height: 3rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
height : 100%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li{
height: inherit;
}
header li:hover {
border-bottom: 2px solid red;
}
I'd suggest the following approach, with explanatory comments in the CSS:
/* removing default padding and margin from all
elements, and forcing the browser to use the
same sizing algorithm - border-box - to calculate
element sizes, including the padding and border
widths in the declared size: */
*, ::before, ::after {
box-sizing: border-box;
padding: 0;
margin: 0;
}
/* setting common properties for the two element
groups: */
header,
header nav ul {
/* using display: flex layout: */
display: flex;
/* forcing the flex-items within the flex parent
to take the full height of that parent: */
align-items: stretch;
}
header {
background-color: lightblue;
block-size: 3em;
position: sticky;
justify-content: space-around;
}
/* using :is() to combine the two selectors
header a,
header li
into one selector: */
header :is(a, li) {
/* using grid layout: */
display: grid;
/* positioning the - including text - content
at the center of the element: */
place-items: center;
}
header nav {
min-width: 50%;
}
header nav ul {
/* the <ul> isn't a flex-item so we have to specify
that we want it to take all available space on
the block-axis (equivalent to 'height' in left-to-right
languages such as English): */
block-size: 100%;
list-style: none;
justify-content: space-between;
}
header li {
/* to prevent the jumping content: */
border-bottom: 2px solid transparent;
}
header li:hover {
/* to style the color of the bottom border: */
border-bottom-color: red;
}
<header>
<a href="/">
<p>Whatever logo</p>
</a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
CONTACT
</header>
JS Fiddle demo.
References:
align-items.
display.
justify-content.
place-items.
Bibliography:
"Aligning items in a flex container," MDN.
"Basic concepts of flexbox," MDN.
"Box alignment in grid layout," MDN.

How to I make this Ul element go below a Div element?

HTML
<div id="dashboard">
<div id="Sidebar">
<div id="User-Icon"></div>
<ul>
<li a href="#">Home</li>
<li a href="#">Home</li>
<li a href="#">Home</li>
<li a href="#">Home</li>
<li a href="#">Home</li>
</ul>
</div>
<div id="Dashboard">
</div>
<div id="UserFeed">
</div>
</div>
CSS
* {
Font-Family: Arial;
Margin: 0;
Padding: 0;
}
#dashboard {
display: flex;
height: 100vh;
width: 100vw;
}
#Sidebar {
background-color: #5c5c5c;
height: 100%;
width: 20%;
display: flex;
justify-content: center;
align-self: flex-end;
}
#Dashboard {
background-color: #999999;
height: 100%;
width: 50%;
}
#UserFeed {
background-color: #D9D5D6;
height: 100%;
width: 30%;
}
#User-Icon {
width: 150px;
height: 150px;
background-color: white;
border-radius: 50px;
margin-top: 50px;
}
ul {
}
li {
list-style: none;
margin-top: 50px;
}
My goal here is to move the ul under my User-Icon in a centered matter, if anyone could help me it would be greatly appreciated!
I tried using things like Clear: left; for the ul, Float: bottom; but I just couldn’t get anything to work. I would like to know more about this so if you can, I would like the explanations to be a bit detailed for me
just add flex-direction and align items
#Sidebar {
background-color: #5c5c5c;
height: 100%;
width: 20%;
display: flex;
flex-direction: column;
align-items:center
}
You tried with z-index ?
try this
ul {
z-index: -1;
}
Change this within sidebar
#Sidebar {
background-color: #5c5c5c;
height: 100%;
width: 20%;
display: flex;
align-items: flex-end;
flex-direction: column;
}
It will keep the formatting you were trying to achieve.
I think you are looking for something like this
If this is what you are looking for please use flex-flow: column;align-items: center; for the parent Element. display flex is a little bit complex. you should read the doc carefully. Here is the updated CSS for you.
#Sidebar {
background-color: #5c5c5c;
height: 100%;
width: 20%;
display: flex;
flex-flow: column;
align-items: center;
}
ul {
width: 100%;
margin-top: 10px;
}
li {
list-style: none;
padding: 12px;
margin-bottom: 2px;
}

How to prevent overflowing of an element in css?

So I am trying to create a logo and a menu icon in the header but for some reason, they are always overflowing the height of the header which I have strictly specified! Why is that ?
And I know I can hide out the overflowing items by using overflow:hidden; property but it is not always a good case.
For example, I tried to create a hamburger icon but I could not because of this overflow issue. The menu lines were working as if the entire element is shown but I had to hide it out so that it could fit into the header.
Here is the code -
<header>
<div class="logo">
Elvis
</div>
<div class="menu">
Hamburger Menu
</div>
</header>
In CSS -
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
border: 2px solid red;
}
.logo {
font-size: 33px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 100px;
width: 100px;
background-color: #bd4439;
}
Here is the codepen link -
https://codepen.io/raghav-sharma333/pen/eYeZYGO
Here is the image of the issue -
Overflowing content
So I just want to know :
Why is it happening?
&
How can it be prevented?
Basically you are forcing your elements to be higher than the header itself by giving them static heights (height 100px on the menu and padding-top/bottom 30px on the logo)
I updated your pen: https://codepen.io/penmasterx/pen/wvPGaGz
Using height 100%, so the elements adapt to the header.
Let me know if this solves your problem. If not, let me know in more detail what you're trying to accomplish.
What I added to the pen:
.logo {
height: 100%;
display: flex;
align-items: center;
/* removed padding top/bottom */
}
.menu {
height: 100%;
}
In such cases, it is better to use the position to manage the inheritance of the elements
I modified your code:
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>
First: the reason you use a 33px font which adds padding, then you use a height:100px on the menu while on your header you put a height:60px
you also need to add align-self: center on your flex-box
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
align-self: center;
border: 2px solid red;
}
.logo {
font-size: 17px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 60px;
width: 100px;
background-color: #bd4439;
}
I did it like 'Ali Memar' answer but the difference is the position of the texts. they are now in the middle of the div.
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
text-align: center;
display: flex;
align-items: center
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
text-align: center;
display: flex;
align-items: center
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>

How can i center image in any div in HTML & CSS?

How can i center image in any div in HTML & CSS ?
I have a nav bar that uses images as links for other pages, the nav bar is multiple divs each has an image and anther div contains a words, how can I center this img in the div
The CSS:
#Nav
{
height: 150px;
margin-top: 50px;
}
#Nav ul
{
list-style-type: none;
padding: 50px;
padding-top: 0px;
width: 100%;
}
#Nav li
{
float: left;
font-size: 12px;
font-weight: bold;
letter-spacing: 0;
margin: 30px;
}
#Nav a
{
display: inline-block;
padding: 5px;
width: 70px;
height: 100px;
color: black;
text-decoration: none;
}
#Nav a:hover
{
border: 2px solid #ddd;
border-radius: 4px;
box-shadow: 0 0 2px 1px rgba (0, 140, 186, 0.5);
}
#img img
{
align-self: middle;
width: 50px;
height: 50px;
}
#desc
{
margin-top: 15px;
text-align: center;
}
The html:
<li> <a target="_blank" href="#">
<div id="img"> <img src="C:/Users/hp1/Desktop/Website/Pictures/Accessories.jpg" alt="Accessories">
<div id="desc"> Accessories </div>
</div>
</a> </li>
You need to change your CSS for image like below:-
#img img
{
display: block;
margin: 0 auto;
}
You can see Many Demos Here-
Note that align-self is for elements inside a flexbox container which is not your case, you can try with text-align: center; on img.
Or if you wish to go full flexbox, set the img container to:
.containerClass {
display: flex;
flex-flow: column wrap;
justify-content: center;
}
With this setup align-self on the img is not need since justify-content on it's container will do.
Flexbox to the rescue:
.parent {
width:200px; height:200px;
border:2px solid #000;
display: flex; /* Make it flex */
flex-direction: column; /* wrap children elements to columns */
align-items: center; /* Center children horizontally */
justify-content: center; /* Center children vertically */
}
<div class="parent ">
<img src="http://placehold.it/100x100/cf5">
<p>Green-ish</p>
</div>
<div class="parent ">
<img src="http://placehold.it/100x100/5fc">
<p>Blue-ish</p>
</div>
align-self: center; is valid, align-self: middle; is not valid.
However, you can probably achieve what you're wanting with text-align: center;. Despite it being an image, you can still center using text-align as images are, by default, inline elements just like text.
#img img
{
align-self: center;
width: 50px;
height: 50px;
}
OR
#img img
{
text-align: center;
width: 50px;
height: 50px;
}
First you have to change the nature of div to table then make its align to center like this
#img img
{
display:table;
align: center;
width: 50px;
height: 50px;
}

CSS3 Flex: Pull child to the right

here's what I have Fiddle
ul {
display: flex;
justify-content: flex-start;
flex-direction: row;
align-items: center;
width: 100%;
height: 100px;
background: #333;
padding: 15px;
}
ul li {
padding: 15px;
margin: 5px;
background: #efefef;
border: 1px solid #ccc;
display: inline-block;
list-style: none;
}
#item-1 {
height: 50px;
}
#item-2 {
height: 70px;
}
<ul>
<li id="item-1">Home</li>
<li id="item-2">Menu</li>
<li>More</li>
<li>Stuff</li>
<li>Settings</li>
</ul>
I want the last item inside the flex-box to be pulled to the right ("Settings" in my fiddle) while keeping all other items the way they are. The "Settings"-item should also be centered vertically and everything.
align-self: flex-end pushes the item to the bottom (I want it on the right).
I would very much prefer a solution using flex-box because my items have variable heights and should always be centered vertically.
What is the cleanest way to achieve this?
Thanks for your help!
Simple fix, use an auto-adjusting margin:
ul li:last-child {
margin-left: auto;
}
You may also want to not use width: 100% so that the element stays inside the visible area:
ul {
display: flex;
justify-content: flex-start;
flex-direction: row;
align-items: center;
/* width: 100%; */
height: 100px;
background: #333;
padding: 15px;
}
http://jsfiddle.net/dwLHE/
See also https://www.w3.org/TR/css-flexbox-1/#auto-margins