HTML - maintain centered text positioning while implementing padding on the left side

Here are the HTML and CSS code snippets I am working with:

#page {
  background-color: #000;
  color: #fff;
  height: 360px;
  width: 360px;
  min-height: 100%;
}

#viewport {
  font-size: 20pt;
  text-align: center;
}

.taskTitle {
  height: 13%;
  font-weight: bold;
  font-size: 30pt;
  white-space: nowrap;
  display: inline-block;
}

.taskNote {
  color: gray;
  white-space: nowrap;
}
<div id="page">
  <div id="viewport">
    <div id="task-view">

      <div style="width: 100%; background-color: aqua; position: relative;">
        <img style="background-color: blue; position: absolute; left :0%; top: 50%; transform: translate(-0%, -50%);" src="./image/tw_btn_radio_holo_dark.png"></img>
        <div style="width: 100%; background-color: red;">
          <div class="taskTitle" id="taskTitle1" style="background-color: green; width: 100%">Long text overlays picture :(</div>
          <div class="taskNote" id="taskNote1">My Note 1</div>
        </div>

      </div>


      <div style="width: 100%; background-color: orange; position: relative;">
        <img style="background-color: blue; position: absolute; left :0%; top: 50%; transform: translate(-0%, -50%);" src="./image/tw_btn_radio_holo_dark.png"></img>
        <div style="width: 100%; background-color: red;">
          <div style="background-color: green;" class="taskTitle" id="taskTitle2">Short</div>
          <div class="taskNote" id="taskNote2">Short text should always centerd in page!</div>
        </div>
      </div>
    </div>
  </div>
</div>

I have some requirements for the layout:

  • The picture should always be displayed on the left side of the screen
  • The picture should always be vertically centered in its parent div
  • The text (Title and Note) should always be centered on the screen
  • The text should not overlap with the picture

How can I achieve these layout specifications?

Answer №1

To avoid overlay, try setting z-index:100; for the picture and z-index:50; for the text.

For centering content, you can use <center></center> or

margin-left:auto; margin-right:auto;

If you want the picture to be on the left side, use left:0; or float:left;

I hope this advice is helpful!

Answer №2

If your text is overflowing the image, you can use the following CSS:

.taskTitle {
    overflow: hidden;
}

Apply the same to the taskNote class to adjust the font-size to fit its parent container.

To learn more about alignment and positioning in CSS, check out this informative article by Chris Coyier on CSS-Tricks.com.

If you have multiple questions, break them down into separate inquiries for easier troubleshooting. Best of luck!

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

"Exploring the interactivity of touch events on JavaScript

Hi there, I'm currently facing an issue with the touch events on my canvas. While the mouse events are functioning correctly and drawing as expected, incorporating touch events seems to be causing a problem. When I touch the canvas, the output remains ...

Leap over in a similar fashion to the provided demonstration

In my attempt to create a unique project, I have created this CodePen example. The goal is to have one ball on the circle that remains in a fixed position, while another rotating main ball must avoid touching it at all costs. Points are awarded for success ...

Navigate between paper cards with Polymer's side scrolling feature

I recently started using Polymer after a long break from HTML and have been relying on stack-overflow and Google's polymer guide for guidance. Currently, I am working on a product catalogue project and I have already started coding the paper-cards for ...

How to style HTML li elements to wrap like sentences within a paragraph

I am looking to format text list elements in a way that they wrap like sentences within a paragraph. Here is the accompanying HTML & CSS code snippet that demonstrates how li element behaves when it exceeds the width of the ul container. ul { ...

Share your hefty files through an online portal

What is the most effective method for allowing users to upload large files from their web browsers to a server? We are talking about file sizes of 200MB or even up to several gigabytes. I have brainstormed some potential solutions to this issue, although I ...

Master page does not recognize JQuery on all pages

Issue identified: The Request URL in the network tab is including an extra directory in the path on a page that is not functioning correctly due to the webform being nested within an additional directory in the solution structure. The home page does not ...

Rails: How come my personalized stylesheet isn't taking priority over the font family?

I have included the Skeleton boilerplate css in my application from this source. It can be found within the directory app/assets/stylesheets. app/assets/stylesheets ├── application.css ├── custom.scss ├── normalize.css └── skeleton ...

I'm struggling to incorporate the JQuery slideDown function into my code. Can someone lend a hand?

Why isn't the video div sliding down and displaying properly in the beginning? Any ideas on how to fix it? Here is the snippet of HTML code and JavaScript: <!DOCTYPE HTML> <html> <head> <title>Team Songs</title> <link ...

Mismatched Container Alignment in HTML and CSS

I am struggling to position the timeline next to the paragraph in the resume section, but it keeps appearing in the next section or below where it's supposed to be. I want the timeline to be on the right side and the heading/paragraph to be on the lef ...

Ways to improve the transition of a <video> background using CSS

My webpage features a unique 10-second video wallpaper achieved using only pure CSS. Below is the code I used: <style> video#bgvid { position: fixed; right: 0; bottom: 0; min-width: 100%; min-height: 100%; width: auto; he ...

Achieving a smooth sliding motion with the help of jQuery animation

Here is the code snippet: In HTML: <div> <div id="div1">12345</div> <div id="div2">67890</div> <div id="div3"><a href="#" id="slide">abcde</a></div> <div id="pad" style="display:non ...

I plan to compile a collection of names in a text field and then have the ability to select and access each name individually with just a click

I am currently working on a project that involves creating an admin site using Firebase. One of the main features of the site is large text fields that display information which can be modified. For instance, the user management page includes text fields ...

Would you like to learn how to dynamically alter a button's color upon clicking it and revert it back to its original color upon clicking it again?

My Upvote button starts off transparent, but when I click on it, the background color changes to green. What I need is for the button to become transparent again when clicked a second time. I've attempted using the following code snippet: function ...

Setting up CSS module class names in a Vue project using Vite js configuration

Within my vite.config.ts file, I have specified a configuration for CSS modules. return defineConfig({ ... css: { ... modules: { localsConvention: "camelCase", generateScopedName: "[name]__[local]__[hash:base64:2]" ...

What is the best way to trigger a javascript modal to open automatically after a specific duration

Let's take an instance where my modal has the ID #modal1. It usually appears through a button-based action. ...

The range slider's color value is not resetting correctly when using the <input>

Before repositioning, the slider is at this location: (both thumb and color are correctly positioned) https://i.stack.imgur.com/J3EGX.png Prior to Reset: Html <input id="xyzslider" class="mdl-slider mdl-js-slider" type="range" min="0.0" max="1.0" va ...

Welcome to the launch of OpenWeatherMap!

I just signed up for an account on the OpenWeatherMap website. My goal is to retrieve the current weather information for a specific location using the City ID API Call: http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=myAPIKey How ca ...

Slideshow through each item using Typescript and Angular8

I came across a solution in this carousel on by one link, but I'm struggling to work with the jQuery code even though I have JQuery installed in my project. For example: const next = jQuery(this).next(); I am looking to convert the JQuery code from ...

Personalize the material design datatable checkbox option

Recently, I delved into the world of material-ui design in conjunction with ReactJS. However, I have encountered a roadblock while attempting to customize the style of a checkbox that is being displayed under the <TableRow /> component. (Check out th ...

Utilizing a function within a span element

Can anyone help me figure out what I'm doing wrong while trying to toggle between a span and an input text field using the on function? If you want to take a look, I've created a fiddle for it here User Interface <div> <span >My va ...