-3

I am trying to make my <li> inside of the <nav> to be positioned at the right side.

I am using display:inline-block; property and I have tried to position elements to the right by using following property: text-align:right;.

<html>
    <head>
        <link type="text/css" rel="stylesheet" href="./style.css">
        <title>
            Practice
        </title>
    </head>
    <body>
    <header class="main">
        <div>
        Gokul
        
        </div>
 
        <nav class="main-nav" >
        <ul class="item">
            <li class="items">
                <a href="/" class="brand">Home</a>
            </li>
            <li class="items">
                <a href="/" class="brand">Contact</a>
            </li>
        </ul>
        </nav>
        </header>
    </body>
</html>    
  

Here is the full code on CodePen: CodePen Link

I want to align the home and contact.

I would be glad if someone could address what the problem and give a short answer for solving it.

str1ng
  • 485
  • 3
  • 14
whynotcode
  • 78
  • 8
  • 1
    Please **provide all code** in your question. **Avoid depending on external sites**, as questions will be of no value when your _external_ code cannot be accessed. – Oskar Grosser Nov 05 '22 at 04:32

3 Answers3

1

I would solve this by giving the header-element display: flex:

header.main {
  display: flex;
  justify-content: space-between;
}

There's a ton of tools available to teach you flexbox layout so I wont go into depth. Just know that getting to know the basics of a flex layout will be very helpful very often.

  • A flex layout is very dynamic and is great for simple, single dimensional layouts
  • justify-content: space-between will make the items inside the container with display: flex have the biggest gap between them as possible. In this case, that would be the child elements <div> with the text content "Practice" and the <nav>. The <nav>-element holds both of the buttons, so they will both get pushed to the right side.

header.main {
  display: flex;
  justify-content: space-between;
}

body {
  padding: 0;
  margin: 0;
}

.main {
  background-color: #252323;
  padding: 20px 30px;
}

.main>div {
  text-align: left;
  display: inline-block;
  color: #ee8686;
  font-size: 25px;
  font-weight: bold;
}

.items {
  display: inline-block;
  margin: 0px 10px;
}

.main-nav {
  display: inline-block;
  text-align: right;
}

.item {
  list-style: none;
  padding: 0;
  margin: 0;
}

.brand {
  color: #d8b4b4;
  text-decoration: none;
  background-color: #711111;
  padding: 10px 15px;
  border-radius: 8px;
}

.brand:active,
.brand:hover {
  background-color: #fff6f6;
  color: #3a1f1f;
}
<html>

<head>

  <link type="text/css" rel="stylesheet" href="./style.css">

  <title>

    Practice

  </title>

</head>

<body>

  <header class="main">

    <div>

      Practice



    </div>



    <nav class="main-nav">

      <ul class="item">

        <li class="items">

          <a href="/" class="brand">Home</a>

        </li>

        <li class="items">

          <a href="/" class="brand">Contact</a>

        </li>

      </ul>

    </nav>

  </header>

</body>

</html>
Sigurd Mazanti
  • 2,098
  • 1
  • 8
  • 24
  • Thanks i learned smtg now but Would be good if you solve using display inline block, I am just beginner and learning one by one – whynotcode Nov 05 '22 at 02:40
  • 3
    @whynotcode use flex instead of inline block, it's much better. – ethry Nov 05 '22 at 03:09
  • 2
    @ethry not just better, but in general better solution and better approach to this, and you have to make decisions what you're going to use depending on your project. I mean, yes I suggested using `float:right`, but the float property will take the elements out of the normal document flow. This could lead to a mess on webpage and it could shift the rest of the elements under the floated elements so they will be partially visible or won’t be visible at all. -> There's solution for that by using `clear: both;` after floated element(s), but in general better idea is to solve this, this way – str1ng Nov 05 '22 at 03:18
1

Update your:

.main-nav
{
    display : inline-block;
    text-align: right;

}

with:

.main-nav
{
    display : inline-block;
    text-align: right;
    float:right;
}

EDIT: Or another approach is based on @SigurdMazadi answer (to avoid warnings): By using flexbox and css properties that he used:

header.main {
  display: flex;
  justify-content: space-between;
}

under the justify-content: space-between; add following line:

align-items:center;

UPDATE REGARDING TO USING FLOAT:RIGHT;:

In general it's better idea and better approach to this to use flexbox. But, everyone have to make decisions on what they're going to use depending on their project.

I mean, yes I suggested using float:right;, but the float property will take the elements out of the normal document flow. This could lead to a mess on webpage and it could shift the rest of the elements under the floated elements so they will be partially visible or won’t be visible at all. -> There's solution for that by using following property: clear: both; after floated element(s), but in general better idea to solve this is to use flexbox.

str1ng
  • 485
  • 3
  • 14
  • But the editor warnings me to not use float with display inline block? Any thoughts on it? – whynotcode Nov 05 '22 at 02:39
  • 1
    @whynotcode I've updated the answer according to avoid warnings and based on previous user's answer (I've just added one more property to make items centered inside of header) as it had some offset (it wasn't positioned in the center) – str1ng Nov 05 '22 at 02:52
  • 1
    @whynotcode Btw. You could've avoided using `display:inline-block;` in this case, and just setted `float` property – str1ng Nov 05 '22 at 02:56
  • 1
    @whynotcode to align in a flex, use the `align-items: center;` thing as shown in the answer instead of your float. – ethry Nov 05 '22 at 03:21
1

It already works! (kinda)

text-align: right is working! It just appears not to work because its surrounding element is so small that it doesn't have any visible effect.

Here's a quick mock-up, with header in red, nav in purple-ish and ul in blue. Analogically to your code, we set nav {text-align: right}. But as you can see, nav is so small that it doesn't have any effect:

ul {list-style-type:none}
div, nav, li {display:inline-block}
nav {text-align:right}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
header {
  padding: 4px;
  background-color: lightcoral;
}
div, li {
  border: 1px solid black;
  background-color: white;
}
nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <header>
    <div>Title</div>
    <nav>
      <ul>
        <li>Button 1</li>
        <li>Button 2</li>
      </ul>
    </nav>
  </header>
</main>

We can prove that nav's size is the issue by testing text-align: right with a larger (fixed) size:

ul {list-style-type:none}
div, nav, li {display:inline-block}
nav {
  width: 12rem;
  text-align: right;
}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
header {
  padding: 4px;
  background-color: lightcoral;
}
div, li {
  border: 1px solid black;
  background-color: white;
}
nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <header>
    <div>Title</div>
    <nav>
      <ul>
        <li>Button 1</li>
        <li>Button 2</li>
      </ul>
    </nav>
  </header>
</main>

Let's fix it

There are a ton of ways to right-align ul. I'll go over several, from outdated approaches to modern methods.

Recommended approaches

Prefer to use the following approaches that use modern and well-supported CSS properties.

CSS Grid

Since our design is supposed to be structured and layout-driven, we may want to choose CSS Grid for our solution.

With CSS Grid we can specify a grid and its dimensions. In our case, we want a 1 by 2 grid, where the element in the second column is right-aligned:

header {
  display: grid;
  grid-template-columns: repeat(2, auto);
  align-items: center; /*Vertical align*/
}
header>div {justify-self:start} /*Horizontal align*/
nav {justify-self:end} /*Horizontal align*/

ul {list-style-type:none}
div, nav, li {display:inline-block}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
header {
  padding: 4px;
  background-color: lightcoral;
}
div, li {
  border: 1px solid black;
  background-color: white;
}
nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <header>
    <div>Title</div>
    <nav>
      <ul>
        <li>Button 1</li>
        <li>Button 2</li>
      </ul>
    </nav>
  </header>
</main>

CSS Flexbox

We can also use Flexbox, which I personally recommend for flow-like and content-driven designs.

Since we want a one-dimensional (flow-like) layout, we can use Flexbox. Many find this easier than CSS Grid.

We specify a flex-parent, and tell it to lay out its children with as much space in between as possible:

header {
  display: flex;
  justify-content: space-between; /*Put space between children*/
  align-items: center; /*Vertical align*/
}

ul {list-style-type:none}
div, nav, li {display:inline-block}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
header {
  padding: 4px;
  background-color: lightcoral;
}
div, li {
  border: 1px solid black;
  background-color: white;
}
nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <header>
    <div>Title</div>
    <nav>
      <ul>
        <li>Button 1</li>
        <li>Button 2</li>
      </ul>
    </nav>
  </header>
</main>

Alternative approaches

These approaches are either outdated or playfully bad ways. Generally, these approaches are highly discouraged. I include them for educational purpose only, so that you can identify them and know not to use these.

Positioning

We can position the left- and right-aligned elements manually with position: absolute.

To position them relative to header, header needs to have its position property set to something different than static:

#header-wrapper {position:relative}
nav {
  position: absolute;
  right: 0;
}

ul {list-style-type:none}
#title, nav, li {display:inline-block}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
header {
  padding: 4px;
  background-color: lightcoral;
}
#title, li {
  border: 1px solid black;
  background-color: white;
}
nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <header>
    <div id="header-wrapper">
      <div id="title">Title</div>
      <nav>
        <ul>
          <li>Button 1</li>
          <li>Button 2</li>
        </ul>
      </nav>
    </div>
  </header>
</main>

As you can see, by using position: absolute we take the element out of the flow. It is essentially on another "layer", and doesn't influence the "base layer's" layout. That is why the header doesn't automatically adjust to fit it inside.

Floating

We can simply tell nav to float: right. This is usually recommended for supporting content in documents, but can also be used to float elements to one side.

Floating an element aside makes it a no-block-level element, so other content can "flow around" it. If you want to stop this behaviour, you may have to use the clear-fix hack. This may be a point of confusion, so your editor might tell you to avoid using float.

nav {float:right}
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

ul {list-style-type:none}
div, /*nav,*/ li {display:inline-block}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
header {
  padding: 4px;
  background-color: lightcoral;
}
div, li {
  border: 1px solid black;
  background-color: white;
}
nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <header class="clearfix">
    <div>Title</div>
    <nav>
      <ul>
        <li>Button 1</li>
        <li>Button 2</li>
      </ul>
    </nav>
  </header>
</main>

Since the parent element is artificially stretched to fit the floated element, vertically centering its children may be difficult for non-fixed lengths.

Tables!

In the early days of the web, many layouts were realized by misusing tables.

Tables allow us to specify rows and columns, and how to lay out content in the cells. However, tables are only intended for tabular data, not for layout!

#header {width:100%}
td:last-child {text-align:right}

ul {list-style-type:none}
li {display:inline-block}

/*IGNORE; make bounding boxes visible*/
* {
  margin: 0;
  padding: 0;
}
#header {
  padding: 4px;
  background-color: lightcoral;
}
td:first-child, li {
  border: 1px solid black;
  background-color: white;
}
#nav {
  padding: 4px;
  background-color: darkslateblue;
}
ul {
  padding: 4px;
  background-color: lightblue;
}
html {height: 100%}
body {
  min-height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}
<main>
  <table id="header">
    <tr>
      <td>Title</td>
      <td id="nav">
        <ul>
          <li>Button 1</li>
          <li>Button 2</li>
        </ul>
      </td>
    </tr>
  </table>
</main>
Oskar Grosser
  • 2,804
  • 1
  • 7
  • 18