What is the best way to align two elements side by side using flexbox?

After trying to find a solution on this site without success, I realized that my situation is quite unique. I need to avoid changing the HTML code, minimize the use of classes, and refrain from using IDs.

The current HTML structure I am working with includes:

html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}

body {
  background: linear-gradient(to top right, #dddddd, #f7f8ff, #ffffff);
  background-attachment: fixed;
  font-family: "faricy-new-web", sans-serif;
  text-align: center;
}

div {
  background-color: #A9B6C7;
  margin: 5% 10% 5% 35%;
  padding: 2%;
  display: block;
}


/*TITLE BOX STYLE*/

.title div {
  display: flex;
}

.title .c4title {
  font-size: 200%;
  font-weight: bold;
  display: block;
}

.title p .c6 {
  display: flex;
  justify-content: space-around;
  padding: 5%;
  margin: 5%;
}
<div class="title">
  <p class="c4title">
    <a name="h.c2v2rn37qt4d"></a><span>Title</span></p>
  <div>
    <p class="c1"><span></span></p>
    <p class="c6"><span>Info: <pre>Result</pre></span></p>
    <p class="c6"><span>Date: <pre>Wed Jun 21 2017</pre></span></p>
    <p class="c1"><span></span></p>
  </div>
</div>

Despite my efforts, I can't seem to achieve the desired alignment in the layout.

Therefore, my query remains – Is there a method to address these challenges while using flexboxes or do I have no choice but to modify the HTML?

Edit: Upon removing "display: flex" from the .title div, floats work perfectly. However, I'm still curious if it's possible to accomplish the same outcome using flexboxes.

Answer №1

Give this a shot!

html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}

body {
  background: linear-gradient(to top right, #dddddd, #f7f8ff, #ffffff);
  background-attachment: fixed;
  font-family: "faricy-new-web", sans-serif;
  text-align: center;
}

div {
  background-color: #A9B6C7;
  margin: 5% 10% 5% 35%;
  padding: 2%;
  display: block;
}


/*TITLE BOX STYLE*/

.title div {
  
}

.title .c4title {
  font-size: 200%;
  font-weight: bold;
  display: block;
}

.c6 {
  float:left;  
  margin: 0;
}

pre{
margin:0;
text-align:left;
}

.c1{float:left;}
<div class="title">
  <p class="c4title">
    <a name="h.c2v2rn37qt4d"></a><span>Title</span></p>
  <div>
    <p class="c1"><span></span></p>
    <p class="c6"><span>Info: <pre>Result</pre></span></p>
    <p class="c6"><span>Date: <pre>Wed Jun 21 2017</pre></span></p>
    <p class="c1"><span></span></p>
  </div>
</div>

Answer №2

To align your elements, try using float:left or float:right. Another option is to experiment with relative positioning and adjusting the offsets. For instance, you could position something 50 pixels to the left of its original placement.

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 the best way to insert a <dv> element following a <Typography> component in the provided code?

After learning React, I have attempted in various ways to get this code snippet to work. I referred to a question on Stackoverflow as well as the HTML div documentation The goal is to have the text and link displayed in a horizontal line, not vertically s ...

Is it possible to modify the flex direction in Bootstrap based on specific breakpoints?

I am currently utilizing Bootstrap 4 for my project. I have a div that is styled with "d-flex" and it is meant to display as a row at the "lg" breakpoint (screens 1200px and above). However, on smaller devices such as mobile phones at the "sm" or "xs" brea ...

Display the contents of a JSON array in an HTML format

I have designed an exercise that requires me to display the cities corresponding to each department based on a JSON file. Currently, I am able to display the departments in their respective selections, but I am struggling to only show the cities that corre ...

Strikethrough feature not specified on the website

Check out this unique website that showcases text with a strikethrough. Interestingly, I couldn't find any mention of it in the CSS or HTML code. Here is a snippet from the code: <div style="width:100%;height:259px;background-image: url('< ...

Value of an object passed as a parameter in a function

I am trying to use jQuery to change the color of a link, but I keep getting an error when trying to reference the object. Here is my HTML : <a onmouseover="loclink(this);return false;" href="locations.html" title="Locations" class="nav-link align_nav" ...

Showing a solo item from an array in HTML using Angular

How can I display the account name instead of the accountId property value from my list of transaction items? I have a list of transactionitems with an accountId property, and all accounts are loaded in the accounts$ array. However, I am having trouble us ...

Changing the background color of the legend text when hovering over a d3 doughnut chart

I have a doughnut chart that displays values on mouse hover. However, I would like to change the background of the legend text when hovering over the respective area of the doughnut chart. return { restrict: 'E', scope: { values: ...

Utilizing JavaScript, create a dynamic grid gallery with Div Spin and expand functionality

I am looking to create a unique effect where a div rotates onclick to reveal a grid gallery on the rear face. Starting this project feels overwhelming and I'm not sure where to begin. <ul class="container-fluid text-center row" id=" ...

Displaying divs inline with overlapping child divs

As a beginner in CSS, I tackle the challenges of learning it every day. Despite feeling clumsy and not naturally adept at it, my determination is strong (although sometimes my brain feels like an old computer on the verge of overheating). My current goal i ...

How about designing a button with a dual-layered border for a unique touch

Looking for a specific button: My attempted CSS code that's not working: button { font-family: 'Ubuntu', sans-serif; font-size: 1em; font-weight: bold; color: white; border: 3px double #f26700; background: #f26700; ...

The nightmare of Parent-Child Inheritance in JQuery/CSS is a constant struggle

Having difficulty implementing specific JQuery effects into a Bootstrap framework due to its extensive CSS causing issues. Created a test file named testjquery.html connected to a stylesheet defining a hidden element and activating the fade in of this ele ...

Refresh the Content of a Page Using AJAX by Forcing a Full Reload

I have a webpage that automatically updates a section using jQuery AJAX every 10 seconds. Whenever I modify the CSS or JavaScript of that page, I would like to include a script in the content fetched via AJAX to trigger a full page reload. The page is ac ...

Use either Jquery or CSS3 to hide a div if one of its inner divs is empty

I have multiple data sets to display in divs with the same class name. Each div contains two inner divs. <div class="div1"> <div class="div2">Abc</div> <div class="div3"><?php echo $row['SOME VALUE']; ?>< ...

Is there a method to generate an endless carousel effect?

Hello, I am trying to create an infinite carousel effect for my images. Currently, I have a method that involves restarting the image carousel when it reaches the end by using the code snippet progress = (progress <= 0) ? 100 : 0;. However, I don't ...

Ways to prevent decreasing the value below zero in ReactJS?

I have created two buttons, one for increasing and another for decreasing a counter value. However, when I click on the minus button, it should not display negative values. But in my case, when I click on the minus button (initially at zero), it shows -1, ...

The HR tag does not display anything following the mailing

Struggling to design a newsletter template with different lines for each product using the HR tag. However, none of my attempts with divs, tables, or CSS properties have been successful. The template looks fine in Chrome, but when sent to the provider, th ...

Different ways to update the AutoComplete Style attribute

MuiInput-formControl { margin-top: 16px; Is there a way to reset the marginTop property to zero? I attempted the following method, but it was not successful. MuiFormControlLabel: { marginTop: 0, }, <Autocomplete disabl ...

How to Keep Bootstrap 3 Accordion Open Even When Collapsed

I am in the process of building an accordion group with bootstrap 3. Here is the code I have so far: <div id="accordion" class="panel-group"> <div class="panel panel-default"> <div class="panel-heading"> <div class ...

Display a "Loading" image in the gallery before anything else loads

Can a animated loading gif be implemented to load before the gallery images, or would it serve no purpose? The image will be loaded as a background using CSS. <link rel="stylesheet" href="loading.gif" /> Appreciate your help! ...

Ensure that the image within the child div occupies the entire height of the parent div

I'm having a bit of trouble figuring this out and could use some input: There's a parent div with an unspecified height, determined by its content. Inside this parent div are two 'child' elements; however, I want only one to dictate t ...