What is the best way to adjust the spacing between elements using CSS?

I am attempting to achieve this layout using CSS:

The margin between each element is set to 10px. The height of the textarea is 100px and the width is 150px

This snippet shows some HTML code for your reference:

main form label {
        margin: 10px;
      }

      textarea {
        height: 100px;
        width: 150px;
      }
<main>
          <h2>Contact Us</h2>
          <form action="#" method="post">
                <!-- More HTML code here -->
          </form>

        </main>

I'm having trouble with my current setup. While I've used margins before, I can't seem to space out ALL elements properly.

Answer №1

When working with inline elements, it's important to note that they ignore vertical margin settings. To address this issue, you can use the inline-block property.

main form label {
  display: inline-block;
  margin: 10px;
}

textarea {
  height: 100px;
  width: 150px;
}
<main>
    <h2>Contact Us</h2>
    <form action="#" method="post">
        <label>
            First name:
            <input type="text" name="firstname">
        </label>
        <label>
            Last name:
            <input type="text" name="lastname">
        </label>
        <label>
            Email:
            <input type="text" name="email">
        </label>
        <label>
            Select Your State:
            <select>
                <option value="florida">Florida</option>
                <option value="california">California</option>
                <option value="michigan" selected="selected">Michigan</option>
                <option value="new york">New York</option>
                <option value="texas">Texas</option>
            </select>
        </label>
        <label>
            Male
            <input type="radio" name="gender" value="male">
        </label>
        <label>
            Female
            <input type="radio" name="gender" value="female">
        </label>
        <label>
            Comment:
            <textarea name="comment" id="comment"></textarea>
        </label>
        <input type='submit' value='Submit' />

    </form>

</main>

Answer №2

To resolve this issue, try changing the label's display property to block or inline-block.

main form label {
  display: block;
  margin: 10px;
}

By default, labels are displayed inline which may affect how the margin value is applied. For further clarification on this issue, you can visit:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Centering an image (svg) vertically inside a link tag

I've been struggling to vertically center the config button (#config) and none of the options I've tried worked the way I wanted. Here is a link to my CodePen: http://codepen.io/fenwil/pen/ONRXwz Here is the crucial part of the code that need ...

Getting the most out of Twitter Bootstrap: How to effectively implement the `.btn` class in a navbar?

Currently, I have set up a navigation bar for the usual navigation options, and now I want to include buttons for both Sign in and Sign up. To implement this, I am using an a tag with the classes btn btn-large btn-success. However, it seems that the navba ...

Modifying the color of a GIF animated image using CSS

I'm currently working on an Angular application and I have a transparent GIF Loader image that I downloaded. I've been trying to figure out how to change the color of the GIF image, but so far, I've only been successful in resizing it. I sea ...

barchart rendered in SVG without the use of any external libraries

Creating a stacked barchart with SVG and HTML without relying on any third-party library has been quite a challenge. Despite searching extensively online, I have not come across a single resource that demonstrates how to build a stacked bar chart using pla ...

My custom CSS being overridden by Bootstrap styles

I'm currently working with Twitter Bootstrap's CSS, but it seems to be overriding some of my custom classes. Initially, I placed it before my own CSS in the HTML header (I am using jade and stylus template engines): doctype html html head ...

The behavior of -webkit-border-radius differs from that of -moz-border-radius

My website is displaying differently on Safari compared to Firefox. I want the CSS to make it look consistent across both browsers. I understand that I could use two div boxes - one for the outline and one for the image. However, I prefer how Firefox only ...

In certain browsers, the link changes color to white

As I assist my brother in setting up a website for his business, we encountered an issue. In some browsers, hyperlinks appear as white and therefore invisible. Interestingly, the hyperlink is white in Firefox, but not when browsing in incognito mode, see t ...

What is the best way to create a triangle shape that is responsive to elements?

I'm trying to make a background shape with a triangle inclusion using CSS. I want it to be responsive to all screen sizes, but I'm having some trouble. Any tips on how to achieve this? header { width: 100%; height:150px; background: #eee; } m ...

Unable to reset disabled styling for CSS select box

My Windows 8 app consists of two pages: Page1.html and Page2.html. Page1 references ui-light.css and page1.css, while Page2 references ui-light.css and page2.css. In the ui-light.css file, there is a rule defined for the disabled state of select boxes. I ...

CSS Styling Dropdown Menu for Internet Explorer 5 and Internet Explorer 11

I am encountering an issue with a select box that is coded as follows: <html:select property="list_data" size="12" style="width: 350px;" ondblclick="JavaScript:doOK()"> <html:optionsCollection property="list_data" label="codeAndNameLabel" val ...

Transformation of a double-row header

Click here to view the design - two-row header image I'm struggling with creating a header design that consists of two rows - the first row includes a centered logo and icons on the right side, while the second row contains the menu. Can someone guid ...

"Divs are now linked and drag as one cohesive unit instead of

As I was experimenting with making images draggable and resizable, I encountered a small issue. When I attempted to drag two or more images, they would merge into one large draggable object instead of remaining separate. My goal was to make each image drag ...

"Enhance User Experience with a Bootstrap Dropdown Button Offering Block Level Function

I'm attempting to modify a bootstrap 3 dropdown button to fully utilize the width properties of .btn-block class. It appears that the dropdown button does not respond in the same way as regular buttons. Below is my current code: <div class="row"&g ...

How can I include inline CSS directly within a string instead of using an object?

Is there a way to write inline CSS in a string format instead of an object format? I attempted the following, but it did not work as expected: <div style={"color:red;font-weight:bold"}> Hello there </div> What is my reason for want ...

Setting the width of a div element dynamically according to its height

Here is a snippet of my HTML code: <div class="persoonal"> <div class="left"></div> <div class="rigth"></div> </div> This is the CSS code I have written: .profile { width: 100%;} .left { width: 50%; float: le ...

Is it possible to utilize Bootstrap Icons as bullet points on a website?

Is it possible to use Bootstrap icons for bullet points with CSS? I am aware that FontAwesome can be used for this purpose, but I would like to confirm if the same can be achieved using Bootstrap. Though there is a method using: <ul> <li>< ...

The selenium element for the submit button is currently not responsive to interactions

Recently, I encountered some challenges with selenium, specifically the anticaptcha API. Although I managed to resolve that issue, I am currently facing a different problem. Below is my existing code: from time import sleep from selenium import webdriver f ...

Firefox throwing XML parsing error when SVG is encoded as data URI

Check out my codepen demo showcasing the issue: codepen.io/acusti/pen/mJmVRy Encountering an error when trying to load svg content in Firefox: XML Parsing Error: unclosed token Location: data:image/svg+xml;utf8,<svg%20viewBox='0%200%20120%2 ...

Switching CSS styles - seeking a smoother integration

I have successfully implemented a JS CSS switcher, which works brilliantly. However, I would like it to function more seamlessly. When opening a new page on the site, the default CSS style sometimes flickers briefly before the selected CSS style is reappli ...

Tips for nesting React components within a grid layout

For my News website, I have a component that showcases articles fetched from an API in a grid format. Additionally, I also have a separate component dedicated to displaying the Latest News. I am looking for a way to position the Latest News widget within t ...