arranging HTML components without relying on inline styling

As someone with an iOS background, I am struggling with arranging elements in HTML and using CSS. My goal is to create a layout as "simple" as the one pictured:

I want to split the screen into separate divs but have all the fieldsets align with each other. (The elements are fieldsets, although they are not depicted in my basic mockup. Also, I did not place anything in the third box, but there is more content there).

Here are some of my questions:

Regarding Box 1: I have included style="display:block;" in all my elements. When I use an overarching div with style=display:block, I do not achieve the desired effect. Is there a better way to accomplish this?

General Question for Box 2: I had to hardcode all my styles to somewhat replicate the shown image. However, this approach does not feel very user-friendly or scalable. Are there any general principles I should adhere to?

<div style="display:inline-block; vertical-align:top; float:left; width:25%">
    <fieldset>
        <legend>First fieldset</legend>
        <div style="display:block;">field 1
            <input type="text" style="display:block;" />
        </div>
        <div style="display:block;">field 2
            <select style="display:block;">
                <option>field 2 options</option>
            </select>
        </div>
        <div style="display:block;">field 3
            <input type="text" style="display:block;" />
        </div>
    </fieldset>
</div>
<div style="display:inline-block; vertical-align:top; width:33%">
    <fieldset>
        <legend>Second fieldset</legend>
        <div style="clear:both"></div>
        <div class="one-half" style="display:inline-block; float:left;">
            <input type="radio" name="scoops" />Single
            <div style="display: block">Radio 1</div>
            <div style="display: inline">Radio 2
                <input type="text" />
            </div>
            <div style="display: block">
                <input type="checkbox" />Radio 3</div>
        </div>
        <div class="one-half" style="display:inline-block;">
            <input type="radio" name="scoops" />Double
            <div style="display: block">Blah 1</div>
            <div style="display: inline">Blah 2
                <input type="text" />
            </div>
            <div style="display: block">
                <input type="checkbox" />Blah 3</div>
        </div>
    </fieldset>
</div>

Answer №1

It is important to avoid using inline styles as they can quickly lead to a messy code structure. Instead, create an external stylesheet to manage all CSS properties and style groups of elements with CSS selectors.

To simplify the structure, consider wrapping the three columns within a div:

<div id="wrapper">
    <div id="col1"></div>
    <div id="col2"></div>
    <div id="col3"></div>
</div>

If you want the columns to be displayed side-by-side, you can use techniques like floating or `inline-block`. For example:

#wrapper { overflow: hidden; } 
#col1, #col2, #col3 { float: left; }
#col1 { width: 25%; }
#col2 { width: 33%; }

You don't necessarily need a div for each field, and labels can be used as blocks without specifying them explicitly as such:

<fieldset>
    <legend>First fieldset</legend>
    <label for="fld1">field 1</label>
    <input id="fld1" type="text">

    <label for="fld2">field 2</label>
    <select id="fld2">
        <option>field 2 options</option>
    </select>

    <label for="fld3">field 3</label>
    <input id="fld3" type="text">
</fieldset>

For consistent styling, make labels, input fields, and select elements block-level:

label, input, select { display: block; }

These principles can be applied to the styling of other columns as well. Hope this helps streamline your layout!

Answer №2

CSS classes serve a specific purpose; you can learn more about them at this link.

To get started, here are two classes for organizing your content into left and right sections:

.left {
    display:inline-block;
    vertical-align:top;
    float:left;
    width:25%;
}
.right {
    display:inline-block;
    vertical-align:top;
    width:33%;
}

See these classes in action by checking out this example.

<div class="left">
    <fieldset>
        <legend>First fieldset</legend>
        <div style="display:block;">field 1
            <input type="text" style="display:block;" />
        </div>
        <div style="display:block;">field 2
            <select style="display:block;">
                <option>field 2 options</option>
            </select>
        </div>
        <div style="display:block;">field 3
            <input type="text" style="display:block;" />
        </div>
    </fieldset>
</div>
<div class="right">
    <fieldset>
        <legend>Second fieldset</legend>
        <div style="clear:both"></div>
        <div class="one-half" style="display:inline-block; float:left;">
            <input type="radio" name="scoops" />Single
            <div style="display: block">Radio 1</div>
            <div style="display: inline">Radio 2
                <input type="text" />
            </div>
            <div style="display: block">
                <input type="checkbox" />Radio 3</div>
        </div>
        <div class="one-half" style="display:inline-block;">
            <input type="radio" name="scoops" />Double
            <div style="display: block">Blah 1</div>
            <div style="display: inline">Blah 2
                <input type="text" />
            </div>
            <div style="display: block">
                <input type="checkbox" />Blah 3</div>
        </div>
    </fieldset>
</div>

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

Creating a seamless full-page grid layout with CSS to eliminate the need for scrolling

My goal is to have 20 items on the grid of this page, each taking up full width and height with a fluid layout. Currently, I have successfully set up the page with 20 items spanning full width in a fluid design. However, the content of the grid is being p ...

Automatically add shimmer with CSS styling

Is it possible to make the shine effect work automatically (without needing hover) every 5 seconds? http://jsfiddle.net/AntonTrollback/nqQc7/ .icon:after { content: ""; position: absolute; top: -110%; left: -210%; width: 200%; height: 200%; ...

Angular 2 dropdown selection data

I'm looking for a way to display the selected value from a dynamically changing number of select boxes without using the ngModel binding property. Currently, my code looks like this: <tr *ngFor="let room of term.rooms"> <th>{{room.nam ...

Hide the search results if the user leaves the input field blank

I am trying to implement Live Search JSON Data Using Ajax jQuery, and I want to be able to search through multiple JSON files. When the page initially loads with an empty input field, no results are displayed. However, if you type and then delete text in ...

Preventing window resize from affecting print screen content in Angular 8

I'm struggling with a print button feature in my Angular 8 project. When the window is resized, the content within the print window also resizes, which is not the behavior I want. I've tried a couple of solutions, but nothing has worked so far: 1 ...

I can't figure out why the item.newStock number is always one less when I click the button with the Up function in React

Currently in the process of developing a small e-commerce platform with a focus on enhancing the shopping cart feature. The objective is to have the array update and display on the screen via item.newStock every time the Up button is clicked. However, upon ...

Discovering the best locations for locating CSS color codes

I came across this question which led me to search for the color codes of green, red and yellow in decimal format for the rgba() function. According to this website, I found that green is represented by #008000, equivalent to rgb(0, 128, 0). However, I a ...

I need to securely store HTML formatted strings in a database

Currently, I am using a react quill component that outputs HTML content like this: EXAMPLE: <p><br></p><p>fsdafsd</p><p>fsdfsda</p><p>fsdafsad</p I want to store this content in a database. However, I ...

The moment a file is uploaded from an HTML page, control is passed on to the servlet

Trying to incorporate file upload functionality in my application, I have implemented the following code in my HTML: <form action="http://localhost:8080/demo/Upload/a" method="post" enctype="multipart/form-data"> <input type="text" name="descript ...

The angular-ui modal is always visible from the start

I'm currently referencing this Angular recipes page for guidance on implementing a modal dialog in my user interface. The suggested markup includes adding a button to one of my views. ... html for my view is here ... <button class="btn" ng-click=" ...

Front-end webpage presenting coding malfunction - Written in HTML/CSS

After completing the development of a website at , I tested it on my PC and everything looked good. However, when I asked a friend to review the site, she pointed out that on mobile devices, the section with 4 boxes was appearing squeezed together despite ...

Storing various text inputs in a MySQL database

Could anyone please assist me with fixing an issue I'm having with inserting data into a database from the form provided below? Unfortunately, I am unable to get it to work as expected. Here is the complete form: <html> <head> <m ...

To modify the color of the breadcrumb navigation bar from purple to #d6df23 using PHP

Can someone help me figure out how to change the color of the bar displayed in this link: I need assistance with modifying the breadcrumb navbar. Is there a way to change the color to yellow? I attempted editing the header file, but it didn't produ ...

I have been attempting to implement validation in JQuery, but it doesn't seem to be functioning correctly

Can someone assist me with adding validation in jQuery? I'm stuck and need help solving this problem. $(function(){ $("#btn").click(function(){ var b=prompt("Enter your link"); $("a").attr("href",b); if($("b").v ...

Switching from 'click' to 'hover' will make the dropdown menu more accessible for keyboard users

I'm currently updating an existing dropdown menu that meets WCAG standards but I need to tweak the functionality so that it activates on hover instead of click. Below is the code snippet: $(document).ready(function() { $('.has-submenu'). ...

Looking to utilize Python Selenium for downloading a PDF file

I am currently working on automating the process of downloading PDFs using Selenium Webdriver in Python An issue I've encountered is that the download button is hidden within an embed tag in the HTML code <embed width="100%" height="100%" name="p ...

Switch up the hover color of the dropdown menu in the Bootstrap navbar

I'm struggling to change the hover color of the dropdown menu in the navigation bar. Can anyone please help me with this? Here is my HTML code. How can I change the hover color using CSS? <div class="navbar navbar-inverse" id="menu"> <di ...

Retrieving HTML content using CURL

Could really use some help with my curl issue: I'm using CURL to connect to a page, then redirecting myself to another page that's only accessible when logged in. How can I retrieve the HTML code from this page? I can see the page displayed, but ...

The positioning of the Zorro table column remains flexible despite the presence of nzLeft

I am currently developing a project using Angular. Within one of my components, I have a table that displays some data. This table is generated by the ng-zorro table module. However, I've encountered an issue where setting a table column or header wi ...

Iconic slim template for data disabling

Having an issue while working on a Rails app with slim. I am facing a problem passing HTML to slim through a button using the data-disable-with attribute. My aim is to display an icon on the button when it's clicked. = f.submit t("basket.next_step") ...