how to make one element follow another elements size - html

like let's say you have an image
<img class="testImage" src="test.png"/>
then there's also a section supposed to always follow the height of the image.
I know these examples below would never work, but just to give an idea, if something like this is possible?
height: .testImage;
height: calc(0 + .testImage);
The reaosn I wanna do this is because I am working on something in wordpress where I've put in an image and then there's some content next to it. https://i.gyazo.com/d85b9d8a5384829589b715c2c36fb059.png This looks fine when the screen width is normal. However, when it becomes smaller then the image becomes smaller as well, because it's responsive. The content does not become smaller though. https://i.gyazo.com/0af7d841c695912cda3bd266174238e1.png
For it to match the image 100% I would have to figure out exactly how the image is styled for every screen size. So I am wondering if I can just get the content block to follow the images height?

You should use flex property for this. In this example image width is fixed but you can also put it in % or em
*{
box-sizing: border-box;
}
body, html{
height: 100%;
width: 100%;
margin: 0;
}
.container{
width: 100%;
display:flex;
flex-direction:row;
}
.container img{
display:block;
margin: 0 auto;
width:200px;
flex-shrink:0;
}
.icontainer
{
margin:0;
background-color:red;
border: 0.2em solid black;
flex-shrink:0;
flex-grow:1;
}
<div class="container">
<img src="http://images.fineartamerica.com/images-medium-large-5/sunflower-abstract-by-nature-square-lee-craig.jpg"/>
<div class="icontainer">
<p>content</p>
</div>
</div>

Use same css class to both images
img 1
img 2
.imageClass{
height: 100px;
}
<img class="imageClass" src="https://s-media-cache-ak0.pinimg.com/originals/bd/8c/24/bd8c241fb4c6b19a48668ae993cd0a34.jpg" >
<img class="imageClass" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTrgEmBoOkIN4gekFDarSmiHlv83AK_7UuQW9SDBJYUM-qhb9AK" >

Related

Image not resizing dynamically with window size

I want an image with the size of 250x50px to resize itself (go smaller) according to the window size, in other words, make it responsive.
The #wrapper holds the content for the whole page. The #headerholds the image and the navigation bar.
I know this may be easier with the use of #media screenbut I am looking for a pure CSS approach.
Here is what I am currently using:
HTML:
<div id="wrapper">
<div id="header">
<div style="max-width:500px;">
<img id="logo" src="images/logo.jpg" alt="logo" href="#">
</div>
</div>
CSS:
#wrapper{
width: 100%;
height: auto;
}
#header{
height: 100px;
min-width: 300px;
border-bottom: 1px solid black;
}
#logo{
float: left;
max-width: 100%;
height: auto;
width: auto\9; /* ie8 */
}
you have a few issues in your code,
don't use inline-styles,
put the border-bottom in child div of #header
no need for IE hacks
no need for a min-width here.
Note: I don't see why you need this image to get smaller, lets see, its 250x50, when the lower screen are 320px so already fits perfectly
here is a snippet
#wrapper {
width: 100%;
border: dashed red 1px
}
#header > div {
max-width: 500px;
border-bottom: 1px solid black;
}
#logo {
max-width: 100%;
height: auto;
display: block
}
<div id="wrapper">
<div id="header">
<div>
<img id="logo" src="//placehold.it/250x50" alt="logo" />
</div>
</div>
</div>
First off, using #media screen is a CSS approach. But if you don't want that, you can just relative units like em or % and give a min-width:
#logo {
width: 20%;
min-width: 100px;
}
thing is, you already put some limits by setting the header to min width 300px and the div inside to max width 500px, so if you want to have more flexibility with those as well, consider different units too.

How to stop img from overflowing?

My problem is that I have an image that I would like to use as the logo for the website. The header and div that it is in are properly sized (i checked by inspecting it on chrome) , but the img is overflowing. How do i scale it down?
(I will use a placeholder image for the code)
Here is the code:
<body>
<header class="mainheader">
<a href="main.html" class="navelement">
<div class="imgcontainer">
<img src="https://lh3.googleusercontent.com/-Ajc8gSO3SRw/VtZj0tsyXhI/AAAAAAAAAHs/tlsdyufJ1J4/w868-h560/2.png"/>
</div>
</a>
</header>
</body>
Here is the accompanying css, I'm confused why the max-height isn't working.
* {
box-sizing: border-box;
}
body{
margin: 0;
}
header{
height:10vh;
width: 100%;
float left:
margin: 0;
background-color:grey;
font-family: 'Teko', sans-serif;
display: inline-block;
}
header .navElement {
vertical-align: middle;
float:left;
width: 17%;
}
.imgcontainer{
float:left;
max-height: 100%;
}
.imgcontainer img{
float:left;
max-height:100%;
}
I realize some things are unnecessary but its because i've tried so much to fix it that i don't know what works. Thanks for any helpful replies.
To prevent overflowing of content, you can simply use the corresponding css-property: overflow: hidden. If you do so, the content which is overflowing, will simply get cut off. In your example, this wouldn't be the right approach.
Instead of using max-height you may use height. By doing so, you'll assign an absolute value for your elements. Since your .imagecontainer only has a max-height property, try replacing it with height, as shown here.
.imgcontainer {
float: left;
height: 100%;
}

Force an image to ALWAYS stay at the middle of a div

I'm searching ways to force an image element to stay always aligned at the middle of a div even when the div element gets too small to properly center the image. Here's an explanation of what I want to do: Question.jpg
I need the solution to be purely CSS if possible?
Using this centering trick should give you what you want:
img {
position:relative; (or absolute, it depends on your needs)
top:50%;
left:50%;
transform:translate(-50%, -50%);
}
This keeps the image centered no matter the size of the container it is in. It doesn't resize the image though. Based on that pic you out it seems like you didn't want that.
Use the following grid layout and styles:
style{
.container{ max-width:800px; margin:0 auto;}
}
#media
only screen and (max-width:600px) {
.mCenter{margin:0 auto;}
.mImage600:{width:100%; height:auto;}
}
<div class=container>
<div class=mCenter>
<img class=mImage600 src 'img.jpg' width=600 >
</div>
<div>
Fiddle
img{
margin: 0 auto;
}
could help
You can do like this, using display flex.
div {
width: 100%;
height: 500px;
border: 1px solid gray;
display: flex;
align-items: flex-start;
text-align: center;
}
img {
align-self: center;
margin: 0 auto;
}
<div>
<img src="http://screenshots.en.sftcdn.net/en/scrn/323000/323026/angry-birds-theme-02-100x100.png" alt="">
</div>

Need to resize banner to fit window CSS

I am trying to make my site logo/banner fit the content box correctly.
Unfortunately, it is appearing at different widths on different computer resolutions and window sizes.
This is also happening with my banner ad within the content box.
CSS
#logo {
max-width: 100%;
height: auto;
width: auto;
}
HTML
<div id="logo">
<center>
<img src="logo.png" alt="Image of Traffic Monsoon">
</center>
</div>
The website is here.
To center an inline level element like <img> tag, you can set text-align:center; on the container, with your example:
#logo {
text-align: center;
}
<div id="logo">
<img src="logo.png" alt="Image of Traffic Monsoon">
</div>
In addition, remove <center>, it has been deprecated. And add following lines to make the image to shrink to fit automatically when its intrinsic width is larger than the container:
#logo img {
max-width: 100%;
height: auto;
}
<center>is deprecated so don't use it.
To fix your issue you need to target the img not the div
use margin:auto and display:block to center the image instead of the deprecated center
#logo img {
max-width: 100%;
height: auto;
width: auto;
margin:auto;
display:block
}
<div id="logo">
<a>
<img src="http://clubtrafficmonsoon.com/banner.gif" alt="Image of Traffic Monsoon">
</a>
</div>
If you want to apply this generally to all images in the site, just do this:
img {
max-width: 100%;
height: auto;
width: auto;
}
Wrap your whole page in a <main> element, or a wrapper class. Set your max-width on that element, and all subsequent elements can have width:100% set.
Please try this:
First of all wrap you entire page with a div named wrapper.
<div class="wrapper">your code here</div>
Then apply this css below:
.wrapper {
margin: 0 auto;
width: 1574px;
}
#logo {
margin: 0 auto;
text-align: center;
width: 1331px;
}
#logo img{
text-align: center;
width: 96%;
}

Bootstrap CSS to set groups of grid rows to 100% height [duplicate]

This is basically what I want to achieve:
I want the total page to be 100% height, but when I put the sidebar and body at 100%, the page adds the 40px from the navbar, so I get a scrollbar even when there shouldn't be one.
I got it fixed by using tables, but I'm sure there must be an easier way
<body>
<div class="container-fluid">
<div class="navbar"></div>
<div class="sidebar"></div>
<div class="body"></div>
</div>
</body>
and css what I've got so far:
body, html, .container-fluid, .sidebar, .body {
height: 100%;
min-height: 100%;
}
.navbar {
height: 40px;
position: relative;
}
You'll have to subtract the height of the navbar from the 100%. There are several solutions, many of which will include JavaScript but you won't need that.
1: box-sizing
Give the navbar an absolute position. Then you have the issue of the content of the other elements disappearing below it.
Then comes the next trick: add a top-padding the size of the navbar.
And the last trick: add box-sizing: border-box; to the .sidebar and .body. For better browser support you also need -moz- and -webkit- prefixes like so: -moz-box-sizing:
Example: Fiddle to box-sizing
2: Actually subtract.
use .body, .sidebar{ height: calc(100% - 40px);
Browser support is very minimal for this from what I know less than for box-sizing, so I would recommend the first solution.
Calc explained on css-tricks
3: Flexbox(added anno 2015)
You should now probably go with using calc when you know the height but an awesome replacement for using tables is flexbox. This is really my saviour for complex designs in responsive websites. By using one of the best features - flex-shrink: 0 - on the header you can force other elements into adjusting themselves to fill the rest of the container.
An old answer is probably not the best place to write down an extensive guide on flexbox so, again, a great link to css-tricks
Probably because it is adding the default margin from the navbar. Try this:
.navbar {
height: 40px;
position: relative;
margin: 0;
padding: 0;
}
Also, try changing the min to max height:
max-height: 100% !important;
I change a litle your code by adding a container around your side bar and body class:
Here is the RESULT
Css:
body, html, .container-fluid, .sidebar, .body {
height: 100%;
min-height: 100%;
}
#container{
width:100%;
height:100%;
}
.sidebar{
background-color: green;
width:10%;
float:left;
height:100%;
}
.body{
background-color: orange;
float:left;
width:90%;
height:100%;
}
.navbar {
height: 40px;
position: relative;
background-color: yellow;
width:100%;
}
HTML
<div class="container-fluid">
<div class="navbar">
navbar
</div>
<div id="container">
<div class="sidebar">
sidebar
</div>
<div class="body">
body
</div>
</div>
</div>
</body>