CSS에서 이미지 위에 텍스트를 배치하는 방법
CSS에서 이미지 위에 텍스트를 중앙에 배치하려면 어떻게합니까?
<div class="image">
<img src="sample.png"/>
<div class="text">
<h2>Some text</h2>
</div>
</div>
나는 아래의 것과 같은 것을하고 싶지만 어려움이 있습니다. 여기에 현재 CSS가 있습니다.
<style>
.image {
position: relative;
}
h2 {
position: absolute;
top: 200px;
left: 0;
width: 100%;
margin: 0 auto;
width: 300px;
height: 50px;
}
</style>
배경 이미지를 사용할 때 html2pdf에서 출력을 얻지 못합니다.
<style>
#image_container{
width: 1000px;
height: 700px;
background-image:url('switch.png');
}
</style>
<a href="prints.php">Print</a>
<?php ob_start(); ?>
<div id="image_container"></div>
<?php
$_SESSION['sess'] = ob_get_contents();
ob_flush();
?>
prints.php는 다음과 같습니다.
<?php require_once('html2pdf/html2pdf.class.php'); ?>
<?php
$html2pdf = new HTML2PDF('L', 'A4', 'en');
$html2pdf->writeHTML($_SESSION['sess']);
$html2pdf->Output('random.pdf');
?>
이런 식으로 어떻습니까 : http://jsfiddle.net/EgLKV/3/
그것의 사용에 의해 수행 position:absolute
및 z-index
이미지를 통해 텍스트를 배치 할 수 있습니다.
#container {
height: 400px;
width: 400px;
position: relative;
}
#image {
position: absolute;
left: 0;
top: 0;
}
#text {
z-index: 100;
position: absolute;
color: white;
font-size: 24px;
font-weight: bold;
left: 150px;
top: 350px;
}
<div id="container">
<img id="image" src="http://www.noao.edu/image_gallery/images/d4/androa.jpg" />
<p id="text">
Hello World!
</p>
</div>
반응 형 크기로 작업하는 또 다른 방법입니다. 텍스트를 중앙에 유지하고 부모 내에서 위치를 유지합니다. 당신이 그것을 중심에두고 싶지 않다면 훨씬 쉽습니다 absolute
. 파라미터로 작업하십시오 . 기본 컨테이너는를 사용하고 display: inline-block
있습니다. 작업하는 내용에 따라 다른 방법이 많이 있습니다.
Based off of Centering the Unknown
HTML
<div class="containerBox">
<div class="text-box">
<h4>Your Text is responsive and centered</h4>
</div>
<img class="img-responsive" src="http://placehold.it/900x100"/>
</div>
CSS
.containerBox {
position: relative;
display: inline-block;
}
.text-box {
position: absolute;
height: 100%;
text-align: center;
width: 100%;
}
.text-box:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
h4 {
display: inline-block;
font-size: 20px; /*or whatever you want*/
color: #FFF;
}
img {
display: block;
max-width: 100%;
height: auto;
}
Why not set sample.png
as background image of text
or h2
css class? This will give effect as you have written over an image.
For a responsive design it is good to use a container having a relative layout and content (placed in container) having fixed layout as.
CSS Styles:
/*Centering element in a base container*/
.contianer-relative{
position: relative;
}
.content-center-text-absolute{
position: absolute;
text-align: center;
width: 100%;
height: 0%;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: 51;
}
HTML code:
<!-- Have used ionic classes -->
<div class="row">
<div class="col remove-padding contianer-relative"><!-- container with position relative -->
<div class="item item-image clear-border" ><a href="#"><img ng-src="img/engg-manl.png" alt="ENGINEERING MANUAL" title="ENGINEERING MANUAL" ></a></div> <!-- Image intended to work as a background -->
<h4 class="content-center-text-absolute white-text"><strong>ENGINEERING <br> MANUALS</strong></h4><!-- content div with position fixed -->
</div>
<div class="col remove-padding contianer-relative"><!-- container with position relative -->
<div class="item item-image clear-border"><a href="#"><img ng-src="img/contract-directory.png" alt="CONTRACTOR DIRECTORY" title="CONTRACTOR DIRECTORY"></a></div><!-- Image intended to work as a background -->
<h4 class="content-center-text-absolute white-text"><strong>CONTRACTOR <br> DIRECTORY</strong></h4><!-- content div with position fixed -->
</div>
</div>
For IONIC Grid layout, evenly spaced grid elements and the classes used in above HTML, please refer - Grid: Evenly Spaced Columns. Hope it helps you out... :)
as Harry Joy points out, set the image as the div's background and then, if you only have one line of text you can set the line-height of the text to be the same as the div height and this will place your text in the center of the div.
두 줄 이상인 경우 디스플레이를 표 셀로 설정하고 세로 맞춤을 중간으로 설정하려고합니다.
2017 년 현재 이것은 더 반응이 좋고 나를 위해 일했습니다. 이것은 배지처럼 텍스트를 안에 넣는 것입니다. 숫자 8 대신 데이터베이스에서 데이터를 가져 오는 변수가 있습니다.
이 코드는 Kailas 의 답변으로 시작되었습니다.
https://jsfiddle.net/jim54729/memmu2wb/3/
내 HTML
<div class="containerBox">
<img class="img-responsive" src="https://s20.postimg.org/huun8e6fh/Gold_Ring.png">
<div class='text-box'>
<p class='dataNumber'> 8 </p>
</div>
</div>
내 CSS :
.containerBox {
position: relative;
display: inline-block;
}
.text-box {
position: absolute;
height: 30%;
text-align: center;
width: 100%;
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
font-size: 30px;
}
.img-responsive {
display: block;
max-width: 100%;
height: 120px;
margin: auto;
padding: auto;
}
.dataNumber {
margin-top: auto;
}
작고 짧은 동일한 방법
HTML
<div class="image">
<p>
<h3>Heading 3</h3>
<h5>Heading 5</h5>
</p>
</div>
CSS
.image {
position: relative;
margin-bottom: 20px;
width: 100%;
height: 300px;
color: white;
background: url('../../Images/myImg.jpg') no-repeat;
background-size: 250px 250px;
}
참고 URL : https://stackoverflow.com/questions/8708945/how-to-position-text-over-an-image-in-css
'Programing' 카테고리의 다른 글
현재로드 된 어셈블리를 어떻게 반복합니까? (0) | 2020.07.17 |
---|---|
이름을 모르는 경우 Javascript 객체의 속성에 어떻게 액세스합니까? (0) | 2020.07.17 |
C #의 정수 나누기가 왜 실수가 아닌 정수를 반환합니까? (0) | 2020.07.17 |
메모리 누수가 얼마나 멀리 갈 수 있습니까? (0) | 2020.07.17 |
Java에서 클래스 내부의 열거 형 유형은 정적입니까? (0) | 2020.07.17 |