Arranging div elements with CSS styling

I always struggle with CSS layouts and positioning 'div' elements on the page. For instance, I have created the following structure:

Check out my jsfiddle: http://jsfiddle.net/JJw7c/3/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Example</title>
</head>
<body>
    <div class="container">
        <div class="header">
            this is the header
            <div class="menu">
                menu goes here
            </div>
        </div>
        <div class="main">
            main content goes here
        </div>
    </div>
    <div class="footer">
        footer        
    </div>
</body>
</html>

body
{
    margin:10px;
}
.container
{
    border:1px solid black;
    min-height:400px;
    width:100%;
}

.header
{
    border:1px solid red;
    height:100px;
}

.menu
{
    border:1px solid green;
    height:20px;
    width:50%;
    float:right;
}

.main
{
    border:1px solid grey;
    min-height:100px;
    margin:20px;
}

.footer
{
    border:1px solid green;
    width:100%;
    padding-top:10px;
}

Suppose I want to position an icon in the lower right corner of the header with precision. Should I use `position: relative` then `top: 20px`, or `position: absolute`? Or maybe `margin-right: 200px`? Could I utilize floats along with position absolute? <<< Please elaborate on when each approach should be used and if possible, update my jsfiddle with examples for each.

Thank you

Answer №1

Implement

position:absolute;
bottom:0px;
right:0px;

for the icon class and include position:relative; in .header

Answer №2

To achieve this layout, you can utilize a combination of relative and absolute positioning styles. Start by applying position: relative to the .header element. Then, insert your icon with position: absolute, making use of the top and left properties. These values will be calculated in relation to the header element, as it is the first non-static element that acts as the parent of the icon.

An added benefit of implementing this method is that the presence of the icon does not disrupt the positioning of other elements within the header, given its absolute positioning.

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

The issue I encountered with Angular's Mat-Select component is that the openedChange

When a user opens the mat-select, I am attempting to set CSS style using custom code. However, I am encountering an undefined error. Upon checking in the console, I noticed that the panel value is returning undefined. Can someone please advise me on how to ...

Which files should be included to define the variable $? in jQuery/JavaScript?

I am looking to create a sliding Div over another div in a simple way. The example provided in this Fiddle is exactly what I have in mind, but as someone new to coding, I encountered the warning Uncaught ReferenceError: $ is not defined after transferring ...

Tips for setting up personalized breakpoints for a Bootstrap navbar

I am currently facing an issue with my navbar on tablet view. Despite implementing custom breakpoints to collapse the navbar at 1050, the menu bar is appearing in two rows instead of one. I have tried using the code below but it doesn't seem to be wor ...

What is the best way to show an image within a div using HTML and fetching data from an API response?

I am attempting to showcase an image within a div using HTML. I utilized fetch to retrieve the JSON data from the following API: . The response contains JSON data with the image URL labeled "primaryImage". While I can display it as text, I am encounterin ...

Remove 'File' option from the Menu in Tiny MCE

Is there a way to selectively remove the "File" option from the Menu Bar without specifying each individual menu item? https://i.sstatic.net/SFgvi.png I attempted the following method: menu: { edit: {title: 'Edit', items: 'undo redo | cut ...

Issue with SALON theme causing logo to not display on Wordpress site

It's puzzling why the logo image I uploaded to the WP theme isn't showing up on the front-end. I've reached out to the theme developers multiple times, but it's been two months without a response, and my client, as well as myself, are ...

Next.js is refusing to render an array of HTML elements

Consider this scenario where I have a block of code in TypeScript that attempts to create and display a list of elements. Here is a sample implementation: const MenuList = ():ReactElement => { const router = useRouter(), liElements:any = []; con ...

Sometimes, the page-wide background in Internet Explorer doesn't load properly

Currently, I am working on a website that uses an image as the background for the entire site. Everything seems to be running smoothly in Firefox and Safari, but Internet Explorer is giving me some trouble. Sometimes the image fails to load at all, other t ...

Utilize a fresh font style and replace the font family components in @mui/material with the new

Looking to bring in a fresh font family stored in a fonts directory and replace the existing mui base font family. import { responsiveFontSizes, createTheme } from '@mui/material'; import NEWFONTFAMILLY from '../fonts/something.otf' c ...

The issue with color swapping in Internet Explorer is not functioning correctly due to

I am facing an issue with a jQuery script that I have created to change the background colors of rows in a large table. Unfortunately, it seems to be malfunctioning on Internet Explorer versions 6 through 9. Below is the snippet of code I am using: <s ...

Difficulty being faced in recognizing anchor tags through CSS selector methods by Mechanize

(Just wanted to check here since I didn't get much response on RailsForum lately.) Has anyone encountered issues with Mechanize not picking up anchor tags using CSS selectors? Here's a snippet of the HTML code: <td class='calendarCell& ...

Setting the height of a box-inner div to full height in Bootstrap 4: A guide

Assistance Needed: I have encountered an issue with my design while using bootstrap-4. The box-inner div works perfectly on desktop, adjusting its height according to the content entered. However, on a real iPad, the columns are not of equal height as sho ...

What is the trick to concealing a div on a webpage through the addition of a designated parameter to the URL?

As a newbie blogger delving into the world of HTML, my knowledge of JavaScript is quite limited. I am eager to find out how I can hide any div on a webpage simply by adding a parameter to the URL; for example, if I were to add ?hide=header-wrapper at the e ...

Problem aligning ul within div element

I'm having trouble aligning my ul element in the center of a div. I tried using the margin: 0 auto method, but it's not working as expected. I've always wondered if setting the width of the ul within a div is necessary for using margin: 0 au ...

Transition delays in Tailwind CSS when dark mode is activated

My dark mode toggle is functioning with the following CSS: :root { --primary: var(--white); } .dark { --primary: var(--black); } However, when I include the following code: * { @apply transition-colors duration-700; } The background colors of ...

Start up a server-side JavaScript instance utilizing Express

My journey into web programming has led me to learning JavaScript, Node.js, and Express.js. My ultimate goal is to execute a server-side JavaScript function (specifically a function that searches for something in a MySQL database) when a button is pressed ...

Understanding ng-if in AngularJSAngularJS includes a useful directive called ng

After using angularJS, I found myself wondering how to effectively implement its if statement in a particular way. I hope that my example will help clarify my question. Here is the if statement I am working with: <div data-ng-if="book.Availability> ...

Is it possible to execute a PHP script within a .css file and ensure it functions correctly?

Recently, I've been making some changes to the .css file in order to get my PHP codes to work. However, when I insert them as usual, they appear as regular text. I'm curious to find out if it's possible to execute a PHP file within a .css fi ...

I am trying to layer 3 x 3 images on top of a background image, but the images are overlapping

I am in the process of creating a website using bootstrap 4. I have a background image that needs to have nine smaller images overlaid on top. However, the images are currently overlapping each other and need to be arranged properly. This layout also needs ...

Brick-themed HTML/CSS elements drift away from each other

I'm currently designing an image collage for my website and attempted to use masonry for the layout. However, when I adjust the size of the blocks, they seem to drift apart, creating large gaps between each block. Any suggestions on how to resolve thi ...