What is the process for adjusting the horizontal position of a flexbox using CSS?

Currently experimenting with flexbox to better understand its functionality. I have created the following layout: https://i.sstatic.net/rxvFD.png using the code provided below:

.container-1 {
  display: flex;
  flex-direction: row;
}

.container-1 div {
  border: 1px #ccc solid;
  padding: 10px;
}

.box-1 {
  flex: 1;
  flex: 0 0 0%;
}

.box-2 {
  flex: 1;
  flex: 0 0 0%;
}

.box-3 {
  flex: 5;
  flex: 0 0 60%;
}
<div class="container-1">
  <div class="box-1">
    <h1>Box One </h1>
    <p>This is my first paragraph!</p>
  </div>
  <div class="box-2">
    <h1>Box Two </h1>
    <p>This is my second paragraph!</p>
  </div>
  <div class="box-3">
    <h1>Box Three </h1>
    <p>This is my third paragraph!</p>
  </div>
</div>

If I wanted my third paragraph to appear as a sidebar on the right side of the screen, how would I accomplish that using flexbox? The traditional method of

position: absolute; width: 20%; left: 80%;
may not work with flexboxes. Any suggestions or insights would be greatly appreciated!

Answer №1

To start, if you happen to specify the same rule twice for a single element, only the last instance will be followed:

.box-2 {
  flex: 1;
  flex: 0 0 0%; /* Only this will take effect as a rule */
}

Additionally, when positioning the third element at 20% width on the right side, consider the following arrangement:

.box-1 {
  flex: 1;
}

.box-2 {
  flex: 1;
}

.box-3 {
  flex: 0 0 20%;
}

In conclusion, it's advisable not to rely solely on flexbox for layouts; grids offer a more robust solution https://developer.mozilla.org/en/docs/Learn/CSS/CSS_layout/Grids

<style>
       .container-one{
        display: flex;
        flex-direction: row;
      }
      
    .container-one div{
      border: 1px #ccc solid;
      padding: 10px;
      }
      
      .box-1 {
        flex: 1;
      }
      
      .box-2 {
        flex: 1;
      }
      
      .box-3 {
        flex: 0 0 20%;
      }
    </style>
    <div class="container-one">
      <div class="box-1">
        <h1>Box One </h1>
        <p>This is my first paragraph!</p>
      </div>
      <div class="box-2">
        <h1>Box Two </h1>
        <p>This is my second paragraph!</p>
      </div>
      <div class="box-3">
        <h1>Box Three </h1>
        <p>This is my third paragraph!</p>
      </div>
    </div>

Answer №2

Are you aiming for this design? :)

.container-1 {
  display: flex;
  flex-direction: row;
}

.container-1 div {
  border: 1px #ccc solid;
  padding: 10px;
}

.box-1 {
  flex: 1;
  flex: 0 0 0%;
}

.box-2 {
  flex: 1;
  flex: 0 0 0%;
}

.box-3 {
  flex: 5;
  flex: 0 0 60%;
  display: flex;
  /* ADDED */
  justify-content: space-between;
  /* ADDED */
  align-items: center;
  /* ADDED */
}

.box-3 p {
  /* ADDED */
  writing-mode: vertical-rl;
  /* ADDED */
}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">



<div class="container-1">
  <div class="box-1">
    <h1>Box One </h1>
    <p>This is my first paragraph!</p>
  </div>
  <div class="box-2">
    <h1>Box Two </h1>
    <p>This is my second paragraph!</p>
  </div>
  <div class="box-3">
    <h1>Box Three </h1>
    <p>This is my third paragraph!</p>
  </div>
</div>

Answer №3

If you're looking to incorporate a side bar into your web design, it's essential to consider its placement on the screen (whether top, bottom, left, or right) and how it functions as a block element in HTML. The order of your HTML elements is crucial for proper positioning, eliminating the need for using absolute positioning.

Once that foundation is set, utilizing flex properties can help you manage spacing and alignment effectively. By specifying properties like "space-between" and organizing elements within container divs with flex, you can achieve the desired layout.

Having this understanding allows you to step back from the code and visualize how block elements are positioned, leading to more accurate implementation choices.

As an added tip, I've renamed the class names for better clarity on their purpose and added some color coding to the elements for easier identification.

Edit: After noticing your use of the flex property to dictate width and flex-grow behavior, I updated the snippet accordingly to ensure responsive design functionality based on your existing code.

.container {
    display: flex;
    align-items: space-between;
}

.container .box-container {
    background-color: green;
    border: 1px #ccc solid;
    display: flex;
    flex-direction: row;
    width: 100%;
}

.container .box-container > div {
    background-color: white;
    flex: 0;
}

.container .box-container > div, .sidebar {
    border: 1px #ccc solid;
    padding: 10px;
}<br/>
.container .sidebar {
    background-color: blue;
    display: flex;
    flex: 5;
}
<div class="container">
    <div class="box-container">
        <div class="box-1">
            <h1>Box One</h1>
            <p>This is my first paragraph!</p>
        </div><br/>
        <div class="box-2">
            <h1>Box Two</h1>
            <p>This is my second paragraph!</p>
        </div>
    </div>
    <div class="sidebar">
        <h1>Box Three</h1>
        <p>This is my third paragraph!</p>
    </div>
</div>

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

Utilizing Angular and the Kendo UI framework to customize the dimensions of a Kendo window

Is there a way to dynamically set the height and width of the kendo window based on the content of the kendo grid? Below is the code snippet for my kendo-window: <div kendo-window="Operation.OperCustWind" k-width="800" k-height="700" ...

CSS: Specify `left:0` or `left:0px` to position

Is there a difference between using left:0; and left:0px;? From what I've noticed, both seem to work fine without any issues in Firefox's error console. Just curious if they have the same interpretation. I am working on some JavaScript that invo ...

Breaking the page with HTML and CSS

* { box-sizing: border-box; } .row { display: flex; } /* Creating two equal columns that sit side by side */ .column { flex: 50%; padding: 10px; } div.row { page-break-after: always; } <body> <div<div class="row"> ...

The CSS slider is stuck on the fifth slide and won't move past it

I encountered an issue with the CSS slider I am using, as it only had five radio buttons / slides in its demo. After attempting to add more slides, I noticed that the slider was not scrolling to the new slides. I'm unsure where I made a mistake in m ...

What is the best way to make a full width div within a container using bootstrap?

Imagine you have the following code snippet: <div class="container"> <div class="row"> <div class="col-md-12"> ... <div class="full-width-div"> </div> </div> & ...

The overflowing pop-up container

When I trigger a popup that dynamically generates a fixed background with a centered dialog, it works well. However, on smaller monitors like laptops, tablets, and phones, the content can overflow beyond the visible area due to its fixed container, making ...

Is there a way to modify the size and color of a specific word in a CSS animation?

As a newcomer to CSS, I am exploring ways to change the font size and color of a specific word in my sentence after an animation. My initial attempt was to enclose the word within a span class like this: <h2>I'm <span style = "font-size: ...

Looking to create a custom loader that adapts to different screen sizes

Can anyone help me make the text in my centered div responsive on my website loader? I'm new to coding and struggling to figure it out. Any assistance would be greatly appreciated! Make sure to view the full page snippet so you can see the text prope ...

Adding multiple styles using ngStyle is currently not possible

When using ngStyle to add height and width styles to an img element, I encountered a strange issue: <img class="image-full-view" [ngStyle]="{'height': selectedImage.heightSize, 'width': selectedImage.widthSize}" [src]="selectedImage ...

In HTML, the size and position of an <a> element cannot be altered or adjusted

I am having trouble with an anchor element that is contained within a div. No matter what I try, I cannot resize or move it up or down without it covering its sibling element. I have attempted using transform-translate and margin properties, but to no avai ...

Locate specific text within the content and apply an underline to it

Is there a way to locate specific text on my website and apply an underline to it? Suppose I have some text displayed and I wish to identify all instances of the word hello and underline them. Here is my attempt at the code: $( "body:contains('hell ...

I'm struggling to activate the eventListener on several elements with the same className or ID. Unfortunately, only the initial child is being triggered in my current code implementation

Hello fellow developers, I'm facing an issue while working on a project. I have about ten menu items with the same ID, and I want to be able to edit each one when it is clicked. Here's what I tried using JavaScript: const menuElement = d ...

Can a CSS Grid layout be achieved without prior knowledge of image sizes?

I am working on a project where I am pulling images from Reddit and aiming to showcase them in a gallery-style grid. I have tried using a flex display and resizing the images to match dimensions, but unfortunately not all images have the same dimensions ...

Using script tags incorrectly (JavaScript platform game)

Currently, I am working on the 'create a platform game' project as outlined in Eloquent JavaScript, and I have encountered an issue related to script tags. As per the instructions in the book, we are advised to showcase our level using: <lin ...

The header remains fixed while scrolling - troubleshooting the If Else statement

Check out this cool pen... http://codepen.io/jareko999/pen/bZXbWP The jQuery code adds a .fixed class to the #header when it reaches or goes below 0, but doesn't remove it when back above 0. I'm new to JS and struggling to understand what' ...

Exploring the position property in CSS

Seeking assistance as a CSS beginner. Currently working on a project where I have managed to position container_main right next to container_menu, utilizing the remaining screen space by assigning them relative and fixed positions, respectively. container ...

CSS 3 - Apply transition to the 'display' property

I have a question about using the transition effect in conjunction with the display property: Currently, I am testing on Safari. input.input_field { display:none; transition-property: display; transition-duration: 2s; -webkit-transition-p ...

Placing an image on the chosen option of a <mat-select> using Angular Material

I have incorporated Angular Material into my Angular 2 project, and I am trying to insert a static image (HTML element) in the selected value of mat-select. Unfortunately, I have been unable to find a solution for this issue. Is there anyone who can ...

The fixed left div and scrollable right div are experiencing issues with their alignment

I am having an issue with a section where the left div is fixed and the right div is scrollable. However, the content of the right div scrolls even before the scroll top reaches the section. My goal is for the right div to scroll only when it reaches the t ...

Display multiple videos next to and below one another in a responsive layout

I am attempting to display multiple Vimeo videos responsively alongside and below each other. I have noticed that when using the embed-responsive feature, the videos may not align perfectly if different videos are used. However, they align perfectly when t ...