What is the best way to position the div in the center of the screen instead of the lower right corner

This is what I was looking for.

https://i.sstatic.net/lb6dY.png

Below is the CSS code I am using:

<html>
<header>
<style type="text/css>
*{margin:0 0 0 0;padding:0 0 0 0;} 
div.block{width:400px;height:121px;border:1px solid red;position:absolute;top:50%;left:50%;}
div.box{float:left;}
div img{margin:0px;padding:0;width:121px;height:121px;float:left;}
div.description{float:left;border 1px solid red;margin:10px 50px;}
</style>

</header>
<body>
    <div class="block">
        <div class="box"><img src="test.jpg" /></div>  
        <div class="description">
                <p>music mane: xxxxxxxx</p>
                <p>author: yyyyyyyy</p>
                <p>publication:20081001</p>
                <p>language: english</p>
          </div>  
     </div>

</body>
</html>

Here is the test image.

https://i.sstatic.net/hd0QR.jpg

The div is currently at the lower right corner of the screen, how can I move it to the center of the screen? Using top:50%;left:50% doesn't achieve the desired result.

https://i.sstatic.net/HT3qM.png

Answer №1

give this a shot

swap out " div.block " with the following snippet of code

div.block {
    width: 400px;
    height: 121px;
    border: 1px solid red;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
}

Answer №2

Implement this CSS snippet:

section.box{
width:300px;
height:175px;
border:2px solid blue;
position:relative;
top:50%;
left:50%;
margin-top:-87.5px;
margin-left:-150px;
}

The key is to set the left margin as half of the width and the top margin as half of the height, both as negative values.

Answer №3

Utilize the margin:auto property for centering elements

Answer №4

Here is a suggestion to try:

*{margin:0 0 0 0;padding:0 0 0 0} 
div.block{width:400px;height:121px;border:1px solid red;position:absolute;top:50%;left:50%;margin-top:-60px;margin-left:-200px;}
div.box{float:left;}
div img{margin:0px;padding:0;width:121px;height:121px;float:left;}
div.description{float:left;border 1px solid red;margin:10px  50px;}

Check out the demo here

Alternatively

*{margin:0 0 0 0;padding:0 0 0 0} 
div.block{width:400px;height:121px;border:1px solid red;position:absolute;top:50%;left:50%;margin-left:-110px;}
div.box{float:left;}
div img{margin:0px;padding:0;width:121px;height:121px;float:left;}
div.description{float:left;border 1px solid red;margin:10px  50px;}

View my demo here

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

What is a method for capturing the value of a nested input element without requiring its ID to be

Currently, I am facing a challenge in selecting the value of an input box from a user-generated ordered list of text boxes and logging its value to the console. It seems like I cannot find a way to select the value of a child's child element when the ...

Disable automatic focus on first button in PrimeNG Dialog

I am looking for a way to stop the autofocus on the first input element when opening the PrimeNG dialog. <p-dialog header="User Details" [(visible)]="displayDialog" [responsive]="true" showEffect="fade" [modal]="true" (onAfterHide)="onDialogHide ...

Creating dynamic drag and drop templates: A step-by-step guide

I am in need of developing a feature that allows users to create custom templates with various elements like labels, textboxes, combo boxes, rows, columns, and establish parent-child relationships among them. These elements may also be tied to different da ...

What is preventing Sublime Text from recognizing my pseudo classes as valid syntax?

I'm attempting to remove the underlines from all the links I've visited on a website. In my CSS file, I added nav li a:visited { text-decoration: none; } The word "visited" appears as white text in Sublime Text, indicating that the syntax ...

Tips for beginning the process of creating a website's user interface

After initially developing my system without any styling, including bootstrap and CSS, I now find myself needing to add them. While I have reviewed the concepts on W3Schools and have a basic understanding, I am struggling with the sequence of implementat ...

Is there a tool available for formatting HTML on the command line in Linux, or is there a PHP alternative for achieving this task

Is there a command line HTML formatter compatible with Linux that can format HTML files like this: <html> <body> <p> hi </p> </body> </html> And output it like this: <html> <body> <p> ...

Type Fury: A Multiplayer Gaming Hub for Speed Typists and Puzzle Solvers

Curious about creating a website that allows users to log in and participate in competitions against each other. Any tips on where to start or recommendations for existing frameworks would be greatly appreciated. Appreciate your help in advance! ...

Having trouble bringing in SVG file into my React project

I am currently using React version 15.4.1 and I have tried implementing a solution from this link. However, instead of the component being rendered, I only see a string like this https://localhost:3001/dist/7553ed8d42edb0d73754cd9a5dea2168.svg This is how ...

input access is granted through the post method using an integer as the name parameter

Suppose there is a dynamic form with multiple fields generated with integer names based on the iteration that creates them. The goal is to retrieve their values using either post or get method (though focusing on post here). Below is the code for generati ...

A step-by-step guide on showcasing a Word document in a popup window using MVC 4 Razor

I'm looking to display a Word file in a pop-up window using MVC 4 Razor. The file is located at a specific path and has already been stored in my project. ...

A plethora of color choices in a Multi-select Box

Hi there! I am facing an issue with dynamically adding multiple select boxes. Currently, I have 4 color-coded options under the selection boxes, which work fine when there is only one box. However, when I add more than one select box, the color coding doe ...

The background image on a lengthy HTML table goes missing

Within a table cell, I have around 7500 short text lines. Interestingly, the background image of the table disappears around the 1800th line. I'm wondering if there is a specific limit on the length of the table that could be causing this? Even ...

Collapse in Bootstrap 3 Navigation Bar

I'm really struggling to understand why my navbar is refusing to collapse. I've included the necessary Bootstrap CSS and JS files, as well as the transition and collapse JS files. Below is the code that I've been working with. Any assistance ...

What could be causing a single image not to show up on an HTML page?

I have been searching tirelessly for a solution but cannot seem to find one. Interestingly, in my HTML document, an image appears perfectly fine when viewed from my browser. However, once I upload my website to a server host, this specific image refuses to ...

The CSS :not selector does not work as intended

My goal is to assign the class no-color to a specific tag. When JavaScript is enabled, I want this class to be removed and the text to be colorized. However, my current CSS solution is not working as expected. Below is a sample code snippet demonstrating t ...

Swapping mouse cursor using JavaScript

Currently, I am working on a painting application in JavaScript that utilizes the Canvas Object. I would like to customize the mouse cursor when it hovers over the Canvas object. Can anyone advise me on how to accomplish this? ...

Guide on redirecting to a different page and triggering a function with Angular's ng-click

One of my challenges involves toggling between different sections on a webpage using Angular functions onclick within home.php. Here is an example code snippet: <button class="btn btn-success btn-lg span6" ng-click="settingManage('setup-guide&apos ...

Issue with Angular 6: date disappearing upon refreshing the page

While working with a textarea that allows users to submit comments, I encountered an issue. Whenever a comment is submitted, the date and time of submission are saved along with the comment in a JSON file. However, upon refreshing the page, the date inform ...

A meteorological anomaly occurred during the execution of an asynchronous function, resulting in an exception in the

Encountered Problem: I am attempting to update a collection in the onAfterAction hook within a route using the code below (where 'pages' is the name of the collection): var document = pages.findOne({page: "stats"})._id; console.log(document); va ...

Tips for seamlessly placing a div with a background image within a parent container, all while preventing any scrolling issues as the child div rotates

I created a simple demo featuring a square image with rounded corners to give it a circular appearance. The image rotates slowly, but as it moves diagonally, the horizontal width fluctuates, causing the parent container to resize. I want the parent contain ...