Pictures are floating to the side instead of being centered in mobile mode

When in desktop mode, one img is aligned to the left and the other one is aligned to the right.

However, when viewed on a phone, I am attempting to center align them but they continue to be aligned to the sides.

What could be the issue here?

Provided below is the code I have been working with:

HTML:

<section>
    <header>
        <h3 class="right">Is your screen broken?</h3>
    </header>
    <p>
        <img class="image left" src="images/moviles/apple/ip4/iphone4.png"/ alt="Replace iPhone 4 Screen, DoctorSmart">
        Is the screen of your iPhone cracked or displaying strange marks? No worries. DoctorSmart will install a brand new one so you can enjoy your iPhone like new again.
    </p>
    <p>DoctorSmart uses top-quality spare parts and offers a 3-month warranty. You won't have to worry about your smartphone anymore</p>
    <p class="price">Get your iPhone 4 screen replaced for just €35, including pickup and delivery costs</p>
    <footer>
        <ul class="actions right">
            <li><a href="#" class="button">Fix It</a></li>

        </ul>
    </footer>
</section>

<section>
    <header>
        <h3 class="left">Is the back glass broken?</h3>
    </header>
    <p>
        <img class="image right" src="images/moviles/apple/ip4/iphone4back.png" alt="Replace iPhone 4 Back Glass, DoctoSmart"/>
        If the back of your precious iPhone 4 has scratches, cracks, don't worry. DoctorSmart can replace the back glass of your iPhone in no time, so you can show off your phone again.
    </p>
    <p>DoctorSmart uses top-quality spare parts and offers a 3-month warranty. You won't have to worry about your smartphone anymore</p>
    <p class="price">Replacing the back glass of your iPhone 4 is only €35, including pickup and delivery costs</p>
    <footer>
        <ul class="actions right">
            <li><a href="#" class="button">Fix It</a></li>       
        </ul>
    </footer>
</section>

DESKTOP CSS:

/* Image */

.image
{
    display: inline-block;
    outline: 0;
}

.image img
{
    display: block;
    width: 100%;
}

.image.centered
{
    display: block;
    margin: 0 0 2em 0;
}

.image.centered img
{
    margin: 0 auto;
    width: auto;
}

.image.featured
{
    display: block;
    width: 100%;
    margin: 0 0 2em 0;
}

.image.left
{
    float: left;
    margin: 2em 2em 2em 2em;
}

.image.right
{
    float: right;
    margin: 2em 2em 2em 2em;
}

PHONE CSS:

.image.left
{
    float: none;
    left: 0px;
    right: 0px;

}

.image.right
{
    float: none;
    margin: 2em 2em 2em 2em;
}

Answer №1

Instead of dividing your CSS into separate files, it is possible to target different viewports using media queries. Consider the following approach:

.image {
    display: inline-block;
    outline: 0;
}

.image img {
    display: block;
    width: 100%;
}

.image.centered {
    display: block;
    margin: 0 0 2em 0;
}

.image.centered img {
    margin: 0 auto;
    width: auto;
}

.image.featured {
    display: block;
    width: 100%;
    margin: 0 0 2em 0;
}

.image.left {
    @media (min-width: 768px) {
        float: left;
        margin: 2em 2em 2em 2em;
    }
}

.image.right {
    @media (min-width: 768px) {
        float: right;
        margin: 2em 2em 2em 2em;
    }
}

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

Web Security Vulnerability: Cross Site Scripting Detected

In our code, we are aiming to prevent XSS (Cross Site Scripting) attacks. However, the solution may involve a combination of JS (JavaScript) and HTML escaping, which could prove to be quite challenging. Below is a snippet that closely resembles our code: ...

When utilizing Blazor to create dynamic URLs within a loop, incorrect references may be generated

While working on a Blazor web assembly application, I encountered an issue when trying to loop over content to generate URLs for MP3 files. The media files are named "1.mp3", "2.mp3", "3.mp3", and so on. Imagine having a razor page with the following con ...

What is the process of importing a database into an application?

Could someone please provide a simple explanation of how this function operates? I have also included a snippet of PHP code that I believe is responsible for fetching data from the database. Please correct me if I'm mistaken. Thanks in advance! ...

Using a background image in CSS for horizontal rules can override other styling rules

Encountered an unusual CSS bug: After using background-image to style hr, none of the subsequent rules or selectors are displaying on the page: hr { border: 0; height: 1px; background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0 ...

Guide to making a single row beneath two columns using CSS Grid

My goal is to utilize CSS Grid in order to design a blurb layout featuring 2 columns at the top and a full row below. This will allow for an FA Icon and Title, accompanied by a full description below, similar to the image provided. I believe I have succes ...

Guidelines for aligning input buttons at the center of an HTML form

My form contains multiple input buttons, however, the inputs are not centered on the page. Currently, they are displayed in a single row within the div. I would like them to fill the page width with a 20px margin on each side and adjust to the browser scre ...

What is the fastest way to emulate CSS3 box radius for IE in terms of rendering speed?

There are numerous options available in JavaScript, .htc, and jQuery to create rounded corners for IE 6,7,8. (Not compatible with IE 8) http://code.google.com/p/jquerycurvycorners/ http://code.google.com/p/curvycorners/ Which solution offers the faste ...

Troubles with JavaScript loops

Hi, I'm having an issue with a loop in my code. The problem is that I need to insert HTML into JavaScript for a meme. The current code works fine but it uses document.write, which doesn't work on Firefox: for (var m = 1; m < 5; m++) { do ...

Using Bootstrap 3 to create a carousel with a seamless fading transition as the background

I want to utilize Bootstrap Carousel as the background for my website. I've successfully incorporated a standard carousel as the background, but I'm encountering difficulties with making fade transitions. Here is my current implementation: HTML: ...

Tips for stacking objects vertically in mobile or tablet view using HTML and CSS

I have been diligently working on a project using a fiddle, and everything appears to be running smoothly in desktop view. The functionality is such that upon clicking any two product items (with one remaining selected by default), a detailed description ...

The display:block links are not properly positioned within the absolute div

I'm having trouble figuring out why my links are not working for the .pushMenu divs on the left and right side, html: <header class="header"> <div class="pushMenu" id="left"> <a href="" title=""><p>l</p>< ...

I'm facing an issue where TailwindCSS fails to stretch my VueJS application to full screen width

My components are all contained within a div with classes flex-row, w-screen, and content-center. However, when I switch to reactive/mobile mode on the browser, it only takes up about 2/3 of the screen and doesn't fill up the remaining space. Below i ...

Template ID not found

I've been experimenting with integrating Knockout.js to merge an HTML view on a web page with HTML code stored in a database table. Although the database code is successfully appended to the DOM, Knockout seems unable to locate the specified ID. Here ...

Scroll through Two DIVs, where one is displayed and the other is concealed

Check out my JSFiddle here: https://jsfiddle.net/rq2tvccg/14/ I'm working on a project with 2 DIVs that each contain elements. Only one of the DIVs is displayed at a time, toggled by a button. I need to add an element that appears in both lists simul ...

JavaScript/CSS manipulation: The power of overriding animation-fill-mode

When animating text using CSS keyframes, I use animation-fill-mode: forwards to maintain the final look of the animation. For example: #my-text { opacity: 0; } .show-me { animation-name: show-me; animation-duration: 2s; animation-fill-mod ...

Incorporating HTML with ng-binds through transclusion

I have been developing an angular directive to enhance the functionality of ng-table by adding filtering and exporting features. The main goal is to make this directive reusable for multiple tables, which requires the <td> elements to be dynamic. To ...

Stuck on using floats and fixed positioning in CSS, can't seem to crack the code

I am working on a mobile website project and I am in need of a fixed navigation bar that has a specific look... Unfortunately, my attempts at using fixed positioning to achieve this have not been successful. Instead of getting the desired result, all the ...

Is it possible to display a pop-up caption in the mobile view?

When I added a caption to my image, it only appeared when hovering over the symbol in the image. However, this feature doesn't work on mobile or tablet due to the image size. How can I make the caption appear when the image is clicked on mobile/tablet ...

The incessant ascent of the div to the page's peak leaves me perplexed as to the cause

While trying to set up my website template from , I noticed that the main div, which includes circles and a call to action at the bottom, keeps aligning with the top of the page and under the navigation bar. Any ideas on what could be causing this issue? ...

Allow the entire list section to be interactive with a click

Is it possible to make the entire list area clickable rather than just where the inner link sits on my website? For example, I want users to be able to click anywhere within the list instead of only on the specific link itself. One such instance is with th ...