0

I've tried like this. how can I center list items inside the div?

.navsection{
    background-color: cadetblue;
    text-align: center;
}

.navsection ul{
    margin: 0;
    padding: 0;
    list-style: none;
    
    /* display: table;
    margin: 0 auto; */
}
.navsection ul li{
   display: block;
   float: left; 
   
}
.navsection ul li a{
    text-decoration: none;
    color: rgb(116, 4, 26);
    display: inline-block;
    padding: 10px 20px;
    background: rgb(127, 210, 212);
    margin: 5px 2px;
}
<div class="navsection tem clear">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Gallery</a></li>
        </ul>
    </div>
I've tried to put the ul tag inside the div and then take it to the center. so I make the text-align center. but it's not working. why this is not working? I've tried to put the ul tag inside the div and then take it to the center. so I make the text-align center. but it's not working. why this is not working?
Al Arefin
  • 75
  • 5

1 Answers1

1

Use the flex rule for selector .navsection. It should look like this:

.navsection {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: cadetblue;
}

.navsection{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: cadetblue;
}

.navsection ul{
    margin: 0;
    padding: 0;
    list-style: none;
    
    /* display: table;
    margin: 0 auto; */
}
.navsection ul li{
   display: block;
   float: left; 
   
}
.navsection ul li a{
    text-decoration: none;
    color: rgb(116, 4, 26);
    display: inline-block;
    padding: 10px 20px;
    background: rgb(127, 210, 212);
    margin: 5px 2px;
}
<div class="navsection tem clear">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Gallery</a></li>
        </ul>
</div>

second solution (no flex):

Remove the float: left and make display: inline-block instead of display: block in the .navsection ul li selector.

It should be like this:

.navsection ul li{
   display: inline-block;
   /*float: left; */   
}

.navsection{
    background-color: cadetblue;
    text-align: center;
}

.navsection ul{
    margin: 0;
    padding: 0;
    list-style: none;
    
    /* display: table;
    margin: 0 auto; */
}

.navsection ul li{
   display: inline-block;
   /*float: left; */   
}

.navsection ul li a{
    text-decoration: none;
    color: rgb(116, 4, 26);
    display: inline-block;
    padding: 10px 20px;
    background: rgb(127, 210, 212);
    margin: 5px 2px;
}
<div class="navsection tem clear">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Gallery</a></li>
        </ul>
</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
  • Thanks, and will you please tell How can I take it to the right? – Al Arefin Dec 05 '20 at 14:13
  • Try `justify-content: flex-end` for `.navsection`. I first gave a solution using flex, but I want to give you another solution, no flex. Wait a bit. – s.kuznetsov Dec 05 '20 at 14:17
  • I gave you a second solution without `flex`. In order to align menu items to the right side use the rule `text-align: end` or `text-align: right` for `.navsection`. – s.kuznetsov Dec 05 '20 at 14:25