文字对齐不起作用

时间:2016-03-25 16:10:11

标签: html css

我正在建立一个网站但由于某种原因,我的文字对齐无效。当我输入它时,它对它没有影响。我需要一些帮助。我希望下拉菜单中的内容与左侧对齐。

HTML代码

<!DOCTYPE html>
<html>
    <head>
        <meta  charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My Cycle - Home</title>
        <link rel="stylesheet" href="css/style.css" type="text/css" />
    </head>
    <body>
        <header>
            <div id="heading">
                <img src="http://i.imgsafe.org/5d32881.png" id="image_symbol"></img>
            </div>
            <div id="navbar">
                <ul id="main_navbar">
                    <li>
                        <a href="#" style="z-index: -1" class="active">Home</a>
                    </li>
                    <li class="dropdown">
                        <a class="dropbtn" style="z-index: -1">Rent</a>
                        <div class="dropdown-content">
                            <a href="rental/mountain_bikes.html">Mountain Bikes</a>
                            <a href="rental/hybrid_bikes.html">Hybrid Bikes</a>
                            <a href="rental/road_bikes.html">Road Bikes</a>
                            <a href="rental/city_bikes.html">City Bikes</a>
                        </div>
                    </li>
                    <li>
                        <a href="faq.html" style="z-index: -1">FAQ's</a>
                    </li>
                    <li>
                        <a href="about.html" style="z-index: -1">About</a>
                    </li>
                </ul>
                <ul id="sub_navbar">
                    <li>
                        <a href="contact_us.html" style="margin-right: 10px">Contact Us</a>
                    </li>
                    <li>
                        <a href="log_in.php">Log In</a>
                    </li>
                    <li>
                        <a href="sign_up.html">Sign Up</a>
                    </li>
                </ul>
            </div>
        </header>
        <section>

        </section>
        <footer>

        </footer>
    </body>
</html>

CSS代码:

body {
  font-family: 'Open-sans', sans-serif, Helvetica;
  text-align: center;
  box-sizing: border-box;
}

#main_navbar {
  text-align: center;
  margin-top: 7px;
  float: left;
  padding-left: 0;
}

#main_navbar li, #sub_navbar li {
  list-style-type: none;
  display: inline-block;
  min-width: 5em;
}

#main_navbar li a, #sub_navbar li a {
  text-decoration: none;
  font-size: 1em;
  text-align: center;
  color: black;
  font-weight: normal;
}

#sub_navbar {
  float: right;
  position: relative;
  right: 15px;
  bottom: 10px;
}

.dropdown {
  position: relative;
}

.dropdown:active {
  color: #32673f;
}

.dropdown-content {
  display: none;
  min-width: 150px;
  right: 50%;
  transform: translate(90%,0);
  position: absolute;
  padding: 10px 0;
  border-radius: 5px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

.dropdown-content a {
  display: block;
  font-weight: normal;
  text-align: left;
}

.dropdown:hover .dropdown-content {
  display: block;
  background-color: #f9f9f9;
}

#image_symbol {
  position: relative;
  float: left;
  bottom: 10px;
  left: 0px;
}

header {
  height: 40px;
  width: 100%;
  padding-right: 30px;
  margin-left: -20px;
  margin-top: -20px;
  padding-top: 20px;
}

#sub_navbar li a:hover, #main_navbar li a:hover {
  color: #FFCC00;
}

#sub_navbar li .active, #main_navbar li .active {
  color: #09c;
}

header {
  background-color: #f6f6f6;
}

提前谢谢!

2 个答案:

答案 0 :(得分:1)

删除

text-align: center; 

从这里

#main_navbar li a, #sub_navbar li a {
text-decoration: none;
font-size: 1em;
text-align: center;
color: black;
font-weight: normal;

}

答案 1 :(得分:0)

正如@Violetluna所说,从text-align: center;

中移除#main_navbar li a, #sub_navbar li a
#main_navbar li a, #sub_navbar li a {
  text-decoration: none;
  font-size: 1em;
  text-align: center;
  color: black;
  font-weight: normal;
}

您不必将text-align: left;放在上面,因为它是默认属性。

此外,<img>的语法不正确,不是:

<img src="http://i.imgsafe.org/5d32881.png" id="image_symbol"></img>

是:

<img src="http://i.imgsafe.org/5d32881.png" id="image_symbol" />

See this fiddle