CSS。改变li边界长度

时间:2016-04-23 11:46:22

标签: html css css3 hover

我有一个导航栏,并在每个列表项上添加了:hover。 但我也有li:after打破了美丽。

nav {
  text-align: center;
}
li {
  list-style: none;
  padding: 10px;
  padding-left: 0;
  margin-top: 20px;
  display: inline-block;
  cursor: pointer;
}
nav li:not(:last-child):after {
  content: '|';
  margin: 10px;
}
li:hover {
  border-bottom: 2px solid black;
}
<nav>
  <ul>
    <li>Chisinau, MD</li>
    <li>Russia, RU</li>
    <li>London, UK</li>
  </ul>
</nav>

我想删除border-bottom处的li:after,我试过了,但它似乎不起作用:

li:after:hover {
  content: '';
  border-bottom: none;
}

Fiddle

3 个答案:

答案 0 :(得分:2)

::after伪元素在元素中 ,而不是在元素之后。

因此,如果您不希望伪元素下方的边框,则应防止它对li的大小产生影响。您可以通过绝对定位来实现这一目标:

li {
  margin: 10px;
}
nav li:not(:last-child):after {
  content: '|';
  position: absolute; /* Take out-of-flow */
  margin-left: 10px;
}

&#13;
&#13;
nav {
  text-align: center;
}
li {
  list-style: none;
  padding: 10px 0;
  margin-top: 20px;
  display: inline-block;
  cursor: pointer;
  margin: 10px;
}
nav li:not(:last-child):after {
  content: '|';
  position: absolute;
  margin-left: 10px;
}
li:hover {
  border-bottom: 2px solid black;
}
&#13;
<nav>
  <ul>
    <li>Chisinau, MD</li>
    <li>Russia, RU</li>
    <li>London, UK</li>
  </ul>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

与@Oriol的答案类似,但在position:relative上使用li::afterli定位li { cursor: pointer; display: inline-block; list-style: outside none none; margin: 10px; position: relative; } nav li:not(:last-child)::after { content: "|"; position: absolute; right: -15px; }

/**
 *  Do not remove the comment below. It's the markers used by wiredep to inject
 *  sass dependencies when defined in the bower.json of your dependencies
 */

// bower:scss
// endbower

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}
body {
  line-height: 1;
}
ol,
ul {
  list-style: none;
}
blockquote,
q {
  quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
/*** */

@import url(http://fonts.googleapis.com/css?family=Roboto+Slab:400,700|Roboto:400,700,700italic,400italic);
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(../../bower_components/material-design-iconfont/iconfont/MaterialIcons-Regular.eot);
  /* For IE6-8 */
  src: local('Material Icons'), local('MaterialIcons-Regular'), url(../../bower_components/material-design-iconfont/iconfont/MaterialIcons-Regular.woff2) format('woff2'), url(../../bower_components/material-design-iconfont/iconfont/MaterialIcons-Regular.woff) format('woff'), url(../../bower_components/material-design-iconfont/iconfont/MaterialIcons-Regular.ttf) format('truetype');
}
.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  /* Preferred icon size */
  display: inline-block;
  width: 1em;
  height: 1em;
  line-height: 1;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  /* Support for all WebKit browsers. */
  -webkit-font-smoothing: antialiased;
  /* Support for Safari and Chrome. */
  text-rendering: optimizeLegibility;
  /* Support for Firefox. */
  -moz-osx-font-smoothing: grayscale;
  /* Support for IE. */
  font-feature-settings: 'liga';
}
html {
  font-family: 'Roboto Slab', serif;
}
[layout=row] {
  flex-direction: row;
}
.browsehappy {
  margin: 0.2em 0;
  background: #ccc;
  color: #000;
  padding: 0.2em 0;
}
md-toolbar.md-default-theme {
  background-color: black;
}
section.jumbotron {
  margin-bottom: 30px;
  padding: 1px 30px;
  background-color: #5aadbb;
  text-align: center;
  color: white;
}
h1 {
  font-size: 3em;
}
.techs {
  display: flex;
  flex-flow: row wrap;
}
md-card {
  width: 30%;
}
img.pull-right {
  float: right;
  width: 100px;
}
//MY-css
h1 {
  text-align: center;
  font-size: 50px;
  // border:1px solid black;
  margin: 0;
  margin-top: 125px;
  margin-bottom: 20px;
}
#head {
  /* margin-top: -15.75px; */
  // padding-top: 0;
  height: 100px;
  line-height: 100px;
  background-color: rgb(153, 153, 153);
  text-align: center;
  font-size: 40px;
  color: #fff;
}
nav {
  // border: 1px solid black;
  text-align: center;
}
li {
  cursor: pointer;
  display: inline-block;
  list-style: outside none none;
  margin: 10px;
  position: relative;
}
nav li:not(:last-child)::after {
  content: "|";
  position: absolute;
  right: -15px;
}
li:hover {
  border-bottom: 2px solid black;
}
/**
 *  Do not remove the comment below. It's the markers used by gulp-inject to inject
 *  all your sass files automatically
 */

// injector
// endinjector

<div id='head'>Star METEO</div>


<nav>
  <ul>
    <!-- 		<li>Chisinau, MD</li> -->
    <li class='li'>Chisinau, MD</li>
    <li class='li'>Russia, RU</li>
    <li class='li'>London, UK</li>
  </ul>
</nav>
fs

答案 2 :(得分:0)

<a><span>标记放在<li>中,如下所示:

<li class='li'><a href="#">Chisinau, MD</a></li>

然后在它悬停时使用border-bottom,就像这样:

li a:hover {
  border-bottom: 2px solid black;
}
  

请记住为你的<a>设计风格。

     

示例:

     

li a {   颜色:#333;   text-decoration:none;   }