473,413 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

How do I horizontally center text and an image?

I have been trying to move an image (logo.png) and text ("The Crazy Cat Lady") to the center of a page. I have already put the image and text inside a div container and typed text-align="center"; in style.css. Can somone please help me with this issue? My code is below.

HTML:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2.  
  3. <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link href="https://fonts.googleapis.com/css?family=Poppins:400,500&display=swap" rel="stylesheet"> <link rel="stylesheet" href="/CSS/style.css"/> <title>The Crazy Cat Lady</title> </head> <body> <header> <div class="logo-container"> <img src="img/logo.png" alt="logo"> <h4 class="logo">The Crazy Cat Lady</h4> </div> <nav> <ul class="nav-links"> <li><a class="nav-link" href="#">Pablo Picatso</a></li> <li><a class="nav-link" href="#">Tailay</a></li> <li><a class="nav-link" href="#">Lucky</a></li> </ul> </nav> </header> </body> </html>
  4.  
  5.  
  6.  
  7. CSS:
  8.  
  9. * {
  10. margin: 5px;
  11. padding: 0px;
  12. box-sizing: border-box;
  13. }
  14. body {
  15. font-family: 'Poppins', sans-serif;
  16. }
  17. header {
  18. display: flex;
  19. width: 90%;
  20. height: 20vh;
  21. margin: 0;
  22. align-items: center;
  23. }
  24.  
  25. .logo-container,nav-links{
  26. display: flex;
  27. text-align: center;
  28. }
  29. .logo-container {
  30. flex: 1;
  31. text-align: center;
  32. }
  33. .logo{
  34. font-size: 150%;
  35. font-weight: 600;
  36. margin: auto;
  37.  
  38. }
  39. li{
  40. display: inline;
  41. padding: 0px 30px 0px 30px;
  42. position: relative;
  43. top: 55px;
  44. font-weight: 500;
  45. }
  46. nav {
  47. flex: 2;
  48. }
  49. .nav-links{
  50. justify-content: space-around;
  51. list-style: none;
  52. }
  53. .nav-link{
  54. color: #5f5f79;
  55. font-size: 18px;
  56. text-decoration: none;
  57. }
  58. img {
  59. width: 125px;
  60. height: auto;
  61. align-self: center;
  62. }
Dec 14 '19 #1
4 4002
SioSio
272 256MB
To display in the center, use the <center> tag
Expand|Select|Wrap|Line Numbers
  1. <center>
  2. <img src="img/logo.png" alt="logo"> 
  3. <h4 class="logo">The Crazy Cat Lady</h4> 
  4. </center>
  5.  
Jan 8 '20 #2
gits
5,390 Expert Mod 4TB
while this solution might work - its not the preferable one. for layout always use css while for structuring the content use html. that means - a part of the content is a heading or an image (html tags because this is what html is meant for) but how that is presented in the browser is a different task. Browsers can show it even differently. So for that layout tasks we have css - thus the center tag shouldn't even exist and was already deprecated in HTML4 (https://developer.mozilla.org/en-US/...Element/center).

the same can be achieved with css like for example:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3.   <style type="text/css">
  4.  
  5.     #centered-img {
  6.       display: block;
  7.       margin-left: auto;
  8.       margin-right: auto;
  9.     }
  10.  
  11.   </style>
  12.  
  13. <body>
  14.   <img id="centered-img" src="https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png"/>
  15. </body>
  16. </html>
PS: since the OP was asking for img and text the following example extends the upper one for such a requirement:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3.   <style type="text/css">
  4.  
  5.   #centered-img {
  6.     display: block;
  7.     margin-left: auto;
  8.     margin-right: auto;
  9.   }
  10.  
  11.   #center-container {
  12.     width: 100%;
  13.     text-align: center;
  14.   }
  15.  
  16.   </style>
  17.  
  18. <body>
  19.   <div id="center-container">
  20.     <img id="centered-img" src="https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png"/>
  21.     The Crazy Cat Lady
  22.   </div>
  23. </body>
  24. </html>
Jan 8 '20 #3
SioSio
272 256MB
Hi gits
Thank you for the information
Jan 9 '20 #4
gits
5,390 Expert Mod 4TB
you are welcome - a pretty good and short overview about how to use what for which purpose in creating websites can be found here:

https://www.codingdojo.com/blog/html...s-inforgraphic

this is just a simple overview though since today's webpages usually are not built in such a static way anymore. Modern single page applications mostly create their user interfaces with javascript and here some of the typical programming principles apply as well. But like there and here we can break it down to something like 'separation of concerns' at least - which is a good principle to apply whenever possible - since it structures the 'program' usually at least in a more easy understandable and maintainable way (even though HTML/CSS cant be seen as programming in a strict way).
Jan 9 '20 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gustav Medler | last post by:
Css only offers float:right; and float:left; Is it possible, to center an image, floated around by text? Cross browser funcionality is needed for NS4.7, IE 5.x, Op 6.x, Moz 1.x, Konqu 3.x. I...
13
by: Mike | last post by:
We are using: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"...
1
by: Richard Cleaveland | last post by:
How to center text in msgbox: is this possible or do I have to do my own thing with a form? Dick
7
by: Sean | last post by:
Hi, I have a tab control containing few tab pages. On each tab pages, I put an image on the tab page's title bar (the area on top of a tab page, ie where tooltip will appear). If i put both...
2
by: Win, Pats | last post by:
Just wondering if anyone has ever seen a 3rd party component that facilitates the creation of text images. I know I can "roll my own" but was not wanting to reinvent the wheel. I searched...
1
by: Krustov | last post by:
The following works fine - but would like to center the text . Whats the best way to do it ? . <?php $text=$kk;
2
by: texvanwinkle | last post by:
"WITH a border" being the defining issue. Here's the deal: I'm new to CSS, but I've managed so far to center a single line of links at the top of my home page and center an image on the bottom of...
3
by: hyphenpipe | last post by:
I am going completely nutso here and I am sure someone will point me in the right direction. I am trying to center and image that is 780 pixels wide at the very top of my page, and for some reason...
1
by: JackRbt | last post by:
I can do that if they're in a cell, but they're not. With CSS, I can center horizontally. Can I have text with is to the left of a textarea, and have the text vertically centered with respect to...
2
by: Jen Dha | last post by:
How do I center an image in CSS? I tried adding: align:center but that did not work it's an image wrapped by a div. <div style="align:center"> <img src=".."/> </div>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.