英文:
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 -->
<!-- The page's footer. -->
<footer>
<!-- The contact infos. -->
<div id="contact" class="white-section">
<!-- Contact text. -->
<p class="highlight contact-highlight">Si vous aimez mes travaux ou souhaitez simplement me contacter,</p>
<p>vous pouvez remplir ce formulaire. Je vous répondrais aussi vite que possible !</p>
<!-- Contact form. -->
<form class="form-element" action="php/contact.php" method="post">
<p>Prénom-Nom<em class="highlight">*</em></p>
<input class="form-component" type="text" name="name" autocomplete="off" required>
<p>Email<em class="highlight">*</em></p>
<input class="form-component" type="email" name="email" autocomplete="off" required>
<p>Objet<em class="highlight">*</em></p>
<input class="form-component" type="text" name="subject" autocomplete="off" required>
<p>Message<em class="highlight">*</em></p>
<textarea class="form-text-area" name="message" autocomplete="off" required></textarea>
<input class="form-submit" type="submit" value="Send">
</form>
</div>
<!-- Footer credits. -->
<p class="footer-text">© Basset Etienne | All rights reserved.</p>
</footer>
<!-- 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(为了便于解释而简化):
<div id="contact">
<form class="form-element">
<!-- 使用 <label> 来标记字段,而不是 <div> -->
<label class="form-field">
<span>Prénom-Nom</span>
<input type="text" required>
</label>
<label class="form-field">
<span>Email</span>
<input type="email" required>
</label>
<label class="form-field">
<span>Objet</span>
<input type="text" required>
</label>
<label class="form-field">
<span>Message</span>
<textarea name="message" required></textarea>
</label>
<div class="form-field">
<input type="submit" value="Send">
</div>
</form>
</div>
.form-element {
display: grid;
/* 四行,高度相同 */
grid-template-rows: repeat(4, 1fr);
/* 右列是左列的两倍宽度 */
grid-template-columns: 1fr 2fr;
}
消息框位于第二列,跨越3行,而提交字段跨越两列。我们在CSS中也指定了这一点:
.form-field:has([name="message"]) {
grid-row: 1 / span 3; /* 从第1行开始,跨越3行 */
grid-column: 2; /* 从第2列开始,默认跨越1列 */
}
.form-field:has([type="submit"]) {
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="message"]) {
grid-row: 1 / span 3;
grid-column: 2;
}
.form-field:has([type="submit"]) {
grid-row: 4;
grid-column: 1 / -1;
}
/* 仅演示用 */
#contact {
margin: 1em auto;
width: min(90%, 1000px);
font-family: system-ui;
}
.form-element > .form-field {
display: flex;
flex-direction: column;
gap: 0.5em;
}
.form-element > .form-field > span:has(+ :required)::after {
content: '*';
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 -->
<footer>
<div id="contact">
<form class="form-element">
<label class="form-field">
<span>Prénom-Nom</span>
<input class="form-component" type="text" autocomplete="off" required>
</label>
<label class="form-field">
<span>Email</span>
<input class="form-component" type="email" autocomplete="off" required>
</label>
<label class="form-field">
<span>Objet</span>
<input class="form-component" type="text" autocomplete="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):
<div id="contact">
<form class="form-element">
<!-- Use <label> to mark a field instead of <div> -->
<label class="form-field">
<span>Prénom-Nom</span>
<input type="text" required>
</label>
<label class="form-field">
<span>Email</span>
<input type="email" required>
</label>
<label class="form-field">
<span>Objet</span>
<input type="text" required>
</label>
<label class="form-field">
<span>Message</span>
<textarea name="message" required></textarea>
</label>
<div class="form-field">
<input type="submit" value="Send">
</div>
</form>
</div>
.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="message"]) {
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="submit"]) {
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="message"]) {
grid-row: 1 / span 3;
grid-column: 2;
}
.form-field:has([type="submit"]) {
grid-row: 4;
grid-column: 1 / -1;
}
/* Demo only */
#contact {
margin: 1em auto;
width: min(90%, 1000px);
font-family: system-ui;
}
.form-element > .form-field {
display: flex;
flex-direction: column;
gap: 0.5em;
}
.form-element > .form-field > span:has(+ :required)::after {
content: '*';
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 -->
<footer>
<div id="contact">
<form class="form-element">
<label class="form-field">
<span>Prénom-Nom</span>
<input class="form-component" type="text" autocomplete="off" required>
</label>
<label class="form-field">
<span>Email</span>
<input class="form-component" type="email" autocomplete="off" required>
</label>
<label class="form-field">
<span>Objet</span>
<input class="form-component" type="text" autocomplete="off" required>
</label>
<label class="form-field">
<span>Message</span>
<textarea class="form-text-area" name="message" autocomplete="off" required></textarea>
</label>
<div class="form-field">
<input class="form-submit" type="submit" value="Send">
</div>
</form>
</div>
</footer>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论