如何使用CSS创建类似设计的布局?

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

How to create a layout similar to this design with CSS?

问题

我正在尝试创建我的网站来展示我的工作,并且正在创建一个“联系我”表单。现在,我正在关注它的外观。我设计了它应该是什么样子,但上次我使用HTML和CSS工作已经有8年了。

这是设计:
设计

这是实际结果:
当前结果

<!-- 开始代码片段:js隐藏:false控制台:falsebabel:false -->

<!-- 语言:lang-css -->
/* ---------- 页脚 ---------- */

#contact {
    margin: 10vh auto;
    max-width: 1800px;
    width: 90%;
    padding: 2%;
    border-radius: 10px;
}

.contact-highlight {
    font-size: 1.5em;
}

.form-element {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.form-component {
    font-family: Poppins, Arial, Helvetica, sans-serif;
    border-radius: 5px;
    border: none;
    background-color: #DAD8DD;
    color: #0D0221;
    width: 25vw;
    height: 5vh;
}

.form-text-area {
    font-family: Poppins, Arial, Helvetica, sans-serif;
    border-radius: 5px;
    border: none;
    background-color: #DAD8DD;
    color: #0D0221;
    width: 25vw;
    height: 20vh;
}

.form-submit {
    font-family: Poppins, Arial, Helvetica, sans-serif;
    border-radius: 10px;
    border: none;
    background-color: #F50056;
    color: #ffffff;
    font-size: 1em;
    width: 10vw;
    height: 5vh;
}

.footer-text {
    color: #ffffff;
    text-align: center;
}
<!-- 语言:lang-html -->
<!-- 页面页脚。 -->

<footer>

    <!-- 联系信息。 -->
    <div id="contact" class="white-section">

        <!-- 联系文本。 -->
        <p class="highlight contact-highlight">如果您喜欢我的工作或只是想联系我,</p>
        <p>您可以填写此表格。我会尽快回复!</p>

        <!-- 联系表单。 -->
        <form class="form-element" action="php/contact.php" method="post">

            <p>名字<em class="highlight">*</em></p>
            <input class="form-component" type="text" name="name" autocomplete="off" required>

            <p>电子邮件<em class="highlight">*</em></p>
            <input class="form-component" type="email" name="email" autocomplete="off" required>

            <p>主题<em class="highlight">*</em></p>
            <input class="form-component" type="text" name="subject" autocomplete="off" required>

            <p>信息<em class="highlight">*</em></p>
            <textarea class="form-text-area" name="message" autocomplete="off" required></textarea>

            <input class="form-submit" type="submit" value="发送">

        </form>

    </div>

    <!-- 页脚版权。 -->
    <p class="footer-text">© Basset Etienne | 保留所有权利。</p>
</footer>

<!-- 结束代码片段 -->

我已经使用了“CSS grid”来创建“项目”部分,我了解“display: flex”是如何工作的,但我不知道如何解决这个问题。我尝试在线查找将元素按特定顺序排列的方法,但我担心响应式排列可能会很麻烦。

英文:

I'm trying to create my website to show my work, and I'm creating a "Contact Me" form. Now, I'm focusing on the aspect of it. I made a design of what it should look like, but last time I worked with HTML and CSS was like 8 years ago.

Here is the design:
The design

Here is the actual result:
The result right now

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

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

/* ---------- Footer ---------- */
#contact {
margin: 10vh auto;
max-width: 1800px;
width: 90%;
padding: 2%;
border-radius: 10px;
}
.contact-highlight {
font-size: 1.5em;
}
.form-element {
display: flex;
flex-direction: column;
align-items: center;
}
.form-component {
font-family: Poppins, Arial, Helvetica, sans-serif;
border-radius: 5px;
border: none;
background-color: #DAD8DD;
color: #0D0221;
width: 25vw;
height: 5vh;
}
.form-text-area {
font-family: Poppins, Arial, Helvetica, sans-serif;
border-radius: 5px;
border: none;
background-color: #DAD8DD;
color: #0D0221;
width: 25vw;
height: 20vh;
}
.form-submit {
font-family: Poppins, Arial, Helvetica, sans-serif;
border-radius: 10px;
border: none;
background-color: #F50056;
color: #ffffff;
font-size: 1em;
width: 10vw;
height: 5vh;
}
.footer-text {
color: #ffffff;
text-align: center;
}

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

&lt;!-- The page&#39;s footer. --&gt;
&lt;footer&gt;
&lt;!-- The contact infos. --&gt;
&lt;div id=&quot;contact&quot; class=&quot;white-section&quot;&gt;
&lt;!-- Contact text. --&gt;
&lt;p class=&quot;highlight contact-highlight&quot;&gt;Si vous aimez mes travaux ou souhaitez simplement me contacter,&lt;/p&gt;
&lt;p&gt;vous pouvez remplir ce formulaire. Je vous r&#233;pondrais aussi vite que possible !&lt;/p&gt;
&lt;!-- Contact form. --&gt;
&lt;form class=&quot;form-element&quot; action=&quot;php/contact.php&quot; method=&quot;post&quot;&gt;
&lt;p&gt;Pr&#233;nom-Nom&lt;em class=&quot;highlight&quot;&gt;*&lt;/em&gt;&lt;/p&gt;
&lt;input class=&quot;form-component&quot; type=&quot;text&quot; name=&quot;name&quot; autocomplete=&quot;off&quot; required&gt;
&lt;p&gt;Email&lt;em class=&quot;highlight&quot;&gt;*&lt;/em&gt;&lt;/p&gt;
&lt;input class=&quot;form-component&quot; type=&quot;email&quot; name=&quot;email&quot; autocomplete=&quot;off&quot; required&gt;
&lt;p&gt;Objet&lt;em class=&quot;highlight&quot;&gt;*&lt;/em&gt;&lt;/p&gt;
&lt;input class=&quot;form-component&quot; type=&quot;text&quot; name=&quot;subject&quot; autocomplete=&quot;off&quot; required&gt;
&lt;p&gt;Message&lt;em class=&quot;highlight&quot;&gt;*&lt;/em&gt;&lt;/p&gt;
&lt;textarea class=&quot;form-text-area&quot; name=&quot;message&quot; autocomplete=&quot;off&quot; required&gt;&lt;/textarea&gt;
&lt;input class=&quot;form-submit&quot; type=&quot;submit&quot; value=&quot;Send&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;!-- Footer credits. --&gt;
&lt;p class=&quot;footer-text&quot;&gt;&#169; Basset Etienne | All rights reserved.&lt;/p&gt;
&lt;/footer&gt;

<!-- end snippet -->

I already used "CSS grid" to create the Projects part, and I understand how "display: flex" works, but I don't know how to approach this problem. I tried to check online for a way to put elements in a certain order, but I'm afraid it would be a pain to put it responsive.

答案1

得分: 0

我会这样做:
在表单内放一个 div 作为 flexbox 容器,然后放另外两个 div,一个用于姓名、邮箱和主题字段,另一个用于消息字段。

英文:

I would do like this:
Inside the form put a div to be a flexbox container, then put two other divs, one for the name, email and object fields and the other for the message field

答案2

得分: 0

Sure, here's the translated content:

.display: grid 是你需要的

通过观察我们可以看到我们的网格有四行和两列如下所示不按比例显示):

```none
┌───────────┬───────────────┐
 输入      文本框      
 ┌───────┐  ┌───────────┐ 
 └───────┘              
├───────────┤             
 输入                  
 ┌───────┐              
 └───────┘              
├───────────┤             
 输入                  
 ┌───────┐              
 └───────┘  └───────────┘ 
├───────────┴───────────────┤
        ┌─────────┐        
         按钮            
        └─────────┘        
└───────────────────────────┘

这可以翻译成HTML和CSS(为了便于解释而简化):

&lt;div id=&quot;contact&quot;&gt;
  &lt;form class=&quot;form-element&quot;&gt;

    &lt;!-- 使用 &lt;label&gt; 来标记字段,而不是 &lt;div&gt; --&gt;
    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Pr&#233;nom-Nom&lt;/span&gt;
      &lt;input type=&quot;text&quot; required&gt;
    &lt;/label&gt;

    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Email&lt;/span&gt;
      &lt;input type=&quot;email&quot; required&gt;
    &lt;/label&gt;

    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Objet&lt;/span&gt;
      &lt;input type=&quot;text&quot; required&gt;
    &lt;/label&gt;

    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Message&lt;/span&gt;
      &lt;textarea name=&quot;message&quot; required&gt;&lt;/textarea&gt;
    &lt;/label&gt;

    &lt;div class=&quot;form-field&quot;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Send&quot;&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;
.form-element {
  display: grid;
  /* 四行,高度相同 */
  grid-template-rows: repeat(4, 1fr);
  /* 右列是左列的两倍宽度 */
  grid-template-columns: 1fr 2fr;
}

消息框位于第二列,跨越3行,而提交字段跨越两列。我们在CSS中也指定了这一点:

.form-field:has([name=&quot;message&quot;]) {
  grid-row: 1 / span 3; /* 从第1行开始,跨越3行 */
  grid-column: 2; /* 从第2列开始,默认跨越1列 */
}

.form-field:has([type=&quot;submit&quot;]) {
  grid-row: 4; /* 从第4行开始,默认跨越1行 */
  grid-column: 1 / -1; /* 从第1列开始,跨越到最后一列 */
  /* 或 1 / 3, 1 / span 2 */
}

试一下:

<!-- 开始片段:js 隐藏:true -->

<!-- 语言:lang-css -->

.form-element {
display: grid;
grid-template-rows: repeat(4, 1fr);
grid-template-columns: 1fr 2fr;
gap: 1em 2em;
}
.form-field:has([name=&quot;message&quot;]) {
grid-row: 1 / span 3;
grid-column: 2;
}
.form-field:has([type=&quot;submit&quot;]) {
grid-row: 4;
grid-column: 1 / -1;
}
/* 仅演示用 */
#contact {
margin: 1em auto;
width: min(90%, 1000px);
font-family: system-ui;
}
.form-element &gt; .form-field {
display: flex;
flex-direction: column;
gap: 0.5em;
}
.form-element &gt; .form-field &gt; span:has(+ :required)::after {
content: &#39;*&#39;;
color: #f20b62;
}
.form-component,
.form-text-area {
border: none;
border-radius: 5px;
background-color: #dad8dd;
color: #0d0221;
}
.form-component,
.form-submit {
padding: 0.5em 1em;
}
.form-text-area {
flex: 1;
padding: 1em;
resize: none;
}
.form-submit {
align-self: center;
border: none;
border-radius: 10px;
background-color: #f50056;
color: #fff;
font-size: 1em;
}

<!-- 语言:lang-html -->

&lt;footer&gt;
&lt;div id=&quot;contact&quot;&gt;
&lt;form class=&quot;form-element&quot;&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Pr&#233;nom-Nom&lt;/span&gt;
&lt;input class=&quot;form-component&quot; type=&quot;text&quot; autocomplete=&quot;off&quot; required&gt;
&lt;/label&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Email&lt;/span&gt;
&lt;input class=&quot;form-component&quot; type=&quot;email&quot; autocomplete=&quot;off&quot; required&gt;
&lt;/label&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Objet&lt;/span&gt;
&lt;input class=&quot;form-component&quot; type=&quot;text&quot; autocomplete=&quot;off
英文:

display: grid is what you need.

By observation, we can see that our grid have four rows and two columns, as visualized below (not to scale):

┌───────────┬───────────────┐
│ Input:    │ Textarea:     │
│ ┌───────┐ │ ┌───────────┐ │
│ └───────┘ │ │           │ │
├───────────┤ │           │ │
│ Input:    │ │           │ │
│ ┌───────┐ │ │           │ │
│ └───────┘ │ │           │ │
├───────────┤ │           │ │
│ Input:    │ │           │ │
│ ┌───────┐ │ │           │ │
│ └───────┘ │ └───────────┘ │
├───────────┴───────────────┤
│        ┌─────────┐        │
│        │ Button  │        │
│        └─────────┘        │
└───────────────────────────┘

This can be translated to HTML and CSS as (simplified for ease of explanation):

&lt;div id=&quot;contact&quot;&gt;
  &lt;form class=&quot;form-element&quot;&gt;

    &lt;!-- Use &lt;label&gt; to mark a field instead of &lt;div&gt; --&gt;
    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Pr&#233;nom-Nom&lt;/span&gt;
      &lt;input type=&quot;text&quot; required&gt;
    &lt;/label&gt;

    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Email&lt;/span&gt;
      &lt;input type=&quot;email&quot; required&gt;
    &lt;/label&gt;

    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Objet&lt;/span&gt;
      &lt;input type=&quot;text&quot; required&gt;
    &lt;/label&gt;

    &lt;label class=&quot;form-field&quot;&gt;
      &lt;span&gt;Message&lt;/span&gt;
      &lt;textarea name=&quot;message&quot; required&gt;&lt;/textarea&gt;
    &lt;/label&gt;

    &lt;div class=&quot;form-field&quot;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Send&quot;&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;
.form-element {
  display: grid;
  /* Four rows with the same height. */
  grid-template-rows: repeat(4, 1fr);
  /* The right column is twice as wide as the left one. */
  grid-template-columns: 1fr 2fr;
}

The message box lies on the second column and span 3 rows, while the submit field span both columns. We specify that in our CSS as well:

.form-field:has([name=&quot;message&quot;]) {
  grid-row: 1 / span 3; /* Starts at row 1, span 3 rows */
  grid-column: 2; /* Starts at column 2, span 1 by default */
}

.form-field:has([type=&quot;submit&quot;]) {
  grid-row: 4; /* Starts at row 4, span 1 by default */
  grid-column: 1 / -1; /* Starts at column 1, span through the last column */
  /* or 1 / 3, 1 / span 2 */
}

Try it:

<!-- begin snippet: js hide: true -->

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

.form-element {
display: grid;
grid-template-rows: repeat(4, 1fr);
grid-template-columns: 1fr 2fr;
gap: 1em 2em;
}
.form-field:has([name=&quot;message&quot;]) {
grid-row: 1 / span 3;
grid-column: 2;
}
.form-field:has([type=&quot;submit&quot;]) {
grid-row: 4;
grid-column: 1 / -1;
}
/* Demo only */
#contact {
margin: 1em auto;
width: min(90%, 1000px);
font-family: system-ui;
}
.form-element &gt; .form-field {
display: flex;
flex-direction: column;
gap: 0.5em;
}
.form-element &gt; .form-field &gt; span:has(+ :required)::after {
content: &#39;*&#39;;
color: #f20b62;
}
.form-component,
.form-text-area {
border: none;
border-radius: 5px;
background-color: #dad8dd;
color: #0d0221;
}
.form-component,
.form-submit {
padding: 0.5em 1em;
}
.form-text-area {
flex: 1;
padding: 1em;
resize: none;
}
.form-submit {
align-self: center;
border: none;
border-radius: 10px;
background-color: #f50056;
color: #fff;
font-size: 1em;
}

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

&lt;footer&gt;
&lt;div id=&quot;contact&quot;&gt;
&lt;form class=&quot;form-element&quot;&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Pr&#233;nom-Nom&lt;/span&gt;
&lt;input class=&quot;form-component&quot; type=&quot;text&quot; autocomplete=&quot;off&quot; required&gt;
&lt;/label&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Email&lt;/span&gt;
&lt;input class=&quot;form-component&quot; type=&quot;email&quot; autocomplete=&quot;off&quot; required&gt;
&lt;/label&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Objet&lt;/span&gt;
&lt;input class=&quot;form-component&quot; type=&quot;text&quot; autocomplete=&quot;off&quot; required&gt;
&lt;/label&gt;
&lt;label class=&quot;form-field&quot;&gt;
&lt;span&gt;Message&lt;/span&gt;
&lt;textarea class=&quot;form-text-area&quot; name=&quot;message&quot; autocomplete=&quot;off&quot; required&gt;&lt;/textarea&gt;
&lt;/label&gt;
&lt;div class=&quot;form-field&quot;&gt;
&lt;input class=&quot;form-submit&quot; type=&quot;submit&quot; value=&quot;Send&quot;&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/footer&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 01:30:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979278.html
匿名

发表评论

匿名网友

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

确定