Using CSS to center the div and align its son on the left

I am struggling to align the elements in my HTML and CSS prototype. This image shows what I am trying to achieve.

I attempted to use display: flex and justify content: center to center the main div. However, I encountered difficulties in positioning the p elements to the left of the centered div. I tried using margin-right: auto for p, but it did not produce the desired result.

Below is the code snippet I have been working on:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<style>
  .wrap {
    display: flex;
    justify-content: center;
    border: red 5px solid;
  }

  p {
    display: flex;
    margin-right: auto;
    color: blue;
  }
</style>

<body>
  <div class="wrap">
    <p>lorem ipsum</p>
  </div>
</body>

</html>

Any assistance or advice on how to correctly align these elements would be greatly appreciated. Thank you!

Answer №1

To ensure that the wrap element is centralized within the body wrapper, you must apply display: flex to the body.

As the p element naturally aligns to the left, there is no need to utilize flexbox for its positioning.

Lastly, it is crucial to assign fixed dimensions (width and height) to the wrap element to prevent it from stretching.

<!DOCTYPE html>
<html lang="en>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE-edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
   body {
     display: flex;
     justify-content: center;
     border: 2px solid purple;
     min-height: 100vh;
     min-width: 100vw;
   }     
  .wrap {
    border: red 5px solid;
    width: 200px;
    height: auto;
    margin: 50px auto auto;
  }
  p {
    color: blue;
  }
</style>
<body>
  <div class="wrap">
    <p>lorem ipsum</p>
  </div>
</body>
</html>

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

Embrace the words enveloped within large quotation marks

I am seeking a way to format paragraphs with large quotation marks surrounding them. I have attempted several methods, but my line-height seems to be affected, as shown in the following image: Can anyone suggest a proper method for achieving this? (Addit ...

The border should not start at the left of the page; instead, I would like it to begin at the letter "T" in "Tech."

I am having an issue with the bottom border starting from the extreme left of the page. I want it to start from the letter "T" in Tech instead. #page-container { width: 1250px; margin: 0 auto; } h2 { font-weight: normal; padding-left: 15px; ...

The page refreshes when the anchor element is clicked

Currently, I am working on enhancing a web application that is built on the foundation of traditional aspx (asp.net) web forms with elements of Angular 6 incorporated into it. My current assignment involves resolving a bug that triggers a page refresh whe ...

The alert feature does not seem to be functioning properly when displaying error messages

// Issue: Alert is not working on error message. I intend to only display up to four issues, after that it should not work. success: function(msg, String, jqXHR) { window.location = 'home.html'; $("#result").html(msg, String, jqX ...

Utilize JSON data fetched from a URL to dynamically populate an HTML content

There is a JSON file located at a URL that looks like this: [{"tier":"SILVER","leagueName":"Tryndamere's Wizards","queueType":"RANKED_SOLO_5x5","playerOrTeamId":"91248124", "playerOrTeamName":"NunoC99","leaguePoints":18,"wins":411,"losses":430,"rank" ...

Apply borders to the table provided in the email using Python

Presently, I am utilizing this approach for sending tables in emails via Python instead of attaching them: Send table as an email body (not attachment) in Python import smtplib from smtplib import SMTPException import csv from tabulate import tabulate te ...

Creating a visually appealing Django filter form with custom styling in HTML

Currently, this is how my django filter form appears: https://i.sstatic.net/JQWFG.png Below is the html code I am using: <form action="" method="get" class="form-inline"> {{myfilter.form|bootstrap}} <but ...

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

Is it important that the end date does not exceed the start date?

In my request, the end date cannot be later than the start date. For example, if the start date is 24/4/2017 and the end date is 23/4/2017, the search function should be disabled for the calendar date 23/4/2017. Only dates from 24/4/2017 onwards should be ...

Performing a function when the ondrop event of a <li> element is triggered

Is there a way to trigger a code-behind click function on the ondrop event of an <li> element? Additionally, I would like to know if it's possible to force a postback on the onclick event. Any ideas on how to achieve this? Just to clarify, thi ...

Tips for designing a horizontal sub-navigation menu using CSS?

I have limited experience with list menus and am struggling to create a horizontal submenu with its own styling. Despite multiple attempts, I have been unsuccessful in finding a solution and have put the project on hold for now. Here is what I currently h ...

Traditional alignment challenges arise when trying to position two elements in a row using HTML and CSS

Greetings to the community of stack overflow! I am facing a mundane issue with two elements (refer to code snippet below) that requires my attention. Any help or advice would be greatly appreciated. I am aiming for the final output to resemble this: With ...

Discover the method to retrieve the month name from an HTML Date input value

Here we have a date input field <input id="customer-date" name="customer-date" type="date" required> Accompanied by this script const customerDate = document.getElementById('customer-date').value; const dateHandler = document.getElementB ...

How can external webpages be effectively integrated under a banner for a cohesive user experience?

Google Images serves as a prime example. Upon clicking on an image, a persistent frame appears at the top of the page, prompting users to easily navigate back to Google. Is there a specific term for this method and what would be the most effective approach ...

What causes webkit browsers to reduce the size of certain floating elements on mobile devices?

Struggling to create a responsive layout for a web app, specifically dealing with rendering issues on mobile webkit browsers. The floating elements are shrinking in an unpredictable way, causing problems on Chrome and Safari for Android and iOS devices. ...

Developing a security system that limits the number of times a code or password can be utilized

Introduction: I have a unique idea for a system where customers can purchase codes from my online store and redeem them on my PHP website. Each code purchased will have a limited number of uses, which decrease with each redemption until the code is no long ...

What is causing the background image to not display within my <div>?

I want to add a background image to only a specific part of my website, using the image as an id called "fruit." However, the image is not showing up. Here is the code I have: <div class="p-1 text-center bg-light" id="fruit"> &l ...

Trouble with jump link functionality within an ASP.NET iframe

On my asp.net page named Home.aspx, I have an iframe with the ID frame1. The iframe contains some HTML design with multiple headers. I am trying to search for the headers using quick links, but it seems to work fine on an HTML page, not on the asp.net page ...

"Implementing responsive design with Bootstrap's carousel through media queries

I'm having some trouble creating a basic carousel using bootstrap because the images are not displaying full screen. Do I need to use media queries to achieve this and if so, how can I implement them? <div id="myCarousel" class="carousel slide" da ...

What is the best way to choose the element directly beneath a specific element using jQuery?

I am facing a challenge where I need to manipulate HTML content using only jQuery, without changing the original structure. The requirement is to display and slide down content with a specific class (current) upon clicking on an h1 tag. However, I am strug ...