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.
Related
I'm having some trouble with the CSS for my page which displays social media style posts including images. When displaying the images they are placed inside of a div tag which is styled to try and reduce the size of the images and display the images horziontally next to eachother.
However, I am having the issue that the images still try to stack on top of each other and I can not get them to resize how I would like, especially on mobile. Currently based on some other thread here this is what I've got
.postImg{
display: inline-block;
height:100%;
width:100%;
object-fit: scale-down;
}
.postImgDiv{
height: 100px;
}
Your image taking 100% of the row width. Change it to: width: auto.
Look at this (your case):
.postImg{
display: inline-block;
height:100%;
width:100%;
object-fit: scale-down;
border: 1px solid;
}
.postImgDiv{
height: 100px;
}
<div class="postImgDiv">
<div class="postImg">A</div>
<div class="postImg">B</div>
<div>
After fix:
.postImg{
display: inline-block;
height:100%;
width:auto;
object-fit: scale-down;
border: 1px solid;
}
.postImgDiv{
height: 100px;
text-align: center;
}
<div class="postImgDiv">
<div class="postImg">A</div>
<div class="postImg">B</div>
<div>
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" >
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%;
}
I have tried everything I know, and I still can't get it to work. I want the four images to be like in the screen shot, but a lot bigger (600px centered). When I do this, however, it causes the entire container to be shifted to the left for some reason unknown to me.
HTML snippet:
<body>
<div class="container">
<div class="header">
..
</div>
<div class="menu">
...
</div>
<div class="content">
<div class="photos">
<h2> Here are some photos.....</h2>
<img src="img1">
<img src="img2">
...
</div>
</div>
</div>
</body>
CSS snippet:
body{
margin: 0;
padding: 0;
}
.container{
background-color: #AAC1CC;
max-width: 1440px;
width: 100%;
height: auto;
margin: 0 auto;
padding-bottom: 10px;
}
.content{
margin-left: 20px;
margin-bottom: 100px;
}
.photos {
background-color: pink; /* for testing */
width: 500px; /*for testing - normally 100% */
padding: 0;
margin: 0;
}
.photos img{
width: 100px;
display: block;
border: 2px solid black;
margin: auto;
margin-top: 40px;
margin-bottom: 40px;
box-shadow: 0 0 30px 3px #333;
}
Screenshot:
When the images are small (like 100px) the style is the same across the other tabs. However, if I increase the size of the images > 150px, the entire container shifts to the left by like ~20 pixels. I have tried using <br> between the images instead of display:block but it doesn't make a difference.
Why does this happen?
The only thing I can think of is that when the images are larger they cause the browser to display a scroll bar. As you have set the container width to 100% and the window width is now slightly smaller this could cause the shift that you mention.
I had a similar problem with a large image causing my main container to shift off center. threeandme's post reminded me to experiment with the overflow-y property in the css. I added overflow-y: scroll; into the css for "body" and it stopped it from shifting for me.
I have variable-width HTML layout with a fixed-width menu to the left of a content <div> of variable width (set by css max-width and min-width). For very narrow browser windows I would like the content to wrap beneath the menu, and I am currently achieving this by setting float:left on both menu and content.
<html>
<head>
<title></title>
</head>
<body>
<div style="width: 200px; float: left; border: 1px black solid">Menu (200px wide)</div>
<div style="max-width: 800px; min-width: 300px; float:left; border: 1px black solid">Content div. This div has max-width: 800px; min-width 300px. It has enough text that it expands to its max-width if there is space available to do so.</div>
</body>
</html>
In this example, wrapping of the content div currently occurs as soon as the browser viewport is smaller than 1000px (menu width + content max-width). I would like to have the width of the content reduce first, and have the content wrap beneath the menu only when viewport is smaller than 500px wide (menu width + content min-width)
Is there a way to achieve this, either with my current arrangement of floated <div>s, or otherwise?
Please check if this is the behavior you want.
DEMO
JSFiddle
HTML
<html>
<head>
<title></title>
</head>
<body>
<div class="menu">Menu (200px wide)</div>
<div class="content">Content div. This div has max-width: 800px; min-width 300px. It has enough text that it expands to its max-width if there is space available to do so.</div>
</body>
</html>
CSS
.menu {
width: 200px;
float: left;
border: 1px black solid
}
.content {
max-width: 800px;
min-width: 300px;
margin-left: 200px;
border: 1px black solid
}
#media all and (max-width: 500px) {
.menu {
float: none;
}
.content {
margin-left: 0;
}
}
I suppose this is what you want: http://jsfiddle.net/joplomacedo/WXFQz/
The solution is a simple media query - below a screen-width of XYZpx do this. If you've never heard of it before here's an article about it http://css-tricks.com/resolution-specific-stylesheets/
For those of you who can't see the fiddle, here's the html and css :
HTML:
<div class="container"> <!-- it's possible to do it without this extra element. it's simply more intuitive this way -->
<div class="menu"></div>
<div class="content"></div>
</div>
CSS:
.container {
max-width: 1000px; /* your self defined 800px max-width for the content-div + 200px from the .menu's width */
min-width: 200px;
}
.menu,
.content {
height: 200px;
}
.menu {
float: left;
width: 200px;
}
.content {
margin-left: 200px; /* same as '.menu's width */
}
#media (max-width : 400px) {
.menu {
float: none;
width: auto;
}
.content {
margin-left: 0;
}
}
there is a demo of this from css-tricks:
http://css-tricks.com/examples/PerfectFluidWidthLayout/
I hope this is good for you.