I am learning HTML / CSS basics and I am stuck at the following problem.
I would like to put 3 fixed-size float divs in a fixed-width outside div, and I would only like to have margins between them, not outside them.
My problem is that I don't know how to achieve this. I tried reading about margin-collapse and negative margins but it is quite hard for me to understand.
I have put my example onto the following jsfiddle: http://jsfiddle.net/6dqR6/
HTML
<div class="outer">
<div class="articles">
<h2>header</h2>
<div class="article">
<p>1</p>
</div>
<div class="article">
<p>2</p>
</div>
<div class="article">
<p>3</p>
</div>
<div class="article">
<p>4</p>
</div>
<div class="article">
<p>5</p>
</div>
<div class="article">
<p>6</p>
</div>
</div>
</div>
CSS
.outer {
width: 940px;
margin: 0 auto;
}
.articles {
height: 1500px;
background-color: #fcdda1;
}
.article {
width: 300px;
height: 350px;
float: left;
background-color: #4ecac3;
margin: 0 20px 20px 0;
}
Can you tell me:
What is the recommended way to solve my problem and fix the above example?
Possibly some beginner friendly articles/books from which I could learn about it?
Thanks a lot, and sorry for asking such a beginner level question here.
1) Solution for a basic CSS knowledge
Based on Css2, fully cross-browser
Here is the solution to have only the inner margin, and not applied to the last on the right, click here, the working fiddle.
You have to make a mask to overflow the plus margin:
.outer {
width: 940px;
overflow:hidden;
/*behaves like a mask*/
}
.articles {
width: 960px; /*set the actual width: 320*3*/
}
2) Advanced solution
Based on Css3, only modern browser (except, of course, IE)
The Css nth-child solution is here in this example: jsfiddle.net:
.articles > :nth-child(3n+1) { margin-right: 0; }
You can find here the complete explanation about how it works.
Since IE have a partial support of Css3 upcoming standards, you can use IE conditional Comments to pass specific Css on for InternetExplorer legacy versions (e.g. Css3 Solution for modern browsers, Css2 solution only for old IEs).
Eventually, here are other solutions based on Css pseudoclass and jQuery.
3) Online resources to make practice on CSS
Here is where you can learn a lot about Css in general:
CssTricks
Smashing Magazine
Tuts Plus: CSS categories
This can be thought as "advanced", but I think is the best way, just add to your CSS:
.article:nth-child(3n-3) {
margin-right:0
}
This makes every third element not having right margin, so it wont fall to the next line. That's it.
Beside CssTricks, Mozilla Developer Network is a good resource to learn too.
Related
I'm currently building a website with Blazor Pages on the .NET platform. I'm new to using HTML and CSS, and I decided to use the Bootstrap CSS library to work on my first website. I'd like to have a text container aligned to the left of my overall jumbotron component along with an image in the right. Something like this:
My website currently resembles this:
(the hair is a photo of me, not going to upload the whole image)
The image itself is rather large, so ideally I'd like to work with scaling. I know the img-fluid class can handle scaling within a parent container, but I'm not sure why it's not scaling down.
Here's my current HTML.
<div class="jumbotron">
<div>
<div class="container-fluid">
<h1 class="display-4">Welcome to my site!</h1>
<div class="container align-items-start">
<p class="lead text-left">Welcome to my personal site. I hope to use this as a place where you can learn more about me, my projects, and my ideas.</p>
<p class="lead text-left">I am just learning web development, so many aspects of this website won't be perfect. I hope that this place can serve as a living example of my progression!</p>
</div>
<div class="container">
<img src="/Images/Headshot2020.jpg" class="img-fluid" alt="Image of Matt Marlow"/>
</div>
</div>
</div>
There are multiple ways to achieve what you're looking for or create layouts in general.
I would strongly recommend avoiding using 3rd party libraries that you're not familiar with to build your webpage layout. You might be able to get help on this particular page, but you will get stuck again as soon as you want to create the next one.
It's kind of like trying to build a car without knowing how to weld.
You should read about using Flex (bootstrap is based on that). It might sound intimidating but it's very straightforward.
Start by building some blocks and see how they react with different settings applied to it. Once you feel comfortable, start expanding it and reach the design you want.
a helpful tip:
For each container or "box" you're building - apply some colorful background or border. This will allow you to see exactly which container is being affected and how by every change. So the thumb rule is: Each div gets it's own color.
Once you're satisfied with the layout, just remove all the colors and start inserting content.
Link explaining how to work with css flexbox
CSS Flexbox
And here's a snippet using basic flex to build the layout you wanted
body, html{
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-family: sans-serif;
}
.main-container{
background: gray;
width: 90%;
height: 90%;
border: solid 1px red;
display: flex;
padding: 50px;
box-sizing: border-box;
}
.left-panel{
width: 70%;
height: 100%;
border: solid 1px blue;
}
.right-panel{
width: 30%;
height: 100%;
border: solid 1px lime;
padding: 50px;
box-sizing: border-box;
}
.title{
font-size: 2.5em;
}
.content{
padding: 30px;
box-sizing: border-box;
border: solid 1px magenta;
line-height: 30px;
}
.image{
border: solid 1px;
width: 100%;
height: 100%;
background: white;
display: flex;
align-items: center;
justify-content: center;
}
<div class="main-container">
<div class="left-panel">
<div class="header">
<div class="title">
Welcome to my site !
</div>
</div>
<div class="content">
Lorem ispum, some random text!
</br>
This is a markup example for my question
</div>
</div>
<div class="right-panel">
<div class="image">
A picture of me !
</div>
</div>
</div>
Bootstrap saves a lot of time and work-- setting breakpoints for a gazillion things is hard.
But when it comes to these kinds of problems, you need to KNOW enough html/css does and how it works. In particular, learn how flex layout works.
Then you will be able to choose the correct Bootstrap classes, knowing that your site will conform to best practices for a dynamic layout.
Use float-end on the image this will allow the text to flow around the image. I moved the image above the heading to align their tops.
<div class="container-fluid">
<img src="/Images/Headshot2020.jpg" class="img-fluid float-end col-4" alt="Image of Matt Marlow" />
<h1>Welcome to my site!</h1>
<p class="lead">Welcome to my personal site. I hope to use this as a place where you can learn more about me, my projects, and my ideas.</p>
<p class="lead">I am just learning web development, so many aspects of this website won't be perfect. I hope that this place can serve as a living example of my progression!</p>
</div>
BTW: jumbotron is obsolete.
Photo by Alison Wang on Unsplash
I am working on a site that I am building for myself on squarespace which is why I am trying to write this using inline css. I am trying to get an image styled in a row with some text. The image should be 1/12 of the row. I need to do this in html/css because there will be additional, identically styled rows below the one that I am depicting in jsfiddle.
I can't seem to get he image to scale down and fit on the same row as the text, with the same height as the text. I had it working and then accidentally reverted my work... so it's time for a break. Hoping someone will take pity on me before I come back to it.
Here's my JSFiddle with image and text: https://jsfiddle.net/hrwj2u7c/
<div style="display:flex;flex-direction:row;flex-wrap:wrap;background-color:#bcc0c6">
<div style="flex:0 1 150px;">
<img src=http://creaturesoundproductions.com/s/HelpSpeaker.png>
</div>
<div style="flex:1 0 auto;">
<span style="font-weight:bold;font-size:24px; ">I'm new to podcasting, and I don't know where to start!</span>
<p>That's OK, we've got you! Start here with us, and we'll do all of the technical stuff for you. Have you heard of hosting, RSS feeds, maybe editing? We'll do all of that for you. All you have to do is use our unique app on any device and start recording. We'll even be happy to teach you, so that you'll be more educated going forward!
</p>
</div>
</div>
Is this what you're trying to achieve?
<div style="display:flex;flex-direction:row;align-items: center;background-color:#bcc0c6">
<div style="flex: 0 0 150px;">
<img src=http://creaturesoundproductions.com/s/HelpSpeaker.png style="width: 100%">
</div>
<div>
<span style="font-weight:bold;font-size:24px; ">I'm new to podcasting, and I don't know where to start!</span>
<p>That's OK, we've got you! Start here with us, and we'll do all of the technical stuff for you. Have you heard of hosting, RSS feeds, maybe editing? We'll do all of that for you. All you have to do is use our unique app on any device and start recording.
We'll even be happy to teach you, so that you'll be more educated going forward!
</p>
</div>
</div>
Key points:
flex-basis: 150px; flex-grow: 0; on image container
width: 100%; on <img>
removing flex-wrap: wrap from the overall wrapper (which would cause the second div to go below).
i also added align-items: center to the wrapper, to align the flex items vertically. You can't really match their height, as you'll notice the second div varies in height significantly at various widths of the page, but you can align them vertically.
Now, the biggest problem with what you're trying to do (which is styling inline) is that it cannot be responsive, because you can't apply #media queries by using inline styles. However, what you can do is use <style> tags inside <body>. Example:
<style type=text/css>
.wrapper {
padding: 25px;
max-width: 1200px;
margin: 0 auto;
display: flex;
flex-direction: row;
align-items: center;
background-color: #bcc0c6;
}
.wrapper> :first-child {
flex: 0 0 200px;
}
.wrapper> :first-child img {
width: 100%;
}
.wrapper> :nth-child(2) {
padding: 0 0 0 25px;
}
.wrapper> :nth-child(2)>span {
font-weight: bold;
font-size: 24px;
}
#media (max-width: 700px) {
.wrapper {
flex-wrap: wrap;
}
.wrapper> :first-child {
margin: 0 auto;
}
}
</style>
<div class="wrapper">
<div>
<img src=http://creaturesoundproductions.com/s/HelpSpeaker.png>
</div>
<div>
<span>I'm new to podcasting, and I don't know where to start!</span>
<p>That's OK, we've got you! Start here with us, and we'll do all of the technical stuff for you. Have you heard of hosting, RSS feeds, maybe editing? We'll do all of that for you. All you have to do is use our unique app on any device and start recording.
We'll even be happy to teach you, so that you'll be more educated going forward!
</p>
</div>
</div>
I have many elements with header of image with static height and some text below. Target is to get that .outer full height as the elements in the row with it. I need that text to get width of image and height to be full as it can be.
This is what i have achieved at the moment.
CSS:
.outer {
display: inline-block;
}
.inner {
background: green;
display: table-caption;
}
.inner>img{
height: 200px;
}
HTML:
<div class="outer">
<div class="inner">
<img src="https://pp.vk.me/c322418/v322418480/131a7/c3HrxpJiYqY.jpg"/>
<p>Useless Text.</p>
</div>
</div>
Here the essential thing is vertical-align:top; I have given a fixed width but you can give width in percentage.
body {
width: 100%;
}
.outer {
display: inline-block;
width: 90%;
}
.inner {
display: inline-block;
vertical-align: top;
background: green;
width: 290px!important;
padding-right: 20px;
padding-left: 15px;
}
.inner>img {
height: 200px;
}
.inner>p {
width: inherit;
}
.inner>img,
.inner>p {
display: block;
}
<body>
<div class="outer">
<div class="inner">
<img src="https://pp.vk.me/c322418/v322418480/131a7/c3HrxpJiYqY.jpg" />
<p>Useless Text. CSS is not an overly complex language. But even if you’ve been writing CSS for many years, you probably still come across new things — properties you’ve never used, values you’ve never considered, or specification details you never
knew about. In my research, I come across new little tidbits all the time, so I thought I’d share some of them in this post. Admittedly, not everything in this post will have a ton of immediate practical value, but maybe you can mentally file
some of these away for later use.</p>
</div>
<div class="inner">
<img src="https://pp.vk.me/c322418/v322418480/131a7/c3HrxpJiYqY.jpg" />
<p>This is an awesome text, but i want my element get height of left space up of my image.</p>
</div>
<div class="inner">
<img src="https://pp.vk.me/c322418/v322418480/131a7/c3HrxpJiYqY.jpg" />
<p>Useless Text. CSS is not an overly complex language. But even if you’ve been writing CSS for many years, you probably still come across new things — properties you’ve never used, values you’ve never considered, or specification details you never
knew about. In my research, I come across new little tidbits all the time, so I thought I’d share some of them in this post. Admittedly, not everything in this post will have a ton of immediate practical value, but maybe you can mentally file
some of these away for later use.</p>
</div>
</div>
</body>
I have made some adjustments to your code, I have added a class table-div and a class table-row and wrapped those div.outer in div.table-div and div.table-row. You can see it here: http://codepen.io/anon/pen/XboROq?editors=110. I hope this is what you are looking for.
I am trying to make my divs appear horizontally across the page but there is an automatic line break in between them. I was wondering how I could fix this.
<div id="box1">
<header id="whyshouldi">
What is iOS Development
</header>
<p id="whatis">
iOS Development is the process used to create native applications for iPhone, iPod, and iPad. Applications are made using the SDK(software development kit), Xcode. Aside from the software, it is necessary that iOS Developers know Objective-C.
</p>
</div>
<div id="box2">
<header id="whyshouldi">
Why Should I learn it?
</header>
<p id="whatis">
Learning native app development can allow you to better expand the horizon of knowledge in iPhone, and can make you a better programmer overall. It is a great skill to know no matter who you are.
</p>
</div>
This is the default behaviour of block-level elements .. there are many options to have the two divs appear side by side but one simple way is by using the float property and giving each div a width of 50%
Example
you can position them absolutely:
#box1,#box2 {
position: absolute;
width: 50%;
}
#box1 {
left: 0;
}
#box2 {
right: 0;
}
This can be quite easily achieved introducing either a class or using some specificity trickery. If you use display: inline-block you can achieve what you're after. So let's say you introduced a class to your #box1 and #box2 ID's you could in theory...
.col { display: inline-block; max-width: 170px; width: 100%; vertical-align: top; }
Always remember when using inline-block to close any gaps in mark up between #box1 closing </div> and #box2 opening <div>. Otherwise you'll be left with 3 or 4 unwanted pixels.
Check this fiddle. I think this is what you're after. http://jsfiddle.net/UsNBj/
Simple problem, though apparently not a simple solution.
Example here: http://myhideout.eu/new/
Basically the site consist of two columns, though with no wrappers or anything like it, as I'd really like to do with as little of the sort as possible, partly for the sake of simplicity, but also to make use of the HTML5 semantics, which in my mind don't really include divs, no matter how appropriately they are be named.
However, I'd like to have the sidebar fill up the full height of the adjacent column, which is not as easy as I first thought it would be. I know it's an old problem, but I was sure I had solved it before.
Anyhow, I tried to figure out how to do it without using wrappers or JavaScript. JavaScript is a no go, but that's another story. I was sure that there would be some sort of smart CSS3 feature or something similar, that would solve my problem, without the need for wrappers, but my search for this much need feature was a failure of epic proportions.
So I said to my self: "Damn it! Oh well, just have to use wrappers then."
I was sure it would work. I tried different configurations, but no matter what I did, I couldn't get it to work without setting an absolute height of the surrounding wrapper. Just imagine my disappointment, failing once again when I was sure I had done it before. So again I went searching for a solution to suit my needs. Though a lot more material turned up this time, it was still a failure. The few solutions I found was questionable to say the least.
So, now I'm here again, asking yet another one of those questions which undoubtedly have been asked a quadrillion times before. I am sorry about this, but I really don't know where else to go.
I do hope you can help.
Thanks in advance and best regards.
edit:
This works exactly as I want it too:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
margin: 0 auto;
width: 800px;
}
#wrapper {
position: relative;
width: 800px;
}
body > header, body > footer {
background-color: red;
width: 800px;
}
#wrapper > article {
margin-right: 200px;
width: 600px;
background-color: blue;
}
#wrapper > aside {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 200px;
background-color: green;
}
</style>
</head>
<body>
<header>This is a header</header>
<div id="wrapper">
<article>
This is the content<br /><br /><br /><br />left<br /><br /><br /><br />left
</article>
<aside>
And this is the sidebar! I dynamically make myself bigger based on the content on the left!
</aside>
</div>
<footer>This is a footer</footer>
</body>
</html>
Only problem left is getting rid of that damn div tag ;)
edit:
the css table display properties have been pointed out to me, and it really seems to be what I'm looking for, as the smart solution, but with multiple elements in one row, and only one in the other, I can't figure out how it should be done.
If IE6 compatibility is not a requirement, then I usually will usually do the following html:
<div class="container">
<div class="content">
This is the content
</div>
<div class="sidebar">
And this is the sidebar! I dynamically make myself bigger based on the content on the left!
</div>
</div>
And this is the CSS:
.container { position:relative; }
.content {margin-right:<SIDEBAR WIDTH HERE>;}
.sidebar {position:absolute; top:0; bottom:0; right:0; width:???; }
JSFiddle example: http://jsfiddle.net/geC3w/
This works in all modern browsers and Internet Explorer 7 and above, it's also immensely simple, as long as IE6 compatibility isn't a requirement
If IE7 compatibility is not a requirement, use display: table-cell;:
body {
margin: 0 auto;
width: 800px;
}
#wrapper {
position: relative;
width: 800px;
}
body > header, body > footer {
background-color: red;
}
#wrapper > * {
display: table-cell;
}
#wrapper > article {
width: 600px;
background-color: blue;
}
#wrapper > aside {
width: 200px;
background-color: green;
}
Working example.