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

如何将HTML/CSS中skill-1、skill-2元素设置为并排居中?

问题:无法让skill-1和skill-2元素并排显示并居中

尝试多次仍无法将class为skill-1和skill-2的元素设置为并排显示且居中,现有HTML与CSS代码如下:

HTML代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Ari's Portfolio</title>
    <link rel="stylesheet" href="style.css" />
  </head>

  <body>
    <main>
      <!-- Top Navigation Bar -->
      <ul class="nav">
        <li><a href="/home/index.html">Home</a></li>
        <li><a href="">About</a></li>
        <li><a href="">Services</a></li>
        <li><a href="">Projects</a></li>
        <li><a href="" class="active">Skills</a></li>
        <li><a href="">Contact</a></li>
      </ul>

      <h1>My Skills</h1>

      <!-- Skills -->
      <div class="skills">
        <!--Skill 1 -->
        <div class="skill-1">
          <img src="https://upload.wikimedia.org/wikipedia/commons/3/38/HTML5_Badge.svg">
        </div>
        <div class="skill-2">
          <img src="https://upload.wikimedia.org/wikipedia/commons/6/62/CSS3_logo.svg">
      </div>
    </div>  
    </main>
  </body>
</html>

CSS代码

@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800;900&display=swap');

* {
    font-family: 'Montserrat', sans-serif;
}

body {
    margin: 0;
    background-color: #ECECEC;
}

.nav {
    list-style: none;
    margin: 0;
    padding: 0;
    text-align: center;
    background-color: #FFFFFF;  
    width: 100%;
}

.nav li {
    display: inline;
    font-size: 1.0625em;
}

.nav a {
    display: inline-block;
    padding: 2% 1%;
    text-decoration: none;
    color: #000000;
    font-weight: 800;
}

.nav a:hover {
    color: #f3ca20;
    transition: 0.25s;
}

h1 {
    text-align: center;
    font-size: 3.125em;
    font-weight: 700;
    color: #000000;
}

.skills {
    width: 100%;
    height: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    position: absolute;
}

.skill-1 {
    background-color: #F7F7F7;
    width: 25%;
    height: 53%;
    border: none;
    border-radius: 25px;
    text-align: center;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
}

.skill-1:hover {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    transition: 0.35s;
    cursor: pointer;
    background-color: #FFFFFF;
}

.skill-1 img {
    height: 80%;
    width: 80%;
    margin-top: 12%;
}

.skill-2 {
    background-color: #F7F7F7;
    width: 25%;
    height: 53%;
    border: none;
    border-radius: 25px;
    text-align: center;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
}

.skill-2:hover {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    transition: 0.35s;
    cursor: pointer;
    background-color: #FFFFFF;
}

.skill-2 img {
    height: 80%;
    width: 80%;
    margin-top: 12%;
}
解决方案

要实现两个skill元素并排且居中,最简单的方式是给父容器.skills添加Flex布局属性:

修改.skills的CSS样式

.skills {
    width: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    /* 新增以下Flex属性 */
    display: flex;
    justify-content: center; /* 让子元素水平居中 */
    gap: 2rem; /* 控制两个skill元素之间的间距,可根据需求调整 */
}

修改后,两个skill元素会自动并排显示且整体居中。

(可选)优化代码结构

当前skill-1和skill-2的CSS代码完全重复,可以统一使用一个类名(比如.skill-item)来减少冗余:

优化后的HTML

<!-- Skills -->
<div class="skills">
  <!--Skill 1 -->
  <div class="skill-item">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/38/HTML5_Badge.svg">
  </div>
  <div class="skill-item">
    <img src="https://upload.wikimedia.org/wikipedia/commons/6/62/CSS3_logo.svg">
  </div>
</div>  

优化后的CSS

.skill-item {
    background-color: #F7F7F7;
    width: 25%;
    height: 53%;
    border: none;
    border-radius: 25px;
    text-align: center;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
}

.skill-item:hover {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    transition: 0.35s;
    cursor: pointer;
    background-color: #FFFFFF;
}

.skill-item img {
    height: 80%;
    width: 80%;
    margin-top: 12%;
}

这样代码更简洁,后续新增skill元素时也无需重复编写相同样式。


内容的提问来源于stack exchange,提问作者Ari'sKindaDumb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 19:45:59