gpt4 book ai didi

html - 当内容文本字体大小变大时,如何避免元素扩展?

转载 作者:行者123 更新时间:2023-11-28 00:42:47 26 4
gpt4 key购买 nike

当我将鼠标悬停在其中一个元素上时,该元素本身会展开,而其他元素会在字体大小变大时进行调整。如何阻止它?

同样的事情发生在红色背景上,所以我设置了一个 height: 38px;避免扩展:

header {
position: relative;
top: 10px;
margin: 0 auto;
height: 38px;
width: 100%;
background: red;
}

现在我尝试将高度和宽度设置为单个 <li>查看它是否根本不会移动/展开,但不起作用:

<li style="width: 130px; height: 38px;"><a href="homepage.html">Home</a></li>

* {
margin: 0;
padding: 0;
}

body {
background: royalblue;
font-family: monospace;
}

header {
position: relative;
top: 10px;
margin: 0 auto;
height: 38px;
width: 100%;
background: red;
}
.parent-ul {
position: relative;
top: 0px;
list-style: none;
text-align: center;
padding: 11px 0;
}

.parent-ul li {
margin: -4px;
display: inline;
padding: 7px 13px;
border-left: 1px solid silver;
transition: background 1s, border 1s,
border-radius 1s;
}

#prod {
border-right: 1px solid silver;
padding-right: 15px;
}

.parent-ul a {
text-decoration: none;
color: white;
padding: 0px 50px;
font-size: 14px;
transition: color 1s, font-size .5s;
}

/* Effects ================================ */

li:hover {
background: black;
border: 0;
border-radius: 2px;
}

li:hover + li {
border: 0;
}

li:hover a {
color: gold;
font-size: 18px;
}

#prod:hover {
border: 0;
}
<body>   
<header>
<ul class="parent-ul">

<li style="width: 130px; height: 38px;"><a href="homepage.html">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Services</a></li>
<li id="prod"><a href="index.html">Products</a></li>

</ul>
</header>
</body>

最佳答案

Yoy 可以使用float: leftbox-sizing: border-box 来对齐导航项。此外,使用 padding : 0 这样当您将文本变大时,它会居中。这是一个例子:

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

body {
background: royalblue;
font-family: monospace;
}

header {
position: relative;
margin: 0 auto;
height: 38px;
width: 100%;
background: red;
}
.parent-ul {
position: relative;
list-style: none;
text-align: center;
}

.parent-ul li {
height: 38px;
width: 20%;
float: left;
transition: background 1s, border 1s,
border-radius 1s;
}

.parent-ul li:not(:first-child) {
border-left: 1px solid silver;
}

.parent-ul a {
text-decoration: none;
color: white;
padding: 0;
font-size: 14px;
transition: color 1s, font-size .5s;
line-height: 38px;
}

/* Effects ================================ */

li:hover {
background: black;
border: 0;
border-radius: 2px;
cursor: pointer;
}

li:hover a {
color: gold;
font-size: 18px;
}
<body>   
<header>
<ul class="parent-ul">

<li><a href="homepage.html">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Services</a></li>
<li><a href="index.html">Products</a></li>

</ul>
</header>
</body>

关于html - 当内容文本字体大小变大时,如何避免元素扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52473981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com