Styling the layout using only <span> elements in CSS

Currently working on a project where I need to create a simple layout of four boxes. The challenge is that no containers or placeholders are allowed, and only four <span> elements should be in the <body>. In addition, all CSS styling must be written inline. When the browser window is maximized, the layout should be centered with Box 1 at the top and Box 4 at the bottom. Boxes 2 and 3 should sit next to each other between them. So far, I have successfully implemented Box 1 and 4, but I'm facing some difficulties with Boxes 2 and 3. I tried using float:left, but it didn't work as expected when combined with margin:auto.

Another requirement I'm struggling with is having only Box 4 visible when the browser is resized to its smallest size. Despite checking various resources and books, I haven't found a solution yet. It's important to note that we're not allowed to use JavaScript or similar technologies for this task.

Any advice or guidance would be greatly appreciated.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS-exercise</title>
</head>
<body >
<span style="background-color:#ccffe5; width:800px; height:200px; display:block; margin:auto;">Box 1</span> 
    <span style="background-color:#cce0ff; width:750px; height:500px;">Box 2</span>
    <span style="background-color:#ffccdd; width:50px; height:500px;">Box 3</span>  
    <span style="background-color:#ffcc99; width:800px; height:400px; display:block; margin:auto;">Box 4</span> 
</body>
</html>

Answer №1

To achieve the desired layout, use display:inline-block in place of float:left.

<body style="text-align:center;">
<span style="background-color:#ccffe5; width:800px; height:200px; display:block; margin:auto;">Box 1</span> 
<span style="background-color:#cce0ff; width:400px; height:500px; display:inline-block;">Box 2</span>
<span style="background-color:#ffccdd; width:400px; height:500px; display:inline-block;">Box 3</span>  
<span style="background-color:#ffcc99; width:800px; height:400px; display:block; margin:auto;">Box 4</span>
</body>

Answer №2

If you need anything else, just give me a shout -

<body >
<span style="background-color:#ccffe5; width:800px; height:200px; display:block; margin:auto;">Box 1</span> 
    <span style="background-color:#cce0ff; width:750px; height:500px; display:inline-block">Box 2</span>
    <span style="background-color:#ffccdd; width:50px; display:inline-block; height:500px;">Box 3</span>  
    <span style="background-color:#ffcc99; width:800px; height:400px; display:block; margin:auto;">Box 4</span> 
</body>

Answer №3

Give it a shot, you'll see results.

<span style="background-color:#ccffe5; width:800px; height:100px; display:block; margin:auto;">Box 1</span>
<span style="background-color:#cce0ff; width:800px; height:100px; display:block; margin:auto;">Box 2</span> 
<span style="background-color:#ffccdd; width:800px; height:100px; display:block; margin:auto;">Box 3</span> 
<span style="background-color:#ffcc99; width:800px; height:100px; display:block; margin:auto;">Box 4</span>

Answer №4

Here is a way to achieve this look:

<body style="text-align: center">
<span style="background-color:#ccffe5; width:800px; height:200px; display:block; margin:auto;">Box 1</span> 
<span style="background-color:#cce0ff; width:750px; height:500px;  display:inline-block">Box 2</span>
<span style="background-color:#ffccdd; width:50px; height:500px; display:inline-block">Box 3</span>  
<span style="background-color:#ffcc99; width:800px; height:400px; display:block; margin:auto;">Box 4</span> 
</body>

I set Box 2 and Box 4 to have "display: inline-block". Additionally, I added "text-align: center" to the body.

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

Does the icon vanish once you alter the button's color?

Warning: Adult content Check out this website: link When you visit this page, you'll notice that the Add to Cart button is orange. The button also has an icon of a shopping cart. This icon only appears if you remove the following code. To make th ...

Tips on looping through a universal array and retrieving user input

My current project involves creating a news site using the Guardian API. I want to implement a search feature that allows users to input keywords and retrieve the webTitle attribute for matching elements. While I could simply insert the user input directly ...

Tips for automatically adjusting the width of dt and dd elements using CSS

My approach to creating a paper form involves using dt and dd elements. The width of dt should align with the word, while dl should occupy the remaining space. Currently, I manually adjust each line, but I am looking for a way to automate this using CSS. ...

Hidden input for Vue form submission

I need to include two Id's with a user's input by adding them in a hidden input field. Since v-model doesn't work with hidden inputs, I have set up my hidden input like this: <input type="hidden" ref="property_id" :nam ...

Warning: Potential Infinite Loop when using Vue JS Category Filter

I developed a program that filters events based on their program and type. The program is working correctly, however, an error message stating "You may have an infinite update loop in a component render function" keeps popping up. I suspect that the issue ...

How to collapse one section while expanding another in Bootstrap 4?

const cur_url = window.location.href.split('#')[0].split('?')[0], body = $('body'), menu_toggle = $('.menu-toggle'), sidebar_menu = $('#sid ...

Using JavaScript, the list of items (images) will be displayed and placed into HTML panels

Below is the layout structure on my website: <div class="panel-heading"><h3 class="panel-title">Suggestions</h3></div> <div class="panel-body"> <div class="col-md-7"> <h3><span class= ...

What HTML template is your go-to when starting a new project?

Is this the ultimate solution for creating a basic "Hello World" HTML template? I have been experimenting with this starter code to develop simple websites. Are there any other interesting tags I could incorporate to enhance it further? Usually, I refer ...

Generate a table containing information organized by category

I am looking to create a component Table that groups countries together. Here is my mockup: enter image description here I am struggling to find a solution for this issue. Can someone please assist me? ...

What is the best way to ensure a floated image matches the height of its parent container?

I am facing an issue with setting the height of a logo within a footer div to match the height of the div itself. Using height: 100% doesn't seem to work as expected. Below is the relevant HTML: <footer> <img src="images/circle_logo.png" ...

Showing how an iframe can adjust its height to 100% within a div element

Currently, I have a player status script implemented on our Minecraft Server. This script displays the server name, IP address, and the heads of the players currently online. The setup includes two div boxes for the status box: one outer box to fix its pos ...

Ways to embed an HTML file into another HTML document

I have been attempting to include a footer.htm file in my index.htm document without success. Below is the code I am using. index.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.d ...

Struggling to Place Image Correctly

Currently in the process of constructing an About Me page, I want the text box to align to the left side of the screen while having a picture inline with the text. However, I've encountered an issue where the picture only displays underneath the text. ...

Obtain the value of an attribute and store it as a variable in Google

Hello! I currently have code on my website that retrieves the site search query stored in the 'value' attribute. Specifically, I am looking to capture the word 'disjoncteur' for a variable within Google Tag Manager (GTM). <div class= ...

Ways to emphasize an HTML element when clicked with the mouse

Can JavaScript be used to create code that highlights the borders of the HTML element where the mouse pointer is placed or clicked, similar to Firebug, but without needing a browser extension and solely relying on JavaScript? If this is possible, how can ...

Error message encountered: "Attempted to serve a new HTML file but received the 'Cannot set headers after they are sent to the client' error."

I'm a beginner in NodeJS and I'm currently working on enhancing a sample project that utilizes the Spotify API and Express. In this project, users are required to authenticate on the homepage, after which they should be directed to a different HT ...

ACTUAL preventing a component from losing its focus

I have been exploring ways to effectively stop a DOM element from losing focus. While researching, I came across different solutions on StackOverflow such as: StackOverflow solution 1 StackOverflow solution 2 StackOverflow solution 3 However, these sol ...

Having an issue with HTML and JavaScript where the button won't open when pressed. Any help is appreciated

https://jsbin.com/haluhifuqe/edit?html,js,output I'm facing an issue with my HTML & JavaScript code - when I click the button, it doesn't open. Can anyone help me out? <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

Steps for creating a read-only Material-UI input text field in Angular 12

I'm looking to create a read-only text field using Material-UI that cannot be edited. I attempted to achieve this by disabling it in the .ts file of the component: this.firstFormGroup.controls['gauthan_nirmit'].disable(); However, when I m ...

Is it possible to utilize onclick for various table cells in jQuery?

I have a situation where I need to loop through dates and display table content accordingly. Could someone please assist me in figuring out how to do this? index.html.erb <% @batches.each do |batch| %> <tr> **<td class = "pie" id ...