CSS - With the abundance of positioning methods available, determining the right approach can be quite a puzzle

As I delved into web development, my fascination with positioning grew. In the beginning, my novice websites were structured like this:

<body>
    <h1> Welcome to my website. </h1>
    <br> <br> <br> <br> <br> <br> <br> <br> <br><br> <br> <br>
    <p> Copyright 2016 </p>
</body>

However, I soon realized that this approach was not in line with professional standards as it limited additional markup and lacked semantic value.

In search of a better way, I experimented with position: absolute, resorting to manually positioning every element on the page. Unfortunately, this method proved to be rigid and led to layout issues upon resizing.

Seeking an alternative, I began using margin for positioning instead of excessive <br> tags. Despite this workaround, I still feel uneasy about my current approach.

I am seeking guidance on best practices for CSS positioning, as I have explored various resources without fully grasping the concept. How do experienced developers tackle this aspect of web design? Any advice would be greatly appreciated.

Answer №1

The top four position values used frequently are initial, relative, absolute, and fixed.

If you use the fixed value, it will position an element in a stationary location on the main screen.

To set an element in a fixed position relative to its parent, utilize the values absolute and relative. Ensure to declare the element as absolute and its parent as relative.

You can implement this code snippet:

<style>
    body{
        position:relative;
    }
    #copyright{
        position:absolute;
        top:5%;
    }
</style>

<body>
    <h1> Welcome to my fantastic website. </h1>
    <p id="copyright"> Copyright 2022 </p>
</body>

For more detailed information, check out w3schools

Answer №2

It's a fact that using position: absolute; will prevent your page from being resizable. However, if you opt for margins instead, it provides flexibility. A better alternative than the <br> tag is to use float: left; on each <div> when multiple elements need to be displayed in a row. This approach allows for easy adjustment based on zoom levels, with elements shifting to new rows as needed.

In summary, Utilize float: left; for creating responsive designs.

Answer №3

To ensure compatibility across different devices, it's important to consider using frameworks such as Material Design and Bootstrap. Personally, I prefer utilizing margins and borders when designing websites, as div elements serve as the foundation for many professional web layouts.

Answer №4

The CSS declaration position:absolute; indicates that the element is placed in relation to its nearest positioned (non-static) ancestor.

I agree with @Blue's suggestion to use margins over <br>. Another option for responsive design is using float:.

Answer №5

In my opinion, it is beneficial to utilize margins for block positioning as a general practice. However, there are instances where certain elements on a page may require absolute positioning due to being out of flow. Despite this, I make an effort to minimize the use of absolute positioning whenever feasible in my projects.

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

Clashing CSS Styles: Font Size Edition

Can someone explain how CSS font sizes work? I set a specific line to be 200% font size, but changing the body font size affected it. Shouldn't the line maintain its specified size? When the body font size is changed from 12pt to 8pt, the line "There ...

Adjusting Font Size for Buttons on Mobile Devices in HTML

I am currently working on a website that I want to ensure is mobile-friendly. When testing it on my phone and in Chrome's device mode, I noticed that the majority of the text on the screen was displayed at an easily readable size without needing to zo ...

Move the element to the top within the navigation bar

I am currently enhancing the appearance of my navbar by adding two "Stripes" on top. I am using bootstrapv4 as a base, but facing an issue where bootstrap centers my decorations. How can I prevent this? Below is a simplified version of my code: HTML: &l ...

Enhancing the Appearance of WooCommerce Product Descriptions

Having some trouble with my website . There are two descriptions on the "Product" page - one above the tabs and one inside the tabs. Trying to change the text color in the tab section to black, but when I adjust the body text color it affects both areas. I ...

Push down on the scroll wheel and then let go

I am looking to create a script that will automatically scroll the window to a specific point if a certain condition is met. Once the scrolling is complete, I want to update the condition so that the window can freely scroll again. Can someone offer guidan ...

Performing mathematical calculations using javascript

In my project, I'm utilizing HTML, CSS, and JavaScript to achieve the following: Dropdown menu for Category (Coffee Appliance) Dropdown menu for Product (Keurig Coffee Maker) Wattage: 1500kWh (auto-filled from Local Storage) Daily Energy Con ...

The span is unresponsive

When focusing on an input or checking its validity, the span should move up but it's not responding to any styles. <div class="login"> <span class="logtxt">Username or email</span> <input type=& ...

The HTML code may fade away, but the JavaScript is still up and running behind the

Switching between different div elements in my HTML document is a challenge. Here's the code I currently have: <div id="screen1" class="current"> <div id="press_any_key_to_continue"> <font style="font-family: verdana" color="yellow ...

Displaying JSON data using FormControls in Angular 5

Upon receiving Json values from the server, I am encountering an issue while binding them to respective textboxes. The problem arises as the value in the textbox appears as [object object] <h1>{{title}}</h1> <h3>Catalog</h3> ...

How may I utilize CSS to generate a dynamically resizing rotated triangle that adjusts based on the text content?

Looking to add a dynamic badge in the top left corner of a div that adjusts its size based on the text content. Check out this example: http://jsfiddle.net/sbick/npxaasht/ <article> <div class="triangle"> <span>NEW</span ...

"Step-by-step guide to building a webgrid or table to organize and display your

These are the HTML codes I have created: <table> <tr> <td><label style="text-align:left">Product Code:</label></td> <td><input type="text" id="productCode" value="" /> </td> </tr> & ...

Positioning the comments box on Facebook platform allows users to

Need assistance, I recently integrated the Facebook comments box into my Arabic website, but I am facing an issue where the position of the box keeps moving to the left. Here is an example of my website: Could someone please suggest a solution to fix the ...

Issue with nav-pill not functioning properly on localhost server set up with XAMPP

<!-- Customized Navigation Pills --> <ul class="nav nav-pills" role="tablist"> <li class="nav-item"> <a class="nav-link active" data-toggle="pill" href="#home">My Home</a> </li> <li class="nav-item ...

Styling with CSS or using tables: What should you choose?

Is there a way to replicate the frame=void functionality in CSS, or will I need to manually add frame=void to each table I create in the future? <table border=1 rules=all frame=void> I attempted to search for a solution online, but unfortunately, m ...

Eliminate HTML nodes that are not currently visible

I am currently developing a Vue/Vuetify application that showcases a large number of images (10-20k) in a grid view along with some additional information. These images are grouped into different sections, and clicking on an image opens an overlay displayi ...

Tips on vertically aligning a div using Bootstrap

I'm looking to vertically align a card in the center <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorig ...

Autoprefixer encountered a TypeError while executing the npm script: Patterns must be specified as a string or an array of strings

Upon executing npm run prefix:css, the following error message is displayed: Error: Patterns must be a string or an array of strings { "name": "natours", "version": "1.0.0", "description": "A project for natours", "main": "index.js", "script ...

Changing the name of a CSS file on the live server

I was recently browsing a website and noticed that they had given their CSS file a really long name. The website in question is It seems like they are using Twitter Bootstrap as their web UI framework, so I'm curious as to why they would choose such ...

Launching a modal in a new browser window with the help of JavaScript or PHP

I need to implement a feature where clicking a link opens a modal in a new tab and redirects the current page to a different link, similar to how retailmenot handles coupons. Here is the code snippet I am currently working with: <div onClick="myFunctio ...

JQuery Falters in Responding to Button's Click Action

Forgive me for what may seem like a silly question, but as someone new to JQuery, I have been struggling to figure out why my function is not displaying an alert when the button is clicked. Despite thorough research on Google, I haven't been able to f ...