使用CSS将两个项目对齐到相同的高度

huangapple go评论75阅读模式
英文:

Aligning on the same height two items using in CSS

问题

I'm currently trying to align my text 'hello' with the pencil icon you see below. But apparently the pencil icon just wants to sit below the text.

Both the text and the icon are contained within a div with the following style:

border-radius: 4px;
height: 50px;
border: 1px solid transparent;

while the icon itself is a div with an icon inside of it, and the div has the following style applied to it:

width: 40px;
opacity: 0;
margin-left: auto;
height: 50px;
background-color: #88888850;

使用CSS将两个项目对齐到相同的高度

How can I move up the edit icon in order to match the top and bottom of the bordered container?

I created also an example in this snippet:

<body>
  <div class="bordered-container">
    <span class="title">Hello</span>
    <div class="edit-icon">
      EDIT
    </div>
  </div>
</body>
英文:

I'm currently trying to align my text 'hello' with the pencil icon you see below. But apparently the pencil icon just wants to sit below the text.

Both the text and the icon are contained within a div with the following style:

  border-radius: 4px;
  height: 50px;
  border: 1px solid transparent;

while the icon itself is a div with an icon inside of it, and the div has the following style applied to it:

  width: 40px;
  opacity: 0;
  margin-left: auto;
  height: 50px;
  background-color: #88888850;

使用CSS将两个项目对齐到相同的高度

How can I move up the edit icon in order to match the top and bottom of the bordered container?

I created also an example in this snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.bordered-container {
    height: 50px;
    border: 1.5px solid #88888850;
    margin: 30px;
}

.title {
  text-align: center;
  flex: 1;
  font-size: 1.8rem;
  font-weight: bolder;
}

.edit-icon {
  width: 20px;
  opacity: 1;
  display: flex;
  justify-content: center;
  padding: 10px;
  align-items: center;
  margin-left: auto;
  margin-top: 0;
  height: 50px;
  background-color: #88888850;
}

<!-- language: lang-html -->

&lt;body&gt;

  &lt;div class=&quot;bordered-container&quot;&gt;
    &lt;span class=&quot;title&quot;&gt;Hello&lt;/span&gt;
    &lt;div class=&quot;edit-icon&quot;&gt;
      EDIT
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/body&gt;

<!-- end snippet -->

答案1

得分: 3

尝试以下我下面的内容。我已经注释掉了很多你不需要的东西,并标记了我添加的部分。基本上容器需要是一个 flexbox,而不是 edit-icon。另外,如果你不定义高度,事物将按照它们占用的大小进行排列,这需要更少的代码。

如果你还没有看过的话,这个页面 是一个关于学习 Flexbox 的非常好的资源。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.bordered-container {
  /*height: 50px;*/
  border: 1.5px solid #88888850;
  margin: 30px;
  
  /* ADDED */
  display: flex;
  align-items: center;
}

.title {
  text-align: center;
  flex: 1;
  font-size: 1.8rem;
  font-weight: bolder;
}

.edit-icon {
  /*width: 20px;
  opacity: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: auto;
  margin-top: 0;
  height: 50px;*/
  
  padding: 10px;
  background-color: #88888850;
}

<!-- language: lang-html -->

<div class="bordered-container">
  <span class="title">Hello</span>
  <div class="edit-icon">
    EDIT
  </div>
</div>

<!-- end snippet -->
英文:

Try what I have below. I've commented out a lot of stuff you didn't need and I marked what I've added. Basally the container needs to be a flexbox, not the edit-icon. Also if you don't define heights, things will just flow to the sizes they take up, and it require less code.

this page is a really great resource on learning about Flexbox if you haven't seen it already.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.bordered-container {
  /*height: 50px;*/
  border: 1.5px solid #88888850;
  margin: 30px;
  
  /* ADDED */
  display: flex;
  align-items: center;
}

.title {
  text-align: center;
  flex: 1;
  font-size: 1.8rem;
  font-weight: bolder;
}

.edit-icon {
  /*width: 20px;
  opacity: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: auto;
  margin-top: 0;
  height: 50px;*/
  
  padding: 10px;
  background-color: #88888850;
}

<!-- language: lang-html -->

&lt;div class=&quot;bordered-container&quot;&gt;
  &lt;span class=&quot;title&quot;&gt;Hello&lt;/span&gt;
  &lt;div class=&quot;edit-icon&quot;&gt;
    EDIT
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月12日 23:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定