简体   繁体   中英

How can I align my text to center while using float: left;

I am trying to make a simple website but I can't align my unordered list's navigation bar to the center.. I positioned "Contact Us" to be in the right cause that's where I want it to be and I want the rest of the 3 to be in the center but I cannot align it for some reason.. Please help me.. Any help would be greatly appreciated...Here's the code:

Html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NavBar</title>
<link rel="stylesheet" href="Css/styles.css" />
</head>
<body>
<ul>
  <li><a href="#" class="nav">Home</a></li>
  <li><a href="#" class="nav">About us</a></li>
  <li><a href="#" class="nav">Products</a></li>
  <li><a href="#" id="contact">Contact us</a></li>
</ul>
</body>
</html>

css:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  background-color: Aqua;
}

ul {
  list-style-type: none;
  overflow: hidden;
}

li a {
  letter-spacing: 1px;
  background: black;
  padding: 10px;
  display: block;
  margin: 25px;
  color: orange;
  text-decoration: none;
  float: left;
  text-align: center;
}
a:hover {
  color: white;
}

#contact {
  position: relative;
  left: 780px;
}

You can use float: right instead of left: 780px;

 * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Verdana, Geneva, Tahoma, sans-serif; background-color: Aqua; } ul { list-style-type: none; overflow: hidden; } li a { letter-spacing: 1px; background: black; padding: 10px; display: block; margin: 25px; color: orange; text-decoration: none; float: left; text-align: center; } a:hover { color: white; } #contact { position: relative; float:right; }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <title>NavBar</title> <link rel="stylesheet" href="Css/styles.css" /> </head> <body> <ul> <li><a href="#" class="nav">Home</a></li> <li><a href="#" class="nav">About us</a></li> <li><a href="#" class="nav">Products</a></li> <li><a href="#" id="contact">Contact us</a></li> </ul> </body> </html>

Please see output in full screen mode.

 ul { width:100%; display:flex; justify-content:space-evenly; align-items:center; }.center-items { width:75%; display:flex; justify-content:center; align-items:center; }.center-items li { padding:0px 50px; list-style:none; }.right-items li { list-style:none; }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <title>NavBar</title> </head> <body> <ul> <div class="center-items"> <li><a href="#" class="nav">Home</a></li> <li><a href="#" class="nav">About us</a></li> <li><a href="#" class="nav">Products</a></li> </div> <div class="right-items"> <li><a href="#" id="contact">Contact us</a></li> </div> </ul> </body> </html>

 * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Verdana, Geneva, Tahoma, sans-serif; background-color: Aqua; } ul { list-style-type: none; overflow: hidden; display: flex; /*Flexbox*/ justify-content: space-between; width: 100%; padding: 25px; } li a { letter-spacing: 1px; background: black; padding: 10px; display: block; color: orange; margin: 0; text-decoration: none; text-align: center; } a:hover { color: white; }
 <ul> <li><a href="#" class="nav">Home</a></li> <li><a href="#" class="nav">About us</a></li> <li><a href="#" class="nav">Products</a></li> <li><a href="#" id="contact">Contact us</a></li> </ul>

Used flexbox and removed float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM