Weiheng Li
Weiheng Li

Reputation: 57

how to control the position of elements in its parents in my case?

I am trying to implement Appearance of one website http://www.2chainz.com/ I am still in the first step, doing the nav bar.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>test</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!--facebook icon-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<style type="text/css">
.navbarleft{
    width: 40%;

}
.navbarmiddle{
    font-size: 2rem;
    margin: 0 auto;
    width: 20%;
    text-align: center;
}
.navbarright{
    width: 40%;
    float: right;
}
.navbar-nav{
    padding-left: auto !important;
}
.fa{
    float: right;
}
.navbar{
    height: 10vh;
}
.middleitem{
    margin: 0 auto;
}
</style>

</head>
<body>
<nav class="navbar navbar-default navbar-static-top navbar-expand-lg navbar-light">
 <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav navbarleft">
      <li class="nav-item">
        <a class="nav-link" href="#">NEWS</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">MUSIC</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">VIDEO</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">PHOTOS</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">STORE</a>
      </li>
    </ul>
    <ul class = "navbar-nav navbarmiddle" style="margin: 0 auto; text-align: center;">
        <span class="middleitem">2 CHAINZ</span>
    </ul>

    <ul class = "navbar-nav navbarright navbar-right" style="float: right;">
        <li class="nav-item">
        <a href=# class="fa fa-facebook"></a>
        </li>

    </ul>
  </div>
</nav>
<div></div>
<br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>


</body></html>

I have two problems, the first is the position of 2 CHAINZ in its parent ul. I want it to be in the middle of ul element. I tried to use margin: 0 auto; and text-align: center; but they don't work, it always stays in left.

The second is I tried to float the facebook icon to right, I put float: right; there, but it still doesn't work. Could you help me out? Thanks!

Also, I tried to put it in the jsfiddle http://jsfiddle.net/raua0Lku/ but it can't show the navbar. But it works well on my laptop chrome. Why is this...

update: I just make some change to make 2 CHAINZ in the middle but I still didn't figure out how to make facebook icon in the right..

Upvotes: 1

Views: 56

Answers (2)

WebDevBooster
WebDevBooster

Reputation: 14954

Here's a clean solution that doesn't require any css hacks and works with native Bootstrap 4 classes only (click the "run code snippet" button and expand to full window to check):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<nav class="navbar navbar-expand-lg navbar-light bg-light">

    <a class="navbar-brand d-block d-lg-none" href="#">2 CHAINZ</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">NEWS</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">MUSIC</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">VIDEO</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">PHOTOS</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">STORE</a>
            </li>

            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>

        <a class="navbar-brand d-none d-lg-block" href="#">2 CHAINZ</a>

        <a href=# class="btn btn-outline-info fa fa-facebook ml-auto"></a>
    </div>
</nav>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

Notice how the first 2 CHAINZ element has the classes d-block d-lg-none which make it appear on smaller screens but disappear on large (lg) screens.

The other 2 CHAINZ element that sits in the middle only appears on large (lg) screens but disappears on smaller screens.

The ml-auto class (margin-left:auto) on the icon pushes the 2 CHAINZ element towards the middle which always keeps it centered.

Upvotes: 1

Erman
Erman

Reputation: 192

Try it;

<ul class = "navbar-nav navbarmiddle" style="margin: 0 auto; text-align: center;">
        2 CHAINZ
    </ul>

<ul class = "navbar-nav navbarright" style="float: right;">
        <a href=# class="fa fa-facebook"></a>
    </ul>

Finally, if you change css file, your site refresh ctrl + f5. There may be a cache related issue.

Upvotes: 1

Related Questions