简体   繁体   English

没有包装div的情况下将元素并排放置

[英]Placing elements next to each other without wrapper div

I'm currently styling a WordPress site's contact form. 我目前正在设计WordPress网站的联系表单。 The desired result should look something like this: 所需的结果应如下所示:

大文本区域旁边的四个文本输入字段

Normally, this could be easily achieved with a wrapper div around the text fields. 通常,这可以通过在文本字段周围使用wrapper div轻松实现。 Since this is WordPress, I can't do this without digging into the plugin (which is the last thing I want to do). 由于这是WordPress,因此我必须深入研究插件(这是我最后要做的事情)。 The HTML WordPress produces looks like this: WordPress生成的HTML如下所示:

<form ...>
  <p class="..." data-id="name" data-type="input">
    <label ...>Naam Achternaam</label>
    <input type="text" ...>
  </p>
  <p class="..." data-id="company" data-type="input">
    <label ...>Bedrijf</label>
    <input type="text" ...>
  </p>
  <p class="..." data-id="email" data-type="input">
    <label ...>E-mail</label>
    <input type="email" ...>
  </p>
  <p class="..." data-id="phone" data-type="input">
    <label ...>Telefoonnummer</label>
    <input type="text" ...>
  </p>
  <p class="..." data-id="message" data-type="text">
    <label ...>Vragen / opmerkingen</label>
    <textarea ...></textarea>
  </p>
  ...
</form>

which currently looks like this: 当前看起来像这样:

四个文本字段和一个文本区域彼此相邻放置(2 x 2)

Is it possible to achieve what I'm after with this HTML structure or do I need to find a way to accommodate a wrapper div? 是否可以用此HTML结构实现我的目标,还是需要找到一种方法来容纳包装div?

You could use position: absolute on the last p ... not the nicest solution but a possible workaround without changing the HTML 您可以在最后一个p上使用position: absolute ...不是最好的解决方案,但是可以在不更改HTML的情况下解决

 form { background: #00CDD4; display: flex; flex-direction: column; position: relative; } form p { margin: 1em; display: flex; flex-direction: column; width: 45%; } form p label { text-transform: uppercase; font-family: sans-serif; opacity: .87; } form p input { border: none; padding: 1em; } form p:last-of-type { position: absolute; top: 0; right: 0; bottom: 0; } form p:last-of-type textarea { height: 100%; } 
 <form> <p class="..." data-id="name" data-type="input"> <label ...>Naam Achternaam</label> <input type="text" ...> </p> <p class="..." data-id="company" data-type="input"> <label ...>Bedrijf</label> <input type="text" ...> </p> <p class="..." data-id="email" data-type="input"> <label ...>E-mail</label> <input type="email" ...> </p> <p class="..." data-id="phone" data-type="input"> <label ...>Telefoonnummer</label> <input type="text" ...> </p> <p class="..." data-id="message" data-type="text"> <label ...>Vragen / opmerkingen</label> <textarea ...></textarea> </p> </form> 

I tried alot but following your requirements it seems only possible through position absolute. 我尝试了很多,但按照您的要求,似乎只能通过绝对位置来实现。 Just for fun I tried to make without display: flex; 只是为了好玩,我尝试不显示而制作:flex;

 body { background-color: #2dccd3; font-family: 'Montserrat', sans-serif; } form.contact{ position: relative; } p.contact-field { width: 45%; } p.contact-field input{ width: 100%; } input, label { display:block; } p.contact-field:last-child{ margin: 0px; } .contact-field:last-child{ position: absolute; right: 0px; top: 0px; width: 50%; height: 89%; } textarea#message{ height: 100%; } 
 <form class="contact"> <p class="contact-field" data-id="name" data-type="input"> <label for="name">Naam Achternaam</label> <input id="name" type="text" name="name"> </p> <p class="contact-field" data-id="company" data-type="input"> <label for="company">Bedrijf</label> <input id="company" type="text" name="company"> </p> <p class="contact-field" data-id="email" data-type="input"> <label for="email">E-mail</label> <input id="email" type="email" name="email"> </p> <p class="contact-field" data-id="phone" data-type="input"> <label for="phone">Telefoonnummer</label> <input id="phone" type="text" name="phone"> </p> <p class="contact-field" data-id="message" data-type="text"> <label for="message">Vragen / opmerkingen</label> <textarea id="message" name="message"></textarea> </p> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM