如何使用其他元素将图像置于列表中心

时间:2017-06-07 05:05:58

标签: html css

我有一个无序列表,里面包含几个列表元素。内部也是要居中的图像元素。我尝试使用保证金:auto;属性,但由于侧面有列表元素,因此在集中自身时会考虑到这一点。如何将图像置于其容器中心?



.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
}

.nav li {
  float: left;
  vertical-align: top;
  margin: 5px 10px;
}

nav li:first-child {
  margin-left: 75px;
}

.nav img {
  display: block;
  margin: auto;
  height: 21px;
  width: 24px;
}

<nav class="nav">
  <ul class="clearfix">
    <li>Home</li>
    <li>Moments</li>
    <li>Notifications</li>
    <li>Messages</li>
    <img src="assets/image/twitter-icon.png" alt="Twitter Icon">
  </ul>
</nav>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

您可能需要使用以下内容将图片absolute放入nav元素中

position: absolute;
left: 50%;
transform: translateX(-50%);

同样position:relative nav元素。

这样,图像会忽略nav元素内的任何内容。

body{
margin:0px;
}

.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
  position:relative;
}

.nav li {
  vertical-align: top;
  margin: 5px 10px;
  display: inline;
}

nav li:first-child {
  margin-left: 75px;
}

.nav img {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  height: 21px;
  width: 24px;
}
<nav class="nav">
  <ul class="clearfix">
    <li>Home</li>
    <li>Moments</li>
    <li>Notifications</li>
    <li>Messages</li>
    <img src="assets/image/twitter-icon.png" alt="Twitter Icon">
  </ul>
</nav>

答案 1 :(得分:0)

将您的img元素放入li并在您的css中进行一些更改,如下面的代码段:

.nav ul {
  width: 800px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
  padding: 0;
  list-style-type: none;
  text-align: center;
}

.nav li {
  display: inline-block;
  vertical-align: middle;
  margin: 5px 10px;
}

.nav img {
  display: block;
  margin: auto;
}

.nav ul:before,
.nav ul:after {
  content: "";
  display: table;
}

.nav ul:after {
  clear: both;
}
<!--html-->
<nav class="nav">
  <ul class="clearfix">
    <li>Home</li>
    <li>Moments</li>
    <li>Notifications</li>
    <li>Messages</li>
    <li><img src="//login.create.net/images/icons/user/twitter_30x30.png" alt="Twitter Icon"></li>
  </ul>
</nav>

答案 2 :(得分:0)

您可以使用display:inline-block来获取它。

.nav img {
  display: inline-block; /*Add this*/
  vertical-align: middle; /*Add this*/
  height: 21px;
  width: 24px;
}

.nav li {
  display:inline-block; /*Add this*/
  vertical-align: middle; /*Add this*/
  margin: 5px 10px;
}

.nav {
  text-align:center; /*Add this*/
}

&#13;
&#13;
.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
  text-align:center;
}

.nav li {
  display:inline-block;
  vertical-align: middle;
  margin: 5px 10px;
}

nav li:first-child {
  margin-left: 75px;
}

.nav img {
  display: inline-block;
  vertical-align: middle;
  height: 21px;
  width: 24px;
}
&#13;
<nav class="nav">
  <ul class="clearfix">
    <li>Home</li>
    <li>Moments</li>
    <li>Notifications</li>
    <li>Messages</li>
    <img src="http://lorempixel.com/200/200/" alt="Twitter Icon">
  </ul>
</nav>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

  

1)所有元素都是中心

.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
}

.nav li {
  float: left;<------Remove
  display: inline-block;<-----------Add
  vertical-align: top;
  margin: 5px 10px;
}


nav li:first-child {
  margin-left: 75px;<---------Change to margin-left: 0px;------
}

.nav img {
  display: block;<----------Change to display: inline-block;---
  margin: auto;<---------Remove
  height: 21px;
  width: 24px;
}

.clearfix{<-----------Add this Selector
  text-align: center;
  width: 100%;
  padding-left: 0;
}

完整代码:

.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
}

.nav li {
  display: inline-block;
  vertical-align: top;
  margin: 5px 10px;
}


nav li:first-child {
  margin-left: 0px;
}

.nav img {
  display: inline-block;
  height: 21px;
  width: 24px;
}

.clearfix{
  text-align: center;
  width: 100%;
  padding-left: 0;
}
  <nav class="nav">
    <ul class="clearfix">
      <li>Home</li>
      <li>Moments</li>
      <li>Notifications</li>
      <li>Messages</li>
      <img src="http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg" alt="Twitter Icon">
    </ul>
  </nav>

  

2)只是图像为中心

.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
  position: relative;<---------Add 
}


.nav li {
  float: left;<---------remove
  vertical-align: top;
  margin: 5px 10px;
}



nav li:first-child {
  margin-left: 75px;
}

.nav img {
  display: block;<-------Change To  display: inline-block;---
  margin: auto;<----------remove---------
  right: 0;<--------------Add
  position: absolute;<-------------Add
  height: 21px;
  width: 24px;
}

.clearfix { <----------------Add
  width: 50%;
  padding-left: 0;
  position: relative;
}

完整代码:

.nav {
  width: 1280px;
  margin: 0 auto;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid black;
  position: relative;
}

.nav li {
  display: inline-block;
  vertical-align: top;
  margin: 5px 10px;

}

nav li:first-child {
  margin-left: 75px;
}

.nav img {
  display: inline-block;
  height: 21px;
  width: 24px;
  right: 0;
  position: absolute;
}

.clearfix { 
      width: 50%;
      padding-left: 0;
      position: relative;
}
<nav class="nav">
    <ul class="clearfix">
      <li>Home</li>
      <li>Moments</li>
      <li>Notifications</li>
      <li>Messages</li>
      <img src="http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg" alt="Twitter Icon">
    </ul>
  </nav>