如何将一个 div(或任何内容)居中

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

How to centre a div (or any content)

问题

我在页面中添加了一个小的div,作为一种很酷的方式来显示3个选择选项,但它显示在左侧。我不太确定我需要使用什么来将这个div“card”显示在中间。我尝试过Align-itemsalign-content甚至text-align,但都不起作用,我不知道是否还有其他选项。

body {
    background: #212121;
    margin: 0;
}

header {
    text-align: center;
    font-family: 'Roboto', sans-serif;
    color: #ff568e;
    border-bottom: 4px dashed #ff568e;
}

.card {
    width: 210px;
    height: 254px;
    border-radius: 4px;
    background: #212121;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 5px;
    padding: .4em;
}

.card p {
    height: 100%;
    flex: 1;
    overflow: hidden;
    cursor: pointer;
    border-radius: 2px;
    transition: all .5s;
    background: #212121;
    border: 1px solid #ff5a91;
    display: flex;
    justify-content: center;
    align-items: center;
}

.card p:hover {
    flex: 4;
}

.card p span {
    min-width: 14em;
    padding: .5em;
    text-align: center;
    transform: rotate(-90deg);
    transition: all .5s;
    text-transform: uppercase;
    color: #ff568e;
    letter-spacing: .1em;
}

.card p:hover span {
    transform: rotate(0);
}

请问有人能告诉我如何居中显示它?

英文:

I added a small div in my page as a cool way to display 3 options to choose, but it's showing the box in the left side. I don't know exactly what I need to use so it displays the div "card" in the middle. I tried Align-items,align-content and even text align but none of them works and I don't know if there is any other option.

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

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

body{
    background: #212121;
    margin: 0;
}

header{
    text-align: center;
    align-items: center;
    font-family: &#39;Roboto&#39;, sans-serif;
    color: #ff568e;
    border-bottom: 4px dashed #ff568e;
}

.card {
    align-items: center;
    width: 210px;
    height: 254px;
    border-radius: 4px;
    background: #212121;
    display: flex;
    gap: 5px;
    padding: .4em;
  }
  
  .card p {
    height: 100%;
    flex: 1;
    overflow: hidden;
    cursor: pointer;
    border-radius: 2px;
    transition: all .5s;
    background: #212121;
    border: 1px solid #ff5a91;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .card p:hover {
    flex: 4;
  }
  
  .card p span {
    min-width: 14em;
    padding: .5em;
    text-align: center;
    transform: rotate(-90deg);
    transition: all .5s;
    text-transform: uppercase;
    color: #ff568e;
    letter-spacing: .1em;
  }
  
  .card p:hover span {
    transform: rotate(0);
  }

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
    &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Roboto&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;estilos.css&quot;&gt;
    &lt;title&gt;Juego&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;header&gt;
        &lt;h1&gt;QUIERO JUGAR UN JUEGO....&lt;/h1&gt; &lt;img src=&quot;icons/jigsaw.png&quot; alt=&quot;foto Saw&quot; height=&quot;256px&quot;&gt;
        &lt;h2&gt;A continuaci&#243;n, podr&#225;s elegir los diferentes niveles...&lt;/h2&gt;
        &lt;h2&gt;Cada uno m&#225;s dificil que el otro. GANAR O MORIR. HAZ TU ELECCI&#211;N&lt;/h2&gt;
    &lt;/header&gt;

        &lt;div class=&quot;card&quot;&gt;
            &lt;p&gt;&lt;span&gt;HOVER ME&lt;/span&gt;&lt;/p&gt;
            &lt;p&gt;&lt;span&gt;HOVER ME&lt;/span&gt;&lt;/p&gt;
            &lt;p&gt;&lt;span&gt;HOVER ME&lt;/span&gt;&lt;/p&gt;
        &lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Please, someone let me know what I need to do to center it.

答案1

得分: 1

在你的卡片类中添加这一行:margin: 0 auto;

.card {
    margin: 0 auto;
    /* 其他样式属性 */
}
英文:

Add to your card class this line: margin: 0 auto;

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

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

body{
    background: #212121;
    margin: 0;
}

header{
    text-align: center;
    align-items: center;
    font-family: &#39;Roboto&#39;, sans-serif;
    color: #ff568e;
    border-bottom: 4px dashed #ff568e;
}

.card {
    margin: 0 auto;
    align-items: center;
    width: 210px;
    height: 254px;
    border-radius: 4px;
    background: #212121;
    display: flex;
    gap: 5px;
    padding: .4em;
  }
  
  .card p {
    height: 100%;
    flex: 1;
    overflow: hidden;
    cursor: pointer;
    border-radius: 2px;
    transition: all .5s;
    background: #212121;
    border: 1px solid #ff5a91;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .card p:hover {
    flex: 4;
  }
  
  .card p span {
    min-width: 14em;
    padding: .5em;
    text-align: center;
    transform: rotate(-90deg);
    transition: all .5s;
    text-transform: uppercase;
    color: #ff568e;
    letter-spacing: .1em;
  }
  
  .card p:hover span {
    transform: rotate(0);
  }

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
    &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Roboto&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;estilos.css&quot;&gt;
    &lt;title&gt;Juego&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;header&gt;
        &lt;h1&gt;QUIERO JUGAR UN JUEGO....&lt;/h1&gt; &lt;img src=&quot;icons/jigsaw.png&quot; alt=&quot;foto Saw&quot; height=&quot;256px&quot;&gt;
        &lt;h2&gt;A continuaci&#243;n, podr&#225;s elegir los diferentes niveles...&lt;/h2&gt;
        &lt;h2&gt;Cada uno m&#225;s dificil que el otro. GANAR O MORIR. HAZ TU ELECCI&#211;N&lt;/h2&gt;
    &lt;/header&gt;

        &lt;div class=&quot;card&quot;&gt;
            &lt;p&gt;&lt;span&gt;HOVER ME&lt;/span&gt;&lt;/p&gt;
            &lt;p&gt;&lt;span&gt;HOVER ME&lt;/span&gt;&lt;/p&gt;
            &lt;p&gt;&lt;span&gt;HOVER ME&lt;/span&gt;&lt;/p&gt;
        &lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 06:40:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051785.html
匿名

发表评论

匿名网友

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

确定