How to make UL list items in a single line

匆匆过客 提交于 2020-01-16 02:52:30

问题


I'm new to HTML, and I am trying to figure out how to align UL list items in a single line.

I'm just starting to learn HTML. I have already checked more than 10 Stack Overflow questions but none of them are working. Here is the code:

display :inline;
float: right;
margin: 0px;
padding: 0px;

And here is my html markup, can you help me to check if there are any errors?

@font-face {
    font-family:'BrandonGrotesqueBold';
    src: url('file:///C:/Users/MyName/Desktop/project/fonts/BrandonGrotesqueBold.ttf');
}
@font-face {
    font-family:'BrandonGrotesqueLight';
    src: url('file:///C:/Users/MyName/Desktop/project/fonts/BrandonGrotesqueLight.ttf');
}
body {
    padding-top: 105px;
    color: #CDCDCD;
    font-family:'BrandonGrotesqueBold';
}
.mainHeader {
    margin-left: auto;
    margin-right: auto;
    padding-left: 20px;
    padding-right: 20px;
    width: 1060px;
    font-size: 22px;
    font-stretch: none;
}
.mainNav {
    margin-bottom: 70px;
}
.mainLogo {
    font-weight: 500;
    s color: black;
    font-size: 40px;
}
.thinMainLogo {
    font-family:'BrandonGrotesquelight';
}
li {
    list-style-type: none;
}
.mainLoc {
    float: right;
    display: inline;
}
<!DOCTYPE html>
<title>My Company Name</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
    <header class="mainHeader">
        <nav class="mainNav">
             <h1 class="mainLogo">My <span class="thinMainLogo">Logo</span></h1>

            <ul class="mainLoc">
                <li>About</li>
                <li>PortFolio</li>
                <li>Contact</li>
                <li>Blog</li>
            </ul>
        </nav>
    </header>
</body>

</html>

http://jsfiddle.net/bLc5eqgp/3/


回答1:


This is the CSS you need to place them on the same line:

ul.mainLoc li {
    display: inline-block;
    margin-left: 10px; // for item spacing
}



回答2:


Check now http://jsfiddle.net/bLc5eqgp/3/

.mainLoc li {
    padding: 0 10px;
    display: inline;
}

added above css to your fiddle.




回答3:


Add display: inline to .mainLogo and display: inline-block to li

Here is the full CSS

body {
  padding-top: 105px;
  color: #CDCDCD;
  font-family:'BrandonGrotesqueBold';
}
.mainHeader {
margin-left: auto;
margin-right: auto;
padding-left: 20px;
padding-right: 20px;
width: 1060px;
font-size: 22px;
font-stretch: none;
}
.mainNav {
margin-bottom: 70px;
}
.mainLogo {
font-weight: 500;
color: black;
font-size: 40px;
display: inline;
}
.thinMainLogo {
font-family:'BrandonGrotesquelight';
}
li {
display: inline-block;
list-style-type: none;
}
.mainLoc {
float: right;
display: inline;
}

Check this JS Fiddle



来源:https://stackoverflow.com/questions/31298447/how-to-make-ul-list-items-in-a-single-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!