Place img relatively to text responsively - html

I have h1 centered in div by
text-align: center;
line-height: 150px;
vertical-align: top;
And now I want to place img for example in left-top corner of the text. Ofc I want it not to move when the window is changing sizes. It has to always be at the corner of text.
I tried setting position: absolute / fixed to img and then manipulate with top and left percent values, but the img is changing position relatively to the text (h1) when I change window sizes.
I'm very new to CSS (also HTML) and I see I don't understand what is happening on the screen and don't have an idea how to solve this problem..
.container {
margin: auto;
}
.logo {
height: 150px;
text-align: center;
line-height: 150px;
vertical-align: top;
font-size: 50px;
background-image: url(../img/forest.jpeg);
background-position: 30% 20%;
}
.logo h1 {
font-family: Coiny-Regular;
color: skyblue;
}
.logo img {
position: fixed;
height: 2.5em;
top: 7%;
left: 30%;
}
<div class="container">
<div class="logo">
<h1> Example </h1>
<img src="img/image.png" />
</div>
</div>

The img:
use position: absolute
place it inside the h1
The h1:
make it display: inline-block;
have position: relative (will make the img's absolute position relative to the h1)
The display: inline-block; on the h1 will make its bounding box match its content and make it possible for the image's absolute position to stay at an exact place.
.container {
margin: auto;
}
.logo {
height: 150px;
text-align: center;
line-height: 150px;
vertical-align: top;
font-size: 50px;
background-image: url(../img/forest.jpeg);
background-position: 30% 20%;
}
.logo h1 {
font-family: Coiny-Regular;
color: skyblue;
position: relative;
display: inline-block;
}
.logo img {
position: absolute;
top: 0;
left: 70px;
}
<div class="container">
<div class="logo">
<h1> Example
<img src="http://placehold.it/50" />
</h1>
</div>
</div>

From your question I understand that you have some sort of text centered inside your div and want a floating image on the top left corner of your text but you don't want the image to change positions inside the div when the position of the text changes.
.test {
width: 400px;
height: 80px;
border: 2px solid blue;
display: inline-block;
text-align: center;
}
.test img {
float: left;
padding: 5px;
}
.test h1 {
padding-right: 45px;
}
<div class="test">
<img src="http://www.techinsights.com/uploadedImages/Public_Website/Content_-_Primary/Teardowncom/Sample_Reports/sample-icon.png" height="40px" width="40px">
<h1>Sample Text</h1>
</div>

Related

Mobile Friendly Site with centered text in img in div

I need to create a responsive layout, wherein basically I have an image in center of screen, and text centered in the image.
I'm not sure how exactly to center all these things properly. I tried making the image a background-image in a div, but couldn't center it while making sure it'd grow/shrink as a screen did.
Now I'm not sure how to make it all very contained, without overflowing, and without setting a px size (which a lot of the help articles I've found specify).
This is my idea:
On my website at the moment I've made part of it work and it looks like this:
Except I need it also to be centered vertically and the span is as wide as the screen rather than as wide as the image.
https://jsfiddle.net/zb50azjx/
HTML:
<div class="news">
<div><img src="http://m.elysiumrpg.com/images/newstitle.png"/><span>News Title</span></div>
</div>
CSS:
.news {
color: #191919;
font-size: 15pt;
}
.news div img {
z-index: 0;
margin-left: auto;
margin-right: auto;
position: relative;
display: inline-block;
}
.news div span {
z-index: 1;
position: absolute;
text-align:center;
left: 0;
width: 100%;
color: #000000;
}
.news {
color: #191919;
font-size: 15pt;
display:inline-block;
width: 100%;
position: relative;
}
.news div img {
z-index: 0;a
width: 100%;
margin-left: auto;
margin-right: auto;
position: relative;
display: inline-block;
}
.news div span {
z-index: 1;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
top: 50%;
position: absolute;
left: 0;
width: 100%;
text-align: center;
}
<div class="news">
<div><img src="http://m.elysiumrpg.com/images/newstitle.png"/><span>News Title</span></div>
</div>
you can make div within news as position relative and span as position:absolute and set top:40% and left:40% this would give you the text centered within the image
.news {
color: #191919;
font-size: 15pt;
}
div.content {
width: 100%;
position: relative;
}
.news div span {
position: absolute;
top: 40%;
left: 40%;
color: #000000;
}
<div class="news">
<div class="content">
<img src="http://m.elysiumrpg.com/images/newstitle.png" /><span>News Title</span>
</div>
</div>
Hope this helps
I'm not 100% clear on what text you want centered on the image but see if this is what you are after:
http://codepen.io/panchroma/pen/gLEGqb
Using a background image with CSS of background-size:contain; can be really useful sometimes, because it will scale the image well to fit the size of the div
I've simplified your HTML and CSS quite a bit.
Good luck
HTML
<div class="news">News Title </div>
CSS
.news {
color: #191919;
font-size: 15pt;
background:url('http://m.elysiumrpg.com/images/newstitle.png') ;
background-size:contain;
text-align:center;
padding:20px 0;
}

Rotate div and have it displayed across entire screen

I have a div that I am wanting to rotate -20deg. I have gotten it to do just that, the problem I am facing is that it is not displaying it across the whole screen.
It is obviously just displaying a div that is 100% width and 100% height. What I am wanting it to do is cover the whole screen at an angle, so you can not see the left/right corners. If I extend the width above 100% it works but that is only going to push the width of the entire browser out.
Is there any way to get it to display at an angle so that it doesn't show the corners and is across the whole screen?
Fiddle: https://jsfiddle.net/2dhkk03b/
.content {
width: 70%;
margin: 0 auto;
position: relative;
height: 700px;
}
.intro {
width: 60%;
text-align: left;
position: absolute;
top: 50px;
color: black;
font-weight: 300;
font-size: 2em;
}
.para_txt {
padding-top: 40px;
}
.intro p {
font-size: 24px;
}
.slide {
position: relative;
width: 100%;
background-color: red;
height: 400px;
transform: rotate(-20deg);
}
<div class="content">
<div class="intro">
<span>Hi there!</span>
<p class="para_txt">My name</p>
</div>
</div>
<div class="slide"></div>
You can use a pseudo element for the rotated rectangle and set overflow hidden on the parent div to hide the corners and prevent the bottom scrollbar :
.content {
width: 70%;
margin: 0 auto;
position: relative;
height: 700px;
}
.intro {
width: 60%;
text-align: left;
position: absolute;
top: 50px;
color: black;
font-weight: 300;
font-size: 2em;
}
.para_txt {
padding-top: 40px;
}
.intro p {
font-size: 24px;
}
.slide {
position: relative;
height: 1000px;
overflow: hidden;
}
.slide:after {
content: '';
position:absolute;
top:35%;
width:100%; height: 30%;
background: red;
transform: rotate(-20deg);
}
<div class="content">
<div class="intro">
<span>Hi there!</span>
<p class="para_txt">My name</p>
</div>
</div>
<div class="slide">
</div>
The problem is the corners of the div are square so if you rotate a rectangle x degrees you will lose some width.
The ideal solution would be to increase the size and minus the left margin for example.
width: 180%;
margin-left: -40%;
Thanks

Image in a divs affects external divs

Right now Im trying to put an image on the top of a div. The divs are in horizontal, and I donĀ“t know why, but when I put the image its position affects all external divs... I mean, the image should only affect the div in which I put it.
I know this can be a little bit difficult to undestand, I took a capture of my divs: Capture. As you can see, the height of my image affects the external divs.
Here is the HTML code:
<div class="hoteles">
<div class="head-hoteles">Los mejores hoteles</div>
<div class="hotel"><img src="images/hotels/hotel-bellevue.jpg" alt="Hotel Bellevue"></div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
</div>
And the CSS:
.hoteles{
background-color: pink;
height: 100%;
width: 65%;
float: left;
padding-left: 2%;
}
.head-hoteles{
width: 100%;
height: 100px;
background-color: yellow;
padding: 5%;
font-size: 1.5em;
}
.hotel{
height: 12.5em;
min-width: 23%;
display: inline-block;
background-color: brown;
margin-bottom: 2%;
}
.hotel img{
width: 100px;
}
Other question is... when I put "width 100%" its does not do it, I just can resize the image with pixels... Thanks !
You need to float the divs, currently your divs are positioned as inline-block which is causing disorder. Additionally you can use vertical-align: top to order the inline-block.
Working example:
JSFiddle
.hoteles {
background-color: pink;
height: 100%;
width: 65%;
float: left;
padding-left: 2%;
}
.head-hoteles {
width: 100%;
height: 100px;
background-color: yellow;
padding: 5%;
font-size: 1.5em;
}
.hotel {
height: 12.5em;
min-width: 23%;
background-color: brown;
float: left;
margin:2% 5px 2% 0;
}
.hotel img {
width: 100px;
float:left;
}
<div class="hoteles">
<div class="head-hoteles">Los mejores hoteles</div>
<div class="hotel">
<img src="images/hotels/hotel-bellevue.jpg" alt="Hotel Bellevue" />
</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
</div>
As for your second question, you need to have a width for the parent of img. Currently it uses min-width, change it to width and give your img the width of 100% and it will expand to the percentage of the parent. Like the following:
.hotel {
width: 23%;
}
.hotel img {
width: 100%;
}
Try adding the following CSS rule:
.hotel { vertical-align: top; }
You are seeing the result of inline elements being positioned along the baseline.

How do I center an element within a div with another element nearby?

I need to center an element within a div while there is another element nearby. When I use text-align center, it places the element close but it's not going exactly where I'd like due to another element nearby. I want the logo element to center exactly while the social element floats to the right.
#header {
width: 100%;
height: auto;
text-align: center;
background-color: #ffffff;
}
#logo {
width: 30%;
margin: 10px auto;
display: inline-block;
text-align: center;
}
#logo img {
width: 100%;
height: auto;
margin: auto;
}
#social {
width: 15%;
display: inline;
float: right;
}
#social img {
width: 25%;
display: inline-block;
}
<div id="header">
<div id="logo">
<img src="images/final/logo2.png">
</div>
<div id="social">
<img src="images/final/fb.png">
</div>
</div>
See this fiddle
To make the logo div centralize irrespective of the div taht holds the social link, you'll have to make the logo div absolute positioned.
Now, to make the absolute positioned logo div centered, you'll have to add left:0;right:0 to the CSS.
The revised CSS is as follows
CSS
#logo {
width: 30%;
margin: 10px auto;
position: absolute;
left: 0;
right: 0;
}
Try add this:
#logo {
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
alternatively if #logo would have width: 100px; then position: absolute; left: 50%; margin-left: -50px;
Add center tag and remove float:right from social
#header {
width: 100%;
height: auto;
text-align: center;
background-color: #ffffff;
}
#logo {
width: 30%;
margin: 10px auto;
display: inline-block;
text-align: center;
}
#logo img {
width: 100%;
height: auto;
margin: auto;
}
#social {
width: 15%;
display: inline;
}
#social img {
width: 25%;
display: inline-block;
}
<div id="header">
<center>
<div id="logo">
<img src="images/final/logo2.png">
</div>
<div id="social">
<img src="images/final/fb.png">
</div>
</center>
</div>

Vertical align multiple elements inside parent

I've been search for more than a day a way to vertical align my fluid designed header so without knowing font-size nor spesific pixels my 3 divs will be the same height and the content inside them in the same line.
Here is an fiddle example of what I have now so you might understand what i need better.
And this is the code:
HTML:
<div id="container">
<div id="header">
<div id="menu">
<a href="#">
<img src='http://s16.postimg.org/uwgkp15r5/icon.png' border='0' alt="icon" />
</a>
</div>
<div id="title">
My site title
</div>
<div id="my_button">
<button id="button">My button</button>
</div>
<div style="clear: both;"></div>
</div>
<div id="content"></div>
</div>
CSS:
html,body {
height: 100%;
font-size: 2vmin;
}
#container {
height: 100%;
min-height: 100%;
}
#header {
height: 20%;
padding: 2vmin 0 2vmin 0;
box-sizing: border-box;
background: #000000;
width: 100%;
}
#menu{
background: #5f5f5f;
float: left;
width: 20%;
text-align: center;
}
#title {
background: #aaaaaa;
height: 100%;
float: left;
font-size: 3vmin;
width: 60%;
text-align: center;
}
div#my_button {
background: #cccccc;
float: right;
width: 20%;
}
button#button {
color: #aaaaaa;
border: none;
}
#content {
height: 70%;
width: 100%;
background: #eeeeee;
}
You can use :after pseudo element for solving your problem.
add this after #header styles in your CSS
#header:after{
height: 100%;
width: 1px;
font-size: 0px;
display: inline-block;
}
Then remove floats from #menu, #title and #my_buttun div's and apply
display: inline-block;
vertical-align: middle;
The inline-block will create small gaps between these div, but if you're not apply background colors to them , then it is ok.
Last: make #my_button width: 19%;
Look here: http://jsfiddle.net/D22Ln/5/
If you mean the three horizontal divs, setting height: 100%; for all of them will do the trick. From there you just modify the size of their parent element (currently at 20%) and they will adapt automatically.
http://jsfiddle.net/D22Ln/2/
If I have understood you correctly this is maybe what you are looking for, I just copied that I have done earlier. But test it out: http://jsfiddle.net/6aE72/1/
By using wrapper and a helper you will have the left and right div same size as middle and helper helps with vertical alignment
#wrapper { display: table; width: 100%; table-layout: fixed; position: absolute; top: 0;}
.content { display: table-cell; }
This FIDDLE might help you. I've used bootstrap framework. Re-size the RESULT grid.