You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为博客侧边栏的链接添加间距?附HTML/CSS代码求优化

没问题!我帮你调整代码来实现你想要的侧边栏效果——带标题、链接(及后续添加的图片)有间距、还有可见边框。下面是修改后的完整代码,我会同步解释关键改动:

修改后的完整代码

CSS 部分

* { box-sizing: border-box; }
@font-face { src: url(fonts/WaywardSans-Regular.otf); font-family: wayward; }
body { margin: 0; background: #fff; font-family: wayward; font-weight: 100; height: 100%; overflow-x: hidden; overflow-y: scroll; display: flex; flex-wrap: wrap; }
header { background: #55d6aa; flex: 0 0 100%; }
header::after { content: ''; display: table; clear: both; }
.logo { float: left; padding: 10px 0; margin-left: 30px; }
nav { float: right; }
nav ul { margin-right: 60px; padding: 0; list-style: none; }
nav li { display: inline-block; margin-left: 100px; padding-top: 30px; position: relative; }
nav a { color: #444; text-decoration: none; text-transform: uppercase; font-size: 20px; }
nav a:hover { color: #000; }
nav a::before { content: ''; display: block; height: 5px; background-color: #444; position: absolute; top: 0; width: 0%; transition: all ease-in-out 250ms; }
nav a:hover::before { width: 100%; }
h1 { margin: 10px; }
img { max-width: 100%; }

/* --- 核心:侧边栏样式修改 --- */
#sidebar {
  flex: 0 0 25%;
  max-width: 250px;
  background-color: #DDD;
  /* 新增:添加边框、内边距和外边距 */
  border: 2px solid #444;
  padding: 15px;
  margin: 10px;
}

/* 侧边栏标题样式 */
#sidebar h2 {
  margin-top: 0;
  text-align: center;
  color: #444;
  border-bottom: 1px solid #444;
  padding-bottom: 8px;
}

/* 重置列表默认样式 */
#sidebar ul {
  list-style: none;
  padding: 0;
  margin: 10px 0 0 0;
}

/* 列表项间距控制 */
#sidebar ul li {
  margin-bottom: 12px;
}

/* 优化链接样式与交互 */
#sidebar ul li a {
  text-decoration: none;
  color: #444;
  display: block; /* 让链接占满列表项宽度,提升点击体验 */
  padding: 8px;
  border-radius: 4px;
  transition: background 0.2s ease;
}

#sidebar ul li a:hover {
  background-color: #55d6aa;
  color: #fff;
}

.review { line-height: 29.25px; padding-top: 5px; text-align: center; border-width: 1px; margin: 10px; padding: 5px; word-wrap: break-word; flex: 1; }
.text-wrapper { max-width: 800px; margin: auto; }

HTML 部分

<header>
 <div class="container">
 <div class="logo">
 <img src="logo.png" height="90" width="280">
 </div>
 <nav>
 <ul>
 <li><a href="#">Our Top Picks</a></li>
 <li><a href="#">Wall of Shame</a></li>
 <li><a href="#">Movies</a></li>
 <li><a href="#">Tv Shows</a></li>
 </ul>
 </nav>
 </div>
</header>

<div id="sidebar">
  <!-- 新增侧边栏标题 -->
  <h2>Movie Reviews</h2>
  <ul>
 <li><a href="#">Star wars movie review</a></li>
 <li><a href="#">E.T movie review</a></li>
 <li><a href="#">Happy Death Day Movie review</a></li>
 <li><a href="#">Thor ragnorok movie review</a></li>
 </ul>
</div>

<div class="review">
 <h1>Titanic Movie Review 1996</h1>
 <h3>-By Some random guy</h3>
 <div class="thumbnail">
 <img src="https://static3.srcdn.com/wordpress/wp-content/uploads/2020/01/Rose-DeWitt-Bukater-and-Jack.png?q=50&fit=crop&w=767&h=450&dpr=1.5" alt="An Image of Jack holding rose from behind">
 </div>
 <div class="text-wrapper">
 <p>
 The Titanic is a classic movie filmed in 1996, with jack and rose, it is a classic tradgedy and feautures kate and leonardo da vinci, one is poor, one is rich, the girl has a expensive random amulet that look quite cool i think, yeah, and then the ships crashes and they all die! except for rose. And heres a random movie review from somewhere:<br><br>
 Like a great iron Sphinx on the ocean floor, the Titanic faces still toward the West, interrupted forever on its only voyage. We see it in the opening shots of “Titanic,” encrusted with the silt of 85 years; a remote-controlled TV camera snakes its way inside, down corridors and through doorways, showing us staterooms built for millionaires and inherited by crustaceans.<br><br>
 These shots strike precisely the right note; the ship calls from its grave for its story to be told, and if the story is made of showbiz and hype, smoke and mirrors--well, so was the Titanic. She was “the largest moving work of man in all history,” a character boasts, neatly dismissing the Pyramids and the Great Wall. There is a shot of her, early in the film, sweeping majestically beneath the camera from bow to stern, nearly 900 feet long and “unsinkable,” it was claimed, until an iceberg made an irrefutable reply.<br><br>
 James Cameron's 194-minute, $200 million film of the tragic voyage is in the tradition of the great Hollywood epics. It is flawlessly crafted, intelligently constructed, strongly acted and spellbinding. If its story stays well within the traditional formulas for such pictures, well, you don't choose the most expensive film ever made as your opportunity to reinvent the wheel.<br><br>
 We know before the movie begins that certain things must happen. We must see the Titanic sail and sink, and be convinced we are looking at a real ship. There must be a human story--probably a romance--involving a few of the passengers. There must be vignettes involving some of the rest and a subplot involving the arrogance and pride of the ship's builders--and perhaps also their courage and dignity. And there must be a reenactment of the ship's terrible death throes; it took two and a half hours to sink, so that everyone aboard had time to know what was happening, and to consider their actions.<br><br>
 All of those elements are present in Cameron's “Titanic,” weighted and balanced like ballast, so that the film always seems in proportion. The ship was made out of models (large and small), visual effects and computer animation. You know intellectually that you're not looking at a real ocean liner--but the illusion is convincing and seamless. The special effects don't call inappropriate attention to themselves but get the job done.<br><br>
 The human story involves an 17-year-old woman named Rose DeWitt Bukater (Kate Winslet) who is sailing to what she sees as her own personal doom: She has been forced by her penniless mother to become engaged to marry a rich, supercilious snob named Cal Hockley (Billy Zane), and so bitterly does she hate this prospect that she tries to kill herself by jumping from the ship. She is saved by Jack Dawson (Leonardo DiCaprio), a brash kid from steerage, and of course they will fall in love during the brief time left to them.<br><br>
 The screenplay tells their story in a way that unobtrusively shows off the ship. Jack is invited to join Rose's party at dinner in the first class dining room, and later, fleeing from Cal's manservant, Lovejoy (David Warner), they find themselves first in the awesome engine room, with pistons as tall as churches, and then at a rousing Irish dance in the crowded steerage. (At one point Rose gives Lovejoy the finger; did young ladies do that in 1912?) Their exploration is intercut with scenes from the command deck, where the captain (Bernard Hill) consults with Andrews (Victor Garber), the ship's designer and Ismay (Jonathan Hyde), the White Star Line's managing director.<br><br>
 </p>
 </div>
</div>

关键改动说明

  1. 侧边栏标题:在HTML的#sidebar里新增了<h2>标题,通过CSS设置了居中、底部边框和内边距,让标题和链接区域清晰区分。
  2. 可见边框与间距:给#sidebar添加了border(边框)、padding(内边距)和margin(外边距),让侧边栏和页面其他元素拉开距离,同时边框清晰可见。
  3. 链接间距优化:重置了侧边栏内<ul>的默认样式,给每个<li>添加margin-bottom,让链接之间有均匀的垂直间距。
  4. 交互体验提升:设置链接为display:block,扩大点击区域;添加hover背景色变化,让用户能直观感知可交互元素。
  5. 图片适配(后续扩展):如果之后要在侧边栏添加图片,只需要在<li>中插入<img>标签,然后添加以下CSS即可保持图片与链接的间距:
    #sidebar ul li img {
      max-width: 100%;
      margin-bottom: 8px;
    }
    

内容的提问来源于stack exchange,提问作者TheTimeVader

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:02:54