What is the best way to horizontal center a flexbox within the browser viewport?

Looking to center a flexibox in the browser window? The code in the following jsfiddle works fine, but it doesn't center the flexibox. When the browser screen changes, the image and text adjust responsively.

Check out the Fiddle here

<!DOCTYPE html><html lang="en">   <head>
  <meta charset="UTF-8>
  <title></title>
  <style>
     #container {
     display: flex;
     /*width: 1400px;*/
     margin: auto;
     }
     #container .video {
     border: 5px solid red;
     width: 750px;
     max-width: 100%:
     }
     #container .desc {
     background-color: blue;
     max-width: 100%;
     }
  </style>   </head>   <body>
  <div id="container">
     <div class="video">
        <img src="https://futuranexa.files.wordpress.com/2012/09/funny_cow_egg-wallpaper-1920x1080.jpg"
           style="max-width: 100%">
     </div>
     <div class="learn">
        <p>
           isque perfecto dissentiet cum et, sit ut quot mandamus, ut vim    tibique splendide instructior.
        <p>
     </div>
  </div>   </body></html>

If I uncomment the /width: 1400px;/ line, the flexibox centers properly. However, the image and text will not adjust responsively when the browser screen changes. Any suggestions? Thanks!

Answer №1

After careful consideration, I believe I have grasped the essence of your query. Allow me to share the adjustments I made to your code:

#container {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    margin: auto;
}

#container .video {
    border: 5px solid red;
    flex-basis: 50%;
    max-width: 100%;
}

#container .desc {
    background-color: blue;
    max-width: 100%;
}
<div id="container">
    <div class="video">
        <img src="https://futuranexa.files.wordpress.com/2012/09/funny_cow_egg-wallpaper-1920x1080.jpg"
             style="max-width: 100%">
    </div>
    <div class="learn">
        <p>
            isque perfecto dissentiet cum et, sit ut quot mandamus, ut vim    tibique splendide instructior.
        <p>
    </div>
</div>

To ensure the text is centered beneath the image, it is advisable to utilize flex-wrap: wrap; and always opt for flex-basis: xyz%; over width: xyz%. I suggest giving these modifications a try and informing me if they align with your expectations.

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

Minimizing the amount of code written

I have the following CSS code: /* unvisited link */ a.underl:link { color: #000000; text-decoration: none; } /* visited link */ a.underl:visited { color: #000000; text-decoration: none; } /* mouse over link */ a.underl:hover { color: ...

How can I use querySelector in JavaScript to target all div elements that have the same class?

I'm having an issue with the document.querySelector in my JavaScript code. It currently only selects the first div that contains the class "test", but I need it to select all such divs. Is there a way to achieve this using my existing JavaScript? ...

Hover over the text below to see an image appear in its place

I'm looking to swap out some text with an image when hovering over it, using basic HTML/CSS. NO: show the image below the text NO: display the text on top of the image NO: replace one image with another YES: replace the text with an image ...

"Discover the versatility of implementing a single ag grid across various components, all while dynamically adjusting its style

I am facing an issue where I have an angular grid ag-grid in component1, which is being used in some other component2. Now, I need to use the same component1 in component3 but with a different class for ag-grid, specifically 'ag-grid-theme-dark'. ...

What causes the lower gap to be smaller than expected when using "top: 50%; translateY(-50%);" on specific elements within the parent container?

My top-bar layout needs to have all elements vertically centered, but I'm experiencing issues with the top-bar_right-part not behaving as expected when using the core_vertical-align class. The left menu works fine, however. To illustrate the problem, ...

Animating Angular ng-template on open/close state status

I am looking to add animation when the status of my ng-template changes, but I am unable to find any information about this component... This is the code in my report.component.html <ngb-accordion (click)="arrowRotation(i)" (panelChange)="isOpen($even ...

Show the textbox automatically when the checkbox is selected, otherwise keep the textbox hidden

Is it possible to display a textbox in javascript when a checkbox is already checked onLoad? And then hide the textbox if the checkbox is not checked onLoad? ...

"Utilize Tailwind to effortlessly place a single item at the center

Looking for a way to align a row of items with one item centered in the div and the others positioned on either side while maintaining their original order? Check out this code snippet: <div className="flex mx-auto justify-center items-center" ...

Removing gaps around rounded corners in jQuery UI accordions is a common issue that often arises when styling web elements

I am currently using jQuery UI Accordion to create collapsible sections that expand when clicked. In order to enhance the appearance, I have applied a background image to the header of each section like this: Check out my jQuery UI Accordion Demo here Up ...

How can one determine the most accurate box-shadow values?

I am trying to extract the precise box-shadow parameters from a CSS style rule generated by the server. My main focus is determining whether the element actually displays a visible shadow or not. There are instances where the shadow rule is set as somethi ...

Eliminate the preset padding on the left and right sides of the container-fluid in Bootstrap 5

How can I remove the default padding-left and padding-right in a container-fluid using Bootstrap 5? I don't want to use "!important" in my CSS file. I have already tried disabling the default padding with Chrome dev tools but it didn't work. Any ...

Several components utilizing a popup module

I have created a modal popup example where scrolling is disabled in the background but enabled within the pop-up itself. Check out my demo here: View Demo My challenge lies in implementing a grid of images on the website: Take a look at the type of grid ...

Could you please provide instructions on how to manipulate the :root CSS variable using JQuery?

Is there a way to update the :root css variable using jquery? :root { --color: #E72D2D; } .box-icon { background-color: var(--color); } I tried but it doesn't seem to work $("#blueColor").click(function(e) { $(":root").css("-- ...

Ever since implementing a new section class, the CSS first-of-type selector has ceased to function

After referencing a previous response, I utilized first-of-type to specifically target the initial menuSection with this code snippet... .menuSection:first-of-type { background: red; } <div id="container"> <section class="menuSection"> ...

Clicking on the icon image that features a Background Video will trigger the video to play in a popup window originating from the same location

A background video is currently playing on the site. When the play icon is clicked, the video should start playing in a popup. The video should start playing continuously from the exact moment and position where it is clicked. Check out the example here. ...

Toggle visibility of an element when clicked

I have recently started learning about jquery. In my code, I have two p tags with classes .angle_up and .angle_down respectively. The angle_up class initially has the active class assigned to it. When a user clicks on .angle_down, I want to remove the .ac ...

Is it allowed to modify <li> tag only when it contains a <strong> child element?

Is it possible to use SCSS to change the background color of a li element based on whether its child is a strong tag or not? li { margin-left: 3px; margin-bottom: 3px; border: solid transparent; background-color: #fff; border-top-left-radius: ...

The background color of the body is being utilized by Div in Internet Explorer version 7

Within my HTML document, I have created a container div that holds several nested divs for a slideshow effect. This "container" div is positioned over the background image of the body element. The CSS styling for the body element is as follows: body { ba ...

Is it possible to protect assets aside from JavaScript in Nodewebkit?

After coming across a post about securing the source code in a node-webkit desktop application on Stack Overflow, I began contemplating ways to safeguard my font files. Would using a snapshot approach, similar to the one mentioned in the post, be a viable ...

What are some creative ways to customize v-slot:cell templates?

Currently, I have the following code snippet within a table: <template v-slot:cell(checkbox)="row" style="border-left='5px dotted blue'"> <input type="checkbox" v-model="row.rowSelected" @input="toggleS ...