简体   繁体   English

如何使用 HTML 和 CSS 将图像定位在居中对齐的标题右侧

[英]How to position an image to the right of a center aligned title with HTML & CSS

I'm working on a HTML page, and I've run into a little problem.我正在处理 HTML 页面,但遇到了一个小问题。 Here's a picture about what I want: https://i.imgsafe.org/2c4d808.png这是我想要的图片: https : //i.imgsafe.org/2c4d808.png

I've got a header section which contains the title and the picture, but I can't move the picture into the right side (red area) of the header.我有一个包含标题和图片的标题部分,但我无法将图片移动到标题的右侧(红色区域)。

I need to meet these conditions:我需要满足以下条件:

  1. The title and the picture must be in the same div (header), however I can use nested divs in the header.标题和图片必须在同一个 div(标题)中,但是我可以在标题中使用嵌套的 div。

  2. The title must be centered.标题必须居中。

  3. The picture must be completely on the right side of the header (both have a height of 100px).图片必须完全位于标题的右侧(两者的高度均为 100 像素)。

The header is 800x100 px and the picture is 160x100 px.标题为 800x100 像素,图片为 160x100 像素。

I can modify anything in my HTML or CSS code.我可以修改 HTML 或 CSS 代码中的任何内容。

Just to be clear: The header is with the "TITLE TITLE TITLE TITLE" and it is blue.需要明确的是:标题带有“TITLE TITLE TITLE TITLE”,它是蓝色的。

 #container{ width: 800px; height: auto; margin-left: auto; margin-right: auto; margin-top: 10px; padding: 10px; background-color: #cccccc; box-shadow: 0px 0px 20px black; } #header{ height: 100px; background-color: #6495ED; } #menu{ width: 150px; height: 400px; background-color: #FFD700; float: left; } #main{ width: 650px; height: 400px; background-color: #00FF00; float: right; } #footer{ height: 80px; clear: both; background-color: #6495ED; } #footerwords{ float: right; } #headerpics{ positioning: absolute; top: 0px; } ul{ font-size: 1.5em; font-family: arial; }
 <div id="container"> <div id="header"> <h1><center>TITLE TITLE TITLE TITLE</center><h1> <div id = "headerpics"> <img src="http://placehold.it/160x100/E8117F/ffffff/?text=Books" /> </div> </div> <div id = "main">xx t xx </div> <div id = "menu"> <ul> <li><a href = "index.html">1111111</a></li> <li><a href = "#">2222222</a></li> <li><a href = "#">33333333</a></li> <li><a href = "#">4444444</a></li> <li><a href = "#">5555555</a></li> </ul> </div> <div id = "footer"> <div id = "footerwords"> <strong></br>footer........<strong> </div> </div> </div>

give the <div> that holds the text/img a position: relative;给保存 text/img 的 <div> 一个位置:relative;

then give the <img> a position: absolute;然后给 <img> 一个位置:绝对; top: 0;顶部:0; right: 0;右:0;

 h1 { text-align: center; } .masthead { position: relative; height:139px; background-color: #9cc; } .masthead-img { position: absolute; right: 0; top: 0; }
 <div class="masthead"> <h1>This is My Title</h1> <img src="http://placekitten.com.s3.amazonaws.com/homepage-samples/200/139.jpg" alt="" class="masthead-img" /> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML / CSS-如何将我的图像放置在右侧,以使其与左侧的文本对齐? - HTML/CSS - How to position my image to the right so that it can be aligned with my text on the left? html / css:如何在单行上居中显示“标题”并在右侧按钮上显示 - html/css: how to on a single line center “title” and have on right buttons HTML CSS position div 左中右 - HTML CSS position div left center right 如何在html / css中将图像居中到导航栏右侧? - How can I center an image to the right of a navigation bar in html/css? 如何使用CSS将表格列中的右对齐数字居中? - How to center right-aligned numbers in table column using CSS? 如何使用 css 和 html 对齐文本和图像? 左右位置? - How to align text and image using css and html ? left and right position? HTML / CSS-如何将图像放置在面板右上角和左上角 - HTML/CSS - how to position an image in the above the right and left corner of a panel 如何在标题行末尾以中心对齐和潜文本对齐的方式编写标题? - How to write title with center align and subtext aligned right to end of title row? 在HTML和CSS中,图像左对齐,文本和标题右对齐 - Image left, text and header to the right aligned top in HTML and CSS 如何在 html 中在一行中显示 2 个短语,一个居中对齐,另一个右对齐? - How to display 2 phrases in a single line , one center aligned ,the other right aligned in html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM