center items after aligning floats

Currently, I am in the process of creating a website using a mobile-first approach with media queries. As I progressed to a larger device, specifically a tablet, I had to utilize the float property on my .textcontainer element to ensure it aligned properly on the right side of the image.

Although the float did help vertically center my content on the page, I faced difficulty in horizontally centering both the image and text content. I resorted to adding a margin-right of 8% to the .textcontainer to achieve a centered appearance. However, I am now contemplating if there exists a more responsive method to align all the content in the center of the page.

Furthermore, due to the use of float, I noticed that the .mailicon image no longer centers both vertically and horizontally at the bottom of the page. I am now seeking a way to bring it back to the middle position.

Below is a snippet of my HTML:

<div class="section section4">
        <img src="icons/ML-network.gif" alt="icon3">
        <div class="textcontainer">
        <h1>Ethereum</h1>
        <p class="ultralight">A turing complete platform with numerous <br> pre-optimized smart contract templates. Backed by the security of Ethereum.</p>
        </div>
        <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680b07061c090b1c2805090f010b0..."><img class="mailicon" src="icons/email-icon%20color.png" alt="mail icon"></a>
    </div>

Here is a glimpse of my CSS:

    main img {
        width: 30%;
    }
    .textcontainer {
    float: right;
    width: 50%;
    margin-right: 8%;  /* ADJUST BETTER... */  
    }
    main .section > * {
    position: relative;
    top: 50%;
    transform: translate(0, -50%);  
}
    main h1 {
    text-align: left;
    font-size: 1.8em; 
    margin-top: 0;    
    }
    main p {
    text-align: left;
    margin: 0;    
    }
    .section4 .mailicon {
    clear: both;
    width: 40px !important;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    }

Answer №1

To ensure alignment of the children element (specifically .textcontainer) with the text-align rules applied to the parent, you can utilize display:inline-block;.

I have incorporated an iPhone media query and adjusted the alignment of .textcontainer to be centered.

img {
  width: 30%;
  float:left;
}
.clearfix{
clear:both;
}
.section{
  text-align:right;
}
.textcontainer {
  width: 50%;
  display:inline-block;
  text-align:left;
}
main.section > * {
  position: relative;
  top: 50 %;
  transform: translate(0, -50%);
}
main h1 {
  text - align: left;
  font - size: 1.8em;
  margin - top: 0;
}
main p {
  text-align: left;
  margin: 0;
}
.section4.mailicon {
  clear: both;
  width: 40px!important;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) {
  .section{
    text-align:center;
  }
}
<div class="section section4">
  <img src="icons/ML-network.gif" alt="icon3">
  <div class="clearfix"></div>
  <div class="textcontainer">
    <h1>Ethereum</h1>
    <p class="ultralight">A turing complete platform with numerous
      <br>pre-optimized smart contract templates. Backed by the security of Ethereum.</p>
  </div>
  <div class="clearfix"></div>
  <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5e5253495c5e497d505c5a545e5158595a584f135e5250">[email protected]</a>">
    <img class="mailicon" src="icons/email-icon%20color.png" alt="mail icon">
  </a>
</div>

Answer №2

It appears that your code is not functioning correctly on my device when it comes to vertical alignment.

To address this issue, you can utilize jQuery with the following approach:

 var containerWidth = $(".section4").width(),
    imageWidth = $(".section4 > img").width(),
    textContainerWidth = $(".textcontainer").width();

$(".textcontainer").css("margin-right", (containerWidth - imageWidth - textContainerWidth) / 2);

As for achieving vertical alignment in the center, I typically employ the following CSS:

.someclass {
    display: table-cell;
    vertical-align: middle;
    width: 100px;
    height: 100px;
}

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 positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Implementing an unordered list in an inline format, even when it is collapsed with Bootstrap

Recently, I've been delving into Bootstrap and experimenting with coding a basic website. Below is the code for the navbar: <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="container"> <div class ...

Using the identical code, Wicked PDF generates distinct reports on separate computers

While utilizing Ruby on Rails to render a PDF using WickedPDF and sending it via email, I encountered an unexpected issue. The same code is present on two separate computers, both with up-to-date versions synced from GitHub. However, the generated PDF repo ...

Exploring ways to integrate Javascript and CSS into a Bootstrap lightbox modal dialog?

Can someone help me figure out how to use the bootstrap lightbox to display a form with specific CSS style and JavaScript code for the form? I'm having some trouble implementing it. I specifically need this setup because I am using Selectize.JS to cr ...

Div's background image displays flawlessly on Code Pen desktop, yet fails to appear when viewed on mobile devices

I am currently experiencing an issue with my background images not rendering properly on mobile devices, specifically safari and chrome on iPhone XR. Although they appear to work perfectly on Code Pen - resizing properly as the viewport shrinks and switchi ...

Activate hover event on UI-Grid for interactive display

I often utilize in the following manner: <div ncy-breadcrumb class="title-shadow"></div> {{ getDeviceTree }} <div> <div ui-grid="gridOptions" ui-grid-resize-columns ui-grid-selection></div> </div> My object ...

Deciding on the proper character formatting for each individual character within the RICHT TEXT EDITOR

After browsing numerous topics on Stackoverflow, I was able to develop my own compact rich text editor. However, one issue I encountered is that when the mouse cursor hovers over already bold or styled text, it's difficult for me to identify the styl ...

Troubleshooting: Issues with Integrating Javascript and CSS into

I'm still new to this platform, so please correct me if I make any mistakes. My goal is to create a "task table" that allows users to edit existing tasks and add new ones. Thanks to the base template provided by Ash Blue, I've managed to program ...

Animated CSS sidemenu (utilized as a filtering panel for a table)

Hi there, I'm having some trouble with CSS Animation. I recently started developing websites and am using Bootstrap 4 along with Animate.css for animations. My goal is to have an icon button expand sideways to reveal a div containing select elements f ...

Expanding list item with jQuery

I am facing an issue with the dropdown functionality of my <li> elements. The problem occurs when clicking on the 4th option, as the expander appears on top instead of dropping down beneath the list item. If you check out this jsFiddle, you can see ...

"Interactive bootstrap tabs nested within a dropdown menu that automatically close upon being clicked

Hey, I've got a dropdown with tabs inside. When I click on certain tabs within it, they close but the content inside the tabs that I click on changes, so it's functioning properly. However, the issue is that it closes when I want it to stay open ...

Ensure equal width for an HTML element by matching it with the dimensions

Hello, I am facing an issue with a dropdown button. Whenever I hover over it, the links to different pages drop down. However, I want the width of these links to be the same as that of the button. The size of the button varies as it is set to 100% width o ...

Designing stylesheets for larger screens to optimize the layout

I am currently working on the front-end design of a website and could use some assistance regarding element sizing. While the layout and elements look great on my 19-inch, 1440x900 resolution monitor, I noticed that everything appears tiny when viewed on a ...

Content within this specific div should move in a marquee fashion

#player #title{ left: 90px; width: 200px; overflow: hidden; } Within this specific div (#Title), the text should scroll like a marquee. The challenge is to achieve this effect using only JavaScript without altering the HTML structure. How can this be acco ...

The child elements are stacking vertically despite implementing inline-block

I have a container with 100% width. I am trying to position three divs inside it next to each other, all with the same height as the parent. Unfortunately, despite setting the display property to inline-block, they are not aligning properly and appear sta ...

How can the printing of content be adjusted when the browser zoom function is activated?

Is there a way to prevent the content from zooming when printing while the browser is zoomed in? The goal is for the printing (using iframe) to remain unchanged even if the browser is zoomed. I attempted: document.body.style.transformOrigin = 'top le ...

How to align text to the left and image to the right using CSS

I'm fairly new to CSS and struggling with a design issue. I want to display a series of stacked divs on my page, each containing text and one or more images. My goal is to have the text left-aligned and vertically centered, while the images are right ...

Tips for changing the state of a toggle button with JavaScript

I need help creating a toggle button. To see the code I'm working on, click here. In my JavaScript code, I am reading the value of a 'checkbox'. If the value is true, I add another div with a close button. When the close button is clicked, ...

The Tailwind preset is generating CSS code, but the webpage is not displaying the intended styles

Can anyone explain why the preset is generating CSS in the output file, but the styles are not displaying in the browser? When I manually write CSS in newstyle.css, it gets outputted to output.css and renders correctly in the browser. I attempted adding t ...

Scrolling to ID or Anchor using jQuery: Automatically scrolls to the top if already at the designated section

I've been on a quest to uncover the cause and solution for this issue. Lately, I've been utilizing $("#content").animate({scrollTop:$(#elementId).offset().top-183}, 600); to achieve smooth scrolling to an ID within a <div>. The number 18 ...