Tips for aligning vertical text perfectly

After spending the entire day researching vertical text, I am still struggling to find a simple solution. I need three items in my header to line up horizontally: an image on the left, a specific-sized box in the center, and another image on the right. Here is the code I have tried:

    #maincontainer{
    width: 1020px; /*Width of main container*/
    margin: 0 auto; /*Center container on page*/
    }

    #header{
    height: 150px; /*Height of top section*/
    float: left;
    }

    #box{
    width: 20px;
    height: 150px;
    border: 1px solid red;
    }

    .verticaltext{
        font-size: 0.600em;
    text-align: center;    
        border: 1px solid red;
        transform: rotate(-90deg) translate(-100%, 0);
        transform-origin: 0 0;
    }
<body>
<div id="maincontainer">
<div id="header">
<div>
<img src="image/left_pic.jpg" alt="left_pic" height=150 width=250>
<div id="box">
<p class="verticaltext">vertical text</p>
    </div>
    <img src="image/right_picture.jpg"     alt="right_pic" height=150 width=748>
    </div>
        </div>
        </div>
    </body>

Despite following this structure, my items are not aligning properly within the header container. They are all over the place, with the second picture being adjusted to account for the border. It's frustrating how such a seemingly simple task can be so challenging to execute. Can anyone help me figure this out?

Answer №1

    #maincontainer{

    width: 1020px; /*Width of main container*/
    height: 700px;
    margin: 0 auto; /*Center container on page*/
    }

    #header{
    width: 1000px;
    min-height: 150px; /*Height of top section*/
    float: left;
    }

    .box{
    width: 250px;
    height: 150px;
    border: 1px solid red;
        float:left;
        margin-left:10x;
    }

    .verticaltext{
        font-size: 16px;
        float:left;
        margin-left: 10px;
        margin-right: 10px;
        vertical-align:middle;
        line-height:150px;
        transform:rotate(270deg);
    }
<body>
<div id="maincontainer">
<div id="header">
<div class="box">
   <img src="image/left_pic.jpg" alt="left_pic" height="150px" width="250px" />
</div>
<div class="verticaltext">
Vertical text
    </div>
    <div class="box">
                   <img src="image/right_picture.jpg" alt="right_pic" height="150px" width="748px">
    </div>
        </div>
        </div>
    </body>

Several errors were found in your code, I have made the necessary corrections. Please review and let me know if this is what you intended. If you need further explanation, feel free to ask.

Answer №2

This is an example of how your CSS code might look:

    #navbar img.img-left {
    position: absolute;
    float: left;
    top: 0;
}

#container p {
    position: absolute;
    left: 25%;
}


#navbar img.img-right {
    position: absolute;
    right: 0;
    top: 0;
}

Answer №3

Have you thought about utilizing flexbox? It appears to be a straightforward solution for your current needs.

.header {
    display:flex;
    align-items: center;
 }

I haven't tested this yet, but essentially all the content within the header should be vertically centered. For more information on flexbox, I recommend checking out this resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Answer №4

After experiencing some initial frustration, I realized that solving this issue was actually quite straightforward. By taking a moment to calm down and think things through, I was able to successfully align the elements in my HTML code. For anyone facing a similar problem, here is what worked for me:

I floated all the elements to the left in the order they appear in the HTML structure, which was the simplest scenario. To ensure proper alignment, I added position:relative to the header container since my page is centered on the viewport. In dealing with the vertical text, I made adjustments to the CSS properties of the .verticaltext class and #verticaltext element, specifically swapping the height and width values. It's important to note that both dimensions are necessary, and setting the line-height equal to the width helps center the text. Additionally, using white-space:nowrap is crucial to prevent text from wrapping (make sure your text fits within the available space).

Below is the updated HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Homepage</title>
    <meta charset="UTF-8">
    <link href="css/mycss.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="maincontainer">
        <div id="header">               
            <div>
                <img src="image/left_pic.jpg" alt="left_pic" height=150 width=260>
            </div>
            <div id="verticaltext">                 
                <p class="verticaltext">vertical text</p>
            </div>              
            <div>
                <img src="image/right_pic.jpg" alt="right_pic" height=150 width=740>
            </div>
        </div>
    </div>
</body>
<html>

And here is the revised CSS:

#maincontainer{
width: 1020px; 
margin: 0 auto; /*Center container on page*/
}

#header{
    position: relative;
    height: 150px; 
    width: 100%;
}

#header >div{
    float: left;
}

#verticaltext{
    width: 20px;
    height: 150px;
}

.verticaltext{
    font-size: 0.600em;
    line-height: 20px;
    padding: 0;
    margin: 0;
    height: 20px;
    width: 150px;
    white-space: nowrap;
    text-align: center;    
    transform: rotate(-90deg) translate(-100%, 0);
    transform-origin: 0 0;
}

Thanks to everyone who took the time to review this and offer suggestions. Your input was truly valuable. - E L

Answer №5

From your description, it seems you are looking to align your header items horizontally in the center.

To achieve this, you can utilize the properties display:flex and margin:0 auto on the main container.

Here is a snippet for reference:

#maincontainer {
  width: 1020px;
  margin: 0 auto; /*Width of main container*/
  /*Center container on page*/
}
#header {
  display: flex;
  height: 150px; /*Height of top section*/
}
#box {
  width: 20px;
  height: 150px;
  border: 1px solid red;
}
.verticaltext {
  font-size: 0.600em;
  text-align: center;
  border: 1px solid red;
  transform: rotate(-90deg) translate(-100%, 0);
  transform-origin: 0 0;
}
<body>
  <div id="maincontainer">
    <div id="header">

      <img src="image/left_pic.jpg" alt="left_pic" height=150 width=250>
      <div id="box">
        <p class="verticaltext">vertical text</p>
      </div>
      <img src="image/right_picture.jpg" alt="right_pic" height=150 width=748>

    </div>
  </div>
</body>

I hope this solution proves helpful!

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

Discover the steps to incorporate a glowing effect into your custom progress bar using JavaFX

After successfully creating a custom progress Bar in JavaFX, my next step is to incorporate a glow effect into the progressBar. To achieve this, I have constructed an ellipse with a Gaussian blur effect and integrated the centerX of the ellipse into a tim ...

Hovering over an element and eliminating any extra space

Hey there, I'm looking for some assistance with removing the whitespace on hover. Below is the code along with a screenshot for reference: div.show_sizes_onhover { background: rgba(255,255,255,.9); bottom: -1px; color: #a1a1a1; left: 0; padding: 5px ...

Is it possible to programmatically bind a click event to each individual word in a div element?

I'm attempting to link each word within an element without altering the markup inside the element using Javascript. ...

Expanding divs in CSS for multiline versus single line text content

I am attempting to display text within a bordered div that is positioned absolutely inside another div. Option 1 using width:auto; - view fiddle: https://jsfiddle.net/c21kt6r4/ The issue here is that the box on the left side expands excessively. https:// ...

Difficulty maintaining hover state on menu while navigating to submenu on an HTML page

I successfully implemented a side menu that displays the submenu when hovering over menu items. However, I am encountering two issues: 1. When I hover over a menu item and the submenu appears, the hover state of the menu item disappears. I would like the m ...

In React, the entire component refreshes every time the modal is opened

<ThemeProvider theme={theme}> <GlobalStyle /> {componentName !== 'questionaire' && componentName !== 'activityResult' && <CardWrapper />} <ErrorModal ...

Customize button appearance within mat-menu in Angular versions 2 and above

Is there a way to style my mat-menu to function similar to a modal dialog? I am having difficulty with the styling aspect and need advice on how to move the save and reset buttons to the right while creating space between them. I have attempted to apply st ...

EaseSlide 'Basic Slideshow' created by Ameenullah Fails to Function

I'm trying to embed a carousel into my webpage. I copied the code from Bootsnips but have been unable to solve this issue with other solutions. This is my HTML Code: <div class="container"> <div class="row> <div class="col ...

Switch up your text display using JQuery and toggle between different texts

I have implemented a jQuery script to toggle the display of certain paragraphs when the "More" link is clicked. However, I am facing an issue where the link always displays "More" even after the content has been expanded. I want it to change to "Less" once ...

The dominance of CSS over other attributes in styling

Imagine having a main body text div and a navigation div for a website. What if you want to make links in the body text green instead of blue, but have the links in the navigation div be red? If you use CSS to make links green, then all links turn green. ...

How can you achieve three layers of nested quotes in Dynamic HTML?

Working on an app that utilizes JQuery and JQTouch for iOS. The issue I'm facing involves dynamically generated HTML lists where the user clicks a row that needs to be highlighted. However, achieving this has proven tricky due to nesting 3 sets of quo ...

Using data variables as arguments in the style section of Vue2

Suppose we have a data variable and I want to utilize this data variable for styling purposes. data() { return{ selected:{ index: 2 } } } <style> .parent-table >>> .table-row:nth-child(/* here I intend to use ...

Should I create CSS rules for different screen sizes or should I dynamically render components based on the screen size?

As I delve into learning React, I find myself unsure about the most effective approach to make websites responsive. Currently, I am utilizing Semantic UI React as my CSS Library. While everything displays perfectly on desktop, I encounter some issues when ...

Issue with a Bootstrap panel displaying incorrect border formatting

I am currently working with Angular UI Bootstrap and facing an issue with an accordion component. Everything works perfectly fine in the development environment with the full version of the Bootstrap Cerulean file. However, when I minify the CSS file for p ...

beforeprinting() and afterprinting() function counterparts for non-IE browsers

Is there a way to send information back to my database when a user prints a specific webpage, without relying on browser-specific functions like onbeforeprint() and onafterprint()? I'm open to using any combination of technologies (PHP, MySQL, JavaScr ...

Creating Header Images for Websites, like the way Facebook does

Exploring how to properly configure the header image on my website has been intriguing. The main issue is that when I view it on my laptop, which has a width resolution of around 1280px, the header image's width measures about 2000px. My goal is to h ...

The HTML file generated from a Quarto document (.qmd) seems to be experiencing compatibility issues for users other than myself

As a beginner in R, I have encountered an issue when trying to share my R Quarto document with others who are not familiar with R. I prefer to share it as an html file, but the appearance of the document changes significantly when opened on another person& ...

Experiencing issues with content rendering in a Cordova iOS app when utilizing the CSS property -webkit-overflow-scrolling: touch

Encountering a rendering problem in a Cordova iOS app with the CSS property -webkit-overflow-scrolling: touch. I have already included this property in the config.xml file. ...

I'm having trouble getting my Bootstrap Navbar list aligned in the center

I've been attempting to align my menu list items in the center of the bootstrap navbar but have had no success. Despite trying various CSS styles, the items remain on the left side. Please take a look at my HTML and CSS below and let me know if there ...

How can we stop every tab pane in (bootstrap five) from sharing the same scroll position? Each tab is currently synchronized with the scroll position of the others

In Bootstrap 5, the scroll position remains the same when switching between tabs in the tab pane. Whether moving from tab #1 to tab #2, both tabs are at the identical scroll position, failing to preserve each tab's specific location. <div class=&qu ...