The top-level container on my site has a max-width to keep it a reasonable size on wide screens. I have a dynamically sized component which can potentially be wider than that max-width. Normally, the component overflows off the side of the screen with it's own independent scrollbar. But on wide screens, with the max-width, the component is cropped by the side margins and it doesn't look great.
I've got a set of styles which can effectively override the top-level max-width and instead left-justifies the component and makes it use the viewport width instead of the top level max-width. It's as follows:
.wide-content {
width: fit-content;
max-width: 100vh;
position: relative;
left: calc(-50vw + 50%);
}
The problem now is that this class is unsuitable when the component isn't too wide. It's left justifying when the component would've fit just fine within the container. I only want those components which would be wider than the container to display this way.
Is there a way to conditionally apply this class, or at least just the left property, based on the components own width? Ie. Only apply that left style if the component is wider than top-max-width?
I'd rather avoid using JS for simplicity sake, but I am using scss if that makes it simpler. I'd take a JS solution if it's the only way, but that's a last resort.
Edit for clarification, here are some pictures of what I'm describing. The cream-colored boxes (labeled Main and Main #2) are the components which get the above styles:
What it looked like originally, without the above styles and with the cropping I don't like:
What it looks like with those styles applied unconditionally:
What I want, ie. the small box displays as it did originally but the large box gets the left-justification treatment:
I'm not sure whether this is what you're looking for, but checkout this
* {
margin: 0;
padding: 0;
}
:root {
--container-max: 300px;
}
.container {
background: lightcoral;
width: var(--container-max);
margin: 0 auto;
}
.graph {
background-color: yellowgreen;
white-space: nowrap;
overflow: visible;
min-width: fit-content;
transform: translate(calc((var(--container-max) - 100%) / 2), 0);
}
<div class="container">
<h1>An H1 Heading</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Velit odio voluptatum minima architecto a omnis at iure sint officia neque sapiente quos cupiditate similique illum doloribus, accusantium natus enim! Et.
</p>
<div class="graph" contenteditable>Lorem ipsum dolor sit amet</div>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Velit odio voluptatum minima architecto a omnis at iure sint officia neque sapiente quos cupiditate similique illum doloribus, accusantium natus enim! Et.
</p>
</div>
The green block is contenteditable, so start typing into it and you'll see its width eventually expands past the outer container, remaining centered in the screen.
Related
I've got a relatively long phrase that consumes way too much space on mobile devices. It looks something like this:
.artificial-phone-viewport {
width: 320px;
height: 500px;
border: 1px solid darkgrey;
}
.container {
width: 100%;
height: 100%;
}
.text {
/*
* Don't want to change font-size, because text
* sometimes maybe shorter and 2.2rem is perfect
* for phrases that are not as long
*/
font-size: 2.2rem;
}
<body class="artificial-phone-viewport">
<div class="container">
<div class="text">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Temporibus saepe illum a magni sequi error fugit dolore neque voluptates in laborum.
</div>
</div>
</body>
What I want is to make this text span at most, let's say, 10rem height. If it can't fit into 10rem of height, it should instead expand horizontally, maybe, overflowing its parent, maybe like this:
.artificial-phone-viewport {
width: 320px;
height: 500px;
border: 1px solid darkgrey;
}
.text {
font-size: 2.2rem;
white-space: nowrap;
}
<body class="artificial-phone-viewport">
<!-- Deleted container to reduce code, it actually
doesn't matter, because it anyway spans
100% width and height of its parent -->
<div class="text">
Lorem ipsum dolor, sit amet consectetur<br/>
adipisicing elit. Temporibus saepe illum<br/>
a magni sequi error fugit dolore neque<br/>
voluptates in laborum.
</div>
</body>
P.S. This snippet is just an example of what I want to see, I don't want any of these <br/>s or white-space: nowrap. Also I want the text to overflow its parent, because I then can use Javascript to scale it propertly, but it is not very relevant for the question, I suppose.
So I figured out a way to do it with Javascript, although I don't like it too much. I just increased the width of the element, until the height was small enough, like this
const text = document.querySelector('.text')
const rem = parseFloat(
getComputedStyle(document.documentElement).fontSize
)
let width = text.clientWidth / rem
while(text.clientWidth > 10*rem) {
width++
text.style.width = `${width}rem`
}
.artificial-phone-viewport {
width: 320px;
height: 500px;
border: 1px solid darkgrey;
}
.text {
font-size: 2.2rem;
}
<body class="artificial-phone-viewport">
<div class="text">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Temporibus saepe illum a magni sequi error fugit dolore neque voluptates in laborum.
</div>
</body>
It's not very nice, but it works for me. If someone finds a way to do it without javascript, I'm open to other solutions
Note, I'm really not trying to create a 'code for me plz' topic, I actually tried to solve this myself but I'm unable to. I found similar topics but they didn't solve my specific issue.
So, I have the following website design in Illustrator:
I need to create a container-fluid, which needs to be 50% image, and 50% color. I tried so many different methods but just couldn't make it work, especially the responsiveness.
Is there anyone who can help me out solving this? Really curious how this is done within Bootstrap.
By the way, for load time efficiency, I'm working with Bootstrap.grid.css, I do not have full access to all Bootstrap classes.
Consider reading carefully Bootstrap's documentation for its Grid Layout
You can do what you need with simple row and columns; how responsive the content inside of those behaves depends on what you build
.img-bg {
background: url("https://via.placeholder.com/1600")
}
.color-bg {
background: green;
text-align: right;
}
.row>div {
height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-6 img-bg">
<span>This background is an image</span>
</div>
<div class="col-6 color-bg">
<h2>Lorem Ipsum</h2>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ea fugiat architecto blanditiis eaque laborum, vel voluptatum voluptas asperiores odio quia error commodi ratione dolorem, cupiditate dolor eius nulla atque quidem?
</p>
</div>
</div>
</div>
In this example I was able put two divs on each other at the bottom of their parent, but only because I knew the height of last div. The first one was moved a bit to the top. What if the height of #second is dynamic? How can we make them sit on each other at the bottom of parent with dynamic heights? Is it even possible with css? Please do not post JavaScript or jQuery versions.
You can use Flexbox. With justify-content: flex-end and you can move content to end of parent element, so if you want to position some other child element on top of parent you can use margin-bottom: auto. This applies if you set flex-direction: column on parent.
.content {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.main {
margin-bottom: auto;
border: 1px solid black;
}
.box {
background: lightblue;
margin: 5px;
}
<div class="content">
<div class="main">Lorem ipsum dolor.</div>
<div class="box">One</div>
<div class="box">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati vel molestiae dolores, ad, nulla harum tenetur minima aperiam debitis id atque fugit, modi error et magni eius repellendus saepe. Vero?</div>
</div>
You can wrap both of them in a div and set position absolute style for that div rather than applying them individually..
working [Fiddle][1]
[1]: https://jsfiddle.net/gd5pqky7/
I'm working on a responsive layout. See this fiddle, or code below, for an example: http://jsfiddle.net/jasonpalter/GBygZ/
The image has a left margin -- necessary in when it's not spanning the full width of the its container. But when the image spans the full width of the container, the left margin needs to be removed. We can accomplish this simply with a breakpoint, but the image itself if dynamic, and we don't know what its dimensions will be.
Is there an automatic way (CSS, or less preferably javascript) to ensure that the image padding can disappear when the image spans the full width of its container?
HMTL
<div class="pod">
<div class="img-right">
<img src="http://placehold.it/460x220" width="460" height="220" />
</div>
<h3>Lorem Ipsum</h3>
<div>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
<p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
</div>
</div>
CSS
.pod {
overflow: hidden;
padding: 5px;
border: 1px dotted #ccc;
font-family: Arial, sans-serif;
font-size: 12px;
line-height: 1.4;
}
.img-right {
float: right;
}
.img-right img {
box-sizing: border-box;
padding: 0 0 0 5px;
max-width: 100%;
height: auto;
}
To do this in javascript, you would need to create an Image object, set its src attribute to the image source, and then pass a function that grabs the images dimensions to the image object's onload attribute. With that information, you could bind a function to the window's resize event that strips the image's padding when the image spans the full width of the container and reinstates it when the opposite happens.
There is unfortunately no way to do this in CSS, though being able to just write a img:not(:resized) selector would be nice!
In my ecperience this would not be possible without knowing the width of the image, in CSS alone. A jQuery solution would be very simple
DEMO http://jsfiddle.net/kevinPHPkevin/GBygZ/5/
var windowWidth = $(window).width();
if ($('#image').width() < windowWidth) {
$('#image').css('padding-left', '10px')
}
When you test this in a fiddle it will not work as the window is the browser window width, not the width of the editable column that is assigned to the fiddle.
I'm building a HTML document that is supposed to be semantically correct. Therefore, <aside> that is related to the website (i.e. sidebar) should be placed outside <article> and <aside> related directly to the article (if any) should be placed inside <article>.
That's not a problem with the left layout on my image because this will work:
<article>article</article>
<aside>sideabr</aside>
.article, .aside { float: left; }
.article { width: 70%; }
.aside { width: 30%; }
But what if main <h1> of the article is supposed to be placed above the article and the sidebar? I can't just nest the article and the sidebar in <article> because the sidebar is not a side note in the article. And I can't nest header + paragraphs in <article> because the sidebar will not align to paragraphs but to the header like in the first case.
Any idea how the HTML structure of such document should look like?
Most websites good as reference use the left version: http://twentytwelvedemo.wordpress.com/about-2/ so I can't find anything reliable to learn from.
This is how it should look like technically but can this be done in HTML at all?:
By separating article in two: heading and content (just a div) and then floating this content, you can achieve the desired effect.
aside may be floating too (you must then set a width) or be set to overflow: hidden.
You can't add a clearfix on article (or aside will stay below) so you've to put it on parent (here section). Same for background if it must have one.
Demo: http://codepen.io/anon/pen/BAjLr
CSS
section {
width: 500px;
background-color: tomato;
}
/* clearfix */
section:after {
content: '';
display: table;
clear: left;
}
/* article */
h1 {
margin-bottom: 0;
}
article .content {
float: left;
width: 300px;
background-color: lightgreen;
}
/* aside of section */
aside {
overflow: hidden;
color: white;
background-color: darkslateblue;
}
HTML
<section>
<article>
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id laudantium beatae quae recusandae!</h1>
<div class="content">
<p>article Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt repudiandae nam commodi iusto ullam neque aliquam ut numquam. Cumque ut tempora consectetur quam velit ad incidunt necessitatibus saepe eos exercitationem.</p>
</div>
</article>
<aside>
<h2>sidebar</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam quae</p>
</aside>
</section>