Mobile Full Width Tailwind
December 15, 2024 · AI Conversations
Here’s an example:
<div class="basis-full sm:basis-1/2">
<!-- Your content here -->
</div>
### Explanation:
1. **`basis-full`**: Sets the element's width to `100%` on smaller screens (default/mobile-first).
2. **`sm:basis-1/2`**: Applies a `basis` of `1/2` (50% width) starting from the `sm` breakpoint (e.g., 640px and up).
### Output Behavior:
– On mobile devices (less than 640px), the element will take the full width.
– On screens `640px` and wider, it will occupy half the width.
You can adjust the breakpoint (`sm`, `md`, `lg`, etc.) as needed based on your design requirements.
Here’s an example:
<div class="p-4 sm:p-6 basis-full sm:basis-1/2">
<!-- Your content here -->
</div>
### Explanation:
1. **`p-4`**: Adds padding of `1rem` (16px) on all sides for mobile (default).
2. **`sm:p-6`**: Increases the padding to `1.5rem` (24px) for screens `640px` and wider.
3. **`basis-full` and `sm:basis-1/2`**: Same as before, to control the width responsively.
This ensures the element has:
– Adequate padding on smaller devices.
– Larger padding on bigger screens if needed. Adjust `p-*` values according to your design requirements.
### Example: Single Column on Mobile, Two Columns on Desktop
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="bg-blue-200 p-4">Column 1</div>
<div class="bg-blue-300 p-4">Column 2</div>
</div>
### Explanation:
1. **`grid`**: Sets the container to use CSS Grid.
2. **`grid-cols-1`**: Displays a single column (default for mobile).
3. **`md:grid-cols-2`**: Changes to a two-column layout starting at the `md` breakpoint (`768px` and above).
4. **`gap-4`**: Adds spacing between the columns and rows (adjustable).
### Example: Three Columns on Desktop, One on Mobile
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div class="bg-green-200 p-4">Column 1</div>
<div class="bg-green-300 p-4">Column 2</div>
<div class="bg-green-400 p-4">Column 3</div>
</div>
### Explanation:
1. **`lg:grid-cols-3`**: Uses three columns for `lg` screens (`1024px` and above).
2. **`grid-cols-1`**: Defaults to a single column on smaller screens.
### Flexbox Alternative
If you prefer `flexbox`, you can use responsive `flex` and `flex-wrap` utilities:
<div class="flex flex-col md:flex-row gap-4">
<div class="flex-1 bg-yellow-200 p-4">Column 1</div>
<div class="flex-1 bg-yellow-300 p-4">Column 2</div>
</div>
This ensures:
– A vertical layout (`flex-col`) on mobile.
– A horizontal layout (`flex-row`) for `md` screens and above.
Choose `grid` or `flex` based on the layout complexity and your specific needs!