Tips for incorporating a logo and company name into the header of a website

How can I code a logo and company name in the header?

This HTML file provided contains only a header at the top, with a horizontal layout. You can view it here: https://jsfiddle.net/SSteven/yeamz57o/

body {
  /* resetting browser defaults */
  margin: 0px;
  padding: 0px;
}

.clearfix {
  overflow: auto;
  border: 1px solid red;
  background-color: lightgray;
}

.center {
  padding: 10px;
  text-align: center;
}

.logo {
  position: absolute;
  left: 0px;
  top: 0px;
  border: 1px solid blue;
  z-index: 1;
}
<div class="clearfix center">
  <img class="logo" src="Logo_MarksC.png" alt="Logo" width="278" height="100">

  <h1>MY COMPANY</h1>
</div>

The header includes:
1) The Company logo on the left.
2) The Company name centered horizontally and vertically within the layout.

Current issues to address:
1) Difficulty in achieving perfect vertical centering of the logo within the layout, necessitating manual padding adjustments. What methods can be implemented for precise vertical alignment of the logo?

2) Utilizing z-index to overlap the logo and Company name ensures perfect horizontal centering of the Company name across the webpage width. However, as the screen size decreases horizontally, the Company name ends up behind the logo due to the overlapping. If the overlap is removed, the Company name shifts off-center when the screen reduces in width, although it prevents the Company name from hiding behind the logo. In this context, I would like to inquire: For a header with a logo on the left and a centered Company name, what represents standard / best practice?

Answer №1

To ensure proper alignment of content, it is advisable to steer clear of absolute positioning and opt for using flexbox instead.

A clever trick is to incorporate a spacer element to maintain balance between the two sides, resulting in a centered heading.

In cases of narrow displays, implementing an @media rule becomes essential to cater to layout adjustments. A distorted heading can be just as detrimental as one that overlaps. (Please note that in this scenario, owing to the limited frame provided by Stackoverflow, clicking on the Full Screen link might be necessary to avoid the media query shift to a vertical layout for narrower windows)

header {
  text-align: center;
}

.spacer {
  display: none
}

@media screen and (min-width: 700px) {
  header {
    display: flex;
  }
  .logo,
  .spacer {
    display: block;
    width: 278px;
    flex: 0 0 auto;
  }
  .heading {
    flex: 1 1 auto;
  }
}
<header>
  <div class="logo">
    <img src="https://placekitten.com/278/100" alt="">
    <!-- alt is blank because the information conveyed in the image is duplicated by the heading -->
  </div>
  <div class="heading">
    <h1>Kitten Co</h1>
  </div>
  <div class="spacer">
    <!-- This exists just to use up space and so the heading is centred -->
  </div>
  </div>

Answer №2

To achieve flawless centering, consider utilizing CSS flexbox. This method is widely supported by major browsers (although IE may require a small workaround), has become a CSS standard, and eliminates the frustrations often associated with aligning elements.

.my_center {
    display: flex;
}
.my_center--items {
    justify-content: center;
    align-items: center;
}

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

Sort out the DIVs containing card elements

Is there a way to filter DIVs containing cards based on their categories? I seem to be encountering an issue where the filtering only works with letters. I suspect the problem lies in the Javascript code, but I can't pinpoint it. Can you help me ident ...

What steps do I need to take to develop a feature similar to Tabzilla?

Exploring the innovative navigation bar on Mozilla.org seems intriguing; commonly referred to as tabzilla, it smoothly moves down to reveal the menu at the top of the page. I wonder if there are any existing libraries or ready-made jQuery code snippets tha ...

Discovering the tiniest value in every row within an HTML table with the help of PHP

I am working with a table that looks like this: 6xx 8xx 9xx 11xx 12xx 1 0.01 0.002 0.004 0.001 0.025 2 0.025 0.125 0.002 0.01 0.011 I need to highlight the smallest value in each row by making that cel ...

Uniqid in React fails to generate unique IDs

Currently working on creating my first react project and developing a todo list. I have incorporated the uniqid generator into my todo object, but it seems to be returning an empty id rather than a random one. Oddly enough, if I move the todo state outsi ...

combine multiple select options values in a single function using jQuery

My HTML code includes two select options for users to choose the origin and destination cities. I need to calculate the cost of travel between these cities. How can I compare the selected options using jQuery? </head> <body> <div> ...

Enhance Vaadin with custom validation and content mode options

Validation is needed for certain fields. The captions of the fields contain various formatting elements such as ", ", etc, with the option captionAsHtml="true". However, when using someField.addValidator(..., errorString), the error message displays both ...

How can I avoid columns from breaking in Bootstrap?

<div id="mydiv" class="bg-danger row"> <div> <img src="https://www.discoservicemusicheria.com/shop/wp-content/uploads/2013/03/Michael-Jackson-Thriller-25th-Anniversary.jpg" class="rounded float-left" width="280" height="280"> ...

Centering items in Bootstrap 4, while excluding the first element

.c-footer { background: #000; padding: 20px 0; color: #fff; } .c-footer__logo { display: inline; } .c-footer__link { color: inherit; } .c-footer__link a { color: inherit; } .c-footer__link a:hover { color: #fff; } ...

Utilize Vue to condense cells within a table

I am attempting to create a schedule table using vue.js. Each row represents an activity (in this example, only one is hardcoded), and each column represents a time period. The cells contain the names of users if they are scheduled during that time. https ...

What could be the reason for the extra tags being returned by YQL?

Currently, I am executing a query in the YQL console with the following data: select * from html where url='http://www.motorni-masla.net/index.php?main_page=product_oil_info&cPath=140&products_id=294&zenid=c8281021bbfed454176247900b3b9d4a ...

What is the reason behind BeautifulSoup continually reminding me to upgrade my browser?

Trying to extract the leaderboard table from the PGA Tour website using bs4. As a newcomer to HTML and Python, I have been following tutorials with sample websites where everything works fine. However, when I attempt to run the following code: from bs4 imp ...

Having trouble getting Flask to apply a css stylesheet (problem with url_for())

I am currently working on integrating a CSS stylesheet with an HTML file using Flask by following some basic tutorials. Despite following the steps outlined in the tutorials, I am facing an issue where the stylesheet is not being applied to the HTML file. ...

I am interested in generating a report for the protractor-html-reporter in the form of an .html file

Currently, I am conducting end-to-end testing using Protractor. To generate a comprehensive report of all the test cases, I have employed Protractor-html-reportor. This tool creates an .html file containing detailed information on each test case. Everyth ...

Why does the second :nth-child rule have more importance over the others in my CSS code

I am seeking clarification on the implementation of :nth-child() in CSS. Consider the following HTML structure: <div class="parent"> <div class="child">child</div> <div class="child">child</div> <div class="child"&g ...

Deleting a row from a table when a similar element is found in another location

My webpage features a table list that showcases users linked to an organization: <script type="text/html" id="userListTemplate"> <div id="user_list_box"> <table class="list" style="margin-bottom: 15px;"> {{#Users} ...

Text and Image Coordination

I'm encountering some challenges with a specific section. My goal is for it to resemble this. However, I am currently achieving something different, like this. .timeline { text-align: center; } #about-us { ul { &:before { ...

Dynamic Loading of CSS Styles

My style-sheet is saved in this unique location: /opt/lampp/htdocs/project/core/styles/Style.css To load the CSS file using the full directory, considering that the files utilizing it are situated here: /opt/lampp/htdocs/project/client/ The ultimate ob ...

Exploring an illustration of Bootstrap

As a newcomer to html and css, I am currently working on building a website using the following example template: http://getbootstrap.com/docs/4.1/examples/dashboard/. My issue is that when I zoom in by pressing cmd + '+' or Ctrl + '+', ...

Customize the List Box Appearance for a Specific HTML Item (Option)

I am working on achieving a specific behavior. When using a listBox (or comboBox), the first element (such as "choose one") should have a unique style. Here is an example of code to test: <style type="text/css"> .testX {font-style: italic;} </ ...

Are HTML mistakes considered as WCAG violations?

In my quest to ensure that my website is WCAG compliant, I have done a lot of research. However, one question still lingers in my mind: Would a document with HTML errors still be considered WCAG 2.0 AA compliant? I recently ran Total Validator and it pro ...