Center elements vertically within a block

Struggling to vertically align elements within a div, especially circles. The red div is set to 100% width with my code, but the issue lies in the vertical alignment and positioning of the circles (essentially divs or spans with background images).

https://i.sstatic.net/lrArg.jpg

Here's my code:

.welcomeArea{
    margin-top: 70px;
    max-height: 98px;
    height: 98px;
    background-color: #293847;

}

.welcomeAreaContent{
        line-height: 98px;
        color: white;
        margin-left: 5%;
    margin-right: 5%;

}
.welcomeAreaContent > span {
    display:inline-block;
}
.welcomeAreaContent .welcomeName{
    font-weight: bold;
    font-size: 1.7em;
}
.verticalLine {
  border-left: thick solid #ff0000;
  content: "";
}


.circle { 
   width: 50px;
   height: 50px;
      border-radius: 50%;
    behavior: url(PIE.htc); 
   -moz-border-radius: 50px; 
   -webkit-border-radius: 50px; 
   border-radius: 50px;
   margin-left: 32px;
   display: inline-block;
}
.twittericon{
    background: url('data:image/png;base64,...') no-repeat center;
    background-color: white;
     background-size: cover;   

}
<div class="welcomeArea">
<div class="welcomeAreaContent">
       <div class="welcomeName">
         TEXT TEXT
         <span class ="circle twittericon"></span>
       </div>
        <div class="verticalLine">
</div>
     </div>
  </div>

The end result looks like this: https://i.sstatic.net/vBVrx.png

Answer №1

employ display:flex

.welcomeAreaContent .welcomeName{
  font-weight: bold;
  font-size: 1.7em;
  display: flex;
  align-items: center;
}

.welcomeArea{
    margin-top: 70px;
    max-height: 98px;
    height: 98px;
    background-color: #293847;

}

.welcomeAreaContent{
        line-height: 98px;
        color: white;
        margin-left: 5%;
    margin-right: 5%;

}
.welcomeAreaContent > span {
    display:inline-block;
}
.welcomeAreaContent .welcomeName{
    font-weight: bold;
    font-size: 1.7em;
display: flex;
    align-items: center;
}
.verticalLine {
  border-left: thick solid #ff0000;
  content: "";
}


.circle { 
   width: 50px;
   height: 50px;
      border-radius: 50%;
    behavior: url(PIE.htc); 
   -moz-border-radius: 50px; 
   -webkit-border-radius: 50px; 
   border-radius: 50px;
   margin-left: 32px;
   display: inline-block;
}
.twittericon{
    background: url('data:image/png;base64,...') no-repeat center;
    background-color: white;
     background-size: cover;   

}
<div class="welcomeArea">
<div class="welcomeAreaContent">
<div class="welcomeName">
TEXT TEXT
<span class ="circle twittericon"></span>
</div>
<div class="verticalLine">
</div>
</div>
</div>

View the working Fiddle

Answer №2

Make the following adjustments in the CSS code below by utilizing flex for inline and vertical center alignment

.welcomeAreaContent .welcomeName {
  font-weight: bold;
  font-size: 1.7em;
  display: flex; /*Include this line*/      
  align-items: center; /*Include this line*/
}

.welcomeArea {
  margin-top: 70px;
  max-height: 98px;
  height: 98px;
  background-color: #293847;
}

.welcomeAreaContent {
  line-height: 98px;
  color: white;
  margin-left: 5%;
  margin-right: 5%;
}

.welcomeAreaContent>span {
  display: inline-block;
}

.welcomeAreaContent .welcomeName {
  font-weight: bold;
  font-size: 1.7em;
  display: flex;
  align-items: center;
}

.verticalLine {
  border-left: thick solid #ff0000;
  content: "";
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  behavior: url(PIE.htc);
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  margin-left: 32px;
  display: inline-block;
}

.twittericon {
  background: url('data:image/png;base64,...') no-repeat center;
  background-color: white;
  background-size: cover;
}
<div class="welcomeArea">
  <div class="welcomeAreaContent">
    <div class="welcomeName">
      TEXT TEXT
      <span class="circle twittericon"></span>
    </div>
    <div class="verticalLine">
    </div>
  </div>
</div>

Answer №3

There are various methods you can utilize

  1. Applying margin-top to the current div

  2. Using padding-top on the parent div

  3. Employing position:relative; top:__px; on the current div

  4. Check out https://www.w3schools.com/css/css_align.asp

I trust this information will be beneficial

Answer №4

Experiment with:

.circle
{ 
    align-content: center;
}

Answer №5

To vertically align inline elements, you can utilize the vertical-align property.

.twittericon {
background: url(data:image/png;base64,...) no-repeat center;
background-color: white;
background-size: cover;
vertical-align: middle;
}

If the elements are block-level, flexbox can be used for alignment.

<ul class="parent">
<li>2</li>
<li>1</li>
</ul>
<style>
.parent { display: flex; align-items: center; }
</style>

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

Ensure that the images maintain a horizontal flow while keeping any overflow hidden

Within a containing DIV, I have a collection of images. The DIV is absolutely positioned to the right of its container. My goal is for the images to flow horizontally when they exceed the width of the parent container, but remain hidden so that I can scrol ...

timings and pauses utilizing JavaScript

Currently, I am facing a challenge while working on a Simon Says game. My struggle lies in the part where I need to illuminate the buttons that the user has to click. I am using a "for" loop to iterate through each element in the array where I have stored ...

"Is it possible to apply an underline decoration to a link without affecting the ::before pseudo

Is there a way to make the link text have an underline but not the Font Awesome icon (::before)? Setting ::before text-decoration:none; doesn't seem to work .btn--tertiary { font-size: 16px; line-height: 24px; padding-left: 0; color ...

Navigating through AngularJS routes leads to an unexpected directory being added

Assigned to an AngularJS project after the departure of a team member, I found myself navigating unfamiliar territory as a Python/Java developer. Despite its outdated AngularJS version 1.0.8, I aim to modernize the system once it's stable. An issue h ...

A step-by-step guide for embedding an image on an HTML webpage with Flask

In the process of developing a web application, I aim to embed a compact image within an HTML table and enable its movement across different table cells. The technologies involved in my project include Python, JavaScript, HTML, and CSS. The concept is ill ...

In Groovy (or Java), learn the techniques to properly escape double quotes within HTML inner text while leaving attributes untouched

I am currently utilizing an HTML rendering engine that is based on Groovy within a Web Content Management (WCM) system. I have encountered a scenario where the user inputs rich text content into a form based on TinyMCE, which appears as follows: <p> ...

Ways to eliminate the leading bullet point from an unordered list

I am currently working on creating a gallery of images using php and css. The images are placed in an unordered list as shown below: <ul style="list-style-type:none;"> <? while($data=mysql_fetch_row($result)) { $pic=$data[2]; if ($pic) { $ ...

Issues have arisen with the @keydown.ctrl and @keyup.ctrl event modifiers in Vue.js, as they are not properly responding

I have a project in VueJS where I need to implement a custom context menu that will pop up when the user hovers over specific elements on the page. This context menu is dynamic, meaning it changes as the user moves between different items. If the user hold ...

Extend the width of a div element to fit the size of its absolutely positioned

I am facing an issue with a Div that contains multiple absolutely positioned divs. The clickable area of the main div expands to cover the children, but the drawn area does not. I need the drawn area to encompass all the container divs. You can view a JSF ...

Spacing Problem with Title Tooltips

After using the padEnd method to ensure equal spacing for the string and binding in the title, I noticed that the console displayed the string perfectly aligned with spaces, but the binded title appeared different. Is it possible for the title to support s ...

encountering a problem with loading an image

I'm experiencing a strange issue when trying to load a sprite image and encountering an error message: "Failed to load resource: the server responded with a status of 404 (Not Found)" In my CSS, I have it loaded like this: background-image: url(&ap ...

Having difficulty locating the identification number of an item within the HTML coding

I'm currently trying to locate the ID of the "proceed to checkout" button for a bot that I am developing for my mother on the Best Buy website. However, it seems like the button's ID is hidden and I'm unable to determine how to uncover it. A ...

Pass data from the view to the controller in CodeIgniter using an array

Take a look at: user_data received from the controller (Database middleware values) <?php $ID['IDS'] = array_column($user_data, 'ID'); print_r($ID); // print_r($ID)=Array( [IDS] => Array([0] => 0 [1] => ABC ...

The second pop-up modal fails to appear

I have successfully implemented 2 modal windows with the help of bootstrap. The first modal is used for adding a user to the database, and the second one is meant for editing an existing user. While the first modal works perfectly fine, the editing modal d ...

The video player in HTML may not be compatible with all MP4 files, as some may work while

I have included the following code for embedding mp4 videos on my website: <video class="video" controls="" width="100%" src="videopath/videoname.mp4"></video> Despite using this code for 3 videos, all in mp4 format, only one of them is funct ...

Once submitted, send the $forum_id

I have developed a script and I am looking to replace posting.php with it on phpbb3. The script is a form with action="application". After clicking on "new topic," I get redirected to submit.php?mode=post&f=3 instead of posting.php?mode=post&f=3. ...

What is the best way to display table rows for a specified date range?

I am working with a table and need to display only the rows that fall between two specific dates. <table id ="Table"> <thead> <tr> <th>Date</th> <th>Name</th> <th>Desc</th> </tr> </thead> ...

Chose to conceal all child elements, but is unable to reveal all elements when clicked

Currently, I am in the process of creating a jQuery accordion by following the tutorial provided on css-tricks at this link. After making some modifications to the code, I was able to hide all the elements within the parent element. However, upon clicking, ...

Unable to display texture and color on .obj files in Three.js

After introducing a new model in .obj and .mtl formats into my three.js code, I encountered an issue where the color of the model would turn white, regardless of its original color or texture. Below is a snippet of the code related to the pig model: // ...

Hover and hover-off functions in navigation menu

html <div class="hidden-nav"> <h4>lorem</h4> <ul class="decor-menu-list"> <li><a href="#">123</a></li> <li><a href="#">123</a></li> <li><a hre ...