How to align a div to the right using CSS

I've recently delved into the world of web development through an intriguing course on this platform. Currently, I find myself tinkering with the code snippet below in order to deepen my understanding of flexbox. HTML:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Playground</title>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="app.css">
</head>

<body>
    <h1>Exploring Flexbox Features</h1>

    <section id="container">
        <div style="background-color: #80ffdb"></div>
        <div style="background-color: #64dfdf"></div>
        <div style="background-color: #48bfe3"></div>
        <div style="background-color: #5390d9"></div>
        <div style="background-color: #6930c3"></div>
    </section>

</body>

</html>

Relevant CSS snippets:

body {
    font-family: 'Open Sans', sans-serif;}
h1 {
    text-align: center;
}

#container {
    background-color: #003049;
    width: 90%;
    height: 500px;
    margin: 0 auto;
    border: 5px solid #003049;
    display: flex;
    flex-direction: column;
    /* justify-content: flex-end; */
    flex-wrap:wrap;
}

#container div{
    height:200px;
    width: 200px;
}

A preview of the webpage design: https://i.sstatic.net/SD3dT.png

I'm curious about how to align the main div either to the right or left within the background. Which specific style or command should be applied for this purpose?

UPDATE: While I can handle the alignment of smaller squares inside the divs, I'm particularly interested in shifting the large blue-ish rectangle in the container's background to either side – rather than it being centered by default. My apologies if this clarification was lacking in my earlier question.

Answer №1

Your content is currently centered because the left and right margins are set to auto. To align it to the right, you can use margin-left: auto;, and vice versa for left alignment.

body {
    font-family: 'Open Sans', sans-serif;}
h1 {
    text-align: center;
}

#container {
    background-color: #003049;
    width: 90%;
    height: 500px;
    margin-left: auto;
    border: 5px solid #003049;
    display: flex;
    flex-direction: column;
    /* justify-content: flex-end; */
    flex-wrap:wrap;
}

#container div{
    height:200px;
    width: 200px;
}

body {
  margin: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Playground</title>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="app.css">
</head>

<body>
    <h1>Let's Play With Flexbox</h1>

    <section id="container">
        <div style="background-color: #80ffdb"></div>
        <div style="background-color: #64dfdf"></div>
        <div style="background-color: #48bfe3"></div>
        <div style="background-color: #5390d9"></div>
        <div style="background-color: #6930c3"></div>
    </section>

</body>

</html>

Answer №2

When it comes to alignment, use align-items: flex-start; for left align and align-items: flex-end; for right align.

body {
  font-family: 'Open Sans', sans-serif;
}
h1 {
  text-align: center;
}

#container {
  background-color: #003049;
  width: 90%;
  height: 500px;
  margin: 0 auto;
  border: 5px solid #003049;
  display: flex;
  flex-direction: column;

  /*right align*/
  align-items: flex-end;

  /*left align*/
  /* align-items: flex-start; */

  /* justify-content: flex-end; */
  flex-wrap: wrap;
}

#container div {
  height: 200px;
  width: 200px;
}

Achieve right alignment here

Answer №3

To make sure that the elements in your section are aligned to the right, add this line of code:

align-items: flex-end;

In order for your section's CSS to achieve this alignment, it should look something like this:

#container {
  background-color: #003049;
  width: 90%;
  height: 500px;
  margin: 0 auto;
  border: 5px solid #003049;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  flex-wrap:wrap;
}

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

Interactive HTML/CSS Periodic Table with Dynamic Design

Recently, I have been immersing myself in learning about responsive design in HTML/CSS and decided to challenge myself by coding the periodic table of elements using HTML/CSS to hone my skills. While I have set up the basic structure of the table with div ...

Looking to personalize the appearance of an iframe using CSS styling?

I am working with an iframe that generates a form, and I would like to customize the CSS of this form. How can I go about editing the CSS? <div class="quiz-container" style="text-align: center;" data-quiz="#######" data-pr ...

Choose just one column within an HTML table

Is there a way to easily select the text of a single column from an HTML table using just the mouse pointer? Imagine you have a table that looks something like this: https://i.sstatic.net/n3lZR.png When trying to select only the text in the Lastname col ...

Designing rectangular divs with arrow elements

As I delve deeper into the world of CSS, my experiments have been yielding exciting results! From creating popup boxes to playing around with CSS styling, it's been a fun journey so far. However, I'm eager to challenge myself and expand my knowle ...

Tips for aligning text to the right and keeping it in line with another section

I have a code snippet and I'm aiming to align the caption for my "DigDug" game directly below the game itself, perfectly lined up horizontally. The id for the caption is "#DigCaption" and the id for the game is "#DigDug". Could anyone provide guidance ...

Issue with Chrome extension: inability to submit values

My Chrome extension is not functioning properly. Here is the code that I have: //manifest.json { "manifest_version": 2, "name": "auto login", "description": "This extension allows for automatic logins.", "version": "1.0", "permissions ...

"Adjusting Flex Items Dynamically When Resizing the Screen

Hello everyone, I am a new web developer looking to create a calendar using only pure JS. Currently, I have managed to wrap elements on overflow. My question is: how can I efficiently 'destroy' and 'create' new nodes in a responsive ma ...

Reducing the width of the bootstrap table does not seem to work properly when trying

I am currently working on a bootstrap table design that features a table with a rowspan of "2" in the right corner, creating a layout with 2 rows on one end and another rowspan of "2" on the other end, as shown in the following image: https://i.sstatic.net ...

Incorporating JQuery UI sortable functionality with the CSS grid display property to create a

I'm currently working on creating a sortable CSS grid using jQuery UI. $( function() { $( "#sortable" ).sortable({ handle: "handle" }); }); gridHolder{ display:grid; background:tan; grid-template-columns: ...

Create a PDF on-the-fly by leveraging Vuejs to dynamically pull data and design elements

Is it possible to create a form that captures name, expiry date, and picture, and upon submission generates a PDF document with the provided details? The PDF should be based on a design created by me and have the input data displayed in a specific location ...

Choosing a picture element based on the source attribute for identification

Currently, I am in the process of writing a script for a webpage and I find myself needing to update the icon associated with an image. The issue lies in the fact that the image only contains a tag structured like this: <img src="/pic1.png" alt="clear" ...

Rotating an image within Unity 3D around a stationary cylindrical background

I have a unique feature on my current e-commerce website that involves displaying a 360-degree rotation effect of a mug (or cylinder) with customizations such as photos, text, and layouts created in Adobe Flex. This was achieved using Paper Vision 3D, bu ...

Enclose elements in a div using a jQuery each function

I have successfully parsed data from an XML feed to JSON, but now I want to wrap each part of the data within a div. Here is the code snippet I am working with: var newsDiv = $('<div class="news-feed">').appendTo($("body")); $(release ...

Selenium is having trouble scrolling to the bottom of the page

Struggling to navigate to the bottom of the webpage, it seems to only scroll once before stopping, leaving a large portion of the page unseen. I'm currently using this code snippet: _inst.driver.execute_script("window.scrollTo(0, document.body.scroll ...

Is it possible to include a hidden default option that provides a description for my `select` tag?

Can someone help me create a description for the select tag that won't show up in the drop-down options? I remember seeing this done somewhere, but I can't recall where. Any suggestions? This is what I am trying to achieve: <fieldset> ...

Rearranging the @use directive in Sass

Here are some style snippets: tokens.scss .text-center { text-align:center; } .bold { font-weight: bold; } @mixin text-style-1 { font-size: 1.2rem; font-weight: bold; } component/card.scss @use "../token.scss" as tokens; .card { @i ...

Warning: Datatables encountered an issue with the table ID 'example'

Good day, I am seeking clarification regarding DataTables. In a tutorial, I came across a table that can be edited "inline". I prefer the table to be in German. The code snippet for the table: <script type="text/javascript" language="javascript" ...

The menu item is having trouble aligning to the right side

Hello, I am currently working on developing a website menu for a project and I am facing an issue with aligning the "Login" Menu item to the right side. Any assistance in resolving this alignment issue would be greatly appreciated. I am also willing to pro ...

The functionality of item.classList.toggle() in HTML+CSS+JS fails to execute

I have been working on a program that aims to flip a card when it is clicked. The JavaScript code I have written for this functionality is as follows: /* Card flipping onclick */ import "./Stylesheets/FlipCardStyle.css" var cards = document.quer ...

Is there a way to showcase items from one table onto another table with the click of a button?

One issue I am currently facing involves creating an "Add to Meal Planner" button that allows a user to select a specific aliment from one table (Aliments) and add it to another table (Meal Planner). public ActionResult MealPlannerAliments(int? id) { ...