Tips for optimizing the positioning of HTML elements in a flexible and responsive manner

For the past two days, I have been struggling to responsively position some HTML elements. The effect I am aiming for is similar to what can be seen on this site before the stacking occurs in the Field Service Management and Intelligent Route Planning & Fleet Management sections:

Both of these sections consist of two parts - an image with a fixed pixel length that shrinks at different breakpoints, and on the other side, an h1, p, and button element that should stay in the same position as the image shrinks. I attempted to achieve this by using absolute positioning for the elements together, but encountered issues with margins pushing the elements down too far.

How can I ensure that the HTML elements (h1, p, and button) remain unaffected when the image size decreases based on media queries? Any help would be greatly appreciated.

The current code I am working with is:

.btn-primary {
    background-color: #de6426 !important;
    border-color: #de6426 !important;
    color: #fff;
  }
  
  .btn-primary:hover {
    background-color: hsla(20, 74%, 41%, 1) !important;
    border-color: hsla(20, 74%, 41%, 1) !important;
    color: #fff;
  }
  
  .btn-primary.focus, .btn-primary:focus {
     background-color: hsla(20, 74%, 41%, 1) !important;
     border-color: hsla(20, 74%, 41%, 1) !important;
     color: #fff;
  }

body {
    margin: 0;
}

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

  @media screen and (max-width: 792px) {
    .container {
        display: flex;
        flex-direction: column;
    }
  }

  .fleet {
      width: 400px;
  }

.items-wrapper {
    display: flex;
    background-color: #0f2c4d;
}

.items {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center; 
    text-align: center; 
    margin-left: 50px; 
    min-width: 390px;
    max-width: 500px;
}

  h1 {
      margin: 0;
      color: #008ad1;
  }

  p {
    color: white;
    margin-left: 6rem;
    margin-right: 6rem;
  }

  .btn {
      height: 30px;
      width: 80px;
  }

  h1 {
    font-family: Carnas-Light;
  }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="./css/bootstrap.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="./css/style.css">
    <title>Document</title>
</head>
<body>
        <div class="container">
                <div class="items-wrapper">
                        <div class="items"> 
                            <h1>Intelligent Route Planning & Fleet Management</h1>
                            <p>Improve efficiency and scalability of fleet operations through dynamic planning, intelligent route optimization and more.</p>
                            <button type="button" class="btn btn-primary">Button</button>
                        </div> 
                </div>
                    <img class="fleet" src="https://i.imgur.com/UakU07V.png" alt="Mountain View">
        </div> 
</body>
</html>

Answer №1

Hey there! It looks like you might be overcomplicating things with excessive HTML/CSS markup that isn't entirely necessary for this project. Have you considered why you're using both a flexbox grid model and Bootstrap simultaneously?

Let's simplify the code. In the provided HTML sample, streamline the markup and apply flex properties to the outer container to allow for customizable styling of text elements and images.

<div class="container">
    <div class="items-wrapper">
        <h1>Optimized Route Planning & Fleet Management</h1>
        <p>Enhance fleet operations efficiency and scalability through dynamic planning and intelligent route optimization.</p>
        <button type="button" class="btn btn-primary">Click Me</button>
    </div>
    <div class="img-wrapper">
        <img class="fleet" src="https://i.imgur.com/UakU07V.png" alt="Mountain View">
    </div>
</div>

Now, onto your CSS file.

/* base styles */

body {
    margin: 0;
}

h1 {
    margin: 0;
    color: #008ad1;
    font-family: Carnas-Light;
}

p {
    color: white;
    margin-left: 6rem;
    margin-right: 6rem;
}

.btn {
    height: 30px;
    width: 80px;
    margin-top: 20px;
}

.btn-primary:hover {
    background-color: hsla(20, 74%, 41%, 1) !important;
    border-color: hsla(20, 74%, 41%, 1) !important;
    color: #fff;
}

/* Flex Container */

.container {
    width: 100%;
    background-color: #0f2c4d;
    display: flex;
    flex-direction: row;
    flex-flow: nowrap;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.items-wrapper,
.img-wrapper {
    width: 50%;
}

.items-wrapper {
    padding: 10px;
}

@media only screen and (max-width: 600px) {

    .container {
        display: block;
    }

    .items-wrapper,
    .img-wrapper {
        width: 100%;
    }
}

Remember to customize the media query based on your preferred viewport size instead of just 600px. Feel free to experiment further!

Check out the JSBIN for a demonstration of this concept.

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

Tips for inserting a php variable into an html document

I am trying to figure out how to export a variable from a php file into an html file. The php file I'm working with is example.php and it contains the following code: <?php $array= ['one','two']; ?> The html file is named ...

Ways to expand logo space within the header of the Twentysixteen Theme on WordPress

I am facing an issue with my logo being resized to a much smaller size of 200px even though it is originally over 2000px wide. In the style.css file, I found the following code: .custom-logo { max-width: 180px; } Despite changing the max-width value t ...

How can I style the options and optgroups of a select dropdown with CSS, focusing on padding and margins?

In my experience, I have found that padding or margin CSS properties only seem to affect the appearance of options within the select menu in Firefox browser. I attempted the same styling in Internet Explorer and Chrome but it did not have any effect. Is ...

The use of DIV tags allows the element to be displayed in an inline

In my project, I decided to add three div tags. The first two are positioned next to each other with the third div tag placed below them. However, when I tried to insert a slideshow into the first div tag, all of the div tags ended up displaying as "block" ...

Adjust the CSS for the "a" tag's "title" attribute

I am using jquery.fancybox to create an image modal on my website. I have noticed that I can add a description using the title attribute. However, when the description is too long, the text is displayed on a single line and some of it disappears. How can ...

Using jQuery, how can I dynamically change the stacking order of an element when clicked?

I'm struggling to change the z-Index on button click. I have a static image and a dynamic div, and I want to send the #imgDiv1 behind the static image. Unfortunately, despite trying everything, I haven't been able to achieve this. You can check ...

Is there a way for me to manipulate the RGB values of the canvas in such a manner that the Red and Green components of the gradient are determined by dividing the position of my mouse cursor

My homework assignment requires using RGB colors where the red value is set to 0, green is the x-coordinate divided by 2, and blue is the y-coordinate divided by 2. I've been trying to control the colors on a canvas using addColorStop functions, but I ...

Step-by-Step Guide: Customize the Background Color in an Angular 4 Select Dropdown Based on the Selected Option

I'm facing a challenge in setting the background color of a select element based on the color attribute of its currently selected option. I'm using Angular 4 for this task. An example of what I am attempting to accomplish is shown below: <se ...

Stylesheet compilation with SCSS scripts

I am facing an issue with prefix:css. I am not very familiar with the scss build process, so if anyone knows what the problem is, please help me out. "watch:sass": "node-sass src/styles/global.scss src/assets/css/style.css -w", ...

Is it possible to eliminate jagged edges in CSS 2D rasterization by applying anti-aliasing to subpixels?

It appears that the HTML/CSS engine automatically rounds values to the nearest whole px unit. It would be interesting to see non-discrete sizing options (floats). A closer look reveals that in Chrome, widths/heights are rounded to the nearest physical pix ...

"Embracing the power of dynamic CSS customization within Umbraco

I recently inherited an Umbraco system without much prior knowledge of the platform, and I am currently investigating the site's performance. The branding and styling of the site is currently managed through a "Brand" document type, allowing the site ...

Choosing between radio buttons either horizontally or vertically within a table

Here is the markup I am working with: <table> <tr> <td><input type="radio" name="radio"></td> <td><input type="radio" name="radio"></td> <td><input type="radio" name="radio"></ ...

Tips for preserving state with history.replaceState

Recently, I experimented with the window.history.replaceState function in order to display a specific URL in the browser. window.history.replaceState("", "", "newurl.php"); Although the method successfully changed the URL, I found myself wondering about t ...

CSS Text ellipsis displays only the beginning of each paragraph

I want to add ellipsis to a text, but it keeps including the first line of all paragraphs. What I actually need is for only the first line of the content to have the ellipsis applied. Here is an example of the content: When using websocket to send message ...

Issue with jQuery Quicksand's CSS rendering in Internet Explorer and Firefox browsers, but not affecting Chrome

I'm attempting to achieve an icon-swapping effect using jQuery+quicksand. Although it works seamlessly in Chrome, I am encountering issues with IE and Firefox. Given that the more intricate quicksand demos function flawlessly across all browsers, I su ...

Poor quality picture captured with the use of the getUserMedia() Javascript function

Is there a way to improve the resolution of mobile phone camera screenshots taken with JavaScript's getUserMedia function? if (navigator.mediaDevices) { navigator.mediaDevices.getUserMedia({ video: { width: { min: 1280, }, heig ...

Issue found (in promise): Invalid Syntax detected at script.js line 8, character 42

I am in the process of developing a tool that retrieves ROBLOX asset IDs using this particular API. I am utilizing the product info function through an API GET request. Despite being near completion, I keep encountering the error Uncaught (in promise) Synt ...

When a td element is clicked, the Textbox will also be clicked

Similar Question: Dealing with jQuery on() when clicking a div but not its child $(oTrPlanning).prev().children('td').each(function () { this.onclick = setCountClick; }); When a TD element is clicked, the function setCountClick() i ...

What is the best way to send information to a different webpage?

I am in search of a solution to a rather simple query that has me puzzled. Should this question already exist elsewhere, I humbly request that you guide me to the answer. My apologies in advance if this is a duplicate. I currently have two URLs: http://12 ...

Adding options to an HTML select dropdown menu using data stored in an array

Having reviewed several questions related to this issue, I am still encountering difficulties. Below is the code snippet that I am struggling with: <body> <?php //To begin, there is a form with a drop-down menu and a button, which will trigge ...