Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1. link in css not working 2. how to vertically center navbars and add padding on each

Tags:

html

css

I am trying to build my website. 1. I am trying to create link in vertical direction in middle of my page; A,B,C,D however it is not working as a link, it works like a paragraph.

  1. I want to make A,B,C,D to have spaces in between them.

  2. Is there a way to make A,B,C,D links covered in circle?

/* navigation */
.navig {
    float: left;
    margin-top: 150px;
}
.navig a{
    text-decoration: none;
    color:black;
}
.navig a:hover{
    
}

.navig li{
    list-style-type: none;
    position: fixed;
    padding-top: 50x;
    line-height: 40px;
}
   
  <div class="container">
    <nav class="navig">
      <ul>
        <li><a href="http://google.com">A</a></li>
        <li><a href="http://google.com">B</a></li>
        <li><a href="http://google.com">C</a></li>
        <li><a href="http://google.com">D</a></li>
      </ul>
    </nav>
    
    
like image 458
haneulkim Avatar asked Feb 14 '19 21:02

haneulkim


People also ask

How do I center a link vertically in CSS?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

How do I center both vertically and horizontally in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I align two objects vertically in CSS?

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout. In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property.

How do you center text with padding in CSS?

To center text in CSS, use the text-align property and define it with the value 'center.

How do I center up and down in CSS?

You can just set text-align to center for an inline element, and margin: 0 auto would do it for a block-level element.


2 Answers

I think this is what you are looking for -> (View snippet behind)

  • If you want to move A,B,C,D around and create spaces in between them the best option is to use flexbox properties.
  • The links A,B,C,D were not displayed properly because you were using position:fixed on the li tags, placing them all in the same tiny space.
  • If you want to put the links inside a circle you just have to add a border and border-radius to the A tags.
  • [EDIT] display:flex on the anchor tag is setting the A,B,C,D letters perfectly in the center of the circle. I think this method is more reliable than others to achieve it.
  • [EDIT] If you would like to move the whole list around instead of having it on the leftside remember you can just apply display: flex to the .navig class and play with the justify-content and align-items properties.

CCS and HTML (HTML remained untouched)

.navig li {
    list-style-type: none;
    margin: 30px 0;
}
.navig a {
    display: flex; 
    justify-content: center; /*flex property to center horizontally*/
    align-items: center; /*flex property to center vertically*/
    width: 50px;
    height: 50px;
    border: 1px solid black;
    border-radius: 50%;
    text-decoration: none;
    color: black;
}
ul, li, a {
    margin: 0;
    padding: 0;
}
<div class="container">
    <nav class="navig">
        <ul>
            <li>
                <a href="http://google.com">
                    A
                </a>
            </li>
            <li>
                <a href="http://google.com">
                    B
                </a>
            </li>
            <li>
                <a href="http://google.com">
                    C
                </a>
            </li>
            <li>
                <a href="http://google.com">
                    D
                </a>
            </li>
        </ul>
    </nav>
</div>
like image 159
Adrian Danlos Avatar answered Dec 01 '22 05:12

Adrian Danlos


You could do something like this:

.link {
    height: 35px;
    width: 35px;
    background-color: #FFFFFF;
    border-radius: 50%;
    border-style: solid;
    border-width: 1px;
    border-color: black;
    list-style-type: none;
    text-align: center;
    margin: 0 auto;
    margin-bottom: 10px;
    display: flex;           /* establish flex container */
    justify-content: center; /* center items vertically, in this case */
    align-items: center;     /* center items horizontally, in this case */
}

.navig a {
    text-decoration: none;
    color: black;
}
<form>

 <div class="container">
    <nav class="navig">
      <ul>
        <div class=link>
           <li><a href="http://google.com">A</a></li>
        </div>
        <div class=link>
           <li><a href="http://google.com">B</a></li>
        </div>
        <div class=link>
           <li><a href="http://google.com">C</a></li>
        </div>
        <div class=link>
          <li><a href="http://google.com">D</a></li>
        </div>
      </ul>
    </nav>
 </div>
  
</form>

Edit: I do agree with Adrian that using Flexbox is better than just aligning items by using {text-align: center} and {vertical-align: middle}, and I have modified my answer accordingly.

like image 20
schustischuster Avatar answered Dec 01 '22 03:12

schustischuster